source: sasview/sansview/perspectives/fitting/modelpage.py @ a81e613

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

hide and show description fixed

  • Property mode set to 100644
File size: 17.6 KB
RevLine 
[c77d859]1
[1b07935d]2import wx
[bb18ef1]3import wx.lib.newevent
[1b07935d]4import numpy
5import copy
[81e4cf7]6import math
7from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion
[26bf293]8
[1b07935d]9from sans.guicomm.events import StatusEvent   
[26bf293]10from sans.guiframe.utils import format_number
[1b07935d]11(ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent()
12_BOX_WIDTH = 80
13
[c77d859]14import basepage
15from basepage import BasicPage
[1b07935d]16
[26bf293]17
[c77d859]18class ModelPage(BasicPage):
[1b07935d]19    """
[fbc3e04]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.
[1b07935d]24 
25    """
[fa58441]26
[b787e68c]27    def __init__(self,parent, page_info):
[cfc0913]28        BasicPage.__init__(self, parent, page_info)
[1b07935d]29        """
30            Initialization of the Panel
31        """
[c77d859]32        self._fill_model_sizer( self.sizer1) 
33        self._fill_range_sizer() 
[fa58441]34         
[cfc0913]35        description=""
36        if self.model!=None:
37            description = self.model.description
[a074145]38           
39            self.select_model(self.model, self.model.name)
[c77d859]40            self.set_model_description(description,self.sizer2)
[a074145]41           
42     
[bb18ef1]43       
[ef8b580]44       
[c77d859]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()
[fbc3e04]51       
[24ea33c]52       
[00561739]53       
[c77d859]54    def _on_display_description_helper(self):
55        """
56            Show or Hide description
57            @param event: wx.EVT_RADIOBUTTON
58        """
[a074145]59       
[c77d859]60        ## Show description
[a81e613]61        if self.description_hide.GetValue():
[c77d859]62            self.sizer_description.Clear(True)
63           
64        else:
65            description=""
[cfc0913]66            if self.model!=None:
67                description = self.model.description
[a074145]68               
[c77d859]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   
[d15c0202]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.")
[1328e03]85        self.npts.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[dcf29d7]86        self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
[d15c0202]87        self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter)
[bb18ef1]88       
[c77d859]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)
[fbc3e04]92       
[cfc0913]93       
[c77d859]94    def _on_select_model(self, event): 
95        """
96             call back for model selection
97        """   
[59a7f2d]98        self._on_select_model_helper() 
[3b605bb]99        self.select_model(self.model, self.model.name)
100       
[c77d859]101       
102    def _fill_model_sizer(self, sizer):
[26bf293]103        """
[c77d859]104            fill sizer containing model info
[26bf293]105        """
[c77d859]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")
[26bf293]110       
[fa58441]111        ## class base method  to add view 2d button   
[c77d859]112        self._set_model_sizer(sizer=sizer, title="Model",object= self.model_view )   
113   
114 
115    def _set_sizer_gaussian(self):
[26bf293]116        """
[c77d859]117            draw sizer with gaussian dispersity parameters
[26bf293]118        """
[c77d859]119        self.fittable_param=[]
120        self.fixed_param=[]
[376916c]121       
[c77d859]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
[bb18ef1]128            return 
[376916c]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       
[c77d859]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)
[59a7f2d]152       
153       
[c77d859]154        for item in self.model.dispersion.keys():
[376916c]155         
156            self.disp_cb_dict[item]= None
[c77d859]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)
[bb18ef1]171                   
[c77d859]172                    ctl1.SetValue(str (format_number(value)))
[1328e03]173                    ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[77e23a2]174                    ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
[c77d859]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)))
[1328e03]188                        Tctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[77e23a2]189                        Tctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
[c77d859]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])
[bb18ef1]196               
[c77d859]197                elif p=="nsigmas":
[59a7f2d]198                       
[c77d859]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)))
[1328e03]204                        Tctl2.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[77e23a2]205                        Tctl2.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
[c77d859]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])
[dc317d1]215       
[c77d859]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             
[7c845cb]225             
226    def reset_page(self, state):
227        """
228            reset the state
229        """
230        self.reset_page_helper(state)
231       
232       
[c77d859]233    def _onModel2D(self, event):
[86c1832]234        """
235         call manager to plot model in 2D
236        """
[24ea33c]237        # If the 2D display is not currently enabled, plot the model in 2D
238        # and set the enable2D flag.
[bb18ef1]239        if self.fitrange:
240            self.enable2D = True
241           
242        if self.enable2D:
[cfc68540]243            self._draw_model()
[e5af88b]244            self.model_view.Disable()
[c77d859]245   
[32d802f]246    def select_model(self, model, name):
[3dc83be]247        """
248            Select a new model
249            @param model: model object
250        """
[ef8b580]251        self.model = model
[c77d859]252        self.set_model_param_sizer(self.model)
[bb18ef1]253        self._draw_model()
[c77d859]254        ## keep the sizer view consistent with the model menu selecting
[376916c]255        self._set_model_sizer_selection( self.model )
[3b605bb]256        self.enable_disp.SetValue(False)
257        self.disable_disp.SetValue(True)
258        self._set_dipers_Param(event=None)
[c77d859]259        self.model_view.SetFocus()
260                         
261   
262    def set_model_description(self,description,sizer):
[1b07935d]263        """
[c77d859]264            fill a sizer with description
265            @param description: of type string
266            @param sizer: wx.BoxSizer()
[1b07935d]267        """
[c77d859]268   
269        sizer.Clear(True)
270        box_description= wx.StaticBox(self, -1, 'Model Description')
271        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
[bb18ef1]272           
[c77d859]273        sizer_selection=wx.BoxSizer(wx.HORIZONTAL)
[a81e613]274       
[6ea1b87]275        self.description_hide = wx.RadioButton(self, -1, 'Hide', style=wx.RB_GROUP)
[a81e613]276        self.description_show = wx.RadioButton(self, -1, 'Show')
277       
[51d47b5]278       
[c77d859]279        if description=="":
280            self.description_hide.SetValue(True)
281            description=" Description unavailable. Click for details"
[bb18ef1]282           
[c77d859]283        self.description = wx.StaticText( self,-1,str(description) )
[51d47b5]284       
[c77d859]285        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
286                   id=self.description_hide.GetId() )
[51d47b5]287       
[c77d859]288        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
289                   id=self.description_show.GetId() )
[51d47b5]290       
[fa58441]291        self.model_description = wx.Button(self,-1, label="Details")
[9d091ed]292        self.model_description.Bind(wx.EVT_BUTTON,self.on_button_clicked)
293        self.model_description.SetToolTipString("Click Model Functions in HelpWindow...")
[a074145]294     
[c77d859]295        sizer_selection.Add( self.description_show )
296        sizer_selection.Add( (20,20)) 
297        sizer_selection.Add( self.description_hide )
[fa58441]298        #sizer_selection.Add( (20,20))
299        sizer_selection.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,67)
[c77d859]300        sizer_selection.Add( self.model_description )
[fa58441]301                     
[c77d859]302         
303        self.sizer_description=wx.BoxSizer(wx.HORIZONTAL)
304        self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 )
305        boxsizer1.Add( sizer_selection) 
306        boxsizer1.Add( (20,20)) 
307        boxsizer1.Add( self.sizer_description) 
308   
[6ea1b87]309        self._on_display_description(event=None)
[c77d859]310        sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
311        sizer.Layout()
312   
[9d091ed]313    def on_button_clicked(self,event):
314        """
[b94a0a6]315        #On 'More details' button
316        """
[9d091ed]317        from helpPanel import  HelpWindow
[1d64748]318       
319        name = self.model.name
320        if name == None:
321            name = 'FuncHelp'
322        frame = HelpWindow(None, -1, name)   
[9d091ed]323        frame.Show(True)
324
[aa306e4]325    def set_range(self, qmin_x, qmax_x, npts):
[ed2ea6a]326        """
[c77d859]327            Set the range for the plotted models
328            @param qmin: minimum Q
329            @param qmax: maximum Q
330            @param npts: number of Q bins
[ed2ea6a]331        """
[c77d859]332        # Set the data members
333        self.qmin_x = qmin_x
334        self.qmax_x = qmax_x
335        self.num_points = npts
[26bf293]336       
[c77d859]337        # Set the controls
338        self.qmin.SetValue(format_number(self.qmin_x))
339        self.qmax.SetValue(format_number(self.qmax_x))
340        self.npts.SetValue(format_number(self.num_points))
[51d47b5]341       
[c77d859]342       
343    def set_model_param_sizer(self, model):
[51d47b5]344        """
[c77d859]345            Build the panel from the model content
346            @param model: the model selected in combo box for fitting purpose
[51d47b5]347        """
[c77d859]348        self.sizer3.Clear(True)
349        self.parameters = []
350        self.param_toFit=[]
351        self.fixed_param=[]
[1b07935d]352       
[c77d859]353        if model ==None:
354            ##no model avaiable to draw sizer
355            return
356        box_description= wx.StaticBox(self, -1,str("Model Parameters"))
357        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
358        sizer = wx.GridBagSizer(5,5)
[1b07935d]359       
[c77d859]360        self.model = model
361        self.set_model_description(self.model.description,self.sizer2)
362       
363        keys = self.model.getParamList()
364        #list of dispersion paramaters
365        self.disp_list=self.model.getDispParamList()
366       
367        keys.sort()
368   
369        iy = 1
370        ix = 0
371        self.text1_2 = wx.StaticText(self, -1, 'Names')
372        sizer.Add(self.text1_2,(iy, ix),(1,1),\
373                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
374        ix +=1
375        self.text2_2 = wx.StaticText(self, -1, 'Values')
376        sizer.Add(self.text2_2,(iy, ix),(1,1),\
377                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
378        ix +=1
379        self.text2_4 = wx.StaticText(self, -1, 'Units')
380        sizer.Add(self.text2_4,(iy, ix),(1,1),\
381                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
382        self.text2_4.Hide()
383       
384
385        for item in keys:
386            if not item in self.disp_list:
387                iy += 1
388                ix = 0
389   
390                name = wx.StaticText(self, -1,item)
391                sizer.Add( name,( iy, ix),(1,1),
392                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
393               
394                ix += 1
395                value= self.model.getParam(item)
396                ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
[8bd4dc0]397                    style=wx.TE_PROCESS_ENTER)
[26bf293]398               
[c77d859]399                ctl1.SetValue(str (format_number(value)))
[1328e03]400                ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[77e23a2]401                ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
[c77d859]402                ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
[1328e03]403               
[c77d859]404                sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
405       
406                ix +=1
407                # Units
[a5aaec9]408                try:
[c77d859]409                    units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT)
[db709e4]410                except:
[c77d859]411                    units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT)
412                sizer.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
[ed2ea6a]413               
[c77d859]414                ##[cb state, name, value, "+/-", error of fit, min, max , units]
415                self.parameters.append([None,item, ctl1,
416                                        None,None, None, None,None])
[ed2ea6a]417               
[c77d859]418        iy+=1
419        sizer.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
420       
421        #Display units text on panel
422        for item in keys:   
423            if self.model.details[item][0]!='':
424                self.text2_4.Show()
425                break
[bb18ef1]426            else:
[c77d859]427                self.text2_4.Hide()
428       
429        boxsizer1.Add(sizer)
430       
431        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
432        self.sizer3.Layout()
433        self.SetScrollbars(20,20,200,100)
[8bd4dc0]434   
[dfae8b3]435 
[2e10b70]436           
[c77d859]437       
438class HelpWindow(wx.Frame):
439    def __init__(self, parent, id, title):
440        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
[ef8b580]441       
[c77d859]442        from sans.models.CylinderModel import CylinderModel
443        model = CylinderModel()
444        #from sans.models.LineModel import LineModel
445        #model = LineModel()
[cfc0913]446        from fitpanel import PageInfo
[c77d859]447        myinfo = PageInfo(self,model)
448        from models import ModelList
449        mylist= ModelList()
[fbc3e04]450       
[c77d859]451        from sans.models.SphereModel import SphereModel
452        from sans.models.SquareWellStructure import SquareWellStructure
453        from sans.models.DebyeModel import DebyeModel
454        from sans.models.LineModel import LineModel
455        name= "shapes"
456        list1= [SphereModel]
457        mylist.set_list( name, list1)
[fbc3e04]458       
[c77d859]459        name= "Shape-independent"
460        list1= [DebyeModel]
461        mylist.set_list( name, list1)
462       
463        name= "Structure Factors"
464        list1= [SquareWellStructure]
465        mylist.set_list( name, list1)
466       
467        name= "Added models"
468        list1= [LineModel]
469        mylist.set_list( name, list1)
470       
471        myinfo.model_list_box = mylist.get_list()
472       
473        self.page = ModelPage(self, myinfo) 
474       
475       
476       
477        self.Centre()
478        self.Show(True)
479
480
481   
482if __name__=="__main__":
483    app = wx.App()
484    HelpWindow(None, -1, 'HelpWindow')
485    app.MainLoop()
486               
Note: See TracBrowser for help on using the repository browser.