source: sasview/sansview/perspectives/fitting/modelpage.py @ 2a5bba7

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

minor interface change

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