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

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

cleaned up a mess

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