source: sasview/sansview/perspectives/fitting/modelpage.py @ 7c845cb

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

open and closing page and simpage update

  • Property mode set to 100644
File size: 17.8 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):
27        BasicPage.__init__(self, parent, page_info)
28        """
29            Initialization of the Panel
30        """
31        self._fill_model_sizer( self.sizer1) 
32        self._fill_range_sizer() 
33       
34        description=""
35        if self.model!=None:
36            #description = self.page_info.model.description
37            description = self.model.description
38            self.set_model_param_sizer(self.model)
39            self.set_model_description(description,self.sizer2)
40       
41   
42
43       
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()
50       
51       
52       
53    def _on_display_description_helper(self):
54        """
55            Show or Hide description
56            @param event: wx.EVT_RADIOBUTTON
57        """
58        ## save state of radiobox
59        #self.page_info. save_radiobox_state( self.description_hide )
60        #self.page_info. save_radiobox_state( self.description_show )
61        ## Show description
62        if not self.description_show.GetValue():
63            self.sizer_description.Clear(True)
64           
65        else:
66            #model=self.page_info.model
67            description=""
68            if self.model!=None:
69                #description = self.page_info.model.description
70                description = self.model.description
71            self.description = wx.StaticText( self,-1,str(description) )
72            self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 )
73           
74        self.Layout()
75   
76   
77    def _fill_range_sizer(self):
78        """
79            Fill the sizer containing the plotting range
80            add  access to npts
81        """
82        sizer_npts= wx.GridSizer(1, 1,5, 5)
83   
84        self.npts    = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20))
85        self.npts.SetValue(format_number(self.num_points))
86        self.npts.SetToolTipString("Number of point to plot.")
87        self.npts.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
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       
96    def _on_select_model(self, event): 
97        """
98             call back for model selection
99        """   
100        self._on_select_model_helper() 
101        self.select_model(self.model, self.model.name)
102       
103       
104    def _fill_model_sizer(self, sizer):
105        """
106            fill sizer containing model info
107        """
108        id = wx.NewId()
109        self.model_view =wx.Button(self,id,'View 2D')
110        self.model_view.Bind(wx.EVT_BUTTON, self._onModel2D,id=id)
111        self.model_view.SetToolTipString("View model in 2D")
112       
113        ## class base method  to add view 2d button   
114        self._set_model_sizer(sizer=sizer, title="Model",object= self.model_view )   
115   
116 
117    def _set_sizer_gaussian(self):
118        """
119            draw sizer with gaussian dispersity parameters
120        """
121        self.fittable_param=[]
122        self.fixed_param=[]
123       
124        self.sizer4_4.Clear(True)
125        if self.model==None:
126            ##no model is selected
127            return
128        if not self.enable_disp.GetValue():
129            ## the user didn't select dispersity display
130            return 
131        self._reset_dispersity()
132        # Create the dispersion objects
133        for item in self.model.dispersion.keys():
134            disp_model =  GaussianDispersion()
135            self._disp_obj_dict[item] = disp_model
136            self.model.set_dispersion(item, disp_model)
137
138       
139        ix=0
140        iy=1
141        disp = wx.StaticText(self, -1, 'Names')
142        self.sizer4_4.Add(disp,( iy, ix),(1,1), 
143                           wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
144        ix += 1 
145        values = wx.StaticText(self, -1, 'Values')
146        self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
147       
148        ix += 1 
149        npts = wx.StaticText(self, -1, 'Npts')
150        self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
151        ix += 1 
152        nsigmas = wx.StaticText(self, -1, 'Nsigmas')
153        self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
154       
155       
156        for item in self.model.dispersion.keys():
157         
158            self.disp_cb_dict[item]= None
159            name1=item+".width"
160            name2=item+".npts"
161            name3=item+".nsigmas"
162            iy += 1
163            for p in self.model.dispersion[item].keys():
164                if p=="width":
165                    ix = 0
166                    name = wx.StaticText(self, -1,  name1)
167                    self.sizer4_4.Add( name,( iy, ix),(1,1), 
168                                       wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
169                    ix = 1
170                    value= self.model.getParam(name1)
171                    ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
172                                        style=wx.TE_PROCESS_ENTER)
173                   
174                    ctl1.SetValue(str (format_number(value)))
175                    ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
176                    ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
177                    ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
178                    self.sizer4_4.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
179                   
180                    self.fittable_param.append([None,name1,ctl1,None,
181                                                None, None, None,None])
182                   
183                elif p=="npts":
184                        ix =2
185                        value= self.model.getParam(name2)
186                        Tctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
187                                            style=wx.TE_PROCESS_ENTER)
188                       
189                        Tctl1.SetValue(str (format_number(value)))
190                        Tctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
191                        Tctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
192                        Tctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
193                        self.sizer4_4.Add(Tctl1, (iy,ix),(1,1),
194                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
195                       
196                        self.fixed_param.append([None,name2, Tctl1,None,None,
197                                                  None, None,None])
198               
199                elif p=="nsigmas":
200                       
201                        ix =3 
202                        value= self.model.getParam(name3)
203                        Tctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
204                                            style=wx.TE_PROCESS_ENTER)
205                        Tctl2.SetValue(str (format_number(value)))
206                        Tctl2.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
207                        Tctl2.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
208                        Tctl2.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
209                        self.sizer4_4.Add(Tctl2, (iy,ix),(1,1),
210                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
211                        ix +=1
212                        self.sizer4_4.Add((20,20), (iy,ix),(1,1),
213                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
214                       
215                        self.fixed_param.append([None,name3, Tctl2,
216                                                 None,None, None, None,None])
217       
218        msg = " Selected Distribution: Gaussian"       
219        wx.PostEvent(self.parent.parent, StatusEvent( status= msg ))   
220        ix =0
221        iy +=1 
222        self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)   
223        self.sizer4_4.Layout()
224        self.sizer4.Layout()
225        self.SetScrollbars(20,20,200,100)
226             
227             
228    def reset_page(self, state):
229        """
230            reset the state
231        """
232        self.reset_page_helper(state)
233       
234       
235    def _onModel2D(self, event):
236        """
237         call manager to plot model in 2D
238        """
239        # If the 2D display is not currently enabled, plot the model in 2D
240        # and set the enable2D flag.
241        if self.fitrange:
242            self.enable2D = True
243           
244        if self.enable2D:
245            self._draw_model()
246            self.model_view.Disable()
247   
248    def select_model(self, model, name):
249        """
250            Select a new model
251            @param model: model object
252        """
253        self.model = model
254        self.set_model_param_sizer(self.model)
255        self._draw_model()
256        ## keep the sizer view consistent with the model menu selecting
257        self._set_model_sizer_selection( self.model )
258        self.enable_disp.SetValue(False)
259        self.disable_disp.SetValue(True)
260        self._set_dipers_Param(event=None)
261        self.model_view.SetFocus()
262                         
263   
264    def set_model_description(self,description,sizer):
265        """
266            fill a sizer with description
267            @param description: of type string
268            @param sizer: wx.BoxSizer()
269        """
270   
271        sizer.Clear(True)
272        box_description= wx.StaticBox(self, -1, 'Model Description')
273        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
274           
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')
279       
280        if description=="":
281            self.description_hide.SetValue(True)
282            description=" Description unavailable. Click for details"
283           
284        self.description = wx.StaticText( self,-1,str(description) )
285       
286        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
287                   id=self.description_hide.GetId() )
288       
289        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
290                   id=self.description_show.GetId() )
291       
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...")
295       
296        #self.page_info.save_radiobox_state( self.description_hide )
297        #self.page_info.save_radiobox_state( self.description_show )
298       
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 )
304       
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   
313
314        sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
315        sizer.Layout()
316   
317    def on_button_clicked(self,event):
318        """
319        #On 'More details' button
320        """
321       
322        from helpPanel import  HelpWindow
323        frame = HelpWindow(None, -1, 'HelpWindow')   
324        frame.Show(True)
325
326    def set_range(self, qmin_x, qmax_x, npts):
327        """
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
332        """
333        # Set the data members
334        self.qmin_x = qmin_x
335        self.qmax_x = qmax_x
336        self.num_points = npts
337       
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))
342       
343       
344    def set_model_param_sizer(self, model):
345        """
346            Build the panel from the model content
347            @param model: the model selected in combo box for fitting purpose
348        """
349        self.sizer3.Clear(True)
350        self.parameters = []
351        self.param_toFit=[]
352        self.fixed_param=[]
353       
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)
360       
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),
398                    style=wx.TE_PROCESS_ENTER)
399               
400                ctl1.SetValue(str (format_number(value)))
401                ctl1.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
402                ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
403                ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
404               
405                sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
406       
407                ix +=1
408                # Units
409                try:
410                    units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT)
411                except:
412                    units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT)
413                sizer.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
414               
415                ##[cb state, name, value, "+/-", error of fit, min, max , units]
416                self.parameters.append([None,item, ctl1,
417                                        None,None, None, None,None])
418               
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
427            else:
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)
435   
436 
437           
438       
439class HelpWindow(wx.Frame):
440    def __init__(self, parent, id, title):
441        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
442       
443        from sans.models.CylinderModel import CylinderModel
444        model = CylinderModel()
445        #from sans.models.LineModel import LineModel
446        #model = LineModel()
447        from fitpanel import PageInfo
448        myinfo = PageInfo(self,model)
449        from models import ModelList
450        mylist= ModelList()
451       
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)
459       
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.