1 | |
---|
2 | import wx |
---|
3 | import wx.lib.newevent |
---|
4 | import numpy |
---|
5 | import copy |
---|
6 | import math |
---|
7 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
8 | |
---|
9 | from sans.guicomm.events import StatusEvent |
---|
10 | from sans.guiframe.utils import format_number |
---|
11 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
12 | _BOX_WIDTH = 80 |
---|
13 | |
---|
14 | import basepage |
---|
15 | from basepage import BasicPage |
---|
16 | |
---|
17 | |
---|
18 | class ModelPage(BasicPage): |
---|
19 | """ |
---|
20 | FitPanel class contains fields allowing to display results when |
---|
21 | fitting a model and one data |
---|
22 | @note: For Fit to be performed the user should check at least one parameter |
---|
23 | on fit Panel window. |
---|
24 | |
---|
25 | """ |
---|
26 | def __init__(self,parent, page_info, name=""): |
---|
27 | BasicPage.__init__(self, parent, page_info,name) |
---|
28 | """ |
---|
29 | Initialization of the Panel |
---|
30 | """ |
---|
31 | self.name ="Model" |
---|
32 | page_info.page_name= "Model Page" |
---|
33 | self._fill_model_sizer( self.sizer1) |
---|
34 | self._fill_range_sizer() |
---|
35 | if hasattr(self.page_info,"model"): |
---|
36 | model=self.page_info.model |
---|
37 | description="" |
---|
38 | if model!=None: |
---|
39 | description = self.page_info.model.description |
---|
40 | self.set_model_param_sizer(self.model) |
---|
41 | self.set_model_description(description,self.sizer2) |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | def _on_display_description(self, event): |
---|
47 | """ |
---|
48 | Show or Hide description |
---|
49 | @param event: wx.EVT_RADIOBUTTON |
---|
50 | """ |
---|
51 | self._on_display_description_helper() |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | def _on_display_description_helper(self): |
---|
56 | """ |
---|
57 | Show or Hide description |
---|
58 | @param event: wx.EVT_RADIOBUTTON |
---|
59 | """ |
---|
60 | ## save state of radiobox |
---|
61 | self.page_info. save_radiobox_state( self.description_hide ) |
---|
62 | self.page_info. save_radiobox_state( self.description_show ) |
---|
63 | ## Show description |
---|
64 | if not self.description_show.GetValue(): |
---|
65 | self.sizer_description.Clear(True) |
---|
66 | |
---|
67 | else: |
---|
68 | model=self.page_info.model |
---|
69 | description="" |
---|
70 | if model!=None: |
---|
71 | description = self.page_info.model.description |
---|
72 | self.description = wx.StaticText( self,-1,str(description) ) |
---|
73 | self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 ) |
---|
74 | |
---|
75 | self.Layout() |
---|
76 | |
---|
77 | |
---|
78 | def _fill_range_sizer(self): |
---|
79 | """ |
---|
80 | Fill the sizer containing the plotting range |
---|
81 | add access to npts |
---|
82 | """ |
---|
83 | sizer_npts= wx.GridSizer(1, 1,5, 5) |
---|
84 | |
---|
85 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
86 | self.npts.SetValue(format_number(self.num_points)) |
---|
87 | self.npts.SetToolTipString("Number of point to plot.") |
---|
88 | self.npts.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
89 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
90 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
91 | |
---|
92 | sizer_npts.Add(wx.StaticText(self, -1, 'Npts'),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
93 | sizer_npts.Add(self.npts,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
94 | self._set_range_sizer( title="Plotted Q Range", object= sizer_npts) |
---|
95 | |
---|
96 | def _on_select_model(self, event): |
---|
97 | """ |
---|
98 | call back for model selection |
---|
99 | """ |
---|
100 | self._on_select_model_helper() |
---|
101 | self.set_model_param_sizer(self.model) |
---|
102 | self._set_sizer_gaussian() |
---|
103 | self.name = self.model.name |
---|
104 | self.model_view.SetFocus() |
---|
105 | self.parent.model_page.name = self.name |
---|
106 | self.parent.draw_model_name = self.name |
---|
107 | self._draw_model() |
---|
108 | |
---|
109 | def _fill_model_sizer(self, sizer): |
---|
110 | """ |
---|
111 | fill sizer containing model info |
---|
112 | """ |
---|
113 | id = wx.NewId() |
---|
114 | self.model_view =wx.Button(self,id,'View 2D') |
---|
115 | self.model_view.Bind(wx.EVT_BUTTON, self._onModel2D,id=id) |
---|
116 | self.model_view.SetToolTipString("View model in 2D") |
---|
117 | |
---|
118 | ## class base method to add view 2d button |
---|
119 | self._set_model_sizer(sizer=sizer, title="Model",object= self.model_view ) |
---|
120 | |
---|
121 | |
---|
122 | def _set_sizer_gaussian(self): |
---|
123 | """ |
---|
124 | draw sizer with gaussian dispersity parameters |
---|
125 | """ |
---|
126 | self.fittable_param=[] |
---|
127 | self.fixed_param=[] |
---|
128 | |
---|
129 | self.sizer4_4.Clear(True) |
---|
130 | if self.model==None: |
---|
131 | ##no model is selected |
---|
132 | return |
---|
133 | if not self.enable_disp.GetValue(): |
---|
134 | ## the user didn't select dispersity display |
---|
135 | return |
---|
136 | self._reset_dispersity() |
---|
137 | # Create the dispersion objects |
---|
138 | for item in self.model.dispersion.keys(): |
---|
139 | disp_model = GaussianDispersion() |
---|
140 | self._disp_obj_dict[item] = disp_model |
---|
141 | self.model.set_dispersion(item, disp_model) |
---|
142 | |
---|
143 | |
---|
144 | ix=0 |
---|
145 | iy=1 |
---|
146 | disp = wx.StaticText(self, -1, 'Names') |
---|
147 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
148 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
149 | ix += 1 |
---|
150 | values = wx.StaticText(self, -1, 'Values') |
---|
151 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
152 | |
---|
153 | ix += 1 |
---|
154 | npts = wx.StaticText(self, -1, 'Npts') |
---|
155 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
156 | ix += 1 |
---|
157 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
158 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
159 | |
---|
160 | |
---|
161 | for item in self.model.dispersion.keys(): |
---|
162 | |
---|
163 | self.disp_cb_dict[item]= None |
---|
164 | name1=item+".width" |
---|
165 | name2=item+".npts" |
---|
166 | name3=item+".nsigmas" |
---|
167 | iy += 1 |
---|
168 | for p in self.model.dispersion[item].keys(): |
---|
169 | if p=="width": |
---|
170 | ix = 0 |
---|
171 | name = wx.StaticText(self, -1, name1) |
---|
172 | self.sizer4_4.Add( name,( iy, ix),(1,1), |
---|
173 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
174 | ix = 1 |
---|
175 | value= self.model.getParam(name1) |
---|
176 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
177 | style=wx.TE_PROCESS_ENTER) |
---|
178 | |
---|
179 | ctl1.SetValue(str (format_number(value))) |
---|
180 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
181 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
182 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
183 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
184 | |
---|
185 | self.fittable_param.append([None,name1,ctl1,None, |
---|
186 | None, None, None,None]) |
---|
187 | |
---|
188 | elif p=="npts": |
---|
189 | ix =2 |
---|
190 | value= self.model.getParam(name2) |
---|
191 | Tctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
192 | style=wx.TE_PROCESS_ENTER) |
---|
193 | |
---|
194 | Tctl1.SetValue(str (format_number(value))) |
---|
195 | Tctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
196 | Tctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
197 | Tctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
198 | self.sizer4_4.Add(Tctl1, (iy,ix),(1,1), |
---|
199 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
200 | |
---|
201 | self.fixed_param.append([None,name2, Tctl1,None,None, |
---|
202 | None, None,None]) |
---|
203 | |
---|
204 | elif p=="nsigmas": |
---|
205 | |
---|
206 | ix =3 |
---|
207 | value= self.model.getParam(name3) |
---|
208 | Tctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
209 | style=wx.TE_PROCESS_ENTER) |
---|
210 | Tctl2.SetValue(str (format_number(value))) |
---|
211 | Tctl2.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
212 | Tctl2.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
213 | Tctl2.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
214 | self.sizer4_4.Add(Tctl2, (iy,ix),(1,1), |
---|
215 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
216 | ix +=1 |
---|
217 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
218 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
219 | |
---|
220 | self.fixed_param.append([None,name3, Tctl2, |
---|
221 | None,None, None, None,None]) |
---|
222 | |
---|
223 | msg = " Selected Distribution: Gaussian" |
---|
224 | wx.PostEvent(self.parent.parent, StatusEvent( status= msg )) |
---|
225 | ix =0 |
---|
226 | iy +=1 |
---|
227 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
228 | self.sizer4_4.Layout() |
---|
229 | self.sizer4.Layout() |
---|
230 | self.SetScrollbars(20,20,200,100) |
---|
231 | |
---|
232 | def _onModel2D(self, event): |
---|
233 | """ |
---|
234 | call manager to plot model in 2D |
---|
235 | """ |
---|
236 | # If the 2D display is not currently enabled, plot the model in 2D |
---|
237 | # and set the enable2D flag. |
---|
238 | if self.fitrange: |
---|
239 | self.enable2D = True |
---|
240 | |
---|
241 | if self.enable2D: |
---|
242 | self._draw_model() |
---|
243 | self.model_view.Disable() |
---|
244 | |
---|
245 | def select_model(self, model, name): |
---|
246 | """ |
---|
247 | Select a new model |
---|
248 | @param model: model object |
---|
249 | """ |
---|
250 | self.model = model |
---|
251 | self.parent.model_page.name = name |
---|
252 | self.parent.draw_model_name = name |
---|
253 | |
---|
254 | self.set_model_param_sizer(self.model) |
---|
255 | self._draw_model() |
---|
256 | ## keep the sizer view consistent with the model menu selecting |
---|
257 | self._set_model_sizer_selection( self.model ) |
---|
258 | |
---|
259 | self.model_view.SetFocus() |
---|
260 | |
---|
261 | |
---|
262 | def set_model_description(self,description,sizer): |
---|
263 | """ |
---|
264 | fill a sizer with description |
---|
265 | @param description: of type string |
---|
266 | @param sizer: wx.BoxSizer() |
---|
267 | """ |
---|
268 | |
---|
269 | sizer.Clear(True) |
---|
270 | box_description= wx.StaticBox(self, -1, 'Model Description') |
---|
271 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
272 | |
---|
273 | sizer_selection=wx.BoxSizer(wx.HORIZONTAL) |
---|
274 | |
---|
275 | self.description_show = wx.RadioButton(self, -1, 'Show', style=wx.RB_GROUP) |
---|
276 | self.description_hide = wx.RadioButton(self, -1, 'Hide') |
---|
277 | |
---|
278 | if description=="": |
---|
279 | self.description_hide.SetValue(True) |
---|
280 | description=" Description unavailable. Click for details" |
---|
281 | |
---|
282 | self.description = wx.StaticText( self,-1,str(description) ) |
---|
283 | |
---|
284 | self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description, |
---|
285 | id=self.description_hide.GetId() ) |
---|
286 | |
---|
287 | self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description, |
---|
288 | id=self.description_show.GetId() ) |
---|
289 | |
---|
290 | self.model_description = wx.Button(self, -1, "More Details") |
---|
291 | self.model_description.SetToolTipString("See more description in help menu.") |
---|
292 | |
---|
293 | self.page_info.save_radiobox_state( self.description_hide ) |
---|
294 | self.page_info.save_radiobox_state( self.description_show ) |
---|
295 | |
---|
296 | sizer_selection.Add( self.description_show ) |
---|
297 | sizer_selection.Add( (20,20)) |
---|
298 | sizer_selection.Add( self.description_hide ) |
---|
299 | sizer_selection.Add( (20,20)) |
---|
300 | sizer_selection.Add( self.model_description ) |
---|
301 | |
---|
302 | |
---|
303 | self.sizer_description=wx.BoxSizer(wx.HORIZONTAL) |
---|
304 | self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 ) |
---|
305 | |
---|
306 | boxsizer1.Add( sizer_selection) |
---|
307 | boxsizer1.Add( (20,20)) |
---|
308 | boxsizer1.Add( self.sizer_description) |
---|
309 | |
---|
310 | |
---|
311 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
312 | sizer.Layout() |
---|
313 | |
---|
314 | |
---|
315 | |
---|
316 | def set_range(self, qmin_x, qmax_x, npts): |
---|
317 | """ |
---|
318 | Set the range for the plotted models |
---|
319 | @param qmin: minimum Q |
---|
320 | @param qmax: maximum Q |
---|
321 | @param npts: number of Q bins |
---|
322 | """ |
---|
323 | # Set the data members |
---|
324 | self.qmin_x = qmin_x |
---|
325 | self.qmax_x = qmax_x |
---|
326 | self.num_points = npts |
---|
327 | |
---|
328 | # Set the controls |
---|
329 | self.qmin.SetValue(format_number(self.qmin_x)) |
---|
330 | self.qmax.SetValue(format_number(self.qmax_x)) |
---|
331 | self.npts.SetValue(format_number(self.num_points)) |
---|
332 | |
---|
333 | |
---|
334 | def set_model_param_sizer(self, model): |
---|
335 | """ |
---|
336 | Build the panel from the model content |
---|
337 | @param model: the model selected in combo box for fitting purpose |
---|
338 | """ |
---|
339 | self.sizer3.Clear(True) |
---|
340 | self.parameters = [] |
---|
341 | self.param_toFit=[] |
---|
342 | self.fixed_param=[] |
---|
343 | |
---|
344 | if model ==None: |
---|
345 | ##no model avaiable to draw sizer |
---|
346 | return |
---|
347 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
348 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
349 | sizer = wx.GridBagSizer(5,5) |
---|
350 | |
---|
351 | self.model = model |
---|
352 | self.set_model_description(self.model.description,self.sizer2) |
---|
353 | |
---|
354 | keys = self.model.getParamList() |
---|
355 | #list of dispersion paramaters |
---|
356 | self.disp_list=self.model.getDispParamList() |
---|
357 | |
---|
358 | keys.sort() |
---|
359 | |
---|
360 | iy = 1 |
---|
361 | ix = 0 |
---|
362 | self.text1_2 = wx.StaticText(self, -1, 'Names') |
---|
363 | sizer.Add(self.text1_2,(iy, ix),(1,1),\ |
---|
364 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
365 | ix +=1 |
---|
366 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
367 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
368 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
369 | ix +=1 |
---|
370 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
371 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
372 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
373 | self.text2_4.Hide() |
---|
374 | |
---|
375 | |
---|
376 | for item in keys: |
---|
377 | if not item in self.disp_list: |
---|
378 | iy += 1 |
---|
379 | ix = 0 |
---|
380 | |
---|
381 | name = wx.StaticText(self, -1,item) |
---|
382 | sizer.Add( name,( iy, ix),(1,1), |
---|
383 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
384 | |
---|
385 | ix += 1 |
---|
386 | value= self.model.getParam(item) |
---|
387 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
388 | style=wx.TE_PROCESS_ENTER) |
---|
389 | |
---|
390 | ctl1.SetValue(str (format_number(value))) |
---|
391 | ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
392 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
393 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
394 | |
---|
395 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
396 | |
---|
397 | ix +=1 |
---|
398 | # Units |
---|
399 | try: |
---|
400 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
401 | except: |
---|
402 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
403 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
404 | |
---|
405 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
406 | self.parameters.append([None,item, ctl1, |
---|
407 | None,None, None, None,None]) |
---|
408 | |
---|
409 | iy+=1 |
---|
410 | sizer.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
411 | |
---|
412 | #Display units text on panel |
---|
413 | for item in keys: |
---|
414 | if self.model.details[item][0]!='': |
---|
415 | self.text2_4.Show() |
---|
416 | break |
---|
417 | else: |
---|
418 | self.text2_4.Hide() |
---|
419 | |
---|
420 | boxsizer1.Add(sizer) |
---|
421 | |
---|
422 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
423 | self.sizer3.Layout() |
---|
424 | self.SetScrollbars(20,20,200,100) |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | |
---|
429 | class HelpWindow(wx.Frame): |
---|
430 | def __init__(self, parent, id, title): |
---|
431 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
432 | |
---|
433 | from sans.models.CylinderModel import CylinderModel |
---|
434 | model = CylinderModel() |
---|
435 | #from sans.models.LineModel import LineModel |
---|
436 | #model = LineModel() |
---|
437 | from pageInfo import PageInfo |
---|
438 | myinfo = PageInfo(self,model) |
---|
439 | from models import ModelList |
---|
440 | mylist= ModelList() |
---|
441 | |
---|
442 | from sans.models.SphereModel import SphereModel |
---|
443 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
444 | from sans.models.DebyeModel import DebyeModel |
---|
445 | from sans.models.LineModel import LineModel |
---|
446 | name= "shapes" |
---|
447 | list1= [SphereModel] |
---|
448 | mylist.set_list( name, list1) |
---|
449 | |
---|
450 | name= "Shape-independent" |
---|
451 | list1= [DebyeModel] |
---|
452 | mylist.set_list( name, list1) |
---|
453 | |
---|
454 | name= "Structure Factors" |
---|
455 | list1= [SquareWellStructure] |
---|
456 | mylist.set_list( name, list1) |
---|
457 | |
---|
458 | name= "Added models" |
---|
459 | list1= [LineModel] |
---|
460 | mylist.set_list( name, list1) |
---|
461 | |
---|
462 | myinfo.model_list_box = mylist.get_list() |
---|
463 | |
---|
464 | self.page = ModelPage(self, myinfo) |
---|
465 | |
---|
466 | |
---|
467 | |
---|
468 | self.Centre() |
---|
469 | self.Show(True) |
---|
470 | |
---|
471 | |
---|
472 | |
---|
473 | if __name__=="__main__": |
---|
474 | app = wx.App() |
---|
475 | HelpWindow(None, -1, 'HelpWindow') |
---|
476 | app.MainLoop() |
---|
477 | |
---|