source: sasview/sansview/perspectives/fitting/modelpage.py @ 5973b566

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

draw panel according to the fitting type
closing and reopen page i am still working on

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