source: sasview/sansview/perspectives/fitting/modelpage.py @ 24415e9

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

removing unsused code in fitpage

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