/[ascend]/trunk/pygtk/integratorreporter.py
ViewVC logotype

Contents of /trunk/pygtk/integratorreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1473 - (show annotations) (download) (as text)
Mon Jun 4 13:10:01 2007 UTC (12 years, 6 months ago) by jpye
File MIME type: text/x-python
File size: 7388 byte(s)
Fix problem with incidence matrix and detection of matplotlib
1 import ascpy
2 import time
3 import sys
4 import gtk
5 import gtk.glade
6 import time
7 from varentry import *
8 from preferences import *
9 from infodialog import *
10 from observer import *
11 import tempfile
12
13 import gobject
14 try:
15 import pylab
16 except:
17 pass
18
19 # When writing this class, we assume that the integrator class has already had
20 # its "analyse" method called, so we know all that stuff like the number of
21 # observed variables, what our time samples are, what the independent variable
22 # is, etc.
23
24 INTEGRATOR_NUM = 0
25
26 class IntegratorReporterPython(ascpy.IntegratorReporterCxx):
27 def __init__(self,browser,integrator):
28 self.browser=browser
29 ascpy.IntegratorReporterCxx.__init__(self,integrator)
30
31 # GUI elements
32 _xml = gtk.glade.XML(browser.glade_file,"integratorstatusdialog")
33 _xml.signal_autoconnect(self)
34 self.window=_xml.get_widget("integratorstatusdialog")
35 self.window.set_transient_for(self.browser.window)
36 self.label=_xml.get_widget("integratorlabel")
37 self.label.set_text("Solving with "+self.getIntegrator().getName())
38 self.progress=_xml.get_widget("integratorprogress")
39 self.data = None
40
41 self.cancelrequested=False
42
43 def run(self):
44 # run the dialog: start solution, monitor use events
45 try:
46 self.getIntegrator().solve()
47
48 except RuntimeError,e:
49 self.browser.reporter.reportError("Integrator failed: %s" % e)
50
51 if self.browser.prefs.getBoolPref("Integrator","writeendmatrix",True):
52 if platform.system()=="Windows":
53 _deffn = "\\TEMP\\ascintegratormatrix.mtx"
54 else:
55 _deffn = "/tmp/ascintegratormatrix.mtx"
56 _fn = self.browser.prefs.getStringPref("Integrator","matrixfilepath",_deffn)
57 self.browser.reporter.reportNote("Writing matrix to file '%s'" % _fn)
58 _fp = file(_fn,"w")
59 try:
60 try:
61 self.getIntegrator().writeMatrix(_fp)
62 except RuntimeError,e:
63 self.browser.reporter.reportError(str(e))
64 finally:
65 _fp.close()
66 self.window.destroy()
67
68 return
69
70 def on_cancelbutton_clicked(self,*args):
71 self.cancelrequested=True
72
73 def initOutput(self):
74 # empty out the data table
75 self.data=[]
76 self.nsteps = self.getIntegrator().getNumSteps()
77 self.progress.set_text("Starting...")
78 self.progress.set_fraction(0.0)
79 #update the GUI
80 while gtk.events_pending():
81 gtk.main_iteration()
82 return 1
83
84 def closeOutput(self):
85 global INTEGRATOR_NUM
86 integrator = self.getIntegrator()
87 # create an empty observer
88 try:
89 _xml = gtk.glade.XML(self.browser.glade_file,"observervbox")
90 _label = gtk.Label();
91 INTEGRATOR_NUM = INTEGRATOR_NUM + 1
92 _name = "Integrator %d" % INTEGRATOR_NUM
93 _tab = self.browser.maintabs.append_page(_xml.get_widget("observervbox"),_label)
94 _obs = ObserverTab(xml=_xml, name=_name, browser=self.browser, tab=_tab, alive=False)
95 _label.set_text(_obs.name)
96 self.browser.observers.append(_obs)
97 self.browser.tabs[_tab]=_obs
98
99 # add the columns
100 _obs.add_instance(integrator.getIndependentVariable().getInstance())
101 for _v in [integrator.getObservedVariable(_i) for _i in range(0,integrator.getNumObservedVars())]:
102 _obs.add_instance(_v.getInstance())
103
104 for _time,_vals in self.data:
105 _obs.do_add_row([_time]+[_v for _v in _vals])
106 except Exception,e:
107 sys.stderr.write("\n\n\nCAUGHT EXCEPTION: %s\n\n\n" % str(e))
108 return 0
109
110 def closeOutput1(self):
111 # output the results (to the console, for now)
112 for _t,_vals in self.data:
113 print _t,_vals
114
115 self.progress.set_fraction(1.0)
116 self.progress.set_text("Finished.")
117 return 1
118
119 def updateStatus(self):
120 # outdate the GUI
121 try:
122 # TODO: change so it's not updating every step!
123 t = self.getIntegrator().getCurrentTime()
124 _frac = float(self.getIntegrator().getCurrentStep())/self.nsteps
125 self.progress.set_text("t = %f" % (self.getIntegrator().getCurrentTime()))
126 self.progress.set_fraction(_frac)
127 while gtk.events_pending():
128 gtk.main_iteration()
129 if self.cancelrequested:
130 return 0
131 return 1
132 except Exception,e:
133 print "\n\nERROR IN UPDATESTATUS!",str(e)
134 return 0
135
136 def recordObservedValues(self):
137 # just add to our in-memory data structure for now...
138 try:
139 i = self.getIntegrator()
140 print str(i.getCurrentObservations())
141 self.data.append((i.getCurrentTime(),i.getCurrentObservations()))
142 except Exception,e:
143 print "\n\nERROR IN RECORDOBSERVEDVALUES!",str(e)
144 return 0
145 return 1
146
147 class IntegratorReporterFile(ascpy.IntegratorReporterCxx):
148 def __init__(self,integrator,filep):
149 self.filep=filep
150 self.numsteps=0
151 self.indepname="t"
152 ascpy.IntegratorReporterCxx.__init__(self,integrator)
153
154
155 def run(self):
156 self.getIntegrator().solve()
157
158 def initOutput(self):
159 try:
160 sys.stderr.write("Integrating...\n")
161 I = self.getIntegrator()
162 self.numsteps=I.getNumSteps()
163 self.indepname = I.getIndependentVariable().getName()
164 names = [I.getObservedVariable(i).getName() for i \
165 in range(I.getNumObservedVars())
166 ]
167 self.filep.write("#%s\t" % self.indepname)
168 self.filep.write("\t".join(names)+"\n")
169 except Exception,e:
170 print "ERROR %s" % str(e)
171 return 0
172 return 1
173
174 def closeOutput(self):
175 sys.stderr.write(" "*20+chr(8)*20)
176 sys.stderr.write("Finished, %d samples recorded.\n" % self.numsteps)
177 self.filep.write("#end\n")
178 return 0
179
180 def updateStatus(self):
181 try:
182 I = self.getIntegrator()
183 t = I.getCurrentTime()
184 pct = 100.0 * I.getCurrentStep() / self.numsteps;
185 sys.stderr.write("%3.0f%% (%s = %6.3f) \r" % (pct,self.indepname,t))
186 except Exception,e:
187 print "ERROR %s" % str(e)
188 return 0
189 return 1
190
191 def recordObservedValues(self):
192 try:
193 I = self.getIntegrator()
194 obs = I.getCurrentObservations()
195 #print str(obs)
196 self.filep.write("%f\t" % I.getCurrentTime())
197 self.filep.write("\t".join([str(i) for i in obs])+"\n")
198 except Exception,e:
199 print "ERROR %s" % str(e)
200 return 0
201 return 1
202
203 class IntegratorReporterPlot(ascpy.IntegratorReporterCxx):
204 """Plotting integrator reporter"""
205 def __init__(self,integrator):
206 self.numsteps=0
207 self.indepname="t"
208 ascpy.IntegratorReporterCxx.__init__(self,integrator)
209
210 def run(self):
211 import loading
212 loading.load_matplotlib(throw=True)
213 self.getIntegrator().solve()
214
215 def initOutput(self):
216 try:
217 sys.stderr.write("Integrating...\n")
218 self.ax = pylab.subplot(111)
219 self.canvas = self.ax.figure.canvas
220 I = self.getIntegrator()
221 self.numsteps=I.getNumSteps()
222 self.indepname = I.getIndependentVariable().getName()
223 self.x = []
224 self.y = []
225 self.line, = pylab.plot(self.x,self.y,animated=True)
226 self.bg = self.canvas.copy_from_bbox(self.ax.bbox)
227 gobject.idle_add(self.plotupdate)
228 pylab.show()
229 except Exception,e:
230 print "ERROR %s" % str(e)
231 return 0
232 return 1
233
234 def closeOutput(self):
235 sys.stderr.write(" "*20+chr(8)*20)
236 sys.stderr.write("Finished, %d samples recorded.\n" % self.numsteps)
237 return 0
238
239 def updateStatus(self):
240 return 1
241
242 def plotupdate(self):
243 sys.stderr.write("%d...\r " % len(self.x))
244 try:
245 self.canvas.restore_region(self.bg)
246 self.line.set_data(self.x, self.y)
247 self.ax.draw_artist(self.line)
248 self.canvas.blit(self.ax.bbox)
249 except Exception,e:
250 print "ERROR %s" % str(e)
251
252 def recordObservedValues(self):
253 try:
254 I = self.getIntegrator()
255 obs = I.getCurrentObservations()
256 self.x.append(I.getCurrentTime())
257 self.y.append(obs[0])
258 except Exception,e:
259 print "ERROR %s" % str(e)
260 return 0
261 return 1
262
263

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22