source: sasview/sansview/perspectives/fitting/modelpage.py @ 59a7f2d

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

select default value in combobox box

  • Property mode set to 100644
File size: 17.2 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() 
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       
154        for item in self.model.dispersion.keys():
155            name1=item+".width"
156            name2=item+".npts"
157            name3=item+".nsigmas"
158            iy += 1
159            for p in self.model.dispersion[item].keys():
160                if p=="width":
161                    ix = 0
162                    name = wx.StaticText(self, -1,  name1)
163                    self.sizer4_4.Add( name,( iy, ix),(1,1), 
164                                       wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
165                    ix = 1
166                    value= self.model.getParam(name1)
167                    ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
168                                        style=wx.TE_PROCESS_ENTER)
169                   
170                    ctl1.SetValue(str (format_number(value)))
171                    ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
172                    ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
173                    self.sizer4_4.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
174                   
175                    self.fittable_param.append([None,name1,ctl1,None,
176                                                None, None, None,None])
177                   
178                elif p=="npts":
179                        ix =2
180                        value= self.model.getParam(name2)
181                        Tctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
182                                            style=wx.TE_PROCESS_ENTER)
183                       
184                        Tctl1.SetValue(str (format_number(value)))
185                        Tctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
186                        Tctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
187                        self.sizer4_4.Add(Tctl1, (iy,ix),(1,1),
188                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
189                       
190                        self.fixed_param.append([None,name2, Tctl1,None,None,
191                                                  None, None,None])
192               
193                elif p=="nsigmas":
194                       
195                        ix =3 
196                        value= self.model.getParam(name3)
197                        Tctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
198                                            style=wx.TE_PROCESS_ENTER)
199                        Tctl2.SetValue(str (format_number(value)))
200                        Tctl2.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
201                        Tctl2.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
202                        self.sizer4_4.Add(Tctl2, (iy,ix),(1,1),
203                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
204                        ix +=1
205                        self.sizer4_4.Add((20,20), (iy,ix),(1,1),
206                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
207                       
208                        self.fixed_param.append([None,name3, Tctl2,
209                                                 None,None, None, None,None])
210       
211        msg = " Selected Distribution: Gaussian"       
212        wx.PostEvent(self.parent.parent, StatusEvent( status= msg ))   
213        ix =0
214        iy +=1 
215        self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)   
216        self.sizer4_4.Layout()
217        self.sizer4.Layout()
218        self.SetScrollbars(20,20,200,100)
219             
220    def _onModel2D(self, event):
221        """
222         call manager to plot model in 2D
223        """
224        # If the 2D display is not currently enabled, plot the model in 2D
225        # and set the enable2D flag.
226        if self.fitrange:
227            self.enable2D = True
228           
229        if self.enable2D:
230            self._draw_model()
231            self.model_view.Disable()
232   
233    def select_model(self, model, name):
234        """
235            Select a new model
236            @param model: model object
237        """
238        self.model = model
239        self.parent.model_page.name = name
240        self.parent.draw_model_name = name
241       
242        self.set_model_param_sizer(self.model)
243        self._draw_model()
244        ## keep the sizer view consistent with the model menu selecting
245        ##
246        self.structurebox.Hide()
247        self.text2.Hide()
248        self.multip_cb.SetValue(False)
249       
250        if hasattr( model,"model1"):
251            self._set_model_sizer_selection( model )
252       
253        self.model_view.SetFocus()
254                         
255   
256    def set_model_description(self,description,sizer):
257        """
258            fill a sizer with description
259            @param description: of type string
260            @param sizer: wx.BoxSizer()
261        """
262   
263        sizer.Clear(True)
264        box_description= wx.StaticBox(self, -1, 'Model Description')
265        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
266           
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')
271       
272        if description=="":
273            self.description_hide.SetValue(True)
274            description=" Description unavailable. Click for details"
275           
276        self.description = wx.StaticText( self,-1,str(description) )
277       
278        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
279                   id=self.description_hide.GetId() )
280       
281        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
282                   id=self.description_show.GetId() )
283       
284        self.model_description = wx.Button(self, -1, "More Details")
285        self.model_description.SetToolTipString("See more description in help menu.")
286       
287        self.page_info.save_radiobox_state( self.description_hide )
288        self.page_info.save_radiobox_state( self.description_show )
289       
290        sizer_selection.Add( self.description_show )
291        sizer_selection.Add( (20,20)) 
292        sizer_selection.Add( self.description_hide )
293        sizer_selection.Add( (20,20)) 
294        sizer_selection.Add( self.model_description )
295       
296         
297        self.sizer_description=wx.BoxSizer(wx.HORIZONTAL)
298        self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 )
299       
300        boxsizer1.Add( sizer_selection) 
301        boxsizer1.Add( (20,20)) 
302        boxsizer1.Add( self.sizer_description) 
303   
304
305        sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
306        sizer.Layout()
307   
308   
309   
310    def set_range(self, qmin_x, qmax_x, npts):
311        """
312            Set the range for the plotted models
313            @param qmin: minimum Q
314            @param qmax: maximum Q
315            @param npts: number of Q bins
316        """
317        # Set the data members
318        self.qmin_x = qmin_x
319        self.qmax_x = qmax_x
320        self.num_points = npts
321       
322        # Set the controls
323        self.qmin.SetValue(format_number(self.qmin_x))
324        self.qmax.SetValue(format_number(self.qmax_x))
325        self.npts.SetValue(format_number(self.num_points))
326       
327       
328    def set_model_param_sizer(self, model):
329        """
330            Build the panel from the model content
331            @param model: the model selected in combo box for fitting purpose
332        """
333        self.sizer3.Clear(True)
334        self.parameters = []
335        self.param_toFit=[]
336        self.fixed_param=[]
337       
338        if model ==None:
339            ##no model avaiable to draw sizer
340            return
341        box_description= wx.StaticBox(self, -1,str("Model Parameters"))
342        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
343        sizer = wx.GridBagSizer(5,5)
344       
345        self.model = model
346        self.set_model_description(self.model.description,self.sizer2)
347       
348        keys = self.model.getParamList()
349        #list of dispersion paramaters
350        self.disp_list=self.model.getDispParamList()
351       
352        keys.sort()
353   
354        iy = 1
355        ix = 0
356        self.text1_2 = wx.StaticText(self, -1, 'Names')
357        sizer.Add(self.text1_2,(iy, ix),(1,1),\
358                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
359        ix +=1
360        self.text2_2 = wx.StaticText(self, -1, 'Values')
361        sizer.Add(self.text2_2,(iy, ix),(1,1),\
362                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
363        ix +=1
364        self.text2_4 = wx.StaticText(self, -1, 'Units')
365        sizer.Add(self.text2_4,(iy, ix),(1,1),\
366                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
367        self.text2_4.Hide()
368       
369
370        for item in keys:
371            if not item in self.disp_list:
372                iy += 1
373                ix = 0
374   
375                name = wx.StaticText(self, -1,item)
376                sizer.Add( name,( iy, ix),(1,1),
377                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
378               
379                ix += 1
380                value= self.model.getParam(item)
381                ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
382                    style=wx.TE_PROCESS_ENTER)
383               
384                ctl1.SetValue(str (format_number(value)))
385                ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
386                ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
387                sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
388       
389                ix +=1
390                # Units
391                try:
392                    units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT)
393                except:
394                    units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT)
395                sizer.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
396               
397                ##[cb state, name, value, "+/-", error of fit, min, max , units]
398                self.parameters.append([None,item, ctl1,
399                                        None,None, None, None,None])
400               
401        iy+=1
402        sizer.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
403       
404        #Display units text on panel
405        for item in keys:   
406            if self.model.details[item][0]!='':
407                self.text2_4.Show()
408                break
409            else:
410                self.text2_4.Hide()
411       
412        boxsizer1.Add(sizer)
413       
414        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
415        self.sizer3.Layout()
416        self.SetScrollbars(20,20,200,100)
417   
418 
419           
420       
421class HelpWindow(wx.Frame):
422    def __init__(self, parent, id, title):
423        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
424       
425        from sans.models.CylinderModel import CylinderModel
426        model = CylinderModel()
427        #from sans.models.LineModel import LineModel
428        #model = LineModel()
429        from pageInfo import PageInfo
430        myinfo = PageInfo(self,model)
431        from models import ModelList
432        mylist= ModelList()
433       
434        from sans.models.SphereModel import SphereModel
435        from sans.models.SquareWellStructure import SquareWellStructure
436        from sans.models.DebyeModel import DebyeModel
437        from sans.models.LineModel import LineModel
438        name= "shapes"
439        list1= [SphereModel]
440        mylist.set_list( name, list1)
441       
442        name= "Shape-independent"
443        list1= [DebyeModel]
444        mylist.set_list( name, list1)
445       
446        name= "Structure Factors"
447        list1= [SquareWellStructure]
448        mylist.set_list( name, list1)
449       
450        name= "Added models"
451        list1= [LineModel]
452        mylist.set_list( name, list1)
453       
454        myinfo.model_list_box = mylist.get_list()
455       
456        self.page = ModelPage(self, myinfo) 
457       
458       
459       
460        self.Centre()
461        self.Show(True)
462
463
464   
465if __name__=="__main__":
466    app = wx.App()
467    HelpWindow(None, -1, 'HelpWindow')
468    app.MainLoop()
469               
Note: See TracBrowser for help on using the repository browser.