source: sasview/sansview/perspectives/fitting/modelpage.py @ 376916c

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 376916c was 376916c, checked in by Gervaise Alina <gervyh@…>, 15 years ago

resettting gaussian dispersity —fixed
reducing access to molde multplication —-done as request

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