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

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

plotting range fixed

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