source: sasview/sansview/perspectives/fitting/basepage.py @ 1a7ec34

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 1a7ec34 was e030625, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working fitpage inheritance

  • Property mode set to 100644
File size: 106.0 KB
Line 
1
2import sys
3import os
4import wx
5import numpy
6import time
7import copy 
8import math
9import string
10from sans.guiframe.panel_base import PanelBase
11from wx.lib.scrolledpanel import ScrolledPanel
12from sans.guiframe.utils import format_number,check_float
13from sans.guiframe.events import PanelOnFocusEvent
14from sans.guiframe.events import StatusEvent
15from sans.guiframe.events import AppendBookmarkEvent
16import pagestate
17from pagestate import PageState
18(PageInfoEvent, EVT_PAGE_INFO)   = wx.lib.newevent.NewEvent()
19(PreviousStateEvent, EVT_PREVIOUS_STATE)   = wx.lib.newevent.NewEvent()
20(NextStateEvent, EVT_NEXT_STATE)   = wx.lib.newevent.NewEvent()
21
22_BOX_WIDTH = 76
23_QMIN_DEFAULT = 0.0005
24_QMAX_DEFAULT = 0.5
25_NPTS_DEFAULT = 50
26#Control panel width
27if sys.platform.count("darwin")==0:
28    PANEL_WIDTH = 450
29    FONT_VARIANT = 0
30    ON_MAC = False
31else:
32    PANEL_WIDTH = 500
33    FONT_VARIANT = 1
34    ON_MAC = True
35
36
37
38class BasicPage(ScrolledPanel, PanelBase):
39    """
40    This class provide general structure of  fitpanel page
41    """
42     ## Internal name for the AUI manager
43    window_name = "Fit Page"
44    ## Title to appear on top of the window
45    window_caption = "Fit Page "
46   
47    def __init__(self, parent,color='blue', **kwargs):
48        """
49        """
50        ScrolledPanel.__init__(self, parent, **kwargs)
51        PanelBase.__init__(self, parent)
52        self.SetupScrolling()
53        #Set window's font size
54        self.SetWindowVariant(variant=FONT_VARIANT)
55     
56        self.SetBackgroundColour(color)
57        ## parent of the page
58        self.parent = parent
59        ## manager is the fitting plugin
60        ## owner of the page (fitting plugin)
61        self.event_owner = None
62         ## current model
63        self.model = None
64        ## data
65        self.data = None
66        self.mask = None
67        self.uid = None
68        ## Q range
69        self.qmin = None
70        self.qmax = None
71        self.qmax_x = _QMAX_DEFAULT
72        self.qmin_x = _QMIN_DEFAULT
73        self.npts_x = _NPTS_DEFAULT
74        ## total number of point: float
75        self.npts = None
76        ## default fitengine type
77        self.engine_type = 'scipy'
78        ## smear default
79        self.smearer = None
80        self.current_smearer = None
81        ## 2D smear accuracy default
82        self.smear2d_accuracy = 'Low'
83        ## slit smear:
84        self.dxl = None
85        self.dxw = None
86        ## pinhole smear
87        self.dx_min = None
88        self.dx_max = None
89       
90        self.disp_cb_dict = {}
91   
92        self.state = PageState(parent=parent)
93        ## dictionary containing list of models
94        self.model_list_box = {}
95       
96        ## Data member to store the dispersion object created
97        self._disp_obj_dict = {}
98        ## selected parameters to apply dispersion
99        self.disp_cb_dict ={}
100
101        ## smearer object
102        self.smearer = None
103       
104        ##list of model parameters. each item must have same length
105        ## each item related to a given parameters
106        ##[cb state, name, value, "+/-", error of fit, min, max , units]
107        self.parameters = []
108        # non-fittable parameter whose value is astring
109        self.str_parameters = []
110        ## list of parameters to fit , must be like self.parameters
111        self.param_toFit = []
112        ## list of looking like parameters but with non fittable parameters info
113        self.fixed_param = []
114        ## list of looking like parameters but with  fittable parameters info
115        self.fittable_param = []
116        ##list of dispersion parameters
117        self.disp_list = []
118        self.disp_name = ""
119       
120        ## list of orientation parameters
121        self.orientation_params = []
122        self.orientation_params_disp = []
123        if self.model != None:
124            self.disp_list = self.model.getDispParamList()
125       
126        ##enable model 2D draw
127        self.enable2D = False
128        ## check that the fit range is correct to plot the model again
129        self.fitrange = True
130        ## Create memento to save the current state
131        self.state = PageState(parent=self.parent,
132                               model=self.model, data=self.data)
133        ## flag to determine if state has change
134        self.state_change = False
135        ## save customized array
136        self.values = []
137        self.weights = []
138        ## retrieve saved state
139        self.number_saved_state = 0
140        ## dictionary of saved state
141        self.saved_states = {} 
142        ## Create context menu for page
143        self.popUpMenu = wx.Menu()
144   
145        id = wx.NewId()
146        self._keep = wx.MenuItem(self.popUpMenu,id,"BookMark",
147                                 " Keep the panel status to recall it later")
148        self.popUpMenu.AppendItem(self._keep)
149        self._keep.Enable(True)
150        self._set_bookmark_flag(False)
151        self._set_save_flag(False)
152        wx.EVT_MENU(self, id, self.on_bookmark)
153        self.popUpMenu.AppendSeparator()
154   
155        ## Default locations
156        self._default_save_location = os.getcwd()     
157        ## save initial state on context menu
158        #self.onSave(event=None)
159        self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu)
160       
161        # bind key event
162        self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
163
164        ## create the basic structure of the panel with empty sizer
165        self.define_page_structure()
166        ## drawing Initial dispersion parameters sizer
167        self.set_dispers_sizer()
168       
169        ## layout
170        self.set_layout()
171       
172   
173       
174    def on_set_focus(self, event):
175        """
176        """
177        if self._manager is not None:
178            wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self))
179       
180    class ModelTextCtrl(wx.TextCtrl):
181        """
182        Text control for model and fit parameters.
183        Binds the appropriate events for user interactions.
184        Default callback methods can be overwritten on initialization
185       
186        :param kill_focus_callback: callback method for EVT_KILL_FOCUS event
187        :param set_focus_callback:  callback method for EVT_SET_FOCUS event
188        :param mouse_up_callback:   callback method for EVT_LEFT_UP event
189        :param text_enter_callback: callback method for EVT_TEXT_ENTER event
190       
191        """
192        ## Set to True when the mouse is clicked while the whole string is selected
193        full_selection = False
194        ## Call back for EVT_SET_FOCUS events
195        _on_set_focus_callback = None
196       
197        def __init__(self, parent, id=-1, 
198                     value=wx.EmptyString, 
199                     pos=wx.DefaultPosition, 
200                     size=wx.DefaultSize,
201                     style=0, 
202                     validator=wx.DefaultValidator,
203                     name=wx.TextCtrlNameStr,
204                     kill_focus_callback=None,
205                     set_focus_callback=None,
206                     mouse_up_callback=None,
207                     text_enter_callback = None):
208             
209            wx.TextCtrl.__init__(self, parent, id, value, pos,
210                                  size, style, validator, name)
211           
212            # Bind appropriate events
213            self._on_set_focus_callback = parent.onSetFocus \
214                      if set_focus_callback is None else set_focus_callback
215            self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
216            self.Bind(wx.EVT_KILL_FOCUS, self._silent_kill_focus \
217                      if kill_focus_callback is None else kill_focus_callback)               
218            self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \
219                      if text_enter_callback is None else text_enter_callback)
220            if not ON_MAC :
221                self.Bind(wx.EVT_LEFT_UP,    self._highlight_text \
222                          if mouse_up_callback is None else mouse_up_callback)
223           
224        def _on_set_focus(self, event):
225            """
226            Catch when the text control is set in focus to highlight the whole
227            text if necessary
228           
229            :param event: mouse event
230           
231            """
232            event.Skip()
233            self.full_selection = True
234            return self._on_set_focus_callback(event)
235       
236 
237           
238        def _highlight_text(self, event):
239            """
240            Highlight text of a TextCtrl only of no text has be selected
241           
242            :param event: mouse event
243           
244            """
245            # Make sure the mouse event is available to other listeners
246            event.Skip()
247            control  = event.GetEventObject()
248            if self.full_selection:
249                self.full_selection = False
250                # Check that we have a TextCtrl
251                if issubclass(control.__class__, wx.TextCtrl):
252                    # Check whether text has been selected,
253                    # if not, select the whole string
254                    (start, end) = control.GetSelection()
255                    if start==end:
256                        control.SetSelection(-1,-1)
257                       
258        def _silent_kill_focus(self,event):
259            """
260            Save the state of the page
261            """
262           
263            event.Skip()
264            pass
265   
266    def set_page_info(self, page_info):
267        """
268        set some page important information at once
269        """
270       ##window_name
271        self.window_name = page_info.window_name
272        ##window_caption
273        self.window_caption = page_info.window_caption
274        ## manager is the fitting plugin
275        self._manager= page_info.manager
276        ## owner of the page (fitting plugin)
277        self.event_owner= page_info.event_owner
278         ## current model
279        self.model = page_info.model
280        ## data
281        self.data = page_info.data
282        ## dictionary containing list of models
283        self.model_list_box = page_info.model_list_box
284        ## Data member to store the dispersion object created
285        self.populate_box(dict=self.model_list_box)
286       
287    def onContextMenu(self, event): 
288        """
289        Retrieve the state selected state
290        """
291        # Skipping the save state functionality for release 0.9.0
292        #return
293   
294        pos = event.GetPosition()
295        pos = self.ScreenToClient(pos)
296       
297        self.PopupMenu(self.popUpMenu, pos) 
298     
299       
300    def onUndo(self, event):
301        """
302        Cancel the previous action
303        """
304        event = PreviousStateEvent(page = self)
305        wx.PostEvent(self.parent, event)
306       
307    def onRedo(self, event):
308        """
309        Restore the previous action cancelled
310        """
311        event = NextStateEvent(page= self)
312        wx.PostEvent(self.parent, event)
313   
314    def define_page_structure(self):
315        """
316        Create empty sizer for a panel
317        """
318        self.vbox  = wx.BoxSizer(wx.VERTICAL)
319        self.sizer0 = wx.BoxSizer(wx.VERTICAL)
320        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
321        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
322        self.sizer3 = wx.BoxSizer(wx.VERTICAL)
323        self.sizer4 = wx.BoxSizer(wx.VERTICAL)
324        self.sizer5 = wx.BoxSizer(wx.VERTICAL)
325        self.sizer6 = wx.BoxSizer(wx.VERTICAL)
326       
327        self.sizer0.SetMinSize((PANEL_WIDTH,-1))
328        self.sizer1.SetMinSize((PANEL_WIDTH,-1))
329        self.sizer2.SetMinSize((PANEL_WIDTH,-1))
330        self.sizer3.SetMinSize((PANEL_WIDTH,-1))
331        self.sizer4.SetMinSize((PANEL_WIDTH,-1))
332        self.sizer5.SetMinSize((PANEL_WIDTH,-1))
333        self.sizer6.SetMinSize((PANEL_WIDTH,-1))
334       
335        self.vbox.Add(self.sizer0)
336        self.vbox.Add(self.sizer1)
337        self.vbox.Add(self.sizer2)
338        self.vbox.Add(self.sizer3)
339        self.vbox.Add(self.sizer4)
340        self.vbox.Add(self.sizer5)
341        self.vbox.Add(self.sizer6)
342       
343    def set_layout(self):
344        """
345        layout
346        """
347        self.vbox.Layout()
348        self.vbox.Fit(self) 
349        self.SetSizer(self.vbox)
350        self.Centre()
351 
352    def set_owner(self,owner):
353        """
354        set owner of fitpage
355       
356        :param owner: the class responsible of plotting
357       
358        """
359        self.event_owner = owner   
360        self.state.event_owner = owner
361       
362    def get_state(self):
363        """
364        """
365        return self.state
366    def get_data(self):
367        """
368        return the current data
369        """
370        return self.data 
371   
372    def set_manager(self, manager):
373        """
374        set panel manager
375       
376        :param manager: instance of plugin fitting
377       
378        """
379        self._manager = manager 
380        self.state.manager = manager
381       
382    def populate_box(self, dict):
383        """
384        Store list of model
385       
386        :param dict: dictionary containing list of models
387       
388        """
389        self.model_list_box = dict
390        self.state.model_list_box = self.model_list_box
391        self.initialize_combox()
392       
393    def initialize_combox(self): 
394        """
395        put default value in the combobox
396        """ 
397        ## fill combox box
398        if self.model_list_box is None:
399            return
400        if len(self.model_list_box) > 0:
401            self._populate_box(self.formfactorbox,
402                               self.model_list_box["Shapes"])
403       
404        if len(self.model_list_box) > 0:
405            self._populate_box(self.structurebox,
406                                self.model_list_box["Structure Factors"])
407            self.structurebox.Insert("None", 0, None)
408            self.structurebox.SetSelection(0)
409            self.structurebox.Hide()
410            self.text2.Hide()
411            self.structurebox.Disable()
412            self.text2.Disable()
413             
414            if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]:
415                self.structurebox.Show()
416                self.text2.Show()
417                self.structurebox.Enable()
418                self.text2.Enable()           
419               
420    def set_dispers_sizer(self):
421        """
422        fill sizer containing dispersity info
423        """
424        self.sizer4.Clear(True)
425        name="Polydispersity and Orientational Distribution"
426        box_description= wx.StaticBox(self, -1,name)
427        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
428        #----------------------------------------------------
429        self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10),
430                                            style=wx.RB_GROUP)
431        self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30))
432
433        self.disp_help_bt = wx.Button(self,wx.NewId(),'?', size=(20,15))
434        self.disp_help_bt.Bind(wx.EVT_BUTTON, 
435                        self.on_pd_help_clicked,id= self.disp_help_bt.GetId())
436        self.disp_help_bt.SetToolTipString("Helps for Polydispersion.")       
437       
438        self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param,
439                     id=self.disable_disp.GetId())
440        self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param,
441                   id=self.enable_disp.GetId())
442        #MAC needs SetValue
443        self.disable_disp.SetValue(True)
444        sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL)
445        sizer_dispersion.Add((20,20))
446        name=""#Polydispersity and \nOrientational Distribution "
447        sizer_dispersion.Add(wx.StaticText(self,-1,name))
448        sizer_dispersion.Add(self.enable_disp )
449        sizer_dispersion.Add((20,20))
450        sizer_dispersion.Add(self.disable_disp )
451        sizer_dispersion.Add((25,20))
452        sizer_dispersion.Add(self.disp_help_bt)
453       
454        ## fill a sizer for dispersion         
455        boxsizer1.Add( sizer_dispersion,0,
456                wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
457        #boxsizer1.Add( (10,10) )
458        #boxsizer1.Add( sizer_select_dispers )
459        self.sizer4_4 = wx.GridBagSizer(6,5)
460
461        boxsizer1.Add( self.sizer4_4  )
462        #-----------------------------------------------------
463        self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
464        self.sizer4_4.Layout()
465        self.sizer4.Layout()
466        self.Layout()
467     
468        self.Refresh()
469        ## saving the state of enable dispersity button
470        self.state.enable_disp= self.enable_disp.GetValue()
471        self.state.disable_disp= self.disable_disp.GetValue()
472        self.SetupScrolling()
473
474   
475    def onResetModel(self, event):
476        """
477        Reset model state
478        """
479        menu = event.GetEventObject()
480        ## post help message for the selected model
481        msg = menu.GetHelpString(event.GetId())
482        msg +=" reloaded"
483        wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
484       
485        name = menu.GetLabel(event.GetId())
486        self._on_select_model_helper()
487       
488        if name in self.saved_states.keys():
489            previous_state = self.saved_states[name]
490            ## reset state of checkbox,textcrtl  and  regular parameters value
491            self.reset_page(previous_state)   
492               
493    def on_report_state(self, event):
494        """
495        Report the current fit results
496        """   
497        event.Skip()
498        # Get plot image from plotpanel
499        images, canvases = self.get_images()
500        # get the report dialog
501        self.state.report(images, canvases)
502       
503         
504    def on_save(self, event):   
505        """
506        Save the current state into file
507        """ 
508        self.save_current_state()
509        new_state = self.state.clone()
510        # Ask the user the location of the file to write to.
511        path = None
512        dlg = wx.FileDialog(self, "Choose a file", self._default_save_location,
513                                                 "", "*.fitv", wx.SAVE)
514        if dlg.ShowModal() == wx.ID_OK:
515            path = dlg.GetPath()
516            self._default_save_location = os.path.dirname(path)
517        else:
518            return None
519        #the manager write the state into file
520        self._manager.save_fit_state(filepath=path, fitstate=new_state)
521        return new_state 
522   
523    def _get_time_stamp(self):
524        """
525        return time and date stings
526        """
527        # date and time
528        year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime()
529        current_time= str(hour)+":"+str(minute)+":"+str(second)
530        current_date= str( month)+"/"+str(day)+"/"+str(year)
531        return current_time, current_date
532     
533    def on_bookmark(self, event):
534        """
535        save history of the data and model
536        """
537        if self.model==None:
538            msg="Can not bookmark; Please select Data and Model first..."
539            wx.MessageBox(msg, 'Info')
540            return 
541        self.save_current_state()
542        new_state = self.state.clone()
543        ##Add model state on context menu
544        self.number_saved_state += 1
545        current_time, current_date = self._get_time_stamp()
546        #name= self.model.name+"[%g]"%self.number_saved_state
547        name = "Fitting: %g]" % self.number_saved_state
548        name += self.model.__class__.__name__
549        name += "bookmarked at %s on %s" % (current_time, current_date)
550        self.saved_states[name]= new_state
551       
552        ## Add item in the context menu
553        msg =  "Model saved at %s on %s"%(current_time, current_date)
554         ## post help message for the selected model
555        msg +=" Saved! right click on this page to retrieve this model"
556        wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
557       
558        #id = wx.NewId()
559        #self.popUpMenu.Append(id,name,str(msg))
560        #wx.EVT_MENU(self, id, self.onResetModel)
561        wx.PostEvent(self.parent.parent, 
562                     AppendBookmarkEvent(title=name, 
563                                         hint=str(msg), handler=self.onResetModel))
564       
565    def old_on_bookmark(self, event):
566        """
567        save history of the data and model
568        """
569        if self.model==None:
570            msg="Can not bookmark; Please select Data and Model first..."
571            wx.MessageBox(msg, 'Info')
572            return 
573        if hasattr(self,"enable_disp"):
574            self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue())
575        if hasattr(self, "disp_box"):
576            self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection())
577
578        self.state.model.name= self.model.name
579       
580        #Remember fit engine_type for fit panel
581        if self.engine_type == None: 
582            self.engine_type = "scipy"
583        if self._manager !=None:
584            self._manager._on_change_engine(engine=self.engine_type)
585       
586            self.state.engine_type = self.engine_type
587
588        new_state = self.state.clone()
589        new_state.model.name = self.state.model.name
590       
591        new_state.enable2D = copy.deepcopy(self.enable2D)
592        ##Add model state on context menu
593        self.number_saved_state += 1
594        #name= self.model.name+"[%g]"%self.number_saved_state
595        name= self.model.__class__.__name__+"[%g]"%self.number_saved_state
596        self.saved_states[name]= new_state
597       
598        ## Add item in the context menu
599       
600        year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime()
601        my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" "
602        date= str( month)+"|"+str(day)+"|"+str(year)
603        msg=  "Model saved at %s on %s"%(my_time, date)
604         ## post help message for the selected model
605        msg +=" Saved! right click on this page to retrieve this model"
606        wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
607       
608        id = wx.NewId()
609        self.popUpMenu.Append(id,name,str(msg))
610        wx.EVT_MENU(self, id, self.onResetModel)
611       
612    def onSetFocus(self, evt):
613        """
614        highlight the current textcrtl and hide the error text control shown
615        after fitting
616        """
617        return
618   
619    def read_file(self, path):
620        """
621        Read two columns file
622       
623        :param path: the path to the file to read
624       
625        """
626        try:
627            if path==None:
628                wx.PostEvent(self.parent.parent, StatusEvent(status=\
629                            " Selected Distribution was not loaded: %s"%path))
630                return None, None
631            input_f = open(path, 'r')
632            buff = input_f.read()
633            lines = buff.split('\n')
634           
635            angles = []
636            weights=[]
637            for line in lines:
638                toks = line.split()
639                try:
640                    angle = float(toks[0])
641                    weight = float(toks[1])
642                except:
643                    # Skip non-data lines
644                    pass
645                angles.append(angle)
646                weights.append(weight)
647            return numpy.array(angles), numpy.array(weights)
648        except:
649            raise 
650
651    def createMemento(self):
652        """
653        return the current state of the page
654        """
655        return self.state.clone()
656   
657   
658    def save_current_state(self):
659        """
660        Store current state
661        """
662        self.state.engine_type = copy.deepcopy(self.engine_type)
663        ## save model option
664        if self.model!= None:
665            self.disp_list= self.model.getDispParamList()
666            self.state.disp_list= copy.deepcopy(self.disp_list)
667            self.state.model = self.model.clone()
668        #save radiobutton state for model selection
669        self.state.shape_rbutton = self.shape_rbutton.GetValue()
670        self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue()
671        self.state.struct_rbutton = self.struct_rbutton.GetValue()
672        self.state.plugin_rbutton = self.plugin_rbutton.GetValue()
673        #model combobox
674        self.state.structurebox = self.structurebox.GetSelection()
675        self.state.formfactorbox = self.formfactorbox.GetSelection()
676       
677        self.state.enable2D = copy.deepcopy(self.enable2D)
678        self.state.values= copy.deepcopy(self.values)
679        self.state.weights = copy.deepcopy( self.weights)
680        ## save data   
681        self.state.data= copy.deepcopy(self.data)
682        self.state.qmax_x = self.qmax_x
683        self.state.qmin_x = self.qmin_x
684     
685        if hasattr(self,"enable_disp"):
686            self.state.enable_disp= self.enable_disp.GetValue()
687            self.state.disable_disp = self.disable_disp.GetValue()
688           
689        self.state.smearer = copy.deepcopy(self.smearer)
690        if hasattr(self,"enable_smearer"):
691            self.state.enable_smearer = \
692                                copy.deepcopy(self.enable_smearer.GetValue())
693            self.state.disable_smearer = \
694                                copy.deepcopy(self.disable_smearer.GetValue())
695
696        self.state.pinhole_smearer = \
697                                copy.deepcopy(self.pinhole_smearer.GetValue())
698        self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) 
699                 
700        if len(self._disp_obj_dict)>0:
701            for k , v in self._disp_obj_dict.iteritems():
702                self.state._disp_obj_dict[k]= v
703                       
704           
705            self.state.values = copy.deepcopy(self.values)
706            self.state.weights = copy.deepcopy(self.weights)
707        ## save plotting range
708        self._save_plotting_range()
709       
710        self.state.orientation_params = []
711        self.state.orientation_params_disp = []
712        self.state.parameters = []
713        self.state.fittable_param = []
714        self.state.fixed_param = []
715        self.state.str_parameters = []
716
717       
718        ## save checkbutton state and txtcrtl values
719        self._copy_parameters_state(self.str_parameters, 
720                                    self.state.str_parameters)
721        self._copy_parameters_state(self.orientation_params,
722                                     self.state.orientation_params)
723        self._copy_parameters_state(self.orientation_params_disp,
724                                     self.state.orientation_params_disp)
725       
726        self._copy_parameters_state(self.parameters, self.state.parameters)
727        self._copy_parameters_state(self.fittable_param,
728                                     self.state.fittable_param)
729        self._copy_parameters_state(self.fixed_param, self.state.fixed_param)
730        #save chisqr
731        self.state.tcChi = self.tcChi.GetValue()
732       
733    def save_current_state_fit(self):
734        """
735        Store current state for fit_page
736        """
737        ## save model option
738        if self.model!= None:
739            self.disp_list= self.model.getDispParamList()
740            self.state.disp_list= copy.deepcopy(self.disp_list)
741            self.state.model = self.model.clone()
742        if hasattr(self, "engine_type"):
743            self.state.engine_type = copy.deepcopy(self.engine_type)
744           
745        self.state.enable2D = copy.deepcopy(self.enable2D)
746        self.state.values= copy.deepcopy(self.values)
747        self.state.weights = copy.deepcopy( self.weights)
748        ## save data   
749        self.state.data= copy.deepcopy(self.data)
750       
751        if hasattr(self,"enable_disp"):
752            self.state.enable_disp= self.enable_disp.GetValue()
753            self.state.disable_disp = self.disable_disp.GetValue()
754           
755        self.state.smearer = copy.deepcopy(self.smearer)
756        if hasattr(self,"enable_smearer"):
757            self.state.enable_smearer = \
758                                copy.deepcopy(self.enable_smearer.GetValue())
759            self.state.disable_smearer = \
760                                copy.deepcopy(self.disable_smearer.GetValue())
761           
762        self.state.pinhole_smearer = \
763                                copy.deepcopy(self.pinhole_smearer.GetValue())
764        self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) 
765           
766        if hasattr(self,"disp_box"):
767            self.state.disp_box = self.disp_box.GetCurrentSelection()
768
769            if len(self.disp_cb_dict) > 0:
770                for k, v in self.disp_cb_dict.iteritems():
771         
772                    if v == None :
773                        self.state.disp_cb_dict[k] = v
774                    else:
775                        try:
776                            self.state.disp_cb_dict[k] = v.GetValue()
777                        except:
778                            self.state.disp_cb_dict[k] = None
779           
780            if len(self._disp_obj_dict) > 0:
781                for k , v in self._disp_obj_dict.iteritems():
782     
783                    self.state._disp_obj_dict[k] = v
784                       
785           
786            self.state.values = copy.deepcopy(self.values)
787            self.state.weights = copy.deepcopy(self.weights)
788           
789        ## save plotting range
790        self._save_plotting_range()
791       
792        ## save checkbutton state and txtcrtl values
793        self._copy_parameters_state(self.orientation_params,
794                                     self.state.orientation_params)
795        self._copy_parameters_state(self.orientation_params_disp,
796                                     self.state.orientation_params_disp)
797        self._copy_parameters_state(self.parameters, self.state.parameters)
798        self._copy_parameters_state(self.fittable_param,
799                                             self.state.fittable_param)
800        self._copy_parameters_state(self.fixed_param, self.state.fixed_param)
801   
802         
803    def check_invalid_panel(self): 
804        """
805        check if the user can already perform some action with this panel
806        """ 
807        flag = False
808        if self.data is None:
809            self.disable_smearer.SetValue(True)
810            self.disable_disp.SetValue(True)
811            msg = "Please load Data and select Model to start..."
812            wx.MessageBox(msg, 'Info')
813            return  True
814       
815    def set_model_state(self, state):
816        """
817        reset page given a model state
818        """
819        self.disp_cb_dict = state.disp_cb_dict
820        self.disp_list = state.disp_list
821     
822        ## set the state of the radio box
823        self.shape_rbutton.SetValue(state.shape_rbutton )
824        self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton)
825        self.struct_rbutton.SetValue(state.struct_rbutton)
826        self.plugin_rbutton.SetValue(state.plugin_rbutton)
827       
828        ## fill model combobox
829        self._show_combox_helper()
830        #select the current model
831        self.formfactorbox.Select(int(state.formfactorcombobox))
832        self.structurebox.SetSelection(state.structurecombobox )
833        if state.multi_factor != None:
834            self.multifactorbox.SetSelection(state.multi_factor)
835           
836         ## reset state of checkbox,textcrtl  and  regular parameters value
837        self._reset_parameters_state(self.orientation_params_disp,
838                                     state.orientation_params_disp)
839        self._reset_parameters_state(self.orientation_params,
840                                     state.orientation_params)
841        self._reset_parameters_state(self.str_parameters,
842                                     state.str_parameters)
843        self._reset_parameters_state(self.parameters,state.parameters)
844         ## display dispersion info layer       
845        self.enable_disp.SetValue(state.enable_disp)
846        self.disable_disp.SetValue(state.disable_disp)
847       
848        if hasattr(self, "disp_box"):
849           
850            self.disp_box.SetSelection(state.disp_box) 
851            n= self.disp_box.GetCurrentSelection()
852            dispersity= self.disp_box.GetClientData(n)
853            name = dispersity.__name__     
854
855            self._set_dipers_Param(event=None)
856       
857            if name == "ArrayDispersion":
858               
859                for item in self.disp_cb_dict.keys():
860                   
861                    if hasattr(self.disp_cb_dict[item], "SetValue") :
862                        self.disp_cb_dict[item].SetValue(\
863                                                    state.disp_cb_dict[item])
864                        # Create the dispersion objects
865                        from sans.models.dispersion_models import ArrayDispersion
866                        disp_model = ArrayDispersion()
867                        if hasattr(state,"values")and\
868                                 self.disp_cb_dict[item].GetValue() == True:
869                            if len(state.values)>0:
870                                self.values=state.values
871                                self.weights=state.weights
872                                disp_model.set_weights(self.values,
873                                                        state.weights)
874                            else:
875                                self._reset_dispersity()
876                       
877                        self._disp_obj_dict[item] = disp_model
878                        # Set the new model as the dispersion object
879                        #for the selected parameter
880                        self.model.set_dispersion(item, disp_model)
881                   
882                        self.model._persistency_dict[item] = \
883                                                [state.values, state.weights]
884                   
885            else:
886                keys = self.model.getParamList()
887                for item in keys:
888                    if item in self.disp_list and \
889                        not self.model.details.has_key(item):
890                        self.model.details[item] = ["", None, None]
891                for k,v in self.state.disp_cb_dict.iteritems():
892                    self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) 
893                    self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict)
894         ## smearing info  restore
895        if hasattr(self, "enable_smearer"):
896            ## set smearing value whether or not the data
897            #contain the smearing info
898            self.enable_smearer.SetValue(state.enable_smearer)
899            self.disable_smearer.SetValue(state.disable_smearer)
900            self.onSmear(event=None)           
901        self.pinhole_smearer.SetValue(state.pinhole_smearer)
902        self.slit_smearer.SetValue(state.slit_smearer)
903        ## we have two more options for smearing
904        if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None)
905        elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None)
906       
907        ## reset state of checkbox,textcrtl  and dispersity parameters value
908        self._reset_parameters_state(self.fittable_param,state.fittable_param)
909        self._reset_parameters_state(self.fixed_param,state.fixed_param)
910       
911        ## draw the model with previous parameters value
912        self._onparamEnter_helper()
913        self.select_param(event=None) 
914        #Save state_fit
915        self.save_current_state_fit()
916        self._lay_out()
917        self.Refresh()
918       
919    def reset_page_helper(self, state):
920        """
921        Use page_state and change the state of existing page
922       
923        :precondition: the page is already drawn or created
924       
925        :postcondition: the state of the underlying data change as well as the
926            state of the graphic interface
927        """
928        if state == None:
929            #self._undo.Enable(False)
930            return 
931        # set data, etc. from the state
932        self.set_data(state.data)
933        self.enable2D= state.enable2D
934        self.engine_type = state.engine_type
935
936        self.disp_cb_dict = state.disp_cb_dict
937        self.disp_list = state.disp_list
938     
939        ## set the state of the radio box
940        self.shape_rbutton.SetValue(state.shape_rbutton )
941        self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton)
942        self.struct_rbutton.SetValue(state.struct_rbutton)
943        self.plugin_rbutton.SetValue(state.plugin_rbutton)
944       
945        ## fill model combobox
946        self._show_combox_helper()
947        #select the current model
948        self.formfactorbox.Select(int(state.formfactorcombobox))
949        self.structurebox.SetSelection(state.structurecombobox )
950        if state.multi_factor != None:
951            self.multifactorbox.SetSelection(state.multi_factor)
952
953        #reset the fitting engine type
954        self.engine_type = state.engine_type
955        #draw the pnael according to the new model parameter
956        self._on_select_model(event=None)
957       
958        if self._manager !=None:
959            self._manager._on_change_engine(engine=self.engine_type)
960        ## set the select all check box to the a given state
961        self.cb1.SetValue(state.cb1)
962     
963        ## reset state of checkbox,textcrtl  and  regular parameters value
964        self._reset_parameters_state(self.orientation_params_disp,
965                                     state.orientation_params_disp)
966        self._reset_parameters_state(self.orientation_params,
967                                     state.orientation_params)
968        self._reset_parameters_state(self.str_parameters,
969                                     state.str_parameters)
970        self._reset_parameters_state(self.parameters,state.parameters)   
971         ## display dispersion info layer       
972        self.enable_disp.SetValue(state.enable_disp)
973        self.disable_disp.SetValue(state.disable_disp)
974        # If the polydispersion is ON
975        if state.enable_disp:
976            # reset dispersion according the state
977            self._set_dipers_Param(event=None)
978            self._reset_page_disp_helper(state)
979        ##plotting range restore   
980        self._reset_plotting_range(state)
981        ## smearing info  restore
982        if hasattr(self, "enable_smearer"):
983            ## set smearing value whether or not the data
984            #contain the smearing info
985            self.enable_smearer.SetValue(state.enable_smearer)
986            self.disable_smearer.SetValue(state.disable_smearer)
987            self.onSmear(event=None)           
988        self.pinhole_smearer.SetValue(state.pinhole_smearer)
989        self.slit_smearer.SetValue(state.slit_smearer)
990        ## we have two more options for smearing
991        if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None)
992        elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None)
993       
994        ## reset state of checkbox,textcrtl  and dispersity parameters value
995        self._reset_parameters_state(self.fittable_param,state.fittable_param)
996        self._reset_parameters_state(self.fixed_param,state.fixed_param)
997       
998        ## draw the model with previous parameters value
999        self._onparamEnter_helper()
1000        #reset the value of chisqr when not consistent with the value computed
1001        self.tcChi.SetValue(str(self.state.tcChi))
1002        ## reset context menu items
1003        self._reset_context_menu()
1004       
1005        ## set the value of the current state to the state given as parameter
1006        self.state = state.clone() 
1007   
1008    def _reset_page_disp_helper(self, state):
1009        """
1010        Help to rest page for dispersions
1011        """
1012        keys = self.model.getParamList()
1013        for item in keys:
1014            if item in self.disp_list and \
1015                not self.model.details.has_key(item):
1016                self.model.details[item] = ["", None, None]
1017        #for k,v in self.state.disp_cb_dict.iteritems():
1018        self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) 
1019        self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict)
1020        self.values = copy.deepcopy(state.values)
1021        self.weights = copy.deepcopy(state.weights)
1022       
1023        for key, disp in state._disp_obj_dict.iteritems():
1024            # From saved file, disp_model can not be sent in model obj.
1025            # it will be sent as a string here, then converted to model object.
1026            if disp.__class__.__name__ == 'str':
1027                com_str  = "from sans.models.dispersion_models "
1028                com_str += "import %s as disp_func"
1029                exec com_str % disp
1030                disp_model = disp_func()
1031            else:
1032                disp_model = disp
1033
1034            self._disp_obj_dict[key] = disp_model
1035            param_name = key.split('.')[0]
1036            # Try to set dispersion only when available
1037            # for eg., pass the orient. angles for 1D Cal
1038            try:
1039                self.model.set_dispersion(param_name, disp_model)
1040                self.model._persistency_dict[key] = \
1041                                 [state.values, state.weights]
1042            except:
1043                pass
1044            selection = self._find_polyfunc_selection(disp_model)
1045            for list in self.fittable_param:
1046                if list[1] == key and list[7] != None:
1047                    list[7].SetSelection(selection)
1048                    # For the array disp_model, set the values and weights
1049                    if selection == 1:
1050                        disp_model.set_weights(self.values[key], 
1051                                              self.weights[key])
1052                        try:
1053                            # Diables all fittable params for array
1054                            list[0].SetValue(False)
1055                            list[0].Disable()
1056                            list[2].Disable()
1057                            list[5].Disable()
1058                            list[6].Disable()
1059                        except:
1060                            pass
1061            # For array, disable all fixed params
1062            if selection == 1:
1063                for item in self.fixed_param:
1064                    if item[1].split(".")[0] == key.split(".")[0]:
1065                        # try it and pass it for the orientation for 1D
1066                        try:
1067                            item[2].Disable()
1068                        except:
1069                            pass
1070   
1071        # Make sure the check box updated when all checked
1072        if self.cb1.GetValue():
1073            self.select_all_param(None)       
1074     
1075    def _selectDlg(self):
1076        """
1077        open a dialog file to selected the customized dispersity
1078        """
1079        import os
1080        dlg = wx.FileDialog(self, "Choose a weight file",
1081                                self._default_save_location , "", 
1082                                "*.*", wx.OPEN)
1083        path = None
1084        if dlg.ShowModal() == wx.ID_OK:
1085            path = dlg.GetPath()
1086        dlg.Destroy()
1087        return path
1088
1089    def _reset_context_menu(self):
1090        """
1091        reset the context menu
1092        """
1093        for name, state in self.state.saved_states.iteritems():
1094            self.number_saved_state += 1
1095            ## Add item in the context menu
1096            id = wx.NewId()
1097            msg = 'Save model and state %g' % self.number_saved_state
1098            self.popUpMenu.Append(id, name, msg)
1099            wx.EVT_MENU(self, id, self.onResetModel)
1100   
1101    def _reset_plotting_range(self, state):
1102        """
1103        Reset the plotting range to a given state
1104        """
1105        # if self.check_invalid_panel():
1106        #    return
1107        self.qmin.SetValue(str(state.qmin))
1108        self.qmax.SetValue(str(state.qmax)) 
1109
1110    def _save_typeOfmodel(self):
1111        """
1112        save radiobutton containing the type model that can be selected
1113        """
1114        self.state.shape_rbutton = self.shape_rbutton.GetValue()
1115        self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue()
1116        self.state.struct_rbutton = self.struct_rbutton.GetValue()
1117        self.state.plugin_rbutton = self.plugin_rbutton.GetValue()
1118        self.state.structurebox= self.structurebox.GetCurrentSelection()
1119        self.state.formfactorbox = self.formfactorbox.GetCurrentSelection()
1120       
1121        #self._undo.Enable(True)
1122        ## post state to fit panel
1123        event = PageInfoEvent(page = self)
1124        wx.PostEvent(self.parent, event)
1125       
1126    def _save_plotting_range(self ):
1127        """
1128        save the state of plotting range
1129        """
1130        self.state.qmin = self.qmin_x
1131        self.state.qmax = self.qmax_x
1132        self.state.npts = self.npts_x
1133           
1134    def _onparamEnter_helper(self):
1135        """
1136        check if values entered by the user are changed and valid to replot
1137        model
1138        """
1139        # Flag to register when a parameter has changed.   
1140        is_modified = False
1141        self.fitrange = True
1142        is_2Ddata = False
1143        #self._undo.Enable(True)
1144        # check if 2d data
1145        if self.data.__class__.__name__ == "Data2D":
1146            is_2Ddata = True
1147        if self.model !=None:
1148            try:
1149                is_modified = self._check_value_enter(self.fittable_param,
1150                                                     is_modified)
1151                is_modified = self._check_value_enter(self.fixed_param,
1152                                                      is_modified)
1153                is_modified = self._check_value_enter(self.parameters,
1154                                                      is_modified) 
1155            except:
1156                pass
1157            #if is_modified:
1158
1159            # Here we should check whether the boundaries have been modified.
1160            # If qmin and qmax have been modified, update qmin and qmax and
1161            # set the is_modified flag to True
1162            if self._validate_qrange(self.qmin, self.qmax):
1163                tempmin = float(self.qmin.GetValue())
1164                if tempmin != self.qmin_x:
1165                    self.qmin_x = tempmin
1166                    is_modified = True
1167                tempmax = float(self.qmax.GetValue())
1168                if tempmax != self.qmax_x:
1169                    self.qmax_x = tempmax
1170                    is_modified = True
1171           
1172                if is_2Ddata:
1173                    # set mask   
1174                    is_modified = self._validate_Npts()
1175                   
1176            else:
1177                self.fitrange = False   
1178
1179            ## if any value is modify draw model with new value
1180            if not self.fitrange:
1181                #self.btFit.Disable()
1182                if is_2Ddata: self.btEditMask.Disable()
1183            else:
1184                #self.btFit.Enable(True)
1185                if is_2Ddata: self.btEditMask.Enable(True)
1186
1187            if is_modified and self.fitrange:
1188                self.state_change= True
1189                self._draw_model() 
1190                self.Refresh()
1191        return is_modified
1192   
1193    def _update_paramv_on_fit(self):
1194        """
1195        make sure that update param values just before the fitting
1196        """
1197        #flag for qmin qmax check values
1198        flag = True
1199        self.fitrange = True
1200        is_modified = False
1201
1202        #wx.PostEvent(self._manager.parent, StatusEvent(status=" \
1203        #updating ... ",type="update"))
1204
1205        ##So make sure that update param values on_Fit.
1206        #self._undo.Enable(True)
1207        if self.model !=None:           
1208            ##Check the values
1209            self._check_value_enter( self.fittable_param ,is_modified)
1210            self._check_value_enter( self.fixed_param ,is_modified)
1211            self._check_value_enter( self.parameters ,is_modified)
1212
1213            # If qmin and qmax have been modified, update qmin and qmax and
1214             # Here we should check whether the boundaries have been modified.
1215            # If qmin and qmax have been modified, update qmin and qmax and
1216            # set the is_modified flag to True
1217            self.fitrange = self._validate_qrange(self.qmin, self.qmax)
1218            if self.fitrange:
1219                tempmin = float(self.qmin.GetValue())
1220                if tempmin != self.qmin_x:
1221                    self.qmin_x = tempmin
1222                tempmax = float(self.qmax.GetValue())
1223                if tempmax != self.qmax_x:
1224                    self.qmax_x = tempmax
1225                if tempmax == tempmin:
1226                    flag = False   
1227                temp_smearer = None
1228                if not self.disable_smearer.GetValue():
1229                    temp_smearer= self.current_smearer
1230                    if self.slit_smearer.GetValue():
1231                        flag = self.update_slit_smear()
1232                    elif self.pinhole_smearer.GetValue():
1233                        flag = self.update_pinhole_smear()
1234                    else:
1235                        self._manager.set_smearer(smearer=temp_smearer,
1236                                                  uid=self.uid,
1237                                                     qmin=float(self.qmin_x),
1238                                                      qmax=float(self.qmax_x),
1239                                                      draw=False)
1240                elif not self._is_2D():
1241                    self._manager.set_smearer(smearer=temp_smearer,
1242                                              qmin=float(self.qmin_x),
1243                                              uid=self.uid, 
1244                                                 qmax= float(self.qmax_x))
1245                    index_data = ((self.qmin_x <= self.data.x)&\
1246                                  (self.data.x <= self.qmax_x))
1247                    val = str(len(self.data.x[index_data==True]))
1248                    self.Npts_fit.SetValue(val)
1249                    flag = True
1250                if self._is_2D():
1251                    # only 2D case set mask   
1252                    flag = self._validate_Npts()
1253                    if not flag:
1254                        return flag
1255            else: flag = False
1256        else: 
1257            flag = False
1258
1259        #For invalid q range, disable the mask editor and fit button, vs.   
1260        if not self.fitrange:
1261            #self.btFit.Disable()
1262            if self._is_2D():self.btEditMask.Disable()
1263        else:
1264            #self.btFit.Enable(True)
1265            if self._is_2D():self.btEditMask.Enable(True)
1266
1267        if not flag:
1268            msg = "Cannot Plot or Fit :Must select a "
1269            msg += " model or Fitting range is not valid!!!  "
1270            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
1271       
1272        self.save_current_state()
1273   
1274        return flag                           
1275               
1276    def _is_modified(self, is_modified):
1277        """
1278        return to self._is_modified
1279        """
1280        return is_modified
1281                       
1282    def _reset_parameters_state(self, listtorestore, statelist):
1283        """
1284        Reset the parameters at the given state
1285        """
1286        if len(statelist) == 0 or len(listtorestore) == 0:
1287            return
1288        if len(statelist) !=  len(listtorestore):
1289            return
1290
1291        for j in range(len(listtorestore)):
1292            item_page = listtorestore[j]
1293            item_page_info = statelist[j]
1294            ##change the state of the check box for simple parameters
1295            if item_page[0]!=None:   
1296                item_page[0].SetValue(item_page_info[0])
1297            if item_page[2]!=None:
1298                item_page[2].SetValue(item_page_info[2])
1299                if item_page[2].__class__.__name__ == "ComboBox":
1300                   if self.model.fun_list.has_key(item_page_info[2]):
1301                       fun_val = self.model.fun_list[item_page_info[2]]
1302                       self.model.setParam(item_page_info[1],fun_val)
1303            if item_page[3]!=None:
1304                ## show or hide text +/-
1305                if item_page_info[2]:
1306                    item_page[3].Show(True)
1307                else:
1308                    item_page[3].Hide()
1309            if item_page[4]!=None:
1310                ## show of hide the text crtl for fitting error
1311                if item_page_info[4][0]:
1312                    item_page[4].Show(True)
1313                    item_page[4].SetValue(item_page_info[4][1])
1314                else:
1315                    item_page[3].Hide()
1316            if item_page[5]!=None:
1317                ## show of hide the text crtl for fitting error
1318                item_page[5].Show(item_page_info[5][0])
1319                item_page[5].SetValue(item_page_info[5][1])
1320               
1321            if item_page[6]!=None:
1322                ## show of hide the text crtl for fitting error
1323                item_page[6].Show(item_page_info[6][0])
1324                item_page[6].SetValue(item_page_info[6][1])
1325
1326                   
1327    def _reset_strparam_state(self, listtorestore, statelist):
1328        """
1329        Reset the string parameters at the given state
1330        """
1331        if len(statelist) == 0:
1332            return
1333
1334        listtorestore = copy.deepcopy(statelist)
1335       
1336        for j in range(len(listtorestore)):
1337            item_page = listtorestore[j]
1338            item_page_info = statelist[j]
1339            ##change the state of the check box for simple parameters
1340           
1341            if item_page[0] != None:   
1342                item_page[0].SetValue(format_number(item_page_info[0], True))
1343
1344            if item_page[2] != None:
1345                param_name = item_page_info[1]
1346                value = item_page_info[2]
1347                selection = value
1348                if self.model.fun_list.has_key(value):
1349                    selection = self.model.fun_list[value]
1350                item_page[2].SetValue(selection)
1351                self.model.setParam(param_name, selection)
1352                                     
1353    def _copy_parameters_state(self, listtocopy, statelist):
1354        """
1355        copy the state of button
1356       
1357        :param listtocopy: the list of check button to copy
1358        :param statelist: list of state object to store the current state
1359       
1360        """
1361        if len(listtocopy)==0:
1362            return
1363       
1364        for item in listtocopy:
1365 
1366            checkbox_state = None
1367            if item[0]!= None:
1368                checkbox_state= item[0].GetValue()
1369            parameter_name = item[1]
1370            parameter_value = None
1371            if item[2]!=None:
1372                parameter_value = item[2].GetValue()
1373            static_text = None
1374            if item[3]!=None:
1375                static_text = item[3].IsShown()
1376            error_value = None
1377            error_state = None
1378            if item[4]!= None:
1379                error_value = item[4].GetValue()
1380                error_state = item[4].IsShown()
1381               
1382            min_value = None
1383            min_state = None
1384            if item[5]!= None:
1385                min_value = item[5].GetValue()
1386                min_state = item[5].IsShown()
1387               
1388            max_value = None
1389            max_state = None
1390            if item[6]!= None:
1391                max_value = item[6].GetValue()
1392                max_state = item[6].IsShown()
1393            unit=None
1394            if item[7]!=None:
1395                unit = item[7].GetLabel()
1396               
1397            statelist.append([checkbox_state, parameter_name, parameter_value,
1398                              static_text ,[error_state, error_value],
1399                                [min_state, min_value],
1400                                [max_state, max_value], unit])
1401           
1402    def _set_model_sizer_selection(self, model):
1403        """
1404        Display the sizer according to the type of the current model
1405        """
1406        if model == None:
1407            return
1408        if hasattr(model ,"s_model"):
1409           
1410            class_name = model.s_model.__class__
1411            name = model.s_model.name
1412            flag = (name != "NoStructure")
1413            if flag and \
1414                (class_name in self.model_list_box["Structure Factors"]):
1415                self.structurebox.Show()
1416                self.text2.Show()               
1417                self.structurebox.Enable()
1418                self.text2.Enable()
1419                items = self.structurebox.GetItems()
1420                self.sizer1.Layout()
1421               
1422                for i in range(len(items)):
1423                    if items[i]== str(name):
1424                        self.structurebox.SetSelection(i)
1425                        break
1426                   
1427        if hasattr(model ,"p_model"):
1428            class_name = model.p_model.__class__
1429            name = model.p_model.name
1430            self.formfactorbox.Clear()
1431           
1432            for k, list in self.model_list_box.iteritems():
1433                if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]:
1434                    self.shape_rbutton.SetValue(True)
1435                    ## fill the form factor list with new model
1436                    self._populate_box(self.formfactorbox,self.model_list_box["Shapes"])
1437                    items = self.formfactorbox.GetItems()
1438                    ## set comboxbox to the selected item
1439                    for i in range(len(items)):
1440                        if items[i]== str(name):
1441                            self.formfactorbox.SetSelection(i)
1442                            break
1443                    return
1444                elif k == "Shape-Independent":
1445                    self.shape_indep_rbutton.SetValue(True)
1446                elif k == "Structure Factors":
1447                     self.struct_rbutton.SetValue(True)
1448                elif  k == "Multi-Functions":
1449                    continue
1450                else:
1451                    self.plugin_rbutton.SetValue(True)
1452               
1453                if class_name in list:
1454                    ## fill the form factor list with new model
1455                    self._populate_box(self.formfactorbox, list)
1456                    items = self.formfactorbox.GetItems()
1457                    ## set comboxbox to the selected item
1458                    for i in range(len(items)):
1459                        if items[i]== str(name):
1460                            self.formfactorbox.SetSelection(i)
1461                            break
1462                    break
1463        else:
1464
1465            ## Select the model from the menu
1466            class_name = model.__class__
1467            name = model.name
1468            self.formfactorbox.Clear()
1469            items = self.formfactorbox.GetItems()
1470   
1471            for k, list in self.model_list_box.iteritems():         
1472                if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]:
1473                    if class_name in self.model_list_box["P(Q)*S(Q)"]:
1474                        self.structurebox.Show()
1475                        self.text2.Show()
1476                        self.structurebox.Enable()
1477                        self.structurebox.SetSelection(0)
1478                        self.text2.Enable()
1479                    else:
1480                        self.structurebox.Hide()
1481                        self.text2.Hide()
1482                        self.structurebox.Disable()
1483                        self.structurebox.SetSelection(0)
1484                        self.text2.Disable()
1485                       
1486                    self.shape_rbutton.SetValue(True)
1487                    ## fill the form factor list with new model
1488                    self._populate_box(self.formfactorbox,self.model_list_box["Shapes"])
1489                    items = self.formfactorbox.GetItems()
1490                    ## set comboxbox to the selected item
1491                    for i in range(len(items)):
1492                        if items[i]== str(name):
1493                            self.formfactorbox.SetSelection(i)
1494                            break
1495                    return
1496                elif k == "Shape-Independent":
1497                    self.shape_indep_rbutton.SetValue(True)
1498                elif k == "Structure Factors":
1499                    self.struct_rbutton.SetValue(True)
1500                elif  k == "Multi-Functions":
1501                    continue
1502                else:
1503                    self.plugin_rbutton.SetValue(True)
1504                if class_name in list:
1505                    self.structurebox.SetSelection(0)
1506                    self.structurebox.Disable()
1507                    self.text2.Disable()                   
1508                    ## fill the form factor list with new model
1509                    self._populate_box(self.formfactorbox, list)
1510                    items = self.formfactorbox.GetItems()
1511                    ## set comboxbox to the selected item
1512                    for i in range(len(items)):
1513                        if items[i]== str(name):
1514                            self.formfactorbox.SetSelection(i)
1515                            break
1516                    break
1517   
1518    def _draw_model(self, update_chisqr=True):
1519        """
1520        Method to draw or refresh a plotted model.
1521        The method will use the data member from the model page
1522        to build a call to the fitting perspective manager.
1523       
1524        :param chisqr: update chisqr value [bool]
1525        """
1526        #if self.check_invalid_panel():
1527        #    return
1528        if self.model !=None:
1529            temp_smear=None
1530            if hasattr(self, "enable_smearer"):
1531                if not self.disable_smearer.GetValue():
1532                    temp_smear= self.current_smearer
1533            toggle_mode_on = self.model_view.IsEnabled()
1534            self._manager.draw_model(self.model, 
1535                                    data=self.data,
1536                                    smearer= temp_smear,
1537                                    qmin=float(self.qmin_x), 
1538                                    qmax=float(self.qmax_x),
1539                                    qstep= float(self.npts_x),
1540                                    page_id=self.uid,
1541                                    toggle_mode_on=toggle_mode_on, 
1542                                    state = self.state,
1543                                    enable2D=self.enable2D,
1544                                    update_chisqr=update_chisqr)
1545       
1546       
1547    def _on_show_sld(self, event=None):
1548        """
1549        Plot SLD profile
1550        """
1551        # get profile data
1552        x,y=self.model.getProfile()
1553
1554        from danse.common.plottools import Data1D
1555        #from sans.perspectives.theory.profile_dialog import SLDPanel
1556        from sans.guiframe.local_perspectives.plotting.profile_dialog \
1557        import SLDPanel
1558        sld_data = Data1D(x,y)
1559        sld_data.name = 'SLD'
1560        sld_data.axes = self.sld_axes
1561        self.panel = SLDPanel(self, data=sld_data,axes =self.sld_axes,id =-1 )
1562        self.panel.ShowModal()   
1563       
1564    def _set_multfactor_combobox(self, multiplicity=10):   
1565        """
1566        Set comboBox for muitfactor of CoreMultiShellModel
1567        :param multiplicit: no. of multi-functionality
1568        """
1569        # build content of the combobox
1570        for idx in range(0,multiplicity):
1571            self.multifactorbox.Append(str(idx),int(idx))
1572            #self.multifactorbox.SetSelection(1)
1573        self._hide_multfactor_combobox()
1574       
1575    def _show_multfactor_combobox(self):   
1576        """
1577        Show the comboBox of muitfactor of CoreMultiShellModel
1578        """ 
1579        if not self.mutifactor_text.IsShown():
1580            self.mutifactor_text.Show(True)
1581            self.mutifactor_text1.Show(True)
1582        if not self.multifactorbox.IsShown():
1583            self.multifactorbox.Show(True) 
1584             
1585    def _hide_multfactor_combobox(self):   
1586        """
1587        Hide the comboBox of muitfactor of CoreMultiShellModel
1588        """ 
1589        if self.mutifactor_text.IsShown():
1590            self.mutifactor_text.Hide()
1591            self.mutifactor_text1.Hide()
1592        if self.multifactorbox.IsShown():
1593            self.multifactorbox.Hide()   
1594
1595       
1596    def _show_combox_helper(self):
1597        """
1598        Fill panel's combo box according to the type of model selected
1599        """
1600        if self.shape_rbutton.GetValue():
1601            ##fill the combobox with form factor list
1602            self.structurebox.SetSelection(0)
1603            self.structurebox.Disable()
1604            self.formfactorbox.Clear()
1605            self._populate_box( self.formfactorbox,self.model_list_box["Shapes"])
1606        if self.shape_indep_rbutton.GetValue():
1607            ##fill the combobox with shape independent  factor list
1608            self.structurebox.SetSelection(0)
1609            self.structurebox.Disable()
1610            self.formfactorbox.Clear()
1611            self._populate_box( self.formfactorbox,
1612                                self.model_list_box["Shape-Independent"])
1613        if self.struct_rbutton.GetValue():
1614            ##fill the combobox with structure factor list
1615            self.structurebox.SetSelection(0)
1616            self.structurebox.Disable()
1617            self.formfactorbox.Clear()
1618            self._populate_box( self.formfactorbox,
1619                                self.model_list_box["Structure Factors"])
1620        if self.plugin_rbutton.GetValue():
1621            ##fill the combobox with form factor list
1622            self.structurebox.Disable()
1623            self.formfactorbox.Clear()
1624            self._populate_box( self.formfactorbox,
1625                                self.model_list_box["Customized Models"])
1626       
1627    def _show_combox(self, event=None):
1628        """
1629        Show combox box associate with type of model selected
1630        """
1631        #if self.check_invalid_panel():
1632        #    self.shape_rbutton.SetValue(True)
1633        #    return
1634
1635        self._show_combox_helper()
1636        self._on_select_model(event=None)
1637        self._save_typeOfmodel()
1638        self.sizer4_4.Layout()
1639        self.sizer4.Layout()
1640        self.Layout()
1641        self.Refresh()
1642 
1643    def _populate_box(self, combobox, list):
1644        """
1645        fill combox box with dict item
1646       
1647        :param list: contains item to fill the combox
1648            item must model class
1649        """
1650        for models in list:
1651            model= models()
1652            name = model.__class__.__name__
1653            if models.__name__!="NoStructure":
1654                if hasattr(model, "name"):
1655                    name = model.name
1656                combobox.Append(name,models)
1657        return 0
1658   
1659    def _onQrangeEnter(self, event):
1660        """
1661        Check validity of value enter in the Q range field
1662       
1663        """
1664        tcrtl = event.GetEventObject()
1665        #Clear msg if previously shown.
1666        msg = ""
1667        wx.PostEvent(self.parent, StatusEvent(status=msg))
1668        # Flag to register when a parameter has changed.
1669        is_modified = False
1670        if tcrtl.GetValue().lstrip().rstrip() != "":
1671            try:
1672                value = float(tcrtl.GetValue())
1673                tcrtl.SetBackgroundColour(wx.WHITE)
1674                # If qmin and qmax have been modified, update qmin and qmax
1675                if self._validate_qrange(self.qmin, self.qmax):
1676                    tempmin = float(self.qmin.GetValue())
1677                    if tempmin != self.qmin_x:
1678                        self.qmin_x = tempmin
1679                    tempmax = float(self.qmax.GetValue())
1680                    if tempmax != self.qmax_x:
1681                        self.qmax_x = tempmax
1682                else:
1683                    tcrtl.SetBackgroundColour("pink")
1684                    msg = "Model Error:wrong value entered : %s" % sys.exc_value
1685                    wx.PostEvent(self.parent, StatusEvent(status=msg))
1686                    return 
1687            except:
1688                tcrtl.SetBackgroundColour("pink")
1689                msg = "Model Error:wrong value entered : %s" % sys.exc_value
1690                wx.PostEvent(self.parent, StatusEvent(status=msg))
1691                return 
1692            #Check if # of points for theory model are valid(>0).
1693            if self.npts != None:
1694                if check_float(self.npts):
1695                    temp_npts = float(self.npts.GetValue())
1696                    if temp_npts !=  self.num_points:
1697                        self.num_points = temp_npts
1698                        is_modified = True
1699                else:
1700                    msg = "Cannot Plot :No npts in that Qrange!!!  "
1701                    wx.PostEvent(self.parent, StatusEvent(status=msg))
1702        else:
1703           tcrtl.SetBackgroundColour("pink")
1704           msg = "Model Error:wrong value entered!!!"
1705           wx.PostEvent(self.parent, StatusEvent(status=msg))
1706        #self._undo.Enable(True)
1707        self.save_current_state()
1708        event = PageInfoEvent(page=self)
1709        wx.PostEvent(self.parent, event)
1710        self.state_change = False
1711        #Draw the model for a different range
1712        self._draw_model()
1713                   
1714    def _theory_qrange_enter(self, event):
1715        """
1716        Check validity of value enter in the Q range field
1717        """
1718       
1719        tcrtl= event.GetEventObject()
1720        #Clear msg if previously shown.
1721        msg= ""
1722        wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
1723        # Flag to register when a parameter has changed.
1724        is_modified = False
1725        if tcrtl.GetValue().lstrip().rstrip()!="":
1726            try:
1727                value = float(tcrtl.GetValue())
1728                tcrtl.SetBackgroundColour(wx.WHITE)
1729
1730                # If qmin and qmax have been modified, update qmin and qmax
1731                if self._validate_qrange(self.theory_qmin, self.theory_qmax):
1732                    tempmin = float(self.theory_qmin.GetValue())
1733                    if tempmin != self.theory_qmin_x:
1734                        self.theory_qmin_x = tempmin
1735                    tempmax = float(self.theory_qmax.GetValue())
1736                    if tempmax != self.qmax_x:
1737                        self.theory_qmax_x = tempmax
1738                else:
1739                    tcrtl.SetBackgroundColour("pink")
1740                    msg= "Model Error:wrong value entered : %s"% sys.exc_value
1741                    wx.PostEvent(self._manager.parent, StatusEvent(status = msg ))
1742                    return 
1743            except:
1744                tcrtl.SetBackgroundColour("pink")
1745                msg= "Model Error:wrong value entered : %s"% sys.exc_value
1746                wx.PostEvent(self._manager.parent, StatusEvent(status = msg ))
1747                return 
1748            #Check if # of points for theory model are valid(>0).
1749            if self.theory_npts != None:
1750                if check_float(self.theory_npts):
1751                    temp_npts = float(self.theory_npts.GetValue())
1752                    if temp_npts !=  self.num_points:
1753                        self.num_points = temp_npts
1754                        is_modified = True
1755                else:
1756                    msg= "Cannot Plot :No npts in that Qrange!!!  "
1757                    wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
1758        else:
1759           tcrtl.SetBackgroundColour("pink")
1760           msg = "Model Error:wrong value entered!!!"
1761           wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
1762        #self._undo.Enable(True)
1763        self.save_current_state()
1764        event = PageInfoEvent(page = self)
1765        wx.PostEvent(self.parent, event)
1766        self.state_change= False
1767        #Draw the model for a different range
1768        self._draw_model()
1769                   
1770    def _on_select_model_helper(self): 
1771        """
1772        call back for model selection
1773        """
1774        ## reset dictionary containing reference to dispersion
1775        self._disp_obj_dict = {}
1776        self.disp_cb_dict ={}
1777       
1778        f_id = self.formfactorbox.GetCurrentSelection()
1779        #For MAC
1780        form_factor = None
1781        if f_id >= 0:
1782            form_factor = self.formfactorbox.GetClientData(f_id)
1783
1784        if not form_factor in  self.model_list_box["multiplication"]:
1785            self.structurebox.Hide()
1786            self.text2.Hide()           
1787            self.structurebox.Disable()
1788            self.structurebox.SetSelection(0)
1789            self.text2.Disable()
1790        else:
1791            self.structurebox.Show()
1792            self.text2.Show()
1793            self.structurebox.Enable()
1794            self.text2.Enable()
1795           
1796        if form_factor != None:   
1797            # set multifactor for Mutifunctional models   
1798            if form_factor().__class__ in self.model_list_box["Multi-Functions"]:
1799                m_id = self.multifactorbox.GetCurrentSelection()
1800                multiplicity = form_factor().multiplicity_info[0]
1801                self.multifactorbox.Clear()
1802                #self.mutifactor_text.SetLabel(form_factor().details[])
1803                self._set_multfactor_combobox(multiplicity)
1804                self._show_multfactor_combobox()
1805                #ToDo:  this info should be called directly from the model
1806                text = form_factor().multiplicity_info[1]#'No. of Shells: '
1807
1808                #self.mutifactor_text.Clear()
1809                self.mutifactor_text.SetLabel(text)
1810                if m_id > multiplicity -1:
1811                    # default value
1812                    m_id = 1
1813                   
1814                self.multi_factor = self.multifactorbox.GetClientData(m_id)
1815                if self.multi_factor == None: self.multi_factor =0
1816                form_factor = form_factor(int(self.multi_factor))
1817                self.multifactorbox.SetSelection(m_id)
1818                # Check len of the text1 and max_multiplicity
1819                text = ''
1820                if form_factor.multiplicity_info[0] == len(form_factor.multiplicity_info[2]):
1821                    text = form_factor.multiplicity_info[2][self.multi_factor]
1822                self.mutifactor_text1.SetLabel(text)
1823                # Check if model has  get sld profile.
1824                if len(form_factor.multiplicity_info[3]) > 0:
1825                    self.sld_axes = form_factor.multiplicity_info[3]
1826                    self.show_sld_button.Show(True)
1827                else:
1828                    self.sld_axes = ""
1829
1830            else:
1831                self._hide_multfactor_combobox()
1832                self.show_sld_button.Hide()
1833                form_factor = form_factor()
1834                self.multi_factor = None
1835        else:
1836            self._hide_multfactor_combobox()
1837            self.show_sld_button.Hide()
1838            self.multi_factor = None 
1839             
1840        s_id = self.structurebox.GetCurrentSelection()
1841        struct_factor = self.structurebox.GetClientData( s_id )
1842       
1843        if  struct_factor !=None:
1844            from sans.models.MultiplicationModel import MultiplicationModel
1845            self.model= MultiplicationModel(form_factor,struct_factor())
1846           
1847        else:
1848            if form_factor != None:
1849                self.model= form_factor
1850            else:
1851                self.model = None
1852                return self.model
1853           
1854
1855        ## post state to fit panel
1856        self.state.parameters =[]
1857        self.state.model =self.model
1858        self.state.qmin = self.qmin_x
1859        self.state.multi_factor = self.multi_factor
1860        self.disp_list =self.model.getDispParamList()
1861        self.state.disp_list = self.disp_list
1862        self.Layout()     
1863       
1864    def _validate_qrange(self, qmin_ctrl, qmax_ctrl):
1865        """
1866        Verify that the Q range controls have valid values
1867        and that Qmin < Qmax.
1868       
1869        :param qmin_ctrl: text control for Qmin
1870        :param qmax_ctrl: text control for Qmax
1871       
1872        :return: True is the Q range is value, False otherwise
1873       
1874        """
1875        qmin_validity = check_float(qmin_ctrl)
1876        qmax_validity = check_float(qmax_ctrl)
1877        if not (qmin_validity and qmax_validity):
1878            return False
1879        else:
1880            qmin = float(qmin_ctrl.GetValue())
1881            qmax = float(qmax_ctrl.GetValue())
1882            if qmin <= qmax:
1883                #Make sure to set both colours white. 
1884                qmin_ctrl.SetBackgroundColour(wx.WHITE)
1885                qmin_ctrl.Refresh()
1886                qmax_ctrl.SetBackgroundColour(wx.WHITE)
1887                qmax_ctrl.Refresh()
1888            else:
1889                qmin_ctrl.SetBackgroundColour("pink")
1890                qmin_ctrl.Refresh()
1891                qmax_ctrl.SetBackgroundColour("pink")
1892                qmax_ctrl.Refresh()
1893                msg= "Invalid Q range: Q min must be smaller than Q max"
1894                wx.PostEvent(self.parent.parent, StatusEvent(status = msg))
1895                return False
1896        return True
1897   
1898    def _validate_Npts(self): 
1899        """
1900        Validate the number of points for fitting is more than 10 points.
1901        If valid, setvalues Npts_fit otherwise post msg.
1902        """
1903        #default flag
1904        flag = True
1905
1906        # q value from qx and qy
1907        radius= numpy.sqrt( self.data.qx_data * self.data.qx_data + 
1908                            self.data.qy_data * self.data.qy_data )
1909        #get unmasked index
1910        index_data = (float(self.qmin.GetValue()) <= radius) & \
1911                        (radius <= float(self.qmax.GetValue()))
1912        index_data = (index_data) & (self.data.mask) 
1913        index_data = (index_data) & (numpy.isfinite(self.data.data))
1914
1915        if len(index_data[index_data]) < 10:
1916            # change the color pink.
1917            self.qmin.SetBackgroundColour("pink")
1918            self.qmin.Refresh()
1919            self.qmax.SetBackgroundColour("pink")
1920            self.qmax.Refresh()
1921            msg= "Cannot Plot :No or too little npts in that data range!!!  "
1922            wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
1923            self.fitrange = False
1924            flag = False
1925        else:
1926            self.Npts_fit.SetValue(str(len(self.data.mask[index_data==True])))
1927            self.fitrange = True
1928           
1929        return flag
1930   
1931    def _check_value_enter(self, list, modified):
1932        """
1933        :param list: model parameter and panel info
1934        :Note: each item of the list should be as follow:
1935            item=[check button state, parameter's name,
1936                paramater's value, string="+/-",
1937                parameter's error of fit,
1938                parameter's minimum value,
1939                parrameter's maximum value ,
1940                parameter's units]
1941        """ 
1942        is_modified =  modified
1943        if len(list)==0:
1944            return is_modified
1945        for item in list:
1946            #skip angle parameters for 1D
1947            if self.data.__class__.__name__ !="Data2D":
1948                if item in self.orientation_params:
1949                    continue
1950            #try:
1951            name = str(item[1])
1952           
1953            if string.find(name,".npts") ==-1 and \
1954                                        string.find(name,".nsigmas")==-1:     
1955                ## check model parameters range             
1956                param_min= None
1957                param_max= None
1958               
1959                ## check minimun value
1960                if item[5]!= None and item[5]!= "":
1961                    if item[5].GetValue().lstrip().rstrip()!="":
1962                        try:
1963                           
1964                            param_min = float(item[5].GetValue())
1965                            if not self._validate_qrange(item[5],item[2]):
1966                                if numpy.isfinite(param_min):
1967                                    item[2].SetValue(format_number(param_min))
1968                           
1969                            item[5].SetBackgroundColour(wx.WHITE)
1970                            item[2].SetBackgroundColour(wx.WHITE)
1971                                           
1972                        except:
1973                            msg = "Wrong Fit parameter range entered "
1974                            wx.PostEvent(self.parent.parent, 
1975                                         StatusEvent(status = msg))
1976                            raise ValueError, msg
1977                        is_modified = True
1978                ## check maximum value
1979                if item[6]!= None and item[6]!= "":
1980                    if item[6].GetValue().lstrip().rstrip()!="":
1981                        try:                         
1982                            param_max = float(item[6].GetValue())
1983                            if not self._validate_qrange(item[2],item[6]):
1984                                if numpy.isfinite(param_max):
1985                                    item[2].SetValue(format_number(param_max)) 
1986                           
1987                            item[6].SetBackgroundColour(wx.WHITE)
1988                            item[2].SetBackgroundColour(wx.WHITE)
1989                        except:
1990                            msg = "Wrong Fit parameter range entered "
1991                            wx.PostEvent(self.parent.parent, 
1992                                         StatusEvent(status = msg))
1993                            raise ValueError, msg
1994                        is_modified = True
1995               
1996
1997                if param_min != None and param_max !=None:
1998                    if not self._validate_qrange(item[5], item[6]):
1999                        msg= "Wrong Fit range entered for parameter "
2000                        msg+= "name %s of model %s "%(name, self.model.name)
2001                        wx.PostEvent(self.parent.parent, 
2002                                     StatusEvent(status = msg))
2003               
2004                if name in self.model.details.keys():   
2005                        self.model.details[name][1:3] = param_min, param_max
2006                        is_modified = True
2007             
2008                else:
2009                        self.model.details [name] = ["", param_min, param_max] 
2010                        is_modified = True
2011            try:   
2012                # Check if the textctr is enabled
2013                if item[2].IsEnabled():
2014                    value= float(item[2].GetValue())
2015                    item[2].SetBackgroundColour("white")
2016                    # If the value of the parameter has changed,
2017                    # +update the model and set the is_modified flag
2018                    if value != self.model.getParam(name) and \
2019                                                numpy.isfinite(value):
2020                        self.model.setParam(name, value)
2021                       
2022            except:
2023                item[2].SetBackgroundColour("pink")
2024                msg = "Wrong Fit parameter value entered "
2025                wx.PostEvent(self.parent.parent, StatusEvent(status = msg))
2026               
2027        return is_modified
2028       
2029 
2030    def _set_dipers_Param(self, event):
2031        """
2032        respond to self.enable_disp and self.disable_disp radio box.
2033        The dispersity object is reset inside the model into Gaussian.
2034        When the user select yes , this method display a combo box for more selection
2035        when the user selects No,the combo box disappears.
2036        Redraw the model with the default dispersity (Gaussian)
2037        """
2038        #if self.check_invalid_panel():
2039        #    return
2040        ## On selction if no model exists.
2041        if self.model ==None:
2042            self.disable_disp.SetValue(True)
2043            msg="Please select a Model first..."
2044            wx.MessageBox(msg, 'Info')
2045            wx.PostEvent(self._manager.parent, StatusEvent(status=\
2046                            "Polydispersion: %s"%msg))
2047            return
2048
2049        self._reset_dispersity()
2050   
2051        if self.model ==None:
2052            self.model_disp.Hide()
2053            self.sizer4_4.Clear(True)
2054            return
2055
2056        if self.enable_disp.GetValue():
2057            ## layout for model containing no dispersity parameters
2058           
2059            self.disp_list= self.model.getDispParamList()
2060             
2061            if len(self.disp_list)==0 and len(self.disp_cb_dict)==0:
2062                self._layout_sizer_noDipers() 
2063            else:
2064                ## set gaussian sizer
2065                self._on_select_Disp(event=None)
2066        else:
2067            self.sizer4_4.Clear(True)
2068           
2069        ## post state to fit panel
2070        self.save_current_state()
2071        if event !=None:
2072            #self._undo.Enable(True)
2073            event = PageInfoEvent(page = self)
2074            wx.PostEvent(self.parent, event)
2075        #draw the model with the current dispersity
2076        self._draw_model()
2077        self.sizer4_4.Layout()
2078        self.sizer5.Layout()
2079        self.Layout()
2080        self.Refresh()     
2081         
2082       
2083    def _layout_sizer_noDipers(self):
2084        """
2085        Draw a sizer with no dispersity info
2086        """
2087        ix=0
2088        iy=1
2089        self.fittable_param=[]
2090        self.fixed_param=[]
2091        self.orientation_params_disp=[]
2092       
2093        self.sizer4_4.Clear(True)
2094        text = "No polydispersity available for this model"
2095        text = "No polydispersity available for this model"
2096        model_disp = wx.StaticText(self, -1, text)
2097        self.sizer4_4.Add(model_disp,( iy, ix),(1,1), 
2098                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
2099        self.sizer4_4.Layout()
2100        self.sizer4.Layout()
2101   
2102    def _reset_dispersity(self):
2103        """
2104        put gaussian dispersity into current model
2105        """
2106        if len(self.param_toFit)>0:
2107            for item in self.fittable_param:
2108                if item in self.param_toFit:
2109                    self.param_toFit.remove(item)
2110
2111            for item in self.orientation_params_disp:
2112                if item in self.param_toFit:
2113                    self.param_toFit.remove(item)
2114         
2115        self.fittable_param=[]
2116        self.fixed_param=[]
2117        self.orientation_params_disp=[]
2118        self.values={}
2119        self.weights={}
2120     
2121        from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion
2122        if len(self.disp_cb_dict)==0:
2123            self.save_current_state()
2124            self.sizer4_4.Clear(True)
2125            self.Layout()
2126 
2127            return 
2128        if (len(self.disp_cb_dict)>0) :
2129            for p in self.disp_cb_dict:
2130                # The parameter was un-selected. Go back to Gaussian model (with 0 pts)                   
2131                disp_model = GaussianDispersion()
2132               
2133                self._disp_obj_dict[p] = disp_model
2134                # Set the new model as the dispersion object for the selected parameter
2135                try:
2136                   self.model.set_dispersion(p, disp_model)
2137                except:
2138
2139                    pass
2140
2141        ## save state into
2142        self.save_current_state()
2143        self.Layout() 
2144        self.Refresh()
2145                 
2146    def _on_select_Disp(self,event):
2147        """
2148        allow selecting different dispersion
2149        self.disp_list should change type later .now only gaussian
2150        """
2151        self._set_sizer_dispersion()
2152
2153        ## Redraw the model
2154        self._draw_model() 
2155        #self._undo.Enable(True)
2156        event = PageInfoEvent(page = self)
2157        wx.PostEvent(self.parent, event)
2158       
2159        self.sizer4_4.Layout()
2160        self.sizer4.Layout()
2161        self.SetupScrolling()
2162   
2163    def _on_disp_func(self, event=None): 
2164        """
2165        Select a distribution function for the polydispersion
2166       
2167        :Param event: ComboBox event
2168        """
2169        # get ready for new event
2170        if event != None:
2171            event.Skip()
2172        # Get event object
2173        disp_box =  event.GetEventObject() 
2174
2175        # Try to select a Distr. function
2176        try:   
2177            disp_box.SetBackgroundColour("white")
2178            selection = disp_box.GetCurrentSelection()
2179            param_name = disp_box.Name.split('.')[0]
2180            disp_name = disp_box.GetValue()
2181            dispersity= disp_box.GetClientData(selection)
2182   
2183            #disp_model =  GaussianDispersion()
2184            disp_model = dispersity()
2185            # Get param names to reset the values of the param
2186            name1 = param_name + ".width"
2187            name2 = param_name + ".npts"
2188            name3 = param_name + ".nsigmas"
2189            # Check Disp. function whether or not it is 'array'
2190            if disp_name.lower() == "array":
2191                value2= ""
2192                value3= ""
2193                value1 = self._set_array_disp(name=name1, disp=disp_model)
2194            else:
2195                self._del_array_values(name1)
2196                #self._reset_array_disp(param_name)
2197                self._disp_obj_dict[name1] = disp_model
2198                self.model.set_dispersion(param_name, disp_model)
2199                self.state._disp_obj_dict[name1]= disp_model
2200 
2201                value1= str(format_number(self.model.getParam(name1), True))
2202                value2= str(format_number(self.model.getParam(name2)))
2203                value3= str(format_number(self.model.getParam(name3)))
2204            # Reset fittable polydispersin parameter value
2205            for item in self.fittable_param:
2206                 if item[1] == name1:
2207                    item[2].SetValue(value1) 
2208                    item[5].SetValue("")
2209                    item[6].SetValue("")
2210                    # Disable for array
2211                    if disp_name.lower() == "array":
2212                        item[0].SetValue(False)
2213                        item[0].Disable()
2214                        item[2].Disable()
2215                        item[5].Disable()
2216                        item[6].Disable()
2217                    else:
2218                        item[0].Enable()
2219                        item[2].Enable()
2220                        item[5].Enable()
2221                        item[6].Enable()                       
2222                    break
2223            # Reset fixed polydispersion params
2224            for item in self.fixed_param:
2225                if item[1] == name2:
2226                    item[2].SetValue(value2) 
2227                    # Disable Npts for array
2228                    if disp_name.lower() == "array":
2229                        item[2].Disable()
2230                    else:
2231                        item[2].Enable()
2232                if item[1] == name3:
2233                    item[2].SetValue(value3) 
2234                    # Disable Nsigs for array
2235                    if disp_name.lower() == "array":
2236                        item[2].Disable()
2237                    else:
2238                        item[2].Enable()
2239               
2240            # Make sure the check box updated when all checked
2241            if self.cb1.GetValue():
2242                self.select_all_param(None)
2243
2244            # update params
2245            self._update_paramv_on_fit() 
2246            # draw
2247            self._draw_model()
2248            self.Refresh()
2249        except:
2250            # Error msg
2251            msg = "Error occurred:"
2252            msg += " Could not select the distribution function..."
2253            msg += " Please select another distribution function."
2254            disp_box.SetBackgroundColour("pink")
2255            # Focus on Fit button so that users can see the pinky box
2256            self.btFit.SetFocus()
2257            wx.PostEvent(self.parent.parent, 
2258                         StatusEvent(status=msg, info="error"))
2259       
2260       
2261    def _set_array_disp(self, name=None, disp=None):
2262        """
2263        Set array dispersion
2264       
2265        :param name: name of the parameter for the dispersion to be set
2266        :param disp: the polydisperion object
2267        """
2268        # The user wants this parameter to be averaged.
2269        # Pop up the file selection dialog.
2270        path = self._selectDlg()
2271        # Array data
2272        values = []
2273        weights = []
2274        # If nothing was selected, just return
2275        if path is None:
2276            self.disp_cb_dict[name].SetValue(False)
2277            #self.noDisper_rbox.SetValue(True)
2278            return
2279        self._default_save_location = os.path.dirname(path)
2280
2281        basename  = os.path.basename(path)
2282        values,weights = self.read_file(path)
2283       
2284        # If any of the two arrays is empty, notify the user that we won't
2285        # proceed
2286        if len(self.param_toFit)>0:
2287            if name in self.param_toFit:
2288                self.param_toFit.remove(name)
2289
2290        # Tell the user that we are about to apply the distribution
2291        msg = "Applying loaded %s distribution: %s" % (name, path)
2292        wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) 
2293
2294        disp.set_weights(values, weights)
2295        self._disp_obj_dict[name] = disp
2296        self.model.set_dispersion(name.split('.')[0], disp)
2297        self.state._disp_obj_dict[name]= disp
2298        self.values[name] = values
2299        self.weights[name] = weights
2300        # Store the object to make it persist outside the
2301        # scope of this method
2302        #TODO: refactor model to clean this up?
2303        self.state.values = {}
2304        self.state.weights = {}
2305        self.state.values = copy.deepcopy(self.values)
2306        self.state.weights = copy.deepcopy(self.weights)
2307
2308        # Set the new model as the dispersion object for the
2309        #selected parameter
2310        #self.model.set_dispersion(p, disp_model)
2311        # Store a reference to the weights in the model object
2312        #so that
2313        # it's not lost when we use the model within another thread.
2314        #TODO: total hack - fix this
2315        self.state.model= self.model.clone()
2316        self.model._persistency_dict[name.split('.')[0]] = \
2317                                        [values, weights]
2318        self.state.model._persistency_dict[name.split('.')[0]] = \
2319                                        [values,weights]
2320        return basename
2321   
2322    def _del_array_values(self, name=None): 
2323        """
2324        Reset array dispersion
2325       
2326        :param name: name of the parameter for the dispersion to be set
2327        """
2328        # Try to delete values and weight of the names array dic if exists
2329        try:
2330            del self.values[name]
2331            del self.weights[name]
2332            # delete all other dic
2333            del self.state.values[name]
2334            del self.state.weights[name]
2335            del self.model._persistency_dict[name.split('.')[0]] 
2336            del self.state.model._persistency_dict[name.split('.')[0]]
2337        except:
2338            pass
2339                                           
2340    def _lay_out(self):
2341        """
2342        returns self.Layout
2343       
2344        :Note: Mac seems to like this better when self.
2345            Layout is called after fitting.
2346        """
2347        self._sleep4sec()
2348        self.Layout()
2349        return 
2350   
2351    def _sleep4sec(self):
2352        """
2353            sleep for 1 sec only applied on Mac
2354            Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model
2355        """
2356        if ON_MAC == True:
2357            time.sleep(1)
2358           
2359    def _find_polyfunc_selection(self, disp_func = None):
2360        """
2361        FInd Comboox selection from disp_func
2362       
2363        :param disp_function: dispersion distr. function
2364        """
2365        # List of the poly_model name in the combobox
2366        list = ["RectangleDispersion", "ArrayDispersion", 
2367                    "LogNormalDispersion", "GaussianDispersion", 
2368                    "SchulzDispersion"]
2369
2370        # Find the selection
2371        try:
2372            selection = list.index(disp_func.__class__.__name__)
2373            return selection
2374        except:
2375             return 3
2376                           
2377    def on_reset_clicked(self,event):
2378        """
2379        On 'Reset' button  for Q range clicked
2380        """
2381        flag = True
2382        #if self.check_invalid_panel():
2383        #    return
2384        ##For 3 different cases: Data2D, Data1D, and theory
2385        if self.data.__class__.__name__ == "Data2D":
2386            data_min= 0
2387            x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) 
2388            y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax))
2389            self.qmin_x = data_min
2390            self.qmax_x = math.sqrt(x*x + y*y)
2391            # check smearing
2392            if not self.disable_smearer.GetValue():
2393                temp_smearer= self.current_smearer
2394                ## set smearing value whether or not the data contain the smearing info
2395                if self.pinhole_smearer.GetValue():
2396                    flag = self.update_pinhole_smear()
2397                else:
2398                    flag = True
2399        elif self.data.__class__.__name__ != "Data2D":
2400            self.qmin_x = min(self.data.x)
2401            self.qmax_x = max(self.data.x)
2402            # check smearing
2403            if not self.disable_smearer.GetValue():
2404                temp_smearer= self.current_smearer
2405                ## set smearing value whether or not the data contain the smearing info
2406                if self.slit_smearer.GetValue():
2407                    flag = self.update_slit_smear()
2408                elif self.pinhole_smearer.GetValue():
2409                    flag = self.update_pinhole_smear()
2410                else:
2411                    flag = True
2412        else:
2413            self.qmin_x = _QMIN_DEFAULT
2414            self.qmax_x = _QMAX_DEFAULT
2415            self.num_points = _NPTS_DEFAULT           
2416            self.state.npts = self.num_points
2417           
2418        if flag == False:
2419            msg= "Cannot Plot :Must enter a number!!!  "
2420            wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
2421        else:
2422            # set relative text ctrs.
2423            self.qmin.SetValue(str(self.qmin_x))
2424            self.qmax.SetValue(str(self.qmax_x))
2425            self.set_npts2fit()
2426            # At this point, some button and variables satatus (disabled?) should be checked
2427            # such as color that should be reset to white in case that it was pink.
2428            self._onparamEnter_helper()
2429
2430        self.save_current_state()
2431        self.state.qmin = self.qmin_x
2432        self.state.qmax = self.qmax_x
2433       
2434        #reset the q range values
2435        self._reset_plotting_range(self.state)
2436        #self.compute_chisqr(smearer=self.current_smearer)
2437        #Re draw plot
2438        self._draw_model()
2439       
2440    def get_images(self):
2441        """
2442        Get the images of the plots corresponding this panel for report
2443       
2444        : return graphs: list of figures
2445        : TODO: Move to guiframe
2446        """
2447        # set list of graphs
2448        graphs = []
2449        canvases = []
2450        # call gui_manager
2451        gui_manager = self.parent.parent
2452        # loops through the panels [dic]
2453        for item1, item2 in gui_manager.panels.iteritems():
2454             data_title = self.data.group_id
2455             data_name = self.data.name
2456            # try to get all plots belonging to this control panel
2457             try:
2458                 # check titles (main plot)
2459                 # and data_names (model plot[2D], and residuals)
2460                 if item2.group_id == data_title or \
2461                                item2.window_name.count(data_name):
2462                     #panel = gui_manager._mgr.GetPane(item2.window_name)
2463                     # append to the list
2464                     graphs.append(item2.figure) 
2465                     canvases.append(item2.canvas)     
2466             except:
2467                 # Not for control panels
2468                 pass
2469        # return the list of graphs
2470        return graphs, canvases
2471
2472    def on_model_help_clicked(self,event):
2473        """
2474        on 'More details' button
2475        """
2476        from help_panel import  HelpWindow
2477        import sans.models as models 
2478       
2479        # Get models help model_function path
2480        path = models.get_data_path(media='media')
2481        model_path = os.path.join(path,"model_functions.html")
2482        if self.model == None:
2483            name = 'FuncHelp'
2484        else:
2485            name = self.model.origin_name
2486
2487        frame = HelpWindow(None, -1,  pageToOpen=model_path)   
2488        frame.Show(True)
2489        if frame.rhelp.HasAnchor(name):
2490            frame.rhelp.ScrollToAnchor(name)
2491        else:
2492           msg= "Model does not contains an available description "
2493           msg +="Please try searching in the Help window"
2494           wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))     
2495   
2496    def on_pd_help_clicked(self, event):
2497        """
2498        Button event for PD help
2499        """
2500        from help_panel import  HelpWindow
2501        import sans.models as models 
2502       
2503        # Get models help model_function path
2504        path = models.get_data_path(media='media')
2505        pd_path = os.path.join(path,"pd_help.html")
2506
2507        frame = HelpWindow(None, -1,  pageToOpen=pd_path)   
2508        frame.Show(True)
2509       
2510    def on_left_down(self, event):
2511        """
2512        Get key stroke event
2513        """
2514        # Figuring out key combo: Cmd for copy, Alt for paste
2515        if event.CmdDown() and event.ShiftDown():
2516            flag = self.get_paste()
2517        elif event.CmdDown():
2518            flag = self.get_copy()
2519        else:
2520            event.Skip()
2521            return
2522        # make event free
2523        event.Skip()
2524        # messages depending on the flag
2525        if flag == None:
2526            msg = " Parameter values are copied to the clipboard..."
2527            infor = 'warning'
2528        elif flag:
2529            msg = " Parameter values are pasted from the clipboad..."
2530            infor = "warning"
2531        else:
2532            msg = "Error was occured during pasting the parameter values "
2533            msg += "from the clipboard..."
2534            infor = "error"
2535        # inform msg to wx
2536        wx.PostEvent( self.parent.parent, 
2537                      StatusEvent(status= msg, info=infor))
2538       
2539           
2540    def get_copy(self): 
2541        """
2542        Get the string copies of the param names and values in the tap
2543        """ 
2544        content = 'sansview_parameter_values:'
2545        # Do it if params exist       
2546        if  self.parameters !=[]:
2547           
2548            # go through the parameters
2549            string = self._get_copy_helper(self.parameters, 
2550                                           self.orientation_params)
2551            content += string
2552           
2553            # go through the fittables
2554            string = self._get_copy_helper(self.fittable_param, 
2555                                           self.orientation_params_disp)
2556            content += string
2557
2558            # go through the fixed params
2559            string = self._get_copy_helper(self.fixed_param, 
2560                                           self.orientation_params_disp)
2561            content += string
2562               
2563            # go through the str params
2564            string = self._get_copy_helper(self.str_parameters, 
2565                                           self.orientation_params)
2566            content += string
2567
2568        if wx.TheClipboard.Open():
2569            wx.TheClipboard.SetData(wx.TextDataObject(str(content)))
2570            data = wx.TextDataObject()
2571            success = wx.TheClipboard.GetData(data)
2572            text = data.GetText()
2573            wx.TheClipboard.Close()
2574           
2575        return None
2576   
2577    def _get_copy_helper(self, param, orient_param):
2578        """
2579        Helping get value and name of the params
2580       
2581        : param param:  parameters
2582        : param orient_param = oritational params
2583        : return content: strings [list] [name,value:....]
2584        """
2585        content = ''
2586        # go through the str params
2587        for item in param: 
2588            # 2D
2589            if self.data.__class__.__name__== "Data2D":
2590                name = item[1]
2591                value = item[2].GetValue()
2592            # 1D
2593            else:
2594                ## for 1D all parameters except orientation
2595                if not item[1] in orient_param:
2596                    name = item[1]
2597                    value = item[2].GetValue()
2598            # add to the content
2599            content += name + ',' + value + ':'
2600           
2601        return content
2602   
2603    def get_paste(self): 
2604        """
2605        Get the string copies of the param names and values in the tap
2606        """ 
2607        context = {}
2608        text = ""
2609       
2610        # Get text from the clip board       
2611        if wx.TheClipboard.Open():
2612            if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
2613                data = wx.TextDataObject()
2614                # get wx dataobject
2615                success = wx.TheClipboard.GetData(data)
2616                # get text
2617                text = data.GetText()
2618            # close clipboard
2619            wx.TheClipboard.Close()
2620           
2621        # put the text into dictionary   
2622        lines = text.split(':')
2623        if lines[0] != 'sansview_parameter_values':
2624            return False
2625        for line in lines[1:-1]:
2626            if len(line) != 0:
2627                item =line.split(',')
2628                name = item[0]
2629                value = item[1]
2630                # Transfer the text to content[dictionary]
2631                context[name] = value
2632       
2633        # Do it if params exist       
2634        if  self.parameters != []:
2635            # go through the parameters 
2636            self._get_paste_helper(self.parameters, 
2637                                   self.orientation_params, context)
2638
2639            # go through the fittables
2640            self._get_paste_helper(self.fittable_param, 
2641                                   self.orientation_params_disp, context)
2642
2643            # go through the fixed params
2644            self._get_paste_helper(self.fixed_param, 
2645                                   self.orientation_params_disp, context)
2646           
2647            # go through the str params
2648            self._get_paste_helper(self.str_parameters, 
2649                                   self.orientation_params, context)
2650           
2651        return True
2652   
2653    def _get_paste_helper(self, param, orient_param, content):
2654        """
2655        Helping set values of the params
2656       
2657        : param param:  parameters
2658        : param orient_param: oritational params
2659        : param content: dictionary [ name, value: name1.value1,...]
2660        """
2661        # go through the str params
2662        for item in param: 
2663            # 2D
2664            if self.data.__class__.__name__== "Data2D":
2665                name = item[1]
2666                if name in content.keys():
2667                    item[2].SetValue(content[name])
2668            # 1D
2669            else:
2670                ## for 1D all parameters except orientation
2671                if not item[1] in orient_param:
2672                    name = item[1]
2673                    if name in content.keys():
2674                        # Avoid changing combox content which needs special care
2675                        if item[2].__class__.__name__ != "ComboBox":
2676                            item[2].SetValue(content[name])
2677                           
2678               
Note: See TracBrowser for help on using the repository browser.