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

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

disable and enable combo box for multiplication factor

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