1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy |
---|
5 | import copy |
---|
6 | |
---|
7 | from sans.guicomm.events import StatusEvent |
---|
8 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
9 | _BOX_WIDTH = 80 |
---|
10 | |
---|
11 | def format_number(value, high=False): |
---|
12 | """ |
---|
13 | Return a float in a standardized, human-readable formatted string |
---|
14 | """ |
---|
15 | try: |
---|
16 | value = float(value) |
---|
17 | except: |
---|
18 | print "returning 0" |
---|
19 | return "0" |
---|
20 | |
---|
21 | if high: |
---|
22 | return "%-6.4g" % value |
---|
23 | else: |
---|
24 | return "%-5.3g" % value |
---|
25 | |
---|
26 | |
---|
27 | class ModelPage(wx.ScrolledWindow): |
---|
28 | """ |
---|
29 | FitPanel class contains fields allowing to display results when |
---|
30 | fitting a model and one data |
---|
31 | @note: For Fit to be performed the user should check at least one parameter |
---|
32 | on fit Panel window. |
---|
33 | |
---|
34 | """ |
---|
35 | ## Internal name for the AUI manager |
---|
36 | window_name = "Fit page" |
---|
37 | ## Title to appear on top of the window |
---|
38 | window_caption = "Fit Page" |
---|
39 | |
---|
40 | |
---|
41 | def __init__(self, parent,model,name, *args, **kwargs): |
---|
42 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
43 | """ |
---|
44 | Initialization of the Panel |
---|
45 | """ |
---|
46 | #self.scroll = wx.ScrolledWindow(self) |
---|
47 | self.manager = None |
---|
48 | self.parent = parent |
---|
49 | self.event_owner=None |
---|
50 | #panel interface |
---|
51 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
52 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
53 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
54 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
55 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
56 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
57 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
58 | self.modelbox = wx.ComboBox(self, -1) |
---|
59 | id = wx.NewId() |
---|
60 | self.vbox.Add(self.sizer3) |
---|
61 | self.vbox.Add(self.sizer1) |
---|
62 | self.vbox.Add(self.sizer2) |
---|
63 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
64 | self.vbox.Add(self.sizer5) |
---|
65 | self.vbox.Add(self.sizer4) |
---|
66 | |
---|
67 | id = wx.NewId() |
---|
68 | self.btClose =wx.Button(self,id,'Close') |
---|
69 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
70 | self.btClose.SetToolTipString("Close page.") |
---|
71 | ix = 1 |
---|
72 | iy = 1 |
---|
73 | self.sizer4.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
74 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
75 | ix += 1 |
---|
76 | self.sizer4.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
77 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
78 | ix += 1 |
---|
79 | self.sizer4.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
80 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
81 | ix = 0 |
---|
82 | iy += 1 |
---|
83 | self.sizer4.Add(wx.StaticText(self, -1, 'x range'),(iy, ix),(1,1),\ |
---|
84 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
85 | ## Q range |
---|
86 | self.qmin= 0.001 |
---|
87 | self.qmax= 0.1 |
---|
88 | self.num_points= 100 |
---|
89 | |
---|
90 | ix += 1 |
---|
91 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
92 | self.xmin.SetValue(format_number(self.qmin)) |
---|
93 | self.xmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
94 | self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
95 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
96 | self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
97 | |
---|
98 | |
---|
99 | ix += 1 |
---|
100 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
101 | self.xmax.SetValue(format_number(self.qmax)) |
---|
102 | self.xmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
103 | self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
104 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
105 | self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
106 | ix += 1 |
---|
107 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
108 | self.npts.SetValue(format_number(self.num_points)) |
---|
109 | self.npts.SetToolTipString("Number of point to plot.") |
---|
110 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
111 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
112 | |
---|
113 | self.sizer4.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
114 | ix = 0 |
---|
115 | iy += 1 |
---|
116 | self.sizer4.Add((20,20),(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
117 | ix +=3 |
---|
118 | self.sizer4.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
119 | ix = 0 |
---|
120 | iy = 1 |
---|
121 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
122 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
123 | ix += 1 |
---|
124 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
125 | #ix = 0 |
---|
126 | #iy += 1 |
---|
127 | ix += 1 |
---|
128 | id = wx.NewId() |
---|
129 | self.model_view =wx.Button(self,id,'View 2D') |
---|
130 | self.model_view.Bind(wx.EVT_BUTTON, self.onModel2D,id=id) |
---|
131 | self.model_view.SetToolTipString("View model in 2D") |
---|
132 | self.sizer3.Add(self.model_view,(iy,ix),(1,1),\ |
---|
133 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
134 | # contains link between model ,all its parameters, and panel organization |
---|
135 | self.parameters=[] |
---|
136 | #contains link between a model and selected parameters to fit |
---|
137 | self.param_toFit=[] |
---|
138 | # model on which the fit would be performed |
---|
139 | self.model=model |
---|
140 | try: |
---|
141 | #print"init modelpage",model.name |
---|
142 | self.set_panel(model) |
---|
143 | except: |
---|
144 | raise |
---|
145 | # preview selected model name |
---|
146 | self.prevmodel_name=name |
---|
147 | #print "model view prev_model",name |
---|
148 | self.modelbox.SetValue(self.prevmodel_name) |
---|
149 | # flag to check if the user has selected a new model in the combox box |
---|
150 | self.model_hasChanged=False |
---|
151 | #dictionary of model name and model class |
---|
152 | self.model_list_box={} |
---|
153 | |
---|
154 | #enable model 2D draw |
---|
155 | self.enable2D= False |
---|
156 | # Data1D to make a deep comparison between 2 Data1D for checking data |
---|
157 | #change |
---|
158 | self.vbox.Layout() |
---|
159 | self.vbox.Fit(self) |
---|
160 | |
---|
161 | self.SetSizer(self.vbox) |
---|
162 | self.SetScrollbars(20,20,55,40) |
---|
163 | self.Centre() |
---|
164 | |
---|
165 | def set_range(self, qmin, qmax, npts): |
---|
166 | """ |
---|
167 | Set the range for the plotted models |
---|
168 | @param qmin: minimum Q |
---|
169 | @param qmax: maximum Q |
---|
170 | @param npts: number of Q bins |
---|
171 | """ |
---|
172 | # Set the data members |
---|
173 | self.qmin = qmin |
---|
174 | self.qmax = qmax |
---|
175 | self.num_points = npts |
---|
176 | |
---|
177 | # Set the controls |
---|
178 | self.xmin.SetValue(format_number(self.qmin)) |
---|
179 | self.xmax.SetValue(format_number(self.qmax)) |
---|
180 | self.npts.SetValue(format_number(self.num_points)) |
---|
181 | |
---|
182 | def onClose(self,event): |
---|
183 | """ close the page associated with this panel""" |
---|
184 | self.GrandParent.onClose() |
---|
185 | |
---|
186 | def set_owner(self,owner): |
---|
187 | """ |
---|
188 | set owner of fitpage |
---|
189 | @param owner: the class responsible of plotting |
---|
190 | """ |
---|
191 | self.event_owner=owner |
---|
192 | |
---|
193 | |
---|
194 | def set_manager(self, manager): |
---|
195 | """ |
---|
196 | set panel manager |
---|
197 | @param manager: instance of plugin fitting |
---|
198 | """ |
---|
199 | self.manager = manager |
---|
200 | |
---|
201 | def onModel2D(self, event): |
---|
202 | """ |
---|
203 | call manager to plot model in 2D |
---|
204 | """ |
---|
205 | # If the 2D display is not currently enabled, plot the model in 2D |
---|
206 | # and set the enable2D flag. |
---|
207 | if self.enable2D==False: |
---|
208 | self.enable2D=True |
---|
209 | self._draw_model() |
---|
210 | else: |
---|
211 | self.manager.menu1.Append(event_id, new_panel.window_caption, |
---|
212 | "Show %s plot panel" % new_panel.window_caption) |
---|
213 | |
---|
214 | def populate_box(self, dict): |
---|
215 | """ |
---|
216 | Populate each combox box of each page |
---|
217 | @param page: the page to populate |
---|
218 | """ |
---|
219 | id=0 |
---|
220 | self.model_list_box=dict |
---|
221 | list_name=[] |
---|
222 | for item in self.model_list_box.itervalues(): |
---|
223 | name = item.__name__ |
---|
224 | if hasattr(item, "name"): |
---|
225 | name = item.name |
---|
226 | list_name.append(name) |
---|
227 | list_name.sort() |
---|
228 | for name in list_name: |
---|
229 | self.modelbox.Insert(name,int(id)) |
---|
230 | id+=1 |
---|
231 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
232 | return 0 |
---|
233 | |
---|
234 | def select_model(self, model): |
---|
235 | """ |
---|
236 | Select a new model |
---|
237 | @param model: model object |
---|
238 | """ |
---|
239 | self.model= model |
---|
240 | self.set_panel(model) |
---|
241 | self._draw_model() |
---|
242 | |
---|
243 | # Select the model from the combo box |
---|
244 | items = self.modelbox.GetItems() |
---|
245 | for i in range(len(items)): |
---|
246 | if items[i]==model.__class__.__name__: |
---|
247 | self.modelbox.SetSelection(i) |
---|
248 | |
---|
249 | def _on_select_model(self,event): |
---|
250 | """ |
---|
251 | react when a model is selected from page's combo box |
---|
252 | post an event to its owner to draw an appropriate theory |
---|
253 | """ |
---|
254 | self.model_view.SetFocus() |
---|
255 | for item in self.model_list_box.itervalues(): |
---|
256 | name = item.__name__ |
---|
257 | if hasattr(item, "name"): |
---|
258 | name = item.name |
---|
259 | if name ==event.GetString(): |
---|
260 | model=item() |
---|
261 | self.model= model |
---|
262 | self.set_panel(model) |
---|
263 | self.name= name |
---|
264 | #self.manager.draw_model(model, name) |
---|
265 | self._draw_model() |
---|
266 | |
---|
267 | |
---|
268 | def get_model_box(self): |
---|
269 | """ return reference to combox box self.model""" |
---|
270 | return self.modelbox |
---|
271 | |
---|
272 | |
---|
273 | def get_param_list(self): |
---|
274 | """ |
---|
275 | @return self.param_toFit: list containing references to TextCtrl |
---|
276 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
277 | @raise: if return an empty list of parameter fit will nnote work |
---|
278 | properly so raise ValueError,"missing parameter to fit" |
---|
279 | """ |
---|
280 | if self.param_toFit !=[]: |
---|
281 | return self.param_toFit |
---|
282 | else: |
---|
283 | raise ValueError,"missing parameter to fit" |
---|
284 | |
---|
285 | |
---|
286 | def set_panel(self,model): |
---|
287 | """ |
---|
288 | Build the panel from the model content |
---|
289 | @param model: the model selected in combo box for fitting purpose |
---|
290 | """ |
---|
291 | |
---|
292 | self.sizer2.Clear(True) |
---|
293 | self.sizer1.Clear(True) |
---|
294 | self.sizer5.Clear(True) |
---|
295 | self.parameters = [] |
---|
296 | self.param_toFit=[] |
---|
297 | self.model = model |
---|
298 | keys = self.model.getParamList() |
---|
299 | keys.sort() |
---|
300 | description=None |
---|
301 | if hasattr(self.model,'description'): |
---|
302 | description =model.description |
---|
303 | disp_list=self.model.getDispParamList() |
---|
304 | ip=0 |
---|
305 | iq=1 |
---|
306 | if len(disp_list)>0: |
---|
307 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
308 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
309 | ip += 1 |
---|
310 | values = wx.StaticText(self, -1, 'Values') |
---|
311 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
312 | |
---|
313 | disp_list.sort() |
---|
314 | #print "went here",self.model.name,model.description |
---|
315 | iy = 1 |
---|
316 | ix = 0 |
---|
317 | self.cb0 = wx.StaticText(self, -1,'Model Description:') |
---|
318 | self.sizer1.Add(self.cb0,(iy, ix),(1,1),\ |
---|
319 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
320 | iy += 1 |
---|
321 | |
---|
322 | self.cb01 = wx.StaticText(self, -1,str(description),style=wx.ALIGN_LEFT) |
---|
323 | self.cb01.Wrap(400) |
---|
324 | #self.cb01 = wx.StaticText(self, -1,str(description),(45, 25),style=wx.ALIGN_LEFT) |
---|
325 | |
---|
326 | self.sizer1.Add(self.cb01,(iy, ix),(1,1),\ |
---|
327 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
328 | ix = 0 |
---|
329 | iy = 1 |
---|
330 | self.cb1 = wx.StaticText(self, -1,'Parameters') |
---|
331 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
332 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
333 | ix +=1 |
---|
334 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
335 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
336 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
337 | ix +=1 |
---|
338 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
339 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
340 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
341 | self.text2_4.Hide() |
---|
342 | for item in keys: |
---|
343 | if not item in disp_list: |
---|
344 | iy += 1 |
---|
345 | ix = 0 |
---|
346 | cb=wx.StaticText(self, -1, item) |
---|
347 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
348 | ix += 1 |
---|
349 | value= self.model.getParam(item) |
---|
350 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
351 | ctl1.SetValue(str (format_number(value))) |
---|
352 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
353 | ctl1.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
354 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
355 | ix +=1 |
---|
356 | |
---|
357 | # Units |
---|
358 | try: |
---|
359 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
360 | except: |
---|
361 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
362 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
363 | else: |
---|
364 | ip = 0 |
---|
365 | iq += 1 |
---|
366 | cb=wx.StaticText(self, -1, item) |
---|
367 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
368 | wx.EVT_CHECKBOX(self, cb.GetId(), self._on_select_model) |
---|
369 | |
---|
370 | ip += 1 |
---|
371 | value= self.model.getParam(item) |
---|
372 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
373 | ctl1.SetValue(str (format_number(value))) |
---|
374 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
375 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
376 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
377 | #save data |
---|
378 | self.parameters.append([cb,ctl1]) |
---|
379 | iy+=1 |
---|
380 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
381 | |
---|
382 | #Display units text on panel |
---|
383 | for item in keys: |
---|
384 | if self.model.details[item][0]!='': |
---|
385 | self.text2_4.Show() |
---|
386 | break |
---|
387 | else: |
---|
388 | self.text2_4.Hide() |
---|
389 | self.vbox.Layout() |
---|
390 | self.GrandParent.GetSizer().Layout() |
---|
391 | |
---|
392 | |
---|
393 | def _onparamEnter(self,event): |
---|
394 | """ |
---|
395 | when enter value on panel redraw model according to changed |
---|
396 | """ |
---|
397 | self.set_model_parameter() |
---|
398 | |
---|
399 | def set_model_parameter(self): |
---|
400 | if len(self.parameters) !=0 and self.model !=None: |
---|
401 | # Flag to register when a parameter has changed. |
---|
402 | is_modified = False |
---|
403 | for item in self.parameters: |
---|
404 | try: |
---|
405 | name=str(item[0].GetLabelText()) |
---|
406 | value= float(item[1].GetValue()) |
---|
407 | # If the value of the parameter has changed, |
---|
408 | # update the model and set the is_modified flag |
---|
409 | if value != self.model.getParam(name): |
---|
410 | self.model.setParam(name,value) |
---|
411 | is_modified = True |
---|
412 | except: |
---|
413 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
414 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
415 | |
---|
416 | # Here we should check whether the boundaries have been modified. |
---|
417 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
418 | # set the is_modified flag to True |
---|
419 | if float(self.xmin.GetValue()) != self.qmin: |
---|
420 | self.qmin = float(self.xmin.GetValue()) |
---|
421 | is_modified = True |
---|
422 | if float(self.xmax.GetValue()) != self.qmax: |
---|
423 | self.qmax = float(self.xmax.GetValue()) |
---|
424 | is_modified = True |
---|
425 | |
---|
426 | if float(self.npts.GetValue()) != self.num_points: |
---|
427 | self.num_points = float(self.npts.GetValue()) |
---|
428 | is_modified = True |
---|
429 | |
---|
430 | if is_modified: |
---|
431 | self._draw_model() |
---|
432 | |
---|
433 | def _draw_model(self): |
---|
434 | """ |
---|
435 | Method to draw or refresh a plotted model. |
---|
436 | The method will use the data member from the model page |
---|
437 | to build a call to the fitting perspective manager. |
---|
438 | |
---|
439 | [Note to coder: This way future changes will be done in only one place.] |
---|
440 | """ |
---|
441 | self.manager.draw_model(self.model, self.model.name, |
---|
442 | qmin=self.qmin, qmax=self.qmax, |
---|
443 | qstep= self.num_points, |
---|
444 | enable2D=self.enable2D) |
---|
445 | |
---|
446 | |
---|
447 | |
---|