source: sasview/sansview/perspectives/fitting/simfitpage.py @ ac11e40

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

simfit page changed

  • Property mode set to 100644
File size: 21.5 KB
RevLine 
[d89f09b]1
[2140e68]2import sys,re,string, wx 
3import wx.lib.newevent 
[d89f09b]4from sans.guicomm.events import StatusEvent   
5
[2140e68]6class FitConstraint:
7    """
8        Contains info on selected model to fit
9        via simultaneous page
10    """
11    def __init__(self, model, page=None):
12        """
13            initialization
14        """
15        self.model= model
16        self.page = page
17        self.fittable_param =[]
18        self._set_fittableParam()
19       
20    def _set_fittableParam(self):
21        """
22            fill self.fittable_param
23        """
24        for item in self.model.getParamList():
25            if not item  in self.model.getDispParamList():
26                self.fittable_param.append(item)
27        for item in self.model.fixed:
28            self.fittable_param.append(item)
29     
30
[3aae6b6]31class SimultaneousFitPage(wx.ScrolledWindow):
[d89f09b]32    """
33        Simultaneous fitting panel
34        All that needs to be defined are the
35        two data members window_name and window_caption
36    """
[925a30e]37    ## Internal name for the AUI manager
[d89f09b]38    window_name = "simultaneous Fit page"
39    ## Title to appear on top of the window
40    window_caption = "Simultaneous Fit Page"
41   
42   
[2140e68]43    def __init__(self, parent,page_finder ={}, *args, **kwargs):
[3aae6b6]44        wx.ScrolledWindow.__init__(self, parent, *args, **kwargs)
[d89f09b]45        """
46             Simultaneous page display
47        """
48        self.parent = parent
[2140e68]49        ## store page_finder
50        self.page_finder=page_finder
51        ## list contaning info to set constraint
[ac11e40]52        ## look like self.constraint_dict[page]=FitConstraint(model, page)
[2140e68]53        self.constraint_dict={}
54        ## item list  self.constraints_list=[combobox1, combobox2,=,textcrtl, button ]
55        self.constraints_list=[]
56        ## list of current model
57        self.model_list=[]
58        ## selected mdoel to fit
59        self.model_toFit=[]
60        ## draw
61        self.define_page_structure()
62        self.draw_page(self.page_finder)
63       
64        self.set_layout()
65       
66       
67       
68    def define_page_structure(self):
69        """
70            Create empty sizer for a panel
71        """
[d89f09b]72        self.vbox  = wx.BoxSizer(wx.VERTICAL)
[2140e68]73        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
74        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
75     
[d89f09b]76        self.vbox.Add(self.sizer1)
77        self.vbox.Add(self.sizer2)
[51d47b5]78       
[2140e68]79    def set_scroll(self):
80        self.SetScrollbars(20,20,200,100)
81        self.Layout() 
82         
83    def set_layout(self):
84        """
85             layout
86        """
[d89f09b]87        self.vbox.Layout()
88        self.vbox.Fit(self) 
89        self.SetSizer(self.vbox)
[2140e68]90       
91        self.set_scroll()
[d89f09b]92        self.Centre()
93       
94    def onFit(self,event):
[1b07935d]95        """ signal for fitting"""
[2140e68]96        ## making sure all parameters content a constraint
97        ## validity of the constraint expression is own by fit engine
[ac11e40]98        if self.show_constraint.GetValue():
99            self._set_constraint()
[2140e68]100        ## get the fit range of very fit problem       
101        for page, value in self.page_finder.iteritems():
102            qmin, qmax= page.get_range()
103            value.set_range(qmin, qmax)
104        ## model was actually selected from this page to be fit
[3215d32]105        if len(self.model_toFit) >= 1 :
[2140e68]106            self.manager.on_simul_fit()
[1b07935d]107        else:
[2140e68]108            msg= "Select at least one model to fit "
109            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
[f343069]110           
[00561739]111           
112           
[d89f09b]113    def set_manager(self, manager):
114        """
115            set panel manager
116            @param manager: instance of plugin fitting
117        """
118        self.manager = manager
[2140e68]119        self.draw_page( self.manager.page_finder)
[d89f09b]120       
[925a30e]121       
[2140e68]122    def check_all_model_name(self,event):
[d89f09b]123        """
124            check all models names
125        """
126        self.model_toFit=[] 
127        if self.cb1.GetValue()==True:
128            for item in self.model_list:
129                item[0].SetValue(True)
[948add7]130                self.manager.schedule_for_fit( value=1,fitproblem =item[1]) 
[d89f09b]131                self.model_toFit.append(item)
[2140e68]132               
133            ## constraint info
134            self._store_model()
135            ## display constraint fields
136            if self.show_constraint.GetValue():
137                self.sizer_couples.Clear(True) 
138                self._show_constraint()
139                return
[d89f09b]140        else:
141            for item in self.model_list:
142                item[0].SetValue(False) 
[89fef2c]143                self.manager.schedule_for_fit( value=0,fitproblem =item[1]) 
[948add7]144               
[d89f09b]145            self.model_toFit=[]
[2140e68]146            ##constraint info
147            self._hide_constraint()
[925a30e]148       
[2140e68]149 
150    def check_model_name(self,event):
[d89f09b]151        """
[2140e68]152            Save information related to checkbox and their states
[d89f09b]153        """
154        self.model_toFit=[]
155        for item in self.model_list:
156            if item[0].GetValue()==True:
157                self.model_toFit.append(item)
[948add7]158                self.manager.schedule_for_fit( value=1,fitproblem =item[1]) 
[d89f09b]159            else:
[948add7]160                self.manager.schedule_for_fit( value=0,fitproblem =item[1]) 
[d89f09b]161                if item in self.model_toFit:
162                    self.model_toFit.remove(item)
163                    self.cb1.SetValue(False)
[2140e68]164        ## display constraint fields
165        if len(self.model_toFit)>=2:
166            self._store_model()
167            if self.show_constraint.GetValue():
168                self._show_constraint()
169                         
170       
171        ## set the value of the main check button         
[d89f09b]172        if len(self.model_list)==len(self.model_toFit):
173            self.cb1.SetValue(True)
[2140e68]174            ## constraint info
175            self._store_model()
176            ## display constraint fields
177            if self.show_constraint.GetValue():
178                self.sizer_couples.Clear(True) 
179                self._show_constraint()
180                return
[d89f09b]181        else:
182            self.cb1.SetValue(False)
[2140e68]183            ##constraint info
184            self._hide_constraint()
[948add7]185       
[2140e68]186 
187    def draw_page(self, page_finder={}):     
188        """
189            Draw a sizer containing couples of data and model
190        """   
191        ## receive a new page_finder 
192        self.page_finder = page_finder 
193       
194        self.model_list=[]
195        self.model_toFit=[]
196        if len(self.model_list)>0:
197            for item in self.model_list:
198                item[0].SetValue(False) 
199                self.manager.schedule_for_fit( value=0,fitproblem =item[1])
200               
201        self.sizer1.Clear(True)
202       
203               
204        box_description= wx.StaticBox(self, -1,"Fit Couples")
205        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
206        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
207        sizer_couples = wx.GridBagSizer(5,5)
208       
209        #------------------------------------------------------
210        if len(self.page_finder)==0:
211            sizer_title.Add(wx.StaticText(self,-1," No fit couple available !"))
212        else:
213            ## store model 
214            self._store_model()
215       
216            self.cb1 = wx.CheckBox(self, -1,'Select all Fit Couples')
217            self.cb1.SetValue(False)
218            wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name)
219           
220            sizer_title.Add((15,15))
221            sizer_title.Add(self.cb1)
222            ## draw list of model name
223            self._fill_sizer_model_list(sizer_couples)
224            ## draw the sizer containing constraint info
225            self._fill_sizer_constraint()
226        #--------------------------------------------------------
227        boxsizer1.Add((5, 5)) 
228        boxsizer1.Add(sizer_title)
229        boxsizer1.Add((5, 5)) 
230        boxsizer1.Add(sizer_couples)
231        boxsizer1.Add((5,5))
232       
233        self.sizer1.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
234        self.sizer1.Layout()
235        self.SetScrollbars(20,20,200,100)
236        self.AdjustScrollbars()
237       
238    def _store_model(self):
[d89f09b]239        """
[2140e68]240            Store selected model
[d89f09b]241        """
[2140e68]242        if len(self.model_toFit) < 2:
243            return
244        for page, value in self.page_finder.iteritems():
245            model = value.get_model()
[d89f09b]246            for item in self.model_toFit:
[2140e68]247                if model in item and not model in self.constraint_dict.keys():
248                    self.constraint_dict[model]=FitConstraint(model=model, page=page)
249                   
250       
251    def _display_constraint(self, event):
252        """
253            Show fields to add constraint
254        """
255        if len(self.model_toFit)< 2:
256            msg= "Select at least 2 models to add constraint "
257            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
258            ## hide button
259            self._hide_constraint()
260            return
261        if self.show_constraint.GetValue():
[ac11e40]262            self._hide_constraint()
[2140e68]263            self._show_constraint()
264            return
265        else:
266           self._hide_constraint()
267           return 
268           
269   
270    def _show_constraint(self):
271        """
272            Show constraint fields
273        """
274        if len(self.constraints_list)!= 0:
275            nb_fit_param = 0
276            for value in self.constraint_dict.values():
277                nb_fit_param += len(value.fittable_param)
278            ##Don't add anymore
279            if len(self.constraints_list) == nb_fit_param:
[ac11e40]280                msg= "Cannot add another constraint .Maximum of number "
281                msg += "Parameters name reached %s"%str(nb_fit_param)
282                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
283                self.sizer_couples.Layout()
284                self.sizer2.Layout()
285                self.SetScrollbars(20,20,200,100)
286                return
287           
288        if len(self.model_toFit) < 2 :
[2140e68]289            msg= "Select at least 2 model to add constraint "
290            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
291            self.sizer_couples.Layout()
292            self.sizer2.Layout()
293            self.SetScrollbars(20,20,200,100)
294            return
295           
296        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL)
297       
298        model_cbox = wx.ComboBox(self, -1)
299        model_cbox.Clear()
300
301        param_cbox = wx.ComboBox(self, -1)
302        wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param)
303        param_cbox.Hide()
304       
305        ctl2 = wx.TextCtrl(self, -1)
306        ctl2.Hide()
307       
308        egal_txt= wx.StaticText(self,-1," = ")
309        egal_txt.Hide()
310       
311       
312        for model, value in self.constraint_dict.iteritems():
313            ## check if all parameters have been selected for constraint
314            ## then do not allow add constraint on parameters
[ac11e40]315            model_cbox.Append( str(model.name), model)
[2140e68]316           
317           
318        wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model)
319       
320        ##[combobox1, combobox2,=,textcrtl ]
321        self.constraints_list.append([model_cbox, param_cbox, egal_txt, ctl2])
322       
323        sizer_constraint.Add(model_cbox)
324        sizer_constraint.Add((10,10))
325        sizer_constraint.Add(param_cbox)
326        sizer_constraint.Add(egal_txt)
327        sizer_constraint.Add(ctl2)
328        sizer_constraint.Add((10,10))
329       
330       
331        self.sizer_couples.Add(sizer_constraint)
332        self.sizer_couples.Add((10,10))
333        self.sizer_couples.Layout()
334        self.sizer2.Layout()
335        self.SetScrollbars(20,20,200,100)
336       
337    def _hide_constraint(self): 
338        """
339            hide buttons related constraint
[ac11e40]340        """ 
[2140e68]341        if len(self.constraint_dict)>0:
[ac11e40]342            for item in self.constraints_list:
343                model = item[0].GetClientData(0)
344                if model  in self.constraint_dict.keys():
345                    page = self.constraint_dict[model].page
346                    self.page_finder[page].clear_model_param()
347                   
348        self.constraint_dict={}
349        self._store_model()
[2140e68]350        self.btAdd.Hide()
351        self.constraints_list=[]         
352        self.sizer_couples.Clear(True) 
353        self.sizer_couples.Layout()   
354        self.sizer2.Layout()
355        self.SetScrollbars(20,20,200,100)
356        self.AdjustScrollbars()   
357               
[ac11e40]358   
[2140e68]359       
360    def _on_select_model(self, event):
361        """
362         fill combox box with list of parameters
363        """
364        model = event.GetClientData()
365        param_list= self.constraint_dict[model].fittable_param
366        length = len(self.constraints_list)
367        if length < 1:
368            return 
369       
370        param_cbox = self.constraints_list[length-1][1]
371        param_cbox.Clear()
372        ## insert only fittable paramaters
373        for param in param_list:
[ac11e40]374            param_cbox.Append( str(param), model)
375           
[2140e68]376        param_cbox.Show(True)
377       
378       
379        self.sizer2.Layout()
380        self.SetScrollbars(20,20,200,100)
381       
382       
383    def _on_select_param(self, event):
384        """
385            Store the appropriate constraint in the page_finder
386        """
387        model = event.GetClientData()
388        param = event.GetString()
[ac11e40]389     
[2140e68]390        length = len(self.constraints_list)
391        if length < 1:
392            return 
393        egal_txt = self.constraints_list[length-1][2]
394        egal_txt.Show(True)       
395       
396        ctl2 = self.constraints_list[length-1][3]
397        ctl2.Show(True)
398       
399        self.btAdd.Show(True)
400        self.sizer2.Layout()
401        self.SetScrollbars(20,20,200,100)
402       
403       
404    def _onAdd_constraint(self, event): 
405        """
406            Add another line for constraint
407        """
408        msg= " "
409        wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
410       
411        ## check that a constraint is added before allow to add another cosntraint
412       
413        for item in self.constraints_list:
414            model_cbox = item[0]
415            if model_cbox.GetString(0)=="":
416                msg= " Select a model Name! "
417                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
418                return 
419            param_cbox = item[1]
420            if param_cbox.GetString(0)=="":
421                msg= " Select a parameter Name! "
422                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
423                return 
424            ctl2 = item[3]
425            if ctl2.GetValue().lstrip().rstrip()=="":
[ac11e40]426                 msg= " Enter a constraint for %s.%s! "%(param_cbox.GetClientData(0).name,
[2140e68]427                                              param_cbox.GetString(0))           
428                 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
429                 return 
430       
431        ## some model or parameters can be constrained
432        self._show_constraint()
433       
434       
435   
436       
437       
438    def _fill_sizer_constraint(self):
439        """
440            Fill sizer containing constraint info
441        """
442        msg= "Select at least 2 model to add constraint "
443        wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
444       
445        self.sizer2.Clear(True)
446 
447        box_description= wx.StaticBox(self, -1,"Fit Constraints")
448        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
449        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
450        self.sizer_couples = wx.BoxSizer(wx.VERTICAL)
451        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
452       
453        self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP)
454        self.show_constraint = wx.RadioButton(self, -1, 'Yes', (10, 30))
455       
456        self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint,
457                    id= self.hide_constraint.GetId() )
458       
459        self.Bind(  wx.EVT_RADIOBUTTON, self._display_constraint,
460                         id= self.show_constraint.GetId()    )
461       
462        sizer_title.Add( wx.StaticText(self,-1," Model") )
463        sizer_title.Add(( 10,10) )
464        sizer_title.Add( wx.StaticText(self,-1," Parameter") )
465        sizer_title.Add(( 10,10) )
466        sizer_title.Add( wx.StaticText(self,-1," Add Constraint?") )
467        sizer_title.Add(( 10,10) )
468        sizer_title.Add( self.show_constraint )
469        sizer_title.Add( self.hide_constraint )
470        sizer_title.Add(( 10,10) )
471       
472        self.btAdd =wx.Button(self,wx.NewId(),'Add')
473        self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint,id= self.btAdd.GetId())
474        self.btAdd.SetToolTipString("Add another constraint?")
475        self.btAdd.Hide()
476       
477        self.btFit = wx.Button(self,wx.NewId(),'Fit')
478        self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId())
479        self.btFit.SetToolTipString("Perform fit.")
480       
481        text_hint = wx.StaticText(self,-1,"Example: M0.paramter = M1.parameter") 
482        sizer_button.Add(text_hint, 0 , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
483        sizer_button.Add(self.btAdd, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
484        sizer_button.Add(self.btFit, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
485       
486        boxsizer1.Add(sizer_title)
487        boxsizer1.Add((10,10))
488        boxsizer1.Add(self.sizer_couples)
489        boxsizer1.Add((10,10))
490        boxsizer1.Add(sizer_button)
491       
492        self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
493        self.sizer2.Layout()
494        self.SetScrollbars(20,20,200,100)
495       
496       
497       
498    def _set_constraint(self):
[d89f09b]499        """
500            get values from the constrainst textcrtl ,parses them into model name
501            parameter name and parameters values.
[925a30e]502            store them in a list self.params .when when params is not empty set_model
503            uses it to reset the appropriate model and its appropriates parameters
[d89f09b]504        """
[2140e68]505        for item in self.constraints_list:
[ac11e40]506           
507            model = item[0].GetClientData(item[0].GetCurrentSelection())
508            param = item[1].GetString(item[1].GetCurrentSelection())
[2140e68]509            constraint = item[3].GetValue().lstrip().rstrip()
510            if model  in self.constraint_dict.keys():
511                page = self.constraint_dict[model].page
[ac11e40]512                if constraint == "":
513                    constraint = None
514               
515                self.page_finder[page].set_model_param(param,constraint)
[d89f09b]516       
[2140e68]517   
518             
519    def _fill_sizer_model_list(self,sizer):
[d89f09b]520        """
[2140e68]521            Receive a dictionary containing information to display model name
522            @param page_finder: the dictionary containing models information
[d89f09b]523        """
[2140e68]524        ix = 0
525        iy = 0
526        list=[]
527        sizer.Clear(True)
528       
529        new_name = wx.StaticText(self, -1, 'New Model Name', style=wx.ALIGN_CENTER)
530        new_name.SetBackgroundColour('orange')
531        sizer.Add(new_name,(iy, ix),(1,1),
532                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
533       
534        ix +=2 
535        model_type = wx.StaticText(self, -1, '  Model Type')
536        model_type.SetBackgroundColour('grey')
537        sizer.Add(model_type,(iy, ix),(1,1),
538                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
539        ix +=1 
540        data_used = wx.StaticText(self, -1, '  Used Data')
541        data_used.SetBackgroundColour('grey')
542        sizer.Add(data_used,(iy, ix),(1,1),
543                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
544       
545        for page, value in self.page_finder.iteritems():
546            try:
547                ix = 0
548                iy += 1 
549                model = value.get_model()
550                cb = wx.CheckBox(self, -1, str(model.name))
551                cb.SetValue(False)
552                sizer.Add( cb,( iy,ix),(1,1),  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
553                wx.EVT_CHECKBOX(self, cb.GetId(), self.check_model_name)
554               
555               
556                ix +=2 
557                type = model.__class__.__name__
558                model_type = wx.StaticText(self, -1, str(type))
559                sizer.Add(model_type,( iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
560               
561                ix +=1 
562                data = value.get_fit_data()
563                data_used= wx.StaticText(self, -1, str(data.name))
564                sizer.Add(data_used,( iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
565               
566               
567                self.model_list.append([cb,value,page,model])
568               
569            except:
570                pass
571        iy +=1
572        sizer.Add((20,20),( iy,ix),(1,1),  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
573        sizer.Layout()   
574       
575   
576 
577       
578class HelpWindow(wx.Frame):
579    def __init__(self, parent, id, title):
580        wx.Frame.__init__(self, parent, id, title, size=(570, 400))
581       
582        page_finder ={}
583        ## create random data
584        from danse.common.plottools.plottables import Data1D
585        data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1])
586        data.name="mydata.txt"
587        ## create model
588        from sans.models.CylinderModel import CylinderModel
589        model = CylinderModel()
590        model.name="M0"
591       
592       
593        from fitproblem import FitProblem
594        page_finder["page"]= FitProblem()
595        ## fill the page_finder
596        page_finder["page"].add_fit_data(data)
597        page_finder["page"].set_model(model)
598        self.page = SimultaneousFitPage(self, page_finder=page_finder) 
599       
600       
601       
602        self.Centre()
603        self.Show(True)
604
605
[d89f09b]606   
[2140e68]607if __name__=="__main__":
608    app = wx.App()
609    HelpWindow(None, -1, 'HelpWindow')
610    app.MainLoop()
[d89f09b]611   
Note: See TracBrowser for help on using the repository browser.