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

Contents of /trunk/pygtk/study.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2598 - (show annotations) (download) (as text)
Tue Apr 17 21:00:29 2012 UTC (12 years, 2 months ago) by jpye
File MIME type: text/x-python
File size: 9458 byte(s)
Rename to StudyReporter in studyreporter.py (seems to be duplication from solverreporter.py?).
Fixed some unit conversions in tmy3.c, test file seems correct now (to float precision at least).
Suppressed some debug output to try to speed up Study feature, not there yet.
1 import gtk
2 import pango
3 import ascpy
4
5 from varentry import *
6 from studyreporter import *
7 from math import log, exp
8
9 class StudyWin:
10 def __init__(self, browser, instance):
11
12 self.instance = instance
13 self.browser = browser
14 self.browser.builder.add_objects_from_file(self.browser.glade_file,["list_of_dist","studywin"])
15
16 widgets = ["lowerb","upperb","nsteps","methodrun","dist","check_dist","studywin","var_to_study"]
17 for n in widgets:
18 setattr(self,n,self.browser.builder.get_object(n))
19
20 self.checkbutton = self.browser.builder.get_object("on_fail_continue")
21 self.method = None
22
23 _p = self.browser.prefs
24 _continue_on_fail = _p.getBoolPref("StudyReporter", "continue_on_fail", True)
25 self.checkbutton.set_active(_continue_on_fail)
26 #--------------------
27 # set up the distributions combobox
28
29 _cell = gtk.CellRendererText()
30 self.dist.pack_start(_cell, True)
31 self.dist.add_attribute(_cell, 'text', 0)
32
33 #--------------------
34 # set up the methods combobox
35
36 _methodstore = self.browser.methodstore
37 _methodrenderer = gtk.CellRendererText()
38 self.methodrun.set_model(_methodstore)
39 self.methodrun.pack_start(_methodrenderer, True)
40 self.methodrun.add_attribute(_methodrenderer, 'text',0)
41
42 self.browser.builder.connect_signals(self)
43 self.fill_values()
44 self.lowerb.select_region(0, -1)
45
46
47 def fill_values(self):
48
49 self.var_to_study.set_text( self.browser.sim.getInstanceName(self.instance) )
50 _nsteps = self.browser.prefs.getStringPref("Study","nsteps","10")
51 self.nsteps.set_text(_nsteps)
52 _u = self.instance.getType().getPreferredUnits();
53 if _u is None:
54 _conversion = 1
55 _u = self.instance.getDimensions().getDefaultUnits().getName().toString()
56 else:
57 _conversion = _u.getConversion() # displayvalue x conversion = SI
58 _u = _u.getName().toString()
59
60 _arr = {
61 self.lowerb: self.instance.getRealValue()
62 ,self.upperb: self.instance.getUpperBound()
63 }
64 for _k,_v in _arr.iteritems():
65 _t = str(_v / _conversion)+" "+_u
66 _k.set_text(_t)
67
68 def ready(self):
69 # To check if all the input is ok
70 if self.on_dist_edited() == 1 and self.on_nsteps_edited() == 1:
71 return True
72 else:
73 return False
74
75 def run(self):
76 _continue = True
77 while _continue:
78 _res = self.studywin.run();
79 if _res == gtk.RESPONSE_OK:
80 if self.ready():
81 self.on_ok_clicked()
82 _continue = False
83 else:
84 self.browser.reporter.reportError("Cannot continue. Please review the errors.")
85 continue
86 elif _res==gtk.RESPONSE_CANCEL:
87 _continue = False
88 self.studywin.destroy()
89
90 def on_studywin_close(self,*args):
91 self.studywin.response(gtk.RESPONSE_CANCEL)
92
93 def on_key_press_event(self,widget,event):
94 keyname = gtk.gdk.keyval_name(event.keyval)
95 if keyname=="Return":
96 self.studywin.response(gtk.RESPONSE_OK)
97 return True
98 elif keyname=="Escape":
99 self.studywin.response(gtk.RESPONSE_CANCEL)
100 return True;
101 return False;
102
103 def on_methodrun_changed(self, *args):
104 _sel = self.methodrun.get_active_text()
105 if _sel:
106 _methods = self.browser.sim.getType().getMethods()
107 for _m in _methods:
108 if _m.getName()==_sel:
109 self.method = _m
110 break
111
112 def taint_entry(self, entry, color):
113 entry.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
114 entry.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
115 entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
116 entry.modify_base(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
117 if color == "#FFBBBB":
118 entry.set_property("secondary-icon-stock", 'gtk-dialog-error')
119 elif color == "white":
120 entry.set_property("secondary-icon-stock", 'gtk-yes')
121 entry.set_property("secondary-icon-tooltip-text", "")
122
123 def parse_entry(self, entry):
124 # A simple function to get the real value from the entered text
125 # and taint the entry box accordingly
126 i = RealAtomEntry(self.instance, entry.get_text())
127 try:
128 i.checkEntry()
129 _value = i.getValue()
130 except InputError, e:
131 _value = None
132 _error = re.split('Input Error: ', str(e), 1)
133 entry.set_property("secondary-icon-tooltip-text", _error[1])
134
135 if _value is not None:
136 self.taint_entry(entry, "white")
137 else:
138 self.taint_entry(entry, "#FFBBBB")
139 return _value
140
141 def on_dist_edited(self, *args):
142 # To update the check_dist image and do a lot of input checking
143 _dist = self.dist.get_active_text()
144 _start = self.parse_entry(self.lowerb)
145 _end = self.parse_entry(self.upperb)
146
147 if _start == None or _end == None:
148 self.check_dist.clear()
149 return 0
150 if _start == _end:
151 self.check_dist.set_from_stock('gtk-dialog-error', gtk.ICON_SIZE_BUTTON)
152 self.check_dist.set_tooltip_text("The bounds should not be equal.")
153 self.taint_entry(self.lowerb, "#FFBBBB")
154 self.taint_entry(self.upperb, "#FFBBBB")
155 self.lowerb.set_property("secondary-icon-tooltip-text", "The bounds should not be equal.")
156 self.upperb.set_property("secondary-icon-tooltip-text", "The bounds should not be equal.")
157 return 0
158 if _dist == "Linear":
159 self.check_dist.set_from_stock('gtk-yes', gtk.ICON_SIZE_BUTTON)
160 self.check_dist.set_tooltip_text("")
161 return 1
162 if _dist == "Logarithmic":
163 if _start == 0 or _end == 0:
164 self.check_dist.set_from_stock('gtk-dialog-error', gtk.ICON_SIZE_BUTTON)
165 self.check_dist.set_tooltip_text("Neither of the bounds can be 0 for logarithmic distribution.")
166 if _start == 0:
167 self.taint_entry(self.lowerb, "#FFBBBB")
168 self.lowerb.set_property("secondary-icon-tooltip-text", "Cannot be 0 for logarithmic distribution")
169 else:
170 self.taint_entry(self.upperb, "#FFBBBB")
171 self.upperb.set_property("secondary-icon-tooltip-text", "Cannot be 0 for logarithmic distribution")
172 return 0
173 if (_start/_end) < 0:
174 self.check_dist.set_from_stock('gtk-dialog-error', gtk.ICON_SIZE_BUTTON)
175 self.check_dist.set_tooltip_text("The bounds cannot be of opposite sign in case of logarithmic distribution.")
176 self.taint_entry(self.lowerb, "#FFBBBB")
177 self.taint_entry(self.upperb, "#FFBBBB")
178 self.lowerb.set_property("secondary-icon-tooltip-text", "The bounds cannot be of opposite sign in case of logarithmic distribution.")
179 self.upperb.set_property("secondary-icon-tooltip-text", "The bounds cannot be of opposite sign in case of logarithmic distribution.")
180 return 0
181 self.check_dist.set_from_stock('gtk-yes', gtk.ICON_SIZE_BUTTON)
182 self.check_dist.set_tooltip_text("")
183 return 1
184
185 def on_nsteps_edited(self, *args):
186 # To update the icon in the entry
187 _failed = False
188
189 try:
190 _nsteps = int(self.nsteps.get_text())
191 except:
192 _failed = True
193
194 if _failed or _nsteps == 0:
195 self.nsteps.set_text("")
196 self.taint_entry(self.nsteps,"#FFBBBB")
197 self.nsteps.set_property("secondary-icon-tooltip-text", "Please give a valid entry")
198 return 0
199 else:
200 self.taint_entry(self.nsteps,"white")
201 return 1
202
203 def on_check_toggled(self, *args):
204 # To update the preference for behaviour on solver fail
205
206 _p = self.browser.prefs
207 _p.setBoolPref("StudyReporter", "continue_on_fail", self.checkbutton.get_active())
208
209 def on_ok_clicked(self, *args):
210 # check that the units of the entered values are acceptable
211
212 _arr = [self.lowerb, self.upperb]
213 _failed = False
214 _parameters = []
215 for _k in _arr:
216 i = RealAtomEntry(self.instance, _k.get_text())
217 try:
218 i.checkEntry()
219 _parameters.append(i.getValue())
220 except InputError, e:
221 print "INPUT ERROR: ",str(e)
222 _failed = True
223
224 if _failed:
225 raise InputError(None) # no message
226
227 else:
228 self.solve(_parameters)
229
230 def solve(self, parameters):
231 _nsteps = int(self.nsteps.get_text())
232 _dist = self.dist.get_active_text()
233 _browser = self.browser
234
235 if _dist == "Linear":
236 _step = (parameters[1] - parameters[0])/_nsteps
237 _log = False
238 else:
239 _diff = log(parameters[1]/parameters[0])/_nsteps
240 _log = True
241
242 if not hasattr(self.browser,'solver'):
243 _browser.reporter.reportError("No solver assigned!")
244 return
245
246 if _browser.no_built_system():
247 return
248 _browser.start_waiting("Solving with %s..." % _browser.solver.getName())
249 _browser.prefs.setStringPref("Study","nsteps",str(_nsteps))
250 self.studywin.destroy()
251 reporter = StudyReporter(_browser, _browser.sim.getNumVars(), self.instance, _nsteps, self)
252 i = 0
253 while i<=_nsteps and reporter.guiinterrupt == False:
254
255 #run method
256 if self.method:
257 try:
258 _browser.sim.run(method)
259 except RuntimeError,e:
260 _browser.reporter.reportError(str(e))
261
262 #set the value
263 ## FIXME do this test outside the loop...
264 if self.instance.getType().isRefinedSolverVar():
265 # for solver vars, set the 'fixed' flag as well
266 ## FIXME shouldn't be necessary to set the 'fixed' flag each time.
267 ## FIXME this function seems to somehow be repeatedly parsing units: avoid doing that every step.
268 self.instance.setFixedValue(parameters[0])
269 else:
270 ## why would we NOT want to fix this variable??
271 self.instance.setRealValue(parameters[0])
272
273 #solve
274 try:
275 reporter.updateVarDetails(i)
276 _browser.sim.solve(_browser.solver, reporter)
277 except RuntimeError,e:
278 _browser.reporter.reportError(str(e))
279
280 i = i+1
281 # any issue with accumulation of rounding errors here?
282 if _log == True:
283 parameters[0] = parameters[0]*exp(_diff)
284 else:
285 parameters[0] = parameters[0] + _step
286 if reporter.continue_on_fail == True:
287 reporter.updateVarDetails(i)
288
289 _browser.stop_waiting()
290 _browser.modelview.refreshtree()
291

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