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

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 fb8daaaf was fa58441, checked in by Jae Cho <jhjcho@…>, 15 years ago

minor changes (some names of titles)

  • 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
61        if not self.description_show.GetValue():
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)
274     
275        self.description_show = wx.RadioButton(self, -1, 'Show', style=wx.RB_GROUP)
276        self.description_hide = wx.RadioButton(self, -1, 'Hide')
[51d47b5]277       
[c77d859]278        if description=="":
279            self.description_hide.SetValue(True)
280            description=" Description unavailable. Click for details"
[bb18ef1]281           
[c77d859]282        self.description = wx.StaticText( self,-1,str(description) )
[51d47b5]283       
[c77d859]284        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
285                   id=self.description_hide.GetId() )
[51d47b5]286       
[c77d859]287        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
288                   id=self.description_show.GetId() )
[51d47b5]289       
[fa58441]290        self.model_description = wx.Button(self,-1, label="Details")
[9d091ed]291        self.model_description.Bind(wx.EVT_BUTTON,self.on_button_clicked)
292        self.model_description.SetToolTipString("Click Model Functions in HelpWindow...")
[a074145]293     
[c77d859]294        sizer_selection.Add( self.description_show )
295        sizer_selection.Add( (20,20)) 
296        sizer_selection.Add( self.description_hide )
[fa58441]297        #sizer_selection.Add( (20,20))
298        sizer_selection.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,67)
[c77d859]299        sizer_selection.Add( self.model_description )
[fa58441]300                     
[c77d859]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   
[fbc3e04]308
[c77d859]309        sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
310        sizer.Layout()
311   
[9d091ed]312    def on_button_clicked(self,event):
313        """
[b94a0a6]314        #On 'More details' button
315        """
[9d091ed]316        from helpPanel import  HelpWindow
[1d64748]317       
318        name = self.model.name
319        if name == None:
320            name = 'FuncHelp'
321        frame = HelpWindow(None, -1, name)   
[9d091ed]322        frame.Show(True)
323
[aa306e4]324    def set_range(self, qmin_x, qmax_x, npts):
[ed2ea6a]325        """
[c77d859]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
[ed2ea6a]330        """
[c77d859]331        # Set the data members
332        self.qmin_x = qmin_x
333        self.qmax_x = qmax_x
334        self.num_points = npts
[26bf293]335       
[c77d859]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))
[51d47b5]340       
[c77d859]341       
342    def set_model_param_sizer(self, model):
[51d47b5]343        """
[c77d859]344            Build the panel from the model content
345            @param model: the model selected in combo box for fitting purpose
[51d47b5]346        """
[c77d859]347        self.sizer3.Clear(True)
348        self.parameters = []
349        self.param_toFit=[]
350        self.fixed_param=[]
[1b07935d]351       
[c77d859]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)
[1b07935d]358       
[c77d859]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),
[8bd4dc0]396                    style=wx.TE_PROCESS_ENTER)
[26bf293]397               
[c77d859]398                ctl1.SetValue(str (format_number(value)))
[1328e03]399                ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[77e23a2]400                ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
[c77d859]401                ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
[1328e03]402               
[c77d859]403                sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
404       
405                ix +=1
406                # Units
[a5aaec9]407                try:
[c77d859]408                    units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT)
[db709e4]409                except:
[c77d859]410                    units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT)
411                sizer.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
[ed2ea6a]412               
[c77d859]413                ##[cb state, name, value, "+/-", error of fit, min, max , units]
414                self.parameters.append([None,item, ctl1,
415                                        None,None, None, None,None])
[ed2ea6a]416               
[c77d859]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
[bb18ef1]425            else:
[c77d859]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)
[8bd4dc0]433   
[dfae8b3]434 
[2e10b70]435           
[c77d859]436       
437class HelpWindow(wx.Frame):
438    def __init__(self, parent, id, title):
439        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
[ef8b580]440       
[c77d859]441        from sans.models.CylinderModel import CylinderModel
442        model = CylinderModel()
443        #from sans.models.LineModel import LineModel
444        #model = LineModel()
[cfc0913]445        from fitpanel import PageInfo
[c77d859]446        myinfo = PageInfo(self,model)
447        from models import ModelList
448        mylist= ModelList()
[fbc3e04]449       
[c77d859]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)
[fbc3e04]457       
[c77d859]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   
481if __name__=="__main__":
482    app = wx.App()
483    HelpWindow(None, -1, 'HelpWindow')
484    app.MainLoop()
485               
Note: See TracBrowser for help on using the repository browser.