source: sasview/sansview/perspectives/fitting/fitpage.py @ 6859338

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

new interface design —→ proposition

  • Property mode set to 100644
File size: 33.2 KB
Line 
1import sys
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 FitPage(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        ## fit page does not content npts txtcrtl
32        self.npts=None
33        ## if no dispersity parameters is avaible
34        self.text_disp_1=None
35       
36        self._fill_datainfo_sizer()
37        self._fill_model_sizer( self.sizer1)
38        self._fill_range_sizer() 
39        if hasattr(self.page_info,"model"):
40            self.set_model_param_sizer(self.model)
41           
42       
43    def _on_display_description(self, event):
44        """
45            Show or Hide description
46            @param event: wx.EVT_RADIOBUTTON
47        """
48        self._on_display_description_helper()
49       
50       
51       
52    def _on_display_description_helper(self):
53        """
54            Show or Hide description
55            @param event: wx.EVT_RADIOBUTTON
56        """
57        ## save state of radiobox
58        self.page_info. save_radiobox_state( self.description_hide )
59        self.page_info. save_radiobox_state( self.description_show )
60        ## Show description
61        if not self.description_show.GetValue():
62            self.sizer_description.Clear(True)
63           
64        else:
65            model=self.page_info.model
66            description=""
67            if model!=None:
68                description = self.page_info.model.description
69            self.description = wx.StaticText( self,-1,str(description) )
70            self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 )
71           
72        self.Layout()
73   
74   
75    def _fill_range_sizer(self):
76        """
77            Fill the sizer containing the plotting range
78            add  access to npts
79        """
80        sizer_fit = wx.GridSizer(2, 2,0, 0)
81   
82        self.btFit = wx.Button(self,wx.NewId(),'Fit')
83        self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId())
84        self.btFit.SetToolTipString("Perform fit.")
85       
86        self.btStopFit = wx.Button(self,wx.NewId(),'Stop Fit')
87        #self.btStopFit.Bind(wx.EVT_BUTTON, self.onStopFit,id= self.btStopFit.GetId())
88        self.btStopFit.SetToolTipString("Stop Current fit job.")
89        #self.btStopFit.Hide()
90       
91        sizer_fit.Add((5,5),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5)       
92        sizer_fit.Add((5,5))
93        sizer_fit.Add(self.btFit,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) 
94        sizer_fit.Add(self.btStopFit,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) 
95       
96        sizer_smearer = wx.BoxSizer(wx.HORIZONTAL)
97        #Filling the sizer containing instruments smearing info.
98        self.disable_smearer = wx.RadioButton(self, -1, 'No', style=wx.RB_GROUP)
99        self.enable_smearer = wx.RadioButton(self, -1, 'Yes')
100        self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId())
101        self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId())
102       
103        sizer_smearer.Add(wx.StaticText(self,-1,'Instrument Smearing? '))
104        sizer_smearer.Add((10, 10))
105        sizer_smearer.Add( self.enable_smearer )
106        sizer_smearer.Add((10,10))
107        sizer_smearer.Add( self.disable_smearer )
108       
109        self._set_range_sizer( title="Fitted Q Range",
110                               object1=sizer_smearer, object= sizer_fit)
111       
112       
113    def _fill_datainfo_sizer(self):
114        """
115            fill sizer 0 with data info
116        """
117        self.sizer0.Clear(True)
118        ## no loaded data , don't fill the sizer
119        if self.data== None:
120            self.sizer0.Layout()
121            return
122       
123        box_description= wx.StaticBox(self, -1, 'Loaded Data')
124        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
125        #----------------------------------------------------------
126        sizer_data = wx.GridSizer(3, 3,5, 5)
127        #Filling the sizer containing data related fields
128        DataSource  =wx.StaticText(self, -1,str(self.data.name))
129
130        sizer_data.Add(wx.StaticText(self, -1, 'Data Source Name : '))
131        sizer_data.Add(DataSource )
132        sizer_data.Add( (5,5) )
133       
134        #---------sizer 2 draw--------------------------------
135        #set maximum range for x in linear scale
136        if not hasattr(self.data,"data"): #Display only for 1D data fit
137            # Minimum value of data   
138            data_min = str(format_number(numpy.min(self.data.x)))
139            # Maximum value of data 
140            data_max = str(format_number(numpy.max(self.data.x)))
141            text4_3 = wx.StaticText(self, -1, 'Maximum Q Range(Linear)',
142                                     style=wx.ALIGN_LEFT)
143            sizer_data.Add( text4_3 )
144            sizer_data.Add(wx.StaticText(self, -1, "Min : %s"%data_min))
145            sizer_data.Add(wx.StaticText(self, -1, "Max : %s"%data_max))
146           
147        else:
148            radius_min= 0
149            x= numpy.max(self.data.xmin, self.data.xmax)
150            y= numpy.max(self.data.ymin, self.data.ymax)
151            radius_max = math.sqrt(x*x + y*y)
152            # Minimum value of data   
153            data_min = str(format_number(radius_min))
154            # Maximum value of data 
155            data_max = str(format_number(radius_max))
156            text4_3 = wx.StaticText(self, -1, 'Maximum Q Range',
157                                     style=wx.ALIGN_LEFT)
158            sizer_data.Add( text4_3 )
159            sizer_data.Add(wx.StaticText(self, -1, "Min : %s"%data_min))
160            sizer_data.Add(wx.StaticText(self, -1, "Max : %s"%data_max))
161           
162        boxsizer1.Add(sizer_data)
163        #------------------------------------------------------------
164        self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
165        self.sizer0.Layout()
166       
167        self.qmin_x= data_min
168        self.qmax_x= data_max
169       
170    def _fill_model_sizer(self, sizer):
171        """
172            fill sizer containing model info
173        """
174        sizer_tcChi = wx.GridSizer(2, 2,5, 5)
175        self.tcChi    =  wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT)
176        #self.tcChi.Hide()
177       
178        self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT)
179        #self.text1_1.Hide()
180       
181        self.btChi = wx.Button(self,wx.NewId(),'View Chi')
182        self.btChi.Bind(wx.EVT_BUTTON, self._onFit,id= self.btChi.GetId())
183        self.btChi.SetToolTipString("View data - model.")
184       
185        sizer_tcChi.Add(self.text1_1,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5)
186        sizer_tcChi.Add(self.btChi,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5)       
187        sizer_tcChi.Add(self.tcChi,1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5)       
188        sizer_tcChi.Add((5,5),1, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5)
189           
190        ## class base method  to add view 2d button   
191        self._set_model_sizer(sizer=sizer, title="Model",object=  sizer_tcChi )   
192   
193   
194    def _set_sizer_gaussian(self):
195        """
196            draw sizer with gaussian dispersity parameters
197        """
198        self.fittable_param=[]
199        self.fixed_param=[]
200        ##reset model dispersity to gaussian
201        self._reset_gaussian_dispers()
202       
203        self.sizer4_4.Clear(True)
204       
205        if self.model==None:
206            ##no model is selected
207            return
208        if not self.enable_disp.GetValue():
209            ## the user didn't select dispersity display
210            return 
211        ix=0
212        iy=1
213        disp = wx.StaticText(self, -1, 'Names')
214        self.sizer4_4.Add(disp,( iy, ix),(1,1), 
215                           wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
216        ix += 1 
217        values = wx.StaticText(self, -1, 'Values')
218        self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
219        ix +=2 
220        self.text_disp_1 = wx.StaticText(self, -1, 'Errors')
221        self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\
222                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
223        self.text_disp_1.Hide()
224        ix += 1 
225        npts = wx.StaticText(self, -1, 'Npts')
226        self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
227        ix += 1 
228        nsigmas = wx.StaticText(self, -1, 'Nsigmas')
229        self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
230       
231        for item in self.model.dispersion.keys():
232            name1=item+".width"
233            name2=item+".npts"
234            name3=item+".nsigmas"
235            iy += 1
236            for p in self.model.dispersion[item].keys():
237   
238                if p=="width":
239                    ix = 0
240                    cb = wx.CheckBox(self, -1, name1, (10, 10))
241                    wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param)
242                   
243                    self.sizer4_4.Add( cb,( iy, ix),(1,1), 
244                                       wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
245                    ix = 1
246                    value= self.model.getParam(name1)
247                    ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
248                                        style=wx.TE_PROCESS_ENTER)
249                   
250                    ctl1.SetValue(str (format_number(value)))
251                    ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
252                    ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
253                    self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND)
254                   
255                    ## text to show error sign
256                    ix = 2
257                    text2=wx.StaticText(self, -1, '+/-')
258                    self.sizer4_4.Add(text2,(iy, ix),(1,1),
259                                      wx.EXPAND|wx.ADJUST_MINSIZE, 0)
260                    text2.Hide() 
261                    ## txtcrtl to add error from fit
262                    ix = 3
263                    ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER)
264                    self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
265                    ctl2.Hide()
266                   
267                    self.fittable_param.append([cb,name1,ctl1,text2,
268                                                ctl2, None, None,None])
269                elif p=="npts":
270                        ix = 4
271                        value= self.model.getParam(name2)
272                        Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
273                                            style=wx.TE_PROCESS_ENTER)
274                       
275                        Tctl.SetValue(str (format_number(value)))
276                        Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
277                        Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
278                        self.sizer4_4.Add(Tctl, (iy,ix),(1,1),
279                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
280                       
281                        self.fixed_param.append([None,name2, Tctl,None,None,
282                                                  None, None,None])
283               
284                elif p=="nsigmas":
285                        ix = 5
286                        value= self.model.getParam(name3)
287                        Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20),
288                                            style=wx.TE_PROCESS_ENTER)
289                        Tctl.SetValue(str (format_number(value)))
290                        Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
291                        Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
292                        self.sizer4_4.Add(Tctl, (iy,ix),(1,1),
293                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
294                        ix +=1
295                        self.sizer4_4.Add((20,20), (iy,ix),(1,1),
296                                           wx.EXPAND|wx.ADJUST_MINSIZE, 0)
297                       
298                        self.fixed_param.append([None,name3, Tctl
299                                                 ,None,None, None, None,None])
300               
301        wx.PostEvent(self.parent, StatusEvent(status=\
302                        " Selected Distribution: Gaussian"))   
303        ix =0
304        iy +=1 
305        self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)       
306        self.sizer4_4.Layout()
307        self.sizer4.Layout()
308        self.SetScrollbars(20,20,200,100)
309       
310    def _onFit(self, event):     
311        """
312            Allow to fit
313        """
314        from sans.guiframe.utils import check_value
315        flag = check_value( self.qmin, self.qmax) 
316       
317        if not flag:
318            msg= "Fitting range invalid"
319            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
320            return 
321       
322        if len(self.param_toFit) <= 0:
323            msg= "Select at least one parameter to fit"
324            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
325            return 
326       
327        self.qmin_x=float(self.qmin.GetValue())
328        self.qmax_x =float( self.qmax.GetValue())
329       
330        self.manager.schedule_for_fit( value=1,fitproblem =None) 
331       
332        #single fit
333        self.manager.on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x)
334           
335        self.sizer5.Layout()
336        self.SetScrollbars(20,20,55,40)
337       
338       
339    def _on_select_model(self, event): 
340        """
341             call back for model selection
342        """   
343        self._on_select_model_helper(event) 
344        self.set_model_param_sizer(self.model)
345        self._set_sizer_gaussian()
346       
347        evt = ModelEventbox(model=self.model)
348        wx.PostEvent(self.event_owner, evt)   
349        try:
350            self.compute_chisqr()
351            self.text1_1.Show()
352            self.tcChi.Show()
353        except:
354            ## error occured on chisqr computation
355            pass
356       
357       
358       
359    def get_param_list(self):
360        """
361            @return self.param_toFit: list containing  references to TextCtrl
362            checked.Theses TextCtrl will allow reference to parameters to fit.
363            @raise: if return an empty list of parameter fit will nnote work
364            properly so raise ValueError,"missing parameter to fit"
365        """
366        if self.param_toFit !=[]:
367            return self.param_toFit
368        else:
369            raise ValueError,"missing parameter to fit"   
370     
371    def onsetValues(self,chisqr, out,cov):
372        """
373            Build the panel from the fit result
374            @param chisqr:Value of the goodness of fit metric
375            @param out:list of parameter with the best value found during fitting
376            @param cov:Covariance matrix
377       
378        """
379        self.tcChi.SetLabel(format_number(chisqr))
380        params = {}
381        is_modified = False
382        has_error = False
383        #set the panel when fit result are float not list
384        if out.__class__==numpy.float64:
385            self.param_toFit[0][2].SetValue(format_number(out))
386            self.param_toFit[0][2].Refresh()
387            if cov !=None :
388                self.text2_3.Show(True)
389                if self.text_disp_1!=None:
390                    self.text_disp_1.Show(True)
391                   
392                self.param_toFit[0][3].Show(True)
393                self.param_toFit[0][4].Clear()
394                self.param_toFit[0][4].SetValue(format_number(cov[0]))
395                self.param_toFit[0][4].Show(True)
396        else:
397            i=0
398            j=0
399            #Set the panel when fit result are list
400            for item in self.param_toFit:
401                if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out):
402                    item[2].SetValue(format_number(self.model.getParam(item[1])))
403                    item[2].Refresh()
404                if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov):
405                    self.text2_3.Show(True) 
406                    if self.text_disp_1!=None:
407                        self.text_disp_1.Show(True)
408                    item[3].Show(True)
409                    item[4].Clear()
410                    for j in range(len(out)):
411                        if out[j]==self.model.getParam(item[1]):
412                            break
413                    item[4].SetValue(format_number(cov[j]))
414                    item[4].Refresh()
415                    item[4].Show(True)   
416                i+=1
417       
418        self.sizer3.Layout()
419        self.sizer4_4.Layout()
420        self.sizer4.Layout()
421        self.SetScrollbars(20,20,200,100)
422       
423       
424     
425    def onSmear(self, event):
426        """
427            Create a smear object that will change the way residuals
428            are compute when fitting
429        """
430        smear =None
431        msg=""
432        if self.enable_smearer.GetValue():
433            from DataLoader.qsmearing import smear_selection
434            smear =smear_selection( self.data )
435            if hasattr(self.data,"dxl"):
436                msg= ": Resolution smearing parameters"
437            if hasattr(self.data,"dxw"):
438                msg= ": Slit smearing parameters"
439            if smear ==None:
440                wx.PostEvent(self.manager.parent, StatusEvent(status=\
441                            "Data contains no smearing information"))
442            else:
443                wx.PostEvent(self.manager.parent, StatusEvent(status=\
444                            "Data contains smearing information %s"%msg))
445        self.manager.set_smearer(smear, qmin= self.qmin_x, qmax= self.qmax_x)   
446             
447       
448       
449    def compute_chisqr2D(self):
450        """
451            compute chi square given a model and data 2D and set the value
452            to the tcChi txtcrl
453        """
454        from sans.guiframe.utils import check_value
455        flag = check_value( self.qmin, self.qmax)
456       
457        res=[]
458        if flag== True:
459            try:
460                self.qmin_x = float(self.qmin.GetValue())
461                self.qmax_x = float(self.qmax.GetValue())
462                for i in range(len(self.data.x_bins)):
463                    for j in range(len(self.data.y_bins)):
464                        #Check the range containing data between self.qmin_x and self.qmax_x
465                        if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2):
466                            if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2):
467                                chisqrji=(self.data.data[j][i]- self.model.runXY(\
468                                                                                    [self.data.y_bins[j],self.data.x_bins[i]]))\
469                                                                                    /self.data.err_data[j][i]
470                                #Vector containing residuals
471                                res.append( math.pow(chisqrji,2) )
472                # compute sum of residual
473                sum=0
474                for item in res:
475                    if numpy.isfinite(item):
476                        sum +=item
477                self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res))))
478            except:
479                wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\
480                            "Chisqr cannot be compute: %s"% sys.exc_value))
481                return
482   
483       
484    def compute_chisqr(self):
485        """
486            compute chi square given a model and data 1D and set the value
487            to the tcChi txtcrl
488        """
489        from sans.guiframe.utils import check_value
490        flag = check_value( self.qmin, self.qmax)
491       
492        if flag== True:
493            try:
494                if hasattr(self.data,"data"):
495                    self.compute_chisqr2D()
496                    return
497                else:
498                    self.qmin_x = float(self.qmin.GetValue())
499                    self.qmax_x = float(self.qmax.GetValue())
500                    # return residuals within self.qmin_x and self.qmax_x
501                    x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)]
502                    if self.qmin_x==None and self.qmax_x==None: 
503                        fx =numpy.asarray([self.model.run(v) for v in x])
504                        temp=(y - fx)/dy
505                        res= temp*temp
506                    else:
507                        idx = (x>= self.qmin_x) & (x <=self.qmax_x)
508                        fx = numpy.asarray([self.model.run(item)for item in x[idx ]])
509                        temp=(y[idx] - fx)/dy[idx]
510                        res= temp*temp
511                    #sum of residuals
512                    sum=0
513                    for item in res:
514                        if numpy.isfinite(item):
515                            sum +=item
516                    self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res))))
517            except:
518                wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\
519                            "Chisqr cannot be compute: %s"% sys.exc_value))
520                return 
521           
522   
523    def select_all_param(self,event): 
524        """
525             set to true or false all checkBox given the main checkbox value cb1
526        """
527        self.param_toFit=[]
528        if  self.parameters !=[]:
529            if  self.cb1.GetValue()==True:
530                for item in self.parameters:
531                    item[0].SetValue(True)
532                    self.param_toFit.append(item )
533                if len(self.fittable_param)>0:
534                    for item in self.fittable_param:
535                        item[0].SetValue(True)
536                        self.param_toFit.append(item )
537            else:
538                for item in self.parameters:
539                    item[0].SetValue(False)
540                for item in self.fittable_param:
541                    item[0].SetValue(False)
542                self.param_toFit=[]
543               
544               
545               
546    def select_param(self,event):
547        """
548            Select TextCtrl  checked for fitting purpose and stores them
549            in  self.param_toFit=[] list
550        """
551        self.param_toFit=[]
552        for item in self.parameters:
553            #Select parameters to fit for list of primary parameters
554            if item[0].GetValue()==True:
555                if not (item in self.param_toFit):
556                    self.param_toFit.append(item ) 
557            else:
558                #remove parameters from the fitting list
559                if item in self.param_toFit:
560                    self.param_toFit.remove(item)
561        #Select parameters to fit for list of fittable parameters with dispersion         
562        for item in self.fittable_param:
563            if item[0].GetValue()==True:
564                if not (item in self.param_toFit):
565                    self.param_toFit.append(item) 
566            else:
567                #remove parameters from the fitting list
568                if item in self.param_toFit:
569                    self.param_toFit.remove(item)           
570        #Set the value of checkbox that selected every checkbox or not           
571        if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit):
572            self.cb1.SetValue(True)
573        else:
574            self.cb1.SetValue(False)
575   
576   
577    def set_model_description(self,description,sizer):
578        """
579            fill a sizer with description
580            @param description: of type string
581            @param sizer: wx.BoxSizer()
582        """
583   
584        sizer.Clear(True)
585        box_description= wx.StaticBox(self, -1, 'Model Description')
586        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
587               
588        self.description = wx.StaticText( self,-1,str(description) )
589        sizer_selection=wx.BoxSizer(wx.HORIZONTAL)
590       
591       
592        self.description_show = wx.RadioButton(self, -1, 'Show', style=wx.RB_GROUP)
593        self.description_hide = wx.RadioButton(self, -1, 'Hide')
594       
595        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
596                   id=self.description_hide.GetId() )
597       
598        self.Bind( wx.EVT_RADIOBUTTON, self._on_display_description,
599                   id=self.description_show.GetId() )
600       
601        self.model_description = wx.Button(self, -1, "More Details")
602     
603       
604        self.page_info.save_radiobox_state( self.description_hide )
605        self.page_info.save_radiobox_state( self.description_show )
606       
607        sizer_selection.Add( self.description_show )
608        sizer_selection.Add( (20,20)) 
609        sizer_selection.Add( self.description_hide )
610        sizer_selection.Add( (20,20)) 
611        sizer_selection.Add( self.model_description )
612       
613         
614        self.sizer_description=wx.BoxSizer(wx.HORIZONTAL)
615        self.sizer_description.Add( self.description, 1, wx.EXPAND | wx.ALL, 10 )
616       
617        boxsizer1.Add( sizer_selection) 
618        boxsizer1.Add( (20,20)) 
619        boxsizer1.Add( self.sizer_description) 
620   
621
622        sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
623        sizer.Layout()
624   
625   
626       
627       
628    def set_model_param_sizer(self, model, engine_type="park"):
629        """
630            Build the panel from the model content
631            @param model: the model selected in combo box for fitting purpose
632        """
633        self.sizer3.Clear(True)
634        self.parameters = []
635        self.param_toFit=[]
636        self.fittable_param=[]
637        self.fixed_param=[]
638       
639        if model ==None:
640            self.sizer3.Layout()
641            self.SetScrollbars(20,20,200,100)
642            return
643        box_description= wx.StaticBox(self, -1,str("Model Parameters"))
644        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
645        sizer = wx.GridBagSizer(5,5)
646        ## save the current model
647        self.model = model
648           
649        keys = self.model.getParamList()
650        #list of dispersion paramaters
651        self.disp_list=self.model.getDispParamList()
652       
653        keys.sort()
654   
655        iy = 1
656        ix = 0
657        self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10))
658        wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param)
659        self.cb1.SetValue(False)
660       
661        sizer.Add(self.cb1,(iy, ix),(1,1),\
662                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
663        ix +=1
664        self.text2_2 = wx.StaticText(self, -1, 'Values')
665        sizer.Add(self.text2_2,(iy, ix),(1,1),\
666                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
667        ix +=2 
668        self.text2_3 = wx.StaticText(self, -1, 'Errors')
669        sizer.Add(self.text2_3,(iy, ix),(1,1),\
670                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
671        self.text2_3.Hide()
672        ix +=1 
673        self.text2_min = wx.StaticText(self, -1, 'Min')
674        sizer.Add(self.text2_min,(iy, ix),(1,1),\
675                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
676        self.text2_min.Hide()
677        ix +=1 
678        self.text2_max = wx.StaticText(self, -1, 'Max')
679        sizer.Add(self.text2_max,(iy, ix),(1,1),\
680                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
681        self.text2_max.Hide()
682        ix += 1
683        self.text2_4 = wx.StaticText(self, -1, 'Units')
684        sizer.Add(self.text2_4,(iy, ix),(1,1),\
685                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
686        self.text2_4.Hide()
687        if  engine_type == "park":
688            self.text2_max.Show(True)
689            self.text2_min.Show(True)
690
691        for item in keys:
692            if not item in self.disp_list:
693                iy += 1
694                ix = 0
695                ## add parameters name with checkbox for selecting to fit
696                cb = wx.CheckBox(self, -1, item )
697                cb.SetValue(False)
698                wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param)
699                sizer.Add( cb,( iy, ix),(1,1),
700                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
701                ## add parameter value
702                ix += 1
703                value= self.model.getParam(item)
704                ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20),
705                                    style=wx.TE_PROCESS_ENTER)
706               
707                ctl1.SetValue(str (format_number(value)))
708                ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
709                ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
710                sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND)
711                ## text to show error sign
712                ix += 1
713                text2=wx.StaticText(self, -1, '+/-')
714                sizer.Add(text2,(iy, ix),(1,1),\
715                                wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
716                text2.Hide() 
717                ## txtcrtl to add error from fit
718                ix += 1
719                ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER)
720                sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
721                ctl2.Hide()
722               
723                ix += 1
724                ctl3 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER)
725                ctl3.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
726                ctl3.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
727                sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
728                ctl3.Hide()
729               
730                ix += 1
731                ctl4 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER)
732                ctl4.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter)
733                ctl4.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter)
734                sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
735                ctl4.Hide()
736                if  engine_type == "park":
737                    ctl3.Show(True)
738                    ctl4.Show(True)
739                   
740                ix +=1
741                # Units
742                try:
743                    units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT)
744                except:
745                    units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT)
746                sizer.Add(units, (iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
747               
748                ##[cb state, name, value, "+/-", error of fit, min, max , units]
749                self.parameters.append([cb,item, ctl1,
750                                        text2,ctl2, ctl3, ctl4,None])
751               
752        iy+=1
753        sizer.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
754       
755        #Display units text on panel
756        for item in keys:   
757            if self.model.details[item][0]!='':
758                self.text2_4.Show()
759                break
760            else:
761                self.text2_4.Hide()
762   
763        boxsizer1.Add(sizer)
764       
765        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
766        self.sizer3.Layout()
767        self.SetScrollbars(20,20,200,100)
768       
769   
770           
771       
772class HelpWindow(wx.Frame):
773    def __init__(self, parent, id, title):
774        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
775       
776        from sans.models.CylinderModel import CylinderModel
777        model = CylinderModel()
778        #from sans.models.LineModel import LineModel
779        #model = LineModel()
780        from danse.common.plottools.plottables import Data1D
781        data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1])
782   
783        from pageInfo import PageInfo
784        myinfo = PageInfo(self,  model, data=data )
785       
786        ## add data
787       
788        from models import ModelList
789        mylist= ModelList()
790
791        from sans.models.SphereModel import SphereModel
792        from sans.models.SquareWellStructure import SquareWellStructure
793        from sans.models.DebyeModel import DebyeModel
794        from sans.models.LineModel import LineModel
795        name= "shapes"
796        list1= [SphereModel]
797        mylist.set_list( name, list1)
798       
799        name= "Shape-independent"
800        list1= [DebyeModel]
801        mylist.set_list( name, list1)
802       
803        name= "Structure Factors"
804        list1= [SquareWellStructure]
805        mylist.set_list( name, list1)
806       
807        name= "Added models"
808        list1= [LineModel]
809        mylist.set_list( name, list1)
810       
811        myinfo.model_list_box = mylist.get_list()
812       
813        self.page = FitPage(self, myinfo) 
814       
815       
816       
817        self.Centre()
818        self.Show(True)
819
820
821   
822if __name__=="__main__":
823    app = wx.App()
824    HelpWindow(None, -1, 'HelpWindow')
825    app.MainLoop()
826               
Note: See TracBrowser for help on using the repository browser.