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