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

Annotation of /trunk/pygtk/integratorreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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