source: sasview/sansview/perspectives/fitting/modelpage.py @ 77e23a2

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 77e23a2 was 77e23a2, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add fit with parameter range when selecting park only

  • Property mode set to 100644
File size: 17.5 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_KILL_FOCUS, self._onparamEnter)
181                    ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
182                    self.sizer4_4.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
183                   
184                    self.fittable_param.append([None,name1,ctl1,None,
185                                                None, None, None,None])
186                   
187                elif p=="npts":
188                        ix =2
189                        value= self.model.getParam(name2)
190                        Tctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
191                                            style=wx.TE_PROCESS_ENTER)
192                       
193                        Tctl1.SetValue(str (format_number(value)))
194                        Tctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
195                        Tctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
196                        Tctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
197                        self.sizer4_4.Add(Tctl1, (iy,ix),(1,1),
198                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
199                       
200                        self.fixed_param.append([None,name2, Tctl1,None,None,
201                                                  None, None,None])
202               
203                elif p=="nsigmas":
204                       
205                        ix =3 
206                        value= self.model.getParam(name3)
207                        Tctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
208                                            style=wx.TE_PROCESS_ENTER)
209                        Tctl2.SetValue(str (format_number(value)))
210                        Tctl2.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
211                        Tctl2.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
212                        Tctl2.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
213                        self.sizer4_4.Add(Tctl2, (iy,ix),(1,1),
214                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
215                        ix +=1
216                        self.sizer4_4.Add((20,20), (iy,ix),(1,1),
217                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
218                       
219                        self.fixed_param.append([None,name3, Tctl2,
220                                                 None,None, None, None,None])
221       
222        msg = " Selected Distribution: Gaussian"       
223        wx.PostEvent(self.parent.parent, StatusEvent( status= msg ))   
224        ix =0
225        iy +=1 
226        self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)   
227        self.sizer4_4.Layout()
228        self.sizer4.Layout()
229        self.SetScrollbars(20,20,200,100)
230             
231    def _onModel2D(self, event):
232        """
233         call manager to plot model in 2D
234        """
235        # If the 2D display is not currently enabled, plot the model in 2D
236        # and set the enable2D flag.
237        if self.fitrange:
238            self.enable2D = True
239           
240        if self.enable2D:
241            self._draw_model()
242            self.model_view.Disable()
243   
244    def select_model(self, model, name):
245        """
246            Select a new model
247            @param model: model object
248        """
249        self.model = model
250        self.parent.model_page.name = name
251        self.parent.draw_model_name = name
252       
253        self.set_model_param_sizer(self.model)
254        self._draw_model()
255        ## keep the sizer view consistent with the model menu selecting
256        self._set_model_sizer_selection( self.model )
257       
258        self.model_view.SetFocus()
259                         
260   
261    def set_model_description(self,description,sizer):
262        """
263            fill a sizer with description
264            @param description: of type string
265            @param sizer: wx.BoxSizer()
266        """
267   
268        sizer.Clear(True)
269        box_description= wx.StaticBox(self, -1, 'Model Description')
270        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
271           
272        sizer_selection=wx.BoxSizer(wx.HORIZONTAL)
273     
274        self.description_show = wx.RadioButton(self, -1, 'Show', style=wx.RB_GROUP)
275        self.description_hide = wx.RadioButton(self, -1, 'Hide')
276       
277        if description=="":
278            self.description_hide.SetValue(True)
279            description=" Description unavailable. Click for details"
280           
281        self.description = wx.StaticText( self,-1,str(description) )
282       
283        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
284                   id=self.description_hide.GetId() )
285       
286        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
287                   id=self.description_show.GetId() )
288       
289        self.model_description = wx.Button(self, -1, "More Details")
290        self.model_description.SetToolTipString("See more description in help menu.")
291       
292        self.page_info.save_radiobox_state( self.description_hide )
293        self.page_info.save_radiobox_state( self.description_show )
294       
295        sizer_selection.Add( self.description_show )
296        sizer_selection.Add( (20,20)) 
297        sizer_selection.Add( self.description_hide )
298        sizer_selection.Add( (20,20)) 
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       
305        boxsizer1.Add( sizer_selection) 
306        boxsizer1.Add( (20,20)) 
307        boxsizer1.Add( self.sizer_description) 
308   
309
310        sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
311        sizer.Layout()
312   
313   
314   
315    def set_range(self, qmin_x, qmax_x, npts):
316        """
317            Set the range for the plotted models
318            @param qmin: minimum Q
319            @param qmax: maximum Q
320            @param npts: number of Q bins
321        """
322        # Set the data members
323        self.qmin_x = qmin_x
324        self.qmax_x = qmax_x
325        self.num_points = npts
326       
327        # Set the controls
328        self.qmin.SetValue(format_number(self.qmin_x))
329        self.qmax.SetValue(format_number(self.qmax_x))
330        self.npts.SetValue(format_number(self.num_points))
331       
332       
333    def set_model_param_sizer(self, model):
334        """
335            Build the panel from the model content
336            @param model: the model selected in combo box for fitting purpose
337        """
338        self.sizer3.Clear(True)
339        self.parameters = []
340        self.param_toFit=[]
341        self.fixed_param=[]
342       
343        if model ==None:
344            ##no model avaiable to draw sizer
345            return
346        box_description= wx.StaticBox(self, -1,str("Model Parameters"))
347        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
348        sizer = wx.GridBagSizer(5,5)
349       
350        self.model = model
351        self.set_model_description(self.model.description,self.sizer2)
352       
353        keys = self.model.getParamList()
354        #list of dispersion paramaters
355        self.disp_list=self.model.getDispParamList()
356       
357        keys.sort()
358   
359        iy = 1
360        ix = 0
361        self.text1_2 = wx.StaticText(self, -1, 'Names')
362        sizer.Add(self.text1_2,(iy, ix),(1,1),\
363                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
364        ix +=1
365        self.text2_2 = wx.StaticText(self, -1, 'Values')
366        sizer.Add(self.text2_2,(iy, ix),(1,1),\
367                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
368        ix +=1
369        self.text2_4 = wx.StaticText(self, -1, 'Units')
370        sizer.Add(self.text2_4,(iy, ix),(1,1),\
371                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
372        self.text2_4.Hide()
373       
374
375        for item in keys:
376            if not item in self.disp_list:
377                iy += 1
378                ix = 0
379   
380                name = wx.StaticText(self, -1,item)
381                sizer.Add( name,( iy, ix),(1,1),
382                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
383               
384                ix += 1
385                value= self.model.getParam(item)
386                ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
387                    style=wx.TE_PROCESS_ENTER)
388               
389                ctl1.SetValue(str (format_number(value)))
390                ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
391                ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
392                ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
393               
394                sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
395       
396                ix +=1
397                # Units
398                try:
399                    units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT)
400                except:
401                    units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT)
402                sizer.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
403               
404                ##[cb state, name, value, "+/-", error of fit, min, max , units]
405                self.parameters.append([None,item, ctl1,
406                                        None,None, None, None,None])
407               
408        iy+=1
409        sizer.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
410       
411        #Display units text on panel
412        for item in keys:   
413            if self.model.details[item][0]!='':
414                self.text2_4.Show()
415                break
416            else:
417                self.text2_4.Hide()
418       
419        boxsizer1.Add(sizer)
420       
421        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
422        self.sizer3.Layout()
423        self.SetScrollbars(20,20,200,100)
424   
425 
426           
427       
428class HelpWindow(wx.Frame):
429    def __init__(self, parent, id, title):
430        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
431       
432        from sans.models.CylinderModel import CylinderModel
433        model = CylinderModel()
434        #from sans.models.LineModel import LineModel
435        #model = LineModel()
436        from pageInfo import PageInfo
437        myinfo = PageInfo(self,model)
438        from models import ModelList
439        mylist= ModelList()
440       
441        from sans.models.SphereModel import SphereModel
442        from sans.models.SquareWellStructure import SquareWellStructure
443        from sans.models.DebyeModel import DebyeModel
444        from sans.models.LineModel import LineModel
445        name= "shapes"
446        list1= [SphereModel]
447        mylist.set_list( name, list1)
448       
449        name= "Shape-independent"
450        list1= [DebyeModel]
451        mylist.set_list( name, list1)
452       
453        name= "Structure Factors"
454        list1= [SquareWellStructure]
455        mylist.set_list( name, list1)
456       
457        name= "Added models"
458        list1= [LineModel]
459        mylist.set_list( name, list1)
460       
461        myinfo.model_list_box = mylist.get_list()
462       
463        self.page = ModelPage(self, myinfo) 
464       
465       
466       
467        self.Centre()
468        self.Show(True)
469
470
471   
472if __name__=="__main__":
473    app = wx.App()
474    HelpWindow(None, -1, 'HelpWindow')
475    app.MainLoop()
476               
Note: See TracBrowser for help on using the repository browser.