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