import sys import wx import wx.lib import numpy import copy from sans.guicomm.events import StatusEvent (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() _BOX_WIDTH = 80 def format_number(value, high=False): """ Return a float in a standardized, human-readable formatted string """ try: value = float(value) except: print "returning 0" return "0" if high: return "%-6.4g" % value else: return "%-5.3g" % value class FitPage(wx.Panel): """ FitPanel class contains fields allowing to display results when fitting a model and one data @note: For Fit to be performed the user should check at least one parameter on fit Panel window. """ ## Internal name for the AUI manager window_name = "Fit page" ## Title to appear on top of the window window_caption = "Fit Page" def __init__(self, parent, *args, **kwargs): wx.Panel.__init__(self, parent, *args, **kwargs) """ Initialization of the Panel """ self.manager = None self.parent = parent self.event_owner=None #panel interface self.vbox = wx.BoxSizer(wx.VERTICAL) self.sizer4 = wx.GridBagSizer(5,5) self.sizer3 = wx.GridBagSizer(5,5) self.sizer2 = wx.GridBagSizer(5,5) self.sizer1 = wx.GridBagSizer(5,5) self.DataSource = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) self.DataSource.SetToolTipString("name of data to fit") self.modelbox = wx.ComboBox(self, -1) id = wx.NewId() self.btFit =wx.Button(self,id,'Fit') self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) self.btFit.SetToolTipString("Perform fit.") self.vbox.Add(self.sizer3) self.vbox.Add(self.sizer2) self.vbox.Add(self.sizer4) self.vbox.Add(self.sizer1) ix = 0 iy = 1 self.sizer3.Add(wx.StaticText(self, -1, 'Data Source'),(iy,ix),\ (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) ix += 1 self.sizer3.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) ix += 1 self.sizer3.Add((20,20),(iy,ix),(1,1),wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE,0) ix = 0 iy += 1 self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) ix += 1 self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) ix = 1 iy = 1 self.text4_1 = wx.StaticText(self, -1, 'Min') self.sizer4.Add(self.text4_1,(iy, ix),(1,1),\ wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) self.text4_1.Hide() ix += 2 self.text4_2 = wx.StaticText(self, -1, 'Max') self.sizer4.Add(self.text4_2,(iy, ix),(1,1),\ wx.EXPAND|wx.ADJUST_MINSIZE, 0) self.text4_2.Hide() ix = 0 iy += 1 #set maximum range for x in linear scale self.text4_3 = wx.StaticText(self, -1, 'Maximum Data\n Range (Linear)', style=wx.ALIGN_LEFT) self.sizer4.Add(self.text4_3,(iy,ix),(1,1),\ wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) self.text4_3.Hide() ix += 1 self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) self.xmin.SetToolTipString("Minimun value of x in linear scale.") self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) self.xmin.Hide() ix += 2 self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) self.xmax.SetToolTipString("Maximum value of x in linear scale.") self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) self.xmax.Hide() #Set chisqr result into TextCtrl ix = 0 iy = 1 self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) self.sizer1.Add(self.text1_1,(iy,ix),(1,1),\ wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) ix += 1 self.tcChi = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") self.sizer1.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) ix +=2 self.sizer1.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) # contains link between model ,all its parameters, and panel organization self.parameters=[] #contains link between a model and selected parameters to fit self.param_toFit=[] # model on which the fit would be performed self.model=None # preview selected model name self.prevmodel_name=None # flag to check if the user has selected a new model in the combox box self.model_hasChanged=False #flag to check if the user has selected a new data self.data_hasChanged=False #dictionary of model name and model class self.model_list_box={} # Data1D to make a deep comparison between 2 Data1D for checking data #change from sans.guitools.plottables import Data1D self.prevData = Data1D(x=[], y=[],dx=None, dy=None) self.prevData.name= None self.data=None self.vbox.Layout() self.vbox.Fit(self) self.SetSizer(self.vbox) self.Centre() def set_owner(self,owner): """ set owner of fitpage @param owner: the class responsible of plotting """ self.event_owner=owner def set_manager(self, manager): """ set panel manager @param manager: instance of plugin fitting """ self.manager = manager def _DataNameEnter(self): """ reset the panel when a new data is selected """ if len(self.parameters )>0: for item in self.parameters: item[0].SetValue(False) wx.EVT_CHECKBOX(self, item[0].GetId(), self.select_param) item[2].Hide() item[3].Clear() item[3].Hide() self.xmax.Disable() self.xmin.Disable() self.text1_1.Hide() self.tcChi.Clear() self.vbox.Layout() self.GrandParent.GetSizer().Layout() def copy_data(self,dataset): """ copy a select data in the panel to compared with the previous selected data @param dataset: the data selected @note : everything a data is selected, the panel check is it is different from the previous selected data """ try: if dataset !=None: self.prevData.x= copy.deepcopy(dataset.x) self.prevData.y= copy.deepcopy(dataset.y) self.prevData.name=dataset.name if dataset.dy==None: self.prevData.dy= copy.deepcopy(dataset.dy) except: raise ValueError, " copy_data: cannot copy data" def onFit(self,event): """ signal for fitting""" flag=self.checkFitRange() self.set_manager(self.manager) qmin=float(self.xmin.GetValue()) qmax =float( self.xmax.GetValue()) if len(self.param_toFit) >0 and flag==True: #self.manager._on_single_fit(model=self.model,qmin=qmin,qmax=qmax) self.manager._on_single_fit(qmin=qmin,qmax=qmax) else: wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ "Select at least on parameter to fit ")) def populate_box(self, dict): """ Populate each combox box of each page @param page: the page to populate """ id=0 self.model_list_box=dict list_name=[] for item in self.model_list_box.itervalues(): if hasattr(item, "name"): name = item.name else: name = item.__name__ list_name.append(name) list_name.sort() for name in list_name: self.modelbox.Insert(name,int(id)) id+=1 wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) return 0 def _on_select_model(self,event): """ react when a model is selected from page's combo box post an event to its owner to draw an appropriate theory """ for item in self.model_list_box.itervalues(): model=item() if hasattr(model, "name"): name = model.name else: name = model.__class__.__name__ try: if name ==event.GetString(): evt = ModelEventbox(model=model,name=name) wx.PostEvent(self.event_owner, evt) except: raise #ValueError,"model.name is not equal to model class name" def _onTextEnter(self,event): """ set a flag to determine if the fitting range entered by the user is valid """ try: flag=self.checkFitRange() if flag==True and self.model!=None: self.manager.redraw_model(float(self.xmin.GetValue())\ ,float(self.xmax.GetValue())) except: wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ "Drawing Error:wrong value entered %s"% sys.exc_value)) def checkFitRange(self): """ Check the validity of fitting range @note: xmin should always be less than xmax or else each control box background is colored in pink. """ flag = True valueMin = self.xmin.GetValue() valueMax = self.xmax.GetValue() # Check for possible values entered try: if (float(valueMax)> float(valueMin)): self.xmax.SetBackgroundColour(wx.WHITE) self.xmin.SetBackgroundColour(wx.WHITE) else: flag = False self.xmin.SetBackgroundColour("pink") self.xmax.SetBackgroundColour("pink") except: flag = False self.xmin.SetBackgroundColour("pink") self.xmax.SetBackgroundColour("pink") self.xmin.Refresh() self.xmax.Refresh() return flag def check_data_change(self,dataset): """ check if data selected has changed and set flag self.data_hasChanged has true is changed or false is unchanged @param dataset: data selected """ self.data_hasChanged=False if self.prevData.__class__ !=dataset.__class__: self.data_hasChanged=True elif self.prevData.name != dataset.name: self.data_hasChanged=True elif len(self.prevData.x) != len(self.prevData.y): self.data_hasChanged=True else: for i in range(len(dataset.x)): if self.prevData.x[i] !=dataset.x[i]: self.data_hasChanged=True break if self.prevData.y[i] !=dataset.y[i]: self.data_hasChanged=True break if dataset.dy !=None: if self.prevData.dy[i] !=dataset.dy[i]: self.data_hasChanged=True break def set_data_name(self,dataset): """ set data's name.if data has changed reset the panel and xmin and xmax @param name: data 's name @attention: still haven't find a better way to display Q name and unit for xmin and xmax range sizer """ self.check_data_change(dataset) if self.data_hasChanged==True: self._DataNameEnter() self.data=dataset self.copy_data(dataset) #Displaying Data information self.DataSource.SetValue(str(dataset.name)) self._xaxis,self._xunit=dataset.get_xaxis() self.text4_3.SetLabel(self._xaxis+"["+self._xunit+"]") self.text4_1.Show() self.text4_2.Show() self.text4_3.Show() self.xmin.SetValue(format_number(min(dataset.x))) self.xmin.Show() self.xmax.SetValue(format_number(max(dataset.x))) self.xmax.Show() if ((len(self.param_toFit ) >0) and self.DataSource.GetValue()and \ self.modelbox.GetValue() and (self.model_hasChanged ==False)): self.xmin.Enable() self.xmax.Enable() else: self.xmin.Disable() self.xmax.Disable() self.vbox.Layout() self.GrandParent.GetSizer().Layout() def set_model_name(self,name): """ set model name. set also self.model_hasChanged to true is the model type has changed or false if it didn't @param name: model 's name """ self.model_hasChanged=False if (name != self.prevmodel_name): self.model_hasChanged=True self.tcChi.Clear() #self.modelbox.SetValue(str(name)) self.prevmodel_name=self.modelbox.GetValue() def get_model_box(self): """ return reference to combox box self.model""" return self.modelbox def get_param_list(self): """ @return self.param_toFit: list containing references to TextCtrl checked.Theses TextCtrl will allow reference to parameters to fit. @raise: if return an empty list of parameter fit will nnote work properly so raise ValueError,"missing parameter to fit" """ if self.param_toFit !=[]: return self.param_toFit else: raise ValueError,"missing parameter to fit" def set_panel(self,model): """ Build the panel from the model content @param model: the model selected in combo box for fitting purpose """ self.sizer2.Clear(True) self.parameters = [] self.param_toFit=[] self.model = model keys = self.model.getParamList() keys.sort() iy = 1 ix = 0 self.cb1 = wx.CheckBox(self, -1,'Parameters', (10, 10)) wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) ix +=1 self.text2_2 = wx.StaticText(self, -1, 'Values') self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ wx.EXPAND|wx.ADJUST_MINSIZE, 0) ix +=2 self.text2_3 = wx.StaticText(self, -1, 'Errors') self.sizer2.Add(self.text2_3,(iy, ix),(1,1),\ wx.EXPAND|wx.ADJUST_MINSIZE, 0) self.text2_3.Hide() ix +=1 self.text2_4 = wx.StaticText(self, -1, 'Units') self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ wx.EXPAND|wx.ADJUST_MINSIZE, 0) self.text2_4.Hide() for item in keys: iy += 1 ix = 0 cb = wx.CheckBox(self, -1, item, (10, 10)) cb.SetValue(False) self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) ix += 1 value= self.model.getParam(item) ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) ctl1.SetValue(str (format_number(value))) ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) ix += 1 text2=wx.StaticText(self, -1, '+/-') self.sizer2.Add(text2,(iy, ix),(1,1),\ wx.EXPAND|wx.ADJUST_MINSIZE, 0) text2.Hide() ix += 1 ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) self.sizer2.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) ctl2.Hide() ix +=1 # Units try: units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) except: units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) #save data self.parameters.append([cb,ctl1,text2,ctl2]) #Display units text on panel for item in keys: if self.model.details[item][0]!='': self.text2_4.Show() break else: self.text2_4.Hide() #Disable or enable fit button if (self.modelbox.GetValue() and self.DataSource.GetValue()): if not (len(self.param_toFit ) >0): self.xmin.Disable() self.xmax.Disable() else: self.xmin.Enable() self.xmax.Enable() else: self.xmin.Disable() self.xmax.Disable() self.vbox.Layout() self.GrandParent.GetSizer().Layout() def _onparamEnter(self,event): """ when enter value on panel redraw model according to changed """ self.set_model_parameter() def set_model_parameter(self): if len(self.parameters) !=0 and self.model !=None: for item in self.parameters: try: name=str(item[0].GetLabelText()) value= float(item[1].GetValue()) self.model.setParam(name,value) self.manager.redraw_model(float(self.xmin.GetValue())\ ,float(self.xmax.GetValue())) except: wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ "Drawing Error:wrong value entered : %s"% sys.exc_value)) def select_all_param(self,event): """ set to true or false all checkBox given the main checkbox value cb1 """ self.param_toFit=[] if self.parameters !=[]: if self.cb1.GetValue()==True: for item in self.parameters: item[0].SetValue(True) list= [item[0],item[1],item[2],item[3]] self.param_toFit.append(list ) if (self.modelbox.GetValue() and self.DataSource.GetValue()): if not (len(self.param_toFit ) >0): self.xmin.Disable() self.xmax.Disable() else: self.xmin.Enable() self.xmax.Enable() else: for item in self.parameters: item[0].SetValue(False) self.param_toFit=[] self.xmin.Disable() self.xmax.Disable() def select_param(self,event): """ Select TextCtrl checked for fitting purpose and stores them in self.param_toFit=[] list """ self.param_toFit=[] for item in self.parameters: if item[0].GetValue()==True: list= [item[0],item[1],item[2],item[3]] self.param_toFit.append(list ) else: if item in self.param_toFit: self.param_toFit.remove(item) if len(self.parameters)==len(self.param_toFit): self.cb1.SetValue(True) else: self.cb1.SetValue(False) if (self.modelbox.GetValue() and self.DataSource.GetValue()): if not (len(self.param_toFit ) >0): self.xmin.Disable() self.xmax.Disable() else: self.xmin.Enable() self.xmax.Enable() else: self.xmin.Disable() self.xmax.Disable() def onsetValues(self,chisqr, out,cov): """ Build the panel from the fit result @param chisqr:Value of the goodness of fit metric @param out:list of parameter with the best value found during fitting @param cov:Covariance matrix """ #print "fitting : onsetvalues out",out self.tcChi.Clear() self.tcChi.SetValue(format_number(chisqr)) params = {} is_modified = False has_error = False if out.__class__==numpy.float64: self.param_toFit[0][1].SetValue(format_number(out)) self.param_toFit[0][1].Refresh() if cov !=None : self.text2_3.Show() self.param_toFit[0][2].Show() self.param_toFit[0][3].Clear() self.param_toFit[0][3].SetValue(format_number(cov[0])) self.param_toFit[0][3].Show() #out is a list : set parameters and errors in TextCtrl else: i=0 #print "fitpage: list param model",list #for item in self.param_toFit: # print "fitpage: list display",item[0].GetLabelText() for item in self.param_toFit: if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): item[1].SetValue(format_number(out[i])) item[1].Refresh() if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): self.text2_3.Show() item[2].Show() item[3].Clear() item[3].SetValue(format_number(cov[i])) item[3].Show() i+=1 self.vbox.Layout() self.GrandParent.GetSizer().Layout()