Пример #1
0
    def Fit(self,showLog=False,showGuess=False):
        if self.silent:
            print "Fitting with "+self.funcName 


        if self.silent:
            resultFig = plt.figure(self.plotNum)

            if not self.twin:
                resultAx = resultFig.add_subplot(111)
            else:
                resultAx = self.twinAx.twinx()

        xRange = linspace(self.xData.min(),self.xData.max(),1000)
        yGuess = self.fitFunc(xRange,*self.iVals)
          
        if self.silent:
            if showLog:

                resultAx.loglog(self.xData,self.yData,color=self.dataColor,marker=self.dataMarker,linestyle='.')
                if showGuess:

                    resultAx.loglog(xRange,yGuess,color=self.guessColor,linestyle=self.fitLineStyle,linewidth=self.fitLineThick)
            if showGuess:

                resultAx.plot(xRange,yGuess,color=self.guessColor,linestyle=self.fitLineStyle)


            self.legData = resultAx.errorbar(self.xData,self.yData,linestyle='.',marker=self.dataMarker, color=self.dataColor,yerr=self.yErr,elinewidth=self.errorbarThick,capsize=self.capsize)
        
                

        fit = mpCurveFit(self.fitFunc, self.xData, self.yData, p0 = self.iVals, sigma = self.yErr, fixed = self.fixed,limits=self.limits,quiet=1)
        params, errors = [fit.params, fit.errors]
            
        if self.silent:
            print "\nFit results: "
        try:
            for x,y,z in zip(self.params, params, errors):
                if self.silent:
                    print x+": "+str(y)+" +/- "+str(z)
            if self.rDisp:
                self._ResultsDisplay(resultAx,params,errors)
        except TypeError:
            print "\n\n\n-----------> FIT FAILED!!!!!\n\n\n"
            self.redChi2 = 1E3
            self.chi2 = [1E4]
            if self.silent:
                self.ax=resultAx
            return
        
        
        xRange = linspace(self.xData.min(),self.xData.max(),100)
        yResult = self.fitFunc(xRange,*params)
        self.result =  zip(params,errors)
        self.result.append([fit.chi2,fit.dof])
        self.result=array(self.result)
        self.covar = fit.covar
        self.chi2 = [fit.chi2,fit.dof]
        self.redChi2 = fit.chi2/fit.dof
         

            
        if self.silent:
            if showLog:
                resultAx.loglog(xRange,yResult,color=self.fitColor,linestyle=self.fitLineStyle,linewidth=self.fitLineThick)

            else:
                resultAx.plot(xRange,yResult,color=self.fitColor,linestyle=self.fitLineStyle,linewidth=self.fitLineThick)
            #resultAx.errorbar(self.xData,self.yData,fmt=self.dataMarker, color=self.dataColor,yerr=self.yErr)
            resultAx.set_xlabel(self.xName,fontsize=self.fontsize, weight = self.fontWeight, family = self.fontFam)
            resultAx.set_ylabel(self.yName,fontsize=self.fontsize, weight = self.fontWeight, family = self.fontFam)
            resultAx.set_title(self.title,fontsize=self.fontsize, weight = self.fontWeight, family = self.fontFam)

            self.ax= resultAx
Пример #2
0
    def FitPulse(self, event=0):

        if self.pms:
            #If there is pms then we need to set the right models
            pulseMod = self.pms.GetPulseMod()
            func = pulseMod.pulseLookup[self.numPulse - 1]

    # func = self.pulseLookup[self.numPulse-1]

        initialValues = pulseMod.GetInitialValues()

        self.tmax = self.tMaxSelector.GetData()

        if self.tmax == []:
            fixPar = pulseMod.GetFixedParams()
            limits = pulseMod.limits
            initialValues = pulseMod.GetInitialValues()

        else:

            print "\n_________________________________\n"
            print "Initial guess(es) for Tmax"
            for x in self.tmax:
                print x
                print "\n_________________________________\n"

            limits = []

            tmp1 = []
            tmp3 = []
            for x in self.tmax:
                tmp2 = initialValues
                if self.useSelector:
                    tmp2[0] = x
                tmp1.extend(tmp2)
                tmp3.extend(pulseMod.GetFixedParams())
                limits.extend(pulseMod.limits)

            intialValues = tmp1
            fixPar = tmp3

        # print limits
        # print initialValues
        # print fixPar

        #### Testing!!!!!
        #dT = array(map(lambda x: x[1] - x[0] ,self.tBins))
        #wErrors = self.errors*dT
        #wData = self.data*dT
        #fit = mpPulseFitEngine(func, array(map(mean,self.tBins))+self.timeOffset, self.data.tolist(), sigma=wErrors,p0=initialValues,fixed=fixPar,maxiter=400, limits=limits,tBins = self.tBins)

        # place back if this does not work
        fit = mpCurveFit(func,
                         array(map(mean, self.tBins)) + self.timeOffset,
                         self.data.tolist(),
                         sigma=self.errors,
                         p0=initialValues,
                         fixed=fixPar,
                         maxiter=400,
                         limits=limits)

        self.fitResults = fit.params
        self.fitCov = fit.errors
        self.fitResults[3] -= self.timeOffset
        self.GoodnessOfFit()
        self.DisplayFitResults()
        self.DisplayFitPlot()
Пример #3
0
    def FitPulse(self,event=0):

        if self.pms:
            #If there is pms then we need to set the right models
            pulseMod = self.pms.GetPulseMod()
            func = pulseMod.pulseLookup[self.numPulse-1]


       # func = self.pulseLookup[self.numPulse-1]
       
        initialValues=pulseMod.GetInitialValues()
       

        self.tmax=self.tMaxSelector.GetData()
       
        if self.tmax == []:
            fixPar = pulseMod.GetFixedParams()
            limits = pulseMod.limits
            initialValues = pulseMod.GetInitialValues()

        else:

            print "\n_________________________________\n"
            print "Initial guess(es) for Tmax"
            for x in self.tmax:
                print x
                print "\n_________________________________\n"


            limits = []
       
            tmp1 = []
            tmp3=[]
            for x in self.tmax:
                tmp2 = initialValues
                if self.useSelector:
                    tmp2[0] = x
                tmp1.extend(tmp2)
                tmp3.extend(pulseMod.GetFixedParams())
                limits.extend(pulseMod.limits)
            
            
        
            intialValues = tmp1
            fixPar=tmp3


       
        # print limits
        # print initialValues
        # print fixPar

        #### Testing!!!!!
        #dT = array(map(lambda x: x[1] - x[0] ,self.tBins))
        #wErrors = self.errors*dT
        #wData = self.data*dT
        #fit = mpPulseFitEngine(func, array(map(mean,self.tBins))+self.timeOffset, self.data.tolist(), sigma=wErrors,p0=initialValues,fixed=fixPar,maxiter=400, limits=limits,tBins = self.tBins) 


        # place back if this does not work
        fit = mpCurveFit(func, array(map(mean,self.tBins))+self.timeOffset, self.data.tolist(), sigma=self.errors,p0=initialValues,fixed=fixPar,maxiter=400, limits=limits) 

       
        self.fitResults = fit.params 
        self.fitCov = fit.errors
        self.fitResults[3]-=self.timeOffset
        self.GoodnessOfFit()
        self.DisplayFitResults()
        self.DisplayFitPlot()