示例#1
0
    def test_gn_jacobi(self):
        J = F.gn_jacobi(self.N_cur, F.assign_ub(self.points), self.points, 1.0)

        r = len(self.points)
        n = len(self.N_cur.ctrlptsw)

        (rows, cols) = np.shape(np.array(J))
        self.assertTrue(rows == (2 * r + n))
        self.assertTrue(cols == (r + 3 * n))
        self.assertTrue(isinstance(J, list))
示例#2
0
    def test_gn_f(self):
        alpha = F.gn_f(self.N_cur, F.assign_ub(self.points), self.points,
                       np.nan)
        fun_val = F.gn_f(self.N_cur, F.assign_ub(self.points), self.points,
                         alpha)

        r = len(self.points)
        n = len(self.N_cur.ctrlptsw)
        (rows, ) = np.shape(np.array(fun_val))
        self.assertTrue(rows == (2 * r + n))
        self.assertTrue(isinstance(alpha, float))
        self.assertTrue(isinstance(fun_val, list))
示例#3
0
文件: Base.py 项目: yongwangCPH/peat
    def findBestModel(
        self,
        dataset,
        update=True,
        models=None,
        mode=None,
        checkfunc=None,
        conv=None,
        grad=None,
        alpha=0.01,
        silent=False,
    ):
        """Finds the best fit model using f-testing"""

        thisdata = self.data[dataset]
        # print 'models', models
        if models == None:
            models = copy.deepcopy(self.mode_definition[self.currentmode])

        fitdata, p = Fitting.findBestModel(
            thisdata, models, checkfunc=checkfunc, conv=conv, grad=grad, alpha=alpha, silent=silent
        )

        if update == True:
            if fitdata == None:
                self.setFitData(dataset)
            else:
                self.setFitData(dataset, fitdata)
        return fitdata, p
示例#4
0
文件: Main.py 项目: JitenDhandha/CFit
    def fitDataAutoButtonFunc(self):

        #Checking if the data file was successfully opened
        if(self.fileCheck==0):

            #Calling the relevant function in Fitting.py to handle the rest
            self.fitCheck = fit.guessParameters()
            
            #Checking if the fit was successful and showing the plot.
            if(self.fitCheck==0):
                self.fitStatus.set("Fit attempt successful!")
                fit.plotFitData(self.plotTitleEntry.get(),self.xAxisTitleEntry.get(),self.yAxisTitleEntry.get(),self.viewGrid.get(),self.viewParameters.get(),self.viewResiduals.get())
            elif(self.fitCheck==1):
                self.fitStatus.set("Max iterations performed but couldn't find a fit!")
            elif(self.fitCheck==2):
                self.fitStatus.set("Number of data points is less than number of fitting parameters!")
示例#5
0
文件: Base.py 项目: tubapala/peat
    def estimateExpUncertainty(self,
                               dataset,
                               xuncert=0.01,
                               yuncert=0.01,
                               runs=10,
                               doplots=False,
                               conv=1e-6,
                               grad=1e-6,
                               guess=True):
        """Chresten's method for estimating the error on each parameter due
           to the effect of the exp uncertainty, which is user provided """

        data = self.data[dataset]
        fitdata = self.__datatabs_fits__[dataset]
        #print 'estimating exp uncertainty for %s' %dataset
        fitstats = Fitting.estimateExpUncertainty(data,
                                                  fitdata,
                                                  xuncert=xuncert,
                                                  yuncert=yuncert,
                                                  runs=runs,
                                                  doplots=doplots,
                                                  conv=conv,
                                                  grad=grad,
                                                  guess=guess)

        return fitstats
示例#6
0
文件: Base.py 项目: tubapala/peat
    def findBestModel(self,
                      dataset,
                      update=True,
                      models=None,
                      mode=None,
                      checkfunc=None,
                      conv=None,
                      grad=None,
                      alpha=0.01,
                      silent=False):
        """Finds the best fit model using f-testing"""

        thisdata = self.data[dataset]
        #print 'models', models
        if models == None:
            models = copy.deepcopy(self.mode_definition[self.currentmode])

        fitdata, p = Fitting.findBestModel(thisdata,
                                           models,
                                           checkfunc=checkfunc,
                                           conv=conv,
                                           grad=grad,
                                           alpha=alpha,
                                           silent=silent)

        if update == True:
            if fitdata == None:
                self.setFitData(dataset)
            else:
                self.setFitData(dataset, fitdata)
        return fitdata, p
示例#7
0
文件: Base.py 项目: yongwangCPH/peat
    def fitDataset(
        self,
        dataset,
        model=None,
        update=True,
        noiter=300,
        conv=None,
        grad=None,
        damper=1.0,
        silent=False,
        guess=True,
        startvalues=None,
        changevars=None,
        callback=None,
    ):
        """Calculate the fit for this current dataset, if a model
           is given we use that instead of the current one.
           update=True means that the dataset fit info will be overwritten"""

        # check case of model, as our names are stupidly kept case sensitive
        for m in Fitting.fitterClasses:
            if model == m or model == m.lower():
                model = m
        datatofit = self.data[dataset]
        if model == None:
            currfitdata = self.getFitData(dataset)
            if currfitdata.has_key("model"):
                model = currfitdata["model"]
            else:
                model = self.defaultmodel
        else:
            currfitdata = None

        fitresult, X = Fitting.doFit(
            ekindata=datatofit,
            fitdata=currfitdata,
            model=model,
            noiter=noiter,
            conv=conv,
            grad=grad,
            LM_damper=damper,
            silent=silent,
            guess=guess,
            startvalues=startvalues,
            changevars=changevars,
            callback=callback,
        )

        if fitresult == None:
            print "Fitter returned None.."
            return

        if update == True:
            self.__datatabs_fits__[dataset] = fitresult

        return fitresult, X
示例#8
0
 def fit(self):
     # set everything up, then call the fit function, finally print out the results in the table
     self.setParametersFromTable()
     import Fitting
     xData, yData = self.getCurrentData()
     result = Fitting.FitOne(self.getCurrentFunctionName(), self.params,
                             xData, yData)
     import lmfit
     lmfit.report_fit(self.params)
     self.updateParametersTable()
     self.updatePlot()
示例#9
0
    def test_gauss_newton2D(self):
        ''' Performs gauss newton search of optimum weights
        '''

        # Fit the NURBS weights and control points
        N_fit = F.gauss_newton2D(self.N_cur, self.points)

        # Plot (debug)
        fig = plt.figure(num=1)
        vis_fit = vis.VisCurve2D()
        N_fit.vis = vis_fit
        N_fit.render()
示例#10
0
    def test_update_curve(self):

        # Initialize x
        CP_list = list()
        for CP in self.N_cur.ctrlptsw:
            for each in CP[:-1]:
                CP_list.append(each)
        W = []
        for elem in self.N_cur.ctrlptsw:
            W.append(elem[-1])
        ub = F.assign_ub(self.points)  # Should be same as for the B-spline!

        x = ub + CP_list + W
        r = len(self.points)

        New_c = F.update_curve(self.N_cur, x, r)

        N_res = self.N_cur.evalpts
        N_new_res = New_c.evalpts

        W_fix = copy.deepcopy(W)  # Change the weights
        W_fix[0] = W_fix[0] + 2
        CP_list_fix = CP_list  # Change the controlpoints
        CP_list_fix[0] = CP_list_fix[3] * 3

        x_w = ub + CP_list + np.log(W_fix).tolist()
        x_p = ub + CP_list_fix + np.log(W).tolist()

        New_w = F.update_curve(self.N_cur, x_w, r)
        New_p = F.update_curve(self.N_cur, x_p, r)

        self.assertListAlmostEqual(N_res, N_new_res,
                                   3)  # nothing should be changed
        self.assertFalse(
            self.N_cur.ctrlptsw[0][-1] == New_w.ctrlptsw[0][-1])  # Changed
        self.assertTrue(
            self.N_cur.ctrlptsw[1][-1] == New_w.ctrlptsw[1][-1])  # Unchanged
        self.assertTrue(self.N_cur.ctrlptsw[2][-1] == New_w.ctrlptsw[2][-1])
        self.assertFalse(self.N_cur.ctrlptsw[0][0] == New_p.ctrlptsw[0][0])
        self.assertTrue(self.N_cur.ctrlptsw[0][-1] == New_p.ctrlptsw[0][-1])
示例#11
0
文件: Base.py 项目: tubapala/peat
    def fitDataset(self,
                   dataset,
                   model=None,
                   update=True,
                   noiter=300,
                   conv=None,
                   grad=None,
                   damper=1.0,
                   silent=False,
                   guess=True,
                   startvalues=None,
                   changevars=None,
                   callback=None):
        """Calculate the fit for this current dataset, if a model
           is given we use that instead of the current one.
           update=True means that the dataset fit info will be overwritten"""

        #check case of model, as our names are stupidly kept case sensitive
        for m in Fitting.fitterClasses:
            if model == m or model == m.lower():
                model = m
        datatofit = self.data[dataset]
        if model == None:
            currfitdata = self.getFitData(dataset)
            if currfitdata.has_key('model'):
                model = currfitdata['model']
            else:
                model = self.defaultmodel
        else:
            currfitdata = None

        fitresult, X = Fitting.doFit(ekindata=datatofit,
                                     fitdata=currfitdata,
                                     model=model,
                                     noiter=noiter,
                                     conv=conv,
                                     grad=grad,
                                     LM_damper=damper,
                                     silent=silent,
                                     guess=guess,
                                     startvalues=startvalues,
                                     changevars=changevars,
                                     callback=callback)

        if fitresult == None:
            print 'Fitter returned None..'
            return

        if update == True:
            self.__datatabs_fits__[dataset] = fitresult

        return fitresult, X
示例#12
0
文件: Base.py 项目: tubapala/peat
 def doPlot(self, ek, fitdata, options):
     """Need a bare bones plotter that can plot any given ekin data
        Should then call this one in plotDatasets below and simplify"""
     x, y, a, xerr, yerr = ek.getAll()
     line = ax.scatter(x, y, marker='o')
     #plotting fit should also be seperate?
     f = fitdata
     d = []
     X = Fitting.makeFitter(f, zip(x, y))
     if X == None: return
     #we now use this model to draw the fit line
     fitclr = lineclr
     if plotoption != 3:
         fitclr = 'r'
     xx, fity = self.updateFit(X, ax, x, clr=fitclr, plotoption=plotoption)
     return
示例#13
0
文件: Base.py 项目: yongwangCPH/peat
 def doPlot(self, ek, fitdata, options):
     """Need a bare bones plotter that can plot any given ekin data
        Should then call this one in plotDatasets below and simplify"""
     x, y, a, xerr, yerr = ek.getAll()
     line = ax.scatter(x, y, marker="o")
     # plotting fit should also be seperate?
     f = fitdata
     d = []
     X = Fitting.makeFitter(f, zip(x, y))
     if X == None:
         return
     # we now use this model to draw the fit line
     fitclr = lineclr
     if plotoption != 3:
         fitclr = "r"
     xx, fity = self.updateFit(X, ax, x, clr=fitclr, plotoption=plotoption)
     return
示例#14
0
    def set_curves(self):
        degree = 2
        num_cp = 2
        CP, U = F.curve_fit2D(self.points, np.ones((len(self.points), 1)),
                              None, None, None, num_cp, degree)

        B_cur = BSpline.Curve()
        B_cur.degree = degree
        B_cur.ctrlpts = CP
        B_cur.knotvector = U
        B_cur.evaluate()
        B_cur.delta = 0.01

        N_cur = convert.bspline_to_nurbs(B_cur)
        # Set evaluation delta
        N_cur.delta = 0.01
        N_cur.evaluate()

        return N_cur, B_cur
示例#15
0
    def updateParametersTable(self):
        import Fitting
        # read the currently selected function
        currFunction = self.getCurrentFunction()
        # set the table size
        self.ui.tableParameters.setColumnCount(len(Fitting.tableHeaders))
        self.ui.tableParameters.setRowCount(len(currFunction.paramList))
        self.ui.tableParameters.setVerticalHeaderLabels(currFunction.paramList)
        self.ui.tableParameters.setHorizontalHeaderLabels(Fitting.tableHeaders)

        # set any defaults? - Handle in updatNewFunction

        # read any parameters results (if any)
        # Use Param.stderr != None to determine if a fit has been run or just read it all and set blank for None
        from itertools import izip
        for colNum, colName in izip(xrange(len(Fitting.tableHeaders)),
                                    Fitting.tableHeaders):
            for rowNum, rowName in izip(xrange(len(currFunction.paramList)),
                                        currFunction.paramList):
                #I will now hit each cell in the table
                try:
                    currParam = self.params[rowName]
                except KeyError:
                    continue
                value = Fitting.GetParameterValue(currParam, colName)
                # convert the value to a string appropriately
                if value == None or value == float('-inf') or value == float(
                        'inf'):
                    value = ''
                elif isinstance(value, bool):
                    value = '%s' % value
                else:
                    try:
                        # I'm a number
                        value = '%e' % value
                    except TypeError:
                        # I'm not a number, could be boolean or string or dict
                        value = '%s' % value
                self.ui.tableParameters.setItem(rowNum, colNum,
                                                QtGui.QTableWidgetItem(value))
示例#16
0
文件: Base.py 项目: yongwangCPH/peat
    def estimateExpUncertainty(
        self, dataset, xuncert=0.01, yuncert=0.01, runs=10, doplots=False, conv=1e-6, grad=1e-6, guess=True
    ):
        """Chresten's method for estimating the error on each parameter due
           to the effect of the exp uncertainty, which is user provided """

        data = self.data[dataset]
        fitdata = self.__datatabs_fits__[dataset]
        # print 'estimating exp uncertainty for %s' %dataset
        fitstats = Fitting.estimateExpUncertainty(
            data,
            fitdata,
            xuncert=xuncert,
            yuncert=yuncert,
            runs=runs,
            doplots=doplots,
            conv=conv,
            grad=grad,
            guess=guess,
        )

        return fitstats
示例#17
0
文件: Main.py 项目: JitenDhandha/CFit
    def browseFileFunc(self):

        #Clearing everything in the GUI first
        self.clear()

        #Letting user browse the file and calling readFile() in Fitting.py
        self.fileLocation = filedialog.askopenfilename(title='Choose a file')
        self.fileCheck = fit.readFile(self.fileLocation)

        #Showing the directory of the file
        self.fileLocationEntry.configure(state='normal')
        self.fileLocationEntry.delete(0,'end')
        if(self.fileLocation==''):
            self.fileLocationEntry.insert(0, string='(no directory selected)')
        else:
            self.fileLocationEntry.insert(0, string=self.fileLocation)
        self.fileLocationEntry.configure(state='readonly')

        #Setting file status
        if(self.fileCheck==0):
            self.fileStatus.set("File opened successfully!")
        elif(self.fileCheck==1):
            self.fileStatus.set("")
        elif(self.fileCheck==2):
            self.fileStatus.set("Not a .txt or .csv file!")
        elif(self.fileCheck==3):
            self.fileStatus.set("Couldn't parse file properly.")
        elif(self.fileCheck==4):
            self.fileStatus.set("File must contain 2 or 3 columns!")
        elif(self.fileCheck==5):
            self.fileStatus.set("File contains NaNs or Infs.")
        elif(self.fileCheck==6):
            self.fileStatus.set("Y-errors need to be a positive number!")

        #Checking if file can be previewed or not
        if(self.fileCheck!=1 and self.fileCheck!=2):
            self.setFilePreview()
示例#18
0
文件: Base.py 项目: tubapala/peat
    def getMetaData(self, dataset):
        """Return the fit data in a formatted dict with the variable
           names as keys, more intelligible than the simple fit data.
           We also include the experimental meta data here, if present
           and all fields which are special keys e.g. __enzyme_conc__"""
        mdata = {}
        fitdata = self.__datatabs_fits__[dataset]
        if fitdata != None and len(fitdata) != 0:
            keys = ['model', 'error', 'comment']
            #use fitter object for this model to get fit param names
            X = Fitting.makeFitter(fitdata)
            if X != None:
                mdata = X.getFitDict()
            else:
                mdata = {}
            for k in keys:
                if fitdata.has_key(k):
                    mdata[k] = fitdata[k]

        if hasattr(self, '__meta_data__'):
            if self.__meta_data__.has_key(dataset):
                for k in self.__meta_data__[dataset].keys():
                    mdata[k] = self.__meta_data__[dataset][k]
        return mdata
示例#19
0
文件: Base.py 项目: yongwangCPH/peat
    def getMetaData(self, dataset):
        """Return the fit data in a formatted dict with the variable
           names as keys, more intelligible than the simple fit data.
           We also include the experimental meta data here, if present
           and all fields which are special keys e.g. __enzyme_conc__"""
        mdata = {}
        fitdata = self.__datatabs_fits__[dataset]
        if fitdata != None and len(fitdata) != 0:
            keys = ["model", "error", "comment"]
            # use fitter object for this model to get fit param names
            X = Fitting.makeFitter(fitdata)
            if X != None:
                mdata = X.getFitDict()
            else:
                mdata = {}
            for k in keys:
                if fitdata.has_key(k):
                    mdata[k] = fitdata[k]

        if hasattr(self, "__meta_data__"):
            if self.__meta_data__.has_key(dataset):
                for k in self.__meta_data__[dataset].keys():
                    mdata[k] = self.__meta_data__[dataset][k]
        return mdata
示例#20
0
文件: Base.py 项目: tubapala/peat
    def plotDatasets(self,
                     datasets='ALL',
                     data=None,
                     fitdata=None,
                     filename=None,
                     plotoption=1,
                     cols=0,
                     size=(6, 4),
                     linecolor=None,
                     figure=None,
                     axis=None,
                     showfitvars=False,
                     dpi=80,
                     **kwargs):
        """Plot a dataset or list of datasets, if none given all are  plotted.
           plotoptions:
           1 - each dataset in one plot, different figures/images
           2 - each dataset in different axes but same figure - one image
           3 - all datasets in one plot, single figure
           Note: errorbars are drawn +/- value supplied"""

        status = self.setPylab(size)
        prms = Params(**kwargs)
        self.current = datasets
        shapes = Options.shapes

        plt.rc('font', family=prms.font)
        plt.rc('font', size=prms.fontsize)
        plt.rc('text', usetex=prms.usetex)

        if plotoption == 2 and cols >= 3:
            size = (3, 4)
        if status == False:
            print 'sorry, install pylab..'
            return
        if figure != None:
            fig = figure
        else:
            fig = plt.figure(figsize=size, dpi=80)
        #fig.clf()
        datasets = self.checkDatasetParam(datasets)
        if plotoption == 2:
            cols, dim = self.getGeometry(datasets, cols)
            n = 1
            plt.rc('font', size=9.0)
            if prms.title != None:
                fig.suptitle(prms.title, fontsize=prms.fontsize)
        else:
            if axis != None:
                ax = axis
            else:
                ax = fig.add_subplot(111)
            ax.cla()
            self.ax = ax

        legendlines = []
        a = numpy.arange(-4, 4, 0.5).tolist()
        #get ekin data into plot values
        xdata = {}
        ydata = {}
        adata = {}
        xerrors = {}
        yerrors = {}

        for name in datasets:
            self.fitline = None
            ek = self.data[name]
            xd, yd = xdata[name], ydata[name] = ek.getxy()
            adata[name] = ek.active
            xerrors[name], yerrors[name] = ek.getErrors()
            #if no error stored for points, try using global error if present
            if xerrors[name] == None and xerror != None:
                xerrors[name] = []
                for i in range(len(xd)):
                    xerrors[name].append(xerror)
            if yerrors[name] == None and yerror != None:
                yerrors[name] = []
                for i in range(len(yd)):
                    yerrors[name].append(yerror)

            if prms.xlabel != '':
                xlabel = prms.xlabel
            else:
                xlabel = ek.labels[0]
            if prms.ylabel != '':
                ylabel = prms.ylabel
            else:
                ylabel = ek.labels[1]

        i = 0
        cc = 0
        for name in datasets:
            if cc >= len(prms.colors):
                cc = 0
            if prms.varyshapes == True:
                if prms.markers != None and len(prms.markers) > cc:
                    prms.marker = prms.markers[cc]
                else:
                    prms.marker = shapes[cc]
            if plotoption == 2:
                ax = fig.add_subplot(int(dim), int(cols), n)
                ax.set_title(name, fontsize=prms.fontsize, fontweight='bold')
                if cols < 4:
                    ax.set_xlabel(xlabel, family=prms.font)
                    ax.set_ylabel(ylabel, family=prms.font)
                if prms.grid == True:
                    ax.grid(True)
                n = n + 1
                self.ax = ax

            x = xdata[name]
            y = ydata[name]
            act = adata[name]
            xerr = xerrors[name]
            yerr = yerrors[name]
            self.currxdata = x
            self.currydata = y
            if len(x) == 0:
                continue
            if prms.normalise == True:
                self.mindata = min(y)
                self.maxdata = max(y)
                y = normList(y, inputmin=self.mindata, inputmax=self.maxdata)
            if prms.graphtype == 'XY':
                if prms.logx == True and prms.logy == False:
                    line, = ax.semilogx(x, y, prms.marker, alpha=prms.alpha)
                elif prms.logy == True and prms.logx == False:
                    line, = ax.semilogy(x, y, prms.marker, alpha=prms.alpha)
                elif prms.logx == True and prms.logy == True:
                    line, = ax.loglog(x, y, prms.marker, alpha=prms.alpha)
                elif prms.marker in ['-', '--', ':', '.', ':.', ',', '|', '*']:
                    line, = ax.plot(x,
                                    y,
                                    prms.marker,
                                    linewidth=prms.linewidth,
                                    mew=prms.linewidth,
                                    alpha=prms.alpha,
                                    ms=prms.markersize)
                else:
                    ptcolors = []
                    if prms.grayscale == True:
                        actclr = '0.5'
                    else:
                        actclr = prms.colors[cc]
                    for i in range(len(x)):
                        if act[i] == 0:
                            ptcolors.append('0.8')
                        else:
                            ptcolors.append(actclr)

                    line = ax.scatter(x,
                                      y,
                                      marker=prms.marker,
                                      c=ptcolors,
                                      s=prms.markersize,
                                      lw=prms.linewidth,
                                      alpha=prms.alpha)
                cc += 1

                lineclr = self._getlineColor(line)
                if prms.showerrorbars == True and (yerr != None
                                                   or xerr != None):
                    #print xerr,yerr
                    errline = ax.errorbar(x,
                                          y,
                                          xerr=xerr,
                                          yerr=yerr,
                                          fmt=None,
                                          elinewidth=1.0,
                                          ecolor=lineclr,
                                          alpha=prms.alpha)
            elif prms.graphtype == 'bar':
                bars = ax.bar(x, y, linewidth=prms.linewidth, alpha=prms.alpha)
                line = bars[0]
                lineclr = line.get_facecolor()

            if len(xd) > 0:
                if abs(min(yd)) > 0.01 and max(yd) < 1e4:
                    major_formatter = plt.FormatStrFormatter('%2.2f')
                    ax.yaxis.set_major_formatter(major_formatter)

            legendlines.append(line)
            X = None
            if self.__datatabs_fits__.has_key(name):
                f = self.__datatabs_fits__[name]
                if f.has_key('model'):
                    #create the required fitter
                    d = []
                    X = Fitting.makeFitter(f, zip(x, y))
                    if X == None:
                        continue
                    #we now use this model to draw the fit line
                    fitclr = lineclr
                    if plotoption != 3:
                        fitclr = 'r'
                    if prms.grayscale == True:
                        fitclr = '0.4'
                    xx, fity = self.updateFit(X,
                                              ax,
                                              xdata[name],
                                              clr=fitclr,
                                              normalise=prms.normalise,
                                              plotoption=plotoption)

            if prms.xlim != 0:
                l = min(x) - prms.xlim
                u = max(y) + prms.xlim
                ax.set_xlim((l, u))
                #print l,u
            if prms.ylim != 0:
                l = min(y) - prms.ylim
                u = max(y) + prms.ylim
                ax.set_ylim((l, u))

        if plotoption != 2:
            if prms.title == None or prms.title == '':
                prms.title = name
            ax.set_title(prms.title, family=prms.font)
            ax.set_xlabel(xlabel, family=prms.font)
            ax.set_ylabel(ylabel, family=prms.font)

            if showfitvars == True:
                self.showfitResults(X, ax)
            if prms.grid == True:
                ax.grid(True)
            if prms.legend == True:
                l = ax.legend(legendlines,
                              datasets,
                              numpoints=1,
                              loc=prms.legendloc,
                              prop=FontProperties(size="smaller"))
                l.get_frame().set_alpha(0.8)
        else:
            fig.subplots_adjust(hspace=0.4, wspace=0.4)

        if filename != None:
            fig.savefig(filename, dpi=dpi)
            plt.close(fig)

        return ax
示例#21
0
文件: Base.py 项目: yongwangCPH/peat
    def plotDatasets(
        self,
        datasets="ALL",
        data=None,
        fitdata=None,
        filename=None,
        plotoption=1,
        cols=0,
        size=(6, 4),
        linecolor=None,
        figure=None,
        axis=None,
        showfitvars=False,
        dpi=80,
        **kwargs
    ):
        """Plot a dataset or list of datasets, if none given all are  plotted.
           plotoptions:
           1 - each dataset in one plot, different figures/images
           2 - each dataset in different axes but same figure - one image
           3 - all datasets in one plot, single figure
           Note: errorbars are drawn +/- value supplied"""

        status = self.setPylab(size)
        prms = Params(**kwargs)
        self.current = datasets
        shapes = Options.shapes

        plt.rc("font", family=prms.font)
        plt.rc("font", size=prms.fontsize)
        plt.rc("text", usetex=prms.usetex)

        if plotoption == 2 and cols >= 3:
            size = (3, 4)
        if status == False:
            print "sorry, install pylab.."
            return
        if figure != None:
            fig = figure
        else:
            fig = plt.figure(figsize=size, dpi=80)
        # fig.clf()
        datasets = self.checkDatasetParam(datasets)
        if plotoption == 2:
            cols, dim = self.getGeometry(datasets, cols)
            n = 1
            plt.rc("font", size=9.0)
            if prms.title != None:
                fig.suptitle(prms.title, fontsize=prms.fontsize)
        else:
            if axis != None:
                ax = axis
            else:
                ax = fig.add_subplot(111)
            ax.cla()
            self.ax = ax

        legendlines = []
        a = numpy.arange(-4, 4, 0.5).tolist()
        # get ekin data into plot values
        xdata = {}
        ydata = {}
        adata = {}
        xerrors = {}
        yerrors = {}

        for name in datasets:
            self.fitline = None
            ek = self.data[name]
            xd, yd = xdata[name], ydata[name] = ek.getxy()
            adata[name] = ek.active
            xerrors[name], yerrors[name] = ek.getErrors()
            # if no error stored for points, try using global error if present
            if xerrors[name] == None and xerror != None:
                xerrors[name] = []
                for i in range(len(xd)):
                    xerrors[name].append(xerror)
            if yerrors[name] == None and yerror != None:
                yerrors[name] = []
                for i in range(len(yd)):
                    yerrors[name].append(yerror)

            if prms.xlabel != "":
                xlabel = prms.xlabel
            else:
                xlabel = ek.labels[0]
            if prms.ylabel != "":
                ylabel = prms.ylabel
            else:
                ylabel = ek.labels[1]

        i = 0
        cc = 0
        for name in datasets:
            if cc >= len(prms.colors):
                cc = 0
            if prms.varyshapes == True:
                if prms.markers != None and len(prms.markers) > cc:
                    prms.marker = prms.markers[cc]
                else:
                    prms.marker = shapes[cc]
            if plotoption == 2:
                ax = fig.add_subplot(int(dim), int(cols), n)
                ax.set_title(name, fontsize=prms.fontsize, fontweight="bold")
                if cols < 4:
                    ax.set_xlabel(xlabel, family=prms.font)
                    ax.set_ylabel(ylabel, family=prms.font)
                if prms.grid == True:
                    ax.grid(True)
                n = n + 1
                self.ax = ax

            x = xdata[name]
            y = ydata[name]
            act = adata[name]
            xerr = xerrors[name]
            yerr = yerrors[name]
            self.currxdata = x
            self.currydata = y
            if len(x) == 0:
                continue
            if prms.normalise == True:
                self.mindata = min(y)
                self.maxdata = max(y)
                y = normList(y, inputmin=self.mindata, inputmax=self.maxdata)
            if prms.graphtype == "XY":
                if prms.logx == True and prms.logy == False:
                    line, = ax.semilogx(x, y, prms.marker, alpha=prms.alpha)
                elif prms.logy == True and prms.logx == False:
                    line, = ax.semilogy(x, y, prms.marker, alpha=prms.alpha)
                elif prms.logx == True and prms.logy == True:
                    line, = ax.loglog(x, y, prms.marker, alpha=prms.alpha)
                elif prms.marker in ["-", "--", ":", ".", ":.", ",", "|", "*"]:
                    line, = ax.plot(
                        x,
                        y,
                        prms.marker,
                        linewidth=prms.linewidth,
                        mew=prms.linewidth,
                        alpha=prms.alpha,
                        ms=prms.markersize,
                    )
                else:
                    ptcolors = []
                    if prms.grayscale == True:
                        actclr = "0.5"
                    else:
                        actclr = prms.colors[cc]
                    for i in range(len(x)):
                        if act[i] == 0:
                            ptcolors.append("0.8")
                        else:
                            ptcolors.append(actclr)

                    line = ax.scatter(
                        x, y, marker=prms.marker, c=ptcolors, s=prms.markersize, lw=prms.linewidth, alpha=prms.alpha
                    )
                cc += 1

                lineclr = self._getlineColor(line)
                if prms.showerrorbars == True and (yerr != None or xerr != None):
                    # print xerr,yerr
                    errline = ax.errorbar(
                        x, y, xerr=xerr, yerr=yerr, fmt=None, elinewidth=1.0, ecolor=lineclr, alpha=prms.alpha
                    )
            elif prms.graphtype == "bar":
                bars = ax.bar(x, y, linewidth=prms.linewidth, alpha=prms.alpha)
                line = bars[0]
                lineclr = line.get_facecolor()

            if len(xd) > 0:
                if abs(min(yd)) > 0.01 and max(yd) < 1e4:
                    major_formatter = plt.FormatStrFormatter("%2.2f")
                    ax.yaxis.set_major_formatter(major_formatter)

            legendlines.append(line)
            X = None
            if self.__datatabs_fits__.has_key(name):
                f = self.__datatabs_fits__[name]
                if f.has_key("model"):
                    # create the required fitter
                    d = []
                    X = Fitting.makeFitter(f, zip(x, y))
                    if X == None:
                        continue
                    # we now use this model to draw the fit line
                    fitclr = lineclr
                    if plotoption != 3:
                        fitclr = "r"
                    if prms.grayscale == True:
                        fitclr = "0.4"
                    xx, fity = self.updateFit(
                        X, ax, xdata[name], clr=fitclr, normalise=prms.normalise, plotoption=plotoption
                    )

            if prms.xlim != 0:
                l = min(x) - prms.xlim
                u = max(y) + prms.xlim
                ax.set_xlim((l, u))
                # print l,u
            if prms.ylim != 0:
                l = min(y) - prms.ylim
                u = max(y) + prms.ylim
                ax.set_ylim((l, u))

        if plotoption != 2:
            if prms.title == None or prms.title == "":
                prms.title = name
            ax.set_title(prms.title, family=prms.font)
            ax.set_xlabel(xlabel, family=prms.font)
            ax.set_ylabel(ylabel, family=prms.font)

            if showfitvars == True:
                self.showfitResults(X, ax)
            if prms.grid == True:
                ax.grid(True)
            if prms.legend == True:
                l = ax.legend(
                    legendlines, datasets, numpoints=1, loc=prms.legendloc, prop=FontProperties(size="smaller")
                )
                l.get_frame().set_alpha(0.8)
        else:
            fig.subplots_adjust(hspace=0.4, wspace=0.4)

        if filename != None:
            fig.savefig(filename, dpi=dpi)
            plt.close(fig)

        return ax
示例#22
0
文件: Main.py 项目: JitenDhandha/CFit
 def plotRawDataButtonFunc(self):
     if(self.fileCheck==0):
         fit.plotRawData(self.plotTitleEntry.get(),self.xAxisTitleEntry.get(),self.yAxisTitleEntry.get(),self.viewGrid.get())
示例#23
0
def strap(residuals, FArr, ZArr, params, ParamNames):

    #Break apart complex array of original residuals
    length = int(len(residuals) / 2)
    Real_residuals = residuals[0:length]
    Imag_residuals = residuals[length:]

    #Break apart original complex array
    RArr = ZArr.real
    ImArr = ZArr.imag
    Modulus = numpy.sqrt(RArr**2 + ImArr**2)

    #Obtain the Statistical parameters of the Residuals from original fitting (Needed input = Real + Imaginary Residuals)
    Real_res_error = statistics.stdev(Real_residuals)
    Imag_res_error = statistics.stdev(Imag_residuals)
    Real_res_mean = statistics.mean(Real_residuals)
    Imag_res_mean = statistics.mean(Imag_residuals)

    #Initialize iterator
    i = 0

    #Initilize Matrix to carry disributions of fitted variables
    Fitted_Variables_Matrix = []

    #Initialize Parameters list
    global Param_names

    #Main Bootstrap. Fitting to be performed x times in i<x
    while i < 50:
        #Generate a new set of residuals based on the statistics acquired above.
        Boot_R_residuals = numpy.random.normal(
            Real_res_mean, Real_res_error, size=length) * Modulus
        Boot_i_residuals = numpy.random.normal(
            Imag_res_mean, Imag_res_error, size=length) * Modulus
        #Generate a new data set based on these residuals (Requires original impedance data)
        new_r_boot = RArr + Boot_R_residuals
        new_i_boot = ImArr + Boot_i_residuals
        total_boot = new_r_boot + 1j * new_i_boot

        #Check simulated impedance (uncomment to check boots)
        #plt.plot(new_r_boot, -new_i_boot)
        #plt.savefig("boots.png")

        #Perform a fit on newly generated data (Requires parameter guesss)
        fit_result, pn = fit.custom_fitting(FArr, total_boot, params)
        #Generate a list of variables from fit
        Fitted_variables = list(fit_result.x)

        Fitted_Variables_Matrix.append(Fitted_variables)

        i = i + 1

        Param_names = pn

    Fitted_Variables_Matrix = pd.DataFrame(Fitted_Variables_Matrix,
                                           columns=ParamNames)
    Correlation_Matrix = Fitted_Variables_Matrix.corr()

    #Take the standard deviation of each of the fitted variables distributions
    Stds = Fitted_Variables_Matrix.std(axis=0)

    #Take the average fit
    means = Fitted_Variables_Matrix.mean(axis=0)

    #Compile all of the fitted variables errors into a single list
    evz = list(Stds)
    ms = list(means)

    #Compile and save these results to a csv
    Params = list(zip(Param_names, ms, evz))
    Paramsdf = pd.DataFrame(data=Params,
                            columns=['Parameters', 'Value', 'Error'])
    Paramsdf.to_csv('Fitted_Parameters.csv', index=False)

    #Clear figure at end of program
    plt.clf()

    #Return Fitted Params
    return ms, Correlation_Matrix
示例#24
0
    Fre = float(fields[0])
    if Fre >= FreqLB and Fre <= FreqUB:
        F=F+[float(fields[0])]
        R=R+[float(fields[1])]
        Im=Im+[float(fields[2])]
        thetas=thetas+[numpy.arctan(float(fields[2])/float(fields[1]))]


#Form data into complex array
FArr=numpy.array(F)
RArr=numpy.array(R)
ImArr=numpy.array(Im)*1j
TotArr=RArr+ImArr

#Fit the data
fit_result, ParamNames = fit.custom_fitting(F, TotArr, params)
Fitted_variables = fit_result.x

#Obtain the residuals
residuals = fit.res_vec( Fitted_variables, FArr, TotArr)

#Bootstrap to get error and generate final model
boot_params, corr = boot.strap(residuals, FArr, TotArr, Fitted_variables, ParamNames)
result = cir.Z(boot_params, FArr, modelname)
boot_generation = result.z
Real_Boot_Fit = boot_generation.real
Imag_Boot_Fit = boot_generation.imag
Thetas_Fit = []
i=0
for x in Real_Boot_Fit:
    theta = numpy.arctan(Imag_Boot_Fit[i]/Real_Boot_Fit[i])
示例#25
0
 def test_assign_ub(self):
     ub = F.assign_ub(self.points)
     # Just check if the output is correct (and that the functions actually works).
     self.assertTrue(isinstance(ub, list))
     self.assertTrue(
         isinstance(F.set_knots(4, 2, len(self.points), ub), np.ndarray))
示例#26
0
 def test_fitting(self):
     CP, U = F.curve_fit2D(self.points, np.ones((len(self.points), 1)),
                           None, None, None, 2, 2)
     self.assertTrue(isinstance(CP, tuple))
     self.assertTrue(isinstance(U, list))
示例#27
0
 def plot_iv(self, th, fits, iv=Data()):
     # Plot the current data
     current_line = self.fig.host.errorbar(x=iv.v_mean,
                                           y=iv.i_mean,
                                           yerr=iv.i_error,
                                           fmt='r.',
                                           label='Current')
     # Label axis
     self.fig.host.set_xlabel("Voltage (V)")
     self.fig.host.set_ylabel("Current ($\mu$A)")
     # List of plotted lines
     lines = [current_line]
     # Tick size
     tkw = dict(size=4, width=1.5)
     self.fig.host.tick_params(axis='x', **tkw)
     # If plotting temperature and humidity lines:
     if th:
         # Overall average temperature and humidity
         temp_averages = iv.average_temp()
         hum_averages = iv.average_hum()
         # Plot temperature and humidity
         temp_line, = self.fig.temp.plot(
             iv.v_mean,
             iv.temperature,
             color='b',
             alpha=0.4,
             label='Temperature, $T_{Av}$ = %s$^\circ$C' % temp_averages[0])
         hum_line, = self.fig.hum.plot(iv.v_mean,
                                       iv.humidity,
                                       color='g',
                                       alpha=0.4,
                                       label='Humidity, $H_{Av}$ = %s%%' %
                                       hum_averages[0])
         # Colour axis
         self.fig.temp.yaxis.label.set_color(temp_line.get_color())
         self.fig.hum.yaxis.label.set_color(hum_line.get_color())
         # Uncomment to constrain axis range
         #self.fig.hum.set_ylim([30, 50])
         #self.fig.temp.set_ylim([18, 22])
         # Label axis
         self.fig.temp.set_ylabel("Temperature ($^\circ$C)")
         self.fig.hum.set_ylabel("Humidity (%)")
         self.fig.temp.tick_params(axis='y',
                                   colors=temp_line.get_color(),
                                   **tkw)
         self.fig.hum.tick_params(axis='y',
                                  colors=hum_line.get_color(),
                                  **tkw)
         # Add to list of lines
         lines.append(temp_line)
         lines.append(hum_line)
     # If finding the breakdown voltage:
     if fits:
         bd_voltage = Fitting.breakdown_voltage(iv)[0]
         bd_statement = Fitting.breakdown_voltage(iv)[1]
         #print(bd_statement)
         # Plot a vertical line at breakdown voltage
         if bd_voltage is not None:
             bd_line = self.fig.host.axvline(
                 x=bd_voltage,
                 color='r',
                 linestyle='--',
                 label='$V_{Breakdown}$ = %sV' %
                 Fitting.round_sig(bd_voltage, 3))
             lines.append(bd_line)
         else:
             no_bd_line, = self.fig.host.plot([], [],
                                              ' ',
                                              label=bd_statement)
             lines.append(no_bd_line)
     # Create legend and title
     self.fig.host.legend(lines, [l.get_label() for l in lines])
     self.fig.suptitle(iv.name)
示例#28
0
#  Plot test function
fig = plt.figure(num=1)
plt.plot(x_samp, y_samp, 'o')  # Nicer plot
plt.plot(np.linspace(0, 1, num=1000), Branin_1d(np.linspace(0, 1, num=1000)))
# plt.show()

'''
Step 2)
Create a B-spline and fit it to the data
'''

degree = 5
n = 5

# Adapt a B-spline to the curve.
CP, U = Fitting.curve_fit2D(Q, np.ones((len(Q), 1)), None, None, None, n, degree)
# Transform to Tupled tuples


# Create a BSpline (NURBS) curve instance
test_c = BSpline.Curve()

# Set up the curve
test_c.degree = degree
test_c.ctrlpts = CP
# test_c.ctrlpts = exchange.import_txt("ex_curve02.cpt")

test_c.knotvector = U
# Auto-generate knot vector
# test_c.knotvector = utilities.generate_knot_vector(test_c.degree, len(test_c.ctrlpts))
# Set evaluation delta
示例#29
0
    def plot_it(self, th, fits, it=Data()):
        # Converts time from seconds to hours
        it.time_to_hours()
        """
        # A very rough method of trying to ignore anomalous points in the It data
        mean_current= sum(it.i_mean)/len(it.i_mean)
        mean_error = stats.tstd(it.i_mean)
        main_data_i = []
        main_data_t = []
        main_data_ierror =[]
        bad_data_i = []
        bad_data_t = []
        bad_data_ierror = []
        for i in range(len(it.i_mean)):
            if mean_current+(3 * mean_error) > it.i_mean[i] > mean_current - (3* mean_error):
                main_data_t.append(it.time[i])
                main_data_i.append(it.i_mean[i])
                main_data_ierror.append(it.i_error[i])
            else:
                bad_data_t.append(it.time[i])
                bad_data_i.append(it.i_mean[i])
                bad_data_ierror.append(it.i_error[i])
                
        current_line = self.fig.host.errorbar(x=main_data_t, y=main_data_i, yerr=main_data_ierror, fmt='r.',
                                              label='Current')
        current_line2 = self.fig.host.errorbar(x=bad_data_t, y=bad_data_i, yerr=bad_data_ierror, fmt='r.', alpha=0.5,
                                               label='Current')
        """

        # Plot the current data
        current_line = self.fig.host.errorbar(x=it.time,
                                              y=it.i_mean,
                                              yerr=it.i_error,
                                              fmt='r.',
                                              label='Current')
        # Set y axis range
        self.fig.host.set_ylim([min(it.i_mean) * 1.1, 0])
        # Label axis
        self.fig.host.set_xlabel("Time (hours)")
        self.fig.host.set_ylabel("Current ($\mu$A)")
        # List of all the lines
        lines = [current_line]
        # Line ticks
        tkw = dict(size=4, width=1.5)
        self.fig.host.tick_params(axis='x', **tkw)
        # If plotting temperature and humidity lines:
        if th:
            # Overall average temperature and humidity
            temp_averages = it.average_temp()
            hum_averages = it.average_hum()
            # Plot temperature and humidity lines
            temp_line, = self.fig.temp.plot(
                it.time,
                it.get_temperature(),
                color='b',
                alpha=0.4,
                label='Temperature, $T_{Av}$ = %s$^\circ$C' % temp_averages[0])
            hum_line, = self.fig.hum.plot(it.time,
                                          it.get_humidity(),
                                          color='g',
                                          alpha=0.4,
                                          label='Humidity, $H_{Av}$ = %s%%' %
                                          hum_averages[0])
            # Colour axis
            self.fig.temp.yaxis.label.set_color(temp_line.get_color())
            self.fig.hum.yaxis.label.set_color(hum_line.get_color())
            # Uncomment if you want to set the temperature and humidity axis range
            #self.fig.hum.set_ylim([30, 50])
            #self.fig.temp.set_ylim([18, 22])
            # Labels and tick sizes
            self.fig.temp.set_ylabel('Temperature ($^\circ$C)')
            self.fig.hum.set_ylabel('Humidity (%)')
            self.fig.temp.tick_params(axis='y',
                                      colors=temp_line.get_color(),
                                      **tkw)
            self.fig.hum.tick_params(axis='y',
                                     colors=hum_line.get_color(),
                                     **tkw)
            # Add to list of plotted lines
            lines.append(temp_line)
            lines.append(hum_line)
        # If testing the stability of the current
        if fits:
            fitting_data = Fitting.it_fits(it)
            maximum_line = self.fig.host.axhline(
                y=fitting_data[0],
                color='r',
                label='$I_{max}$ = %s$\mu$A' %
                Fitting.round_sig(fitting_data[0], 2))
            allowed_line = self.fig.host.axhline(
                y=fitting_data[2],
                color='k',
                label='Allowed $I_{min}$ = %s$\mu$A, Result: %s' %
                (Fitting.round_sig(fitting_data[2], 2), fitting_data[3]))
            minimum_line = self.fig.host.axhline(
                y=fitting_data[1],
                color='r',
                alpha=0.6,
                label='$I_{min}$ = %s$\mu$A' %
                Fitting.round_sig(fitting_data[1], 2))
            # Add to list of plotted lines
            lines.append(minimum_line)
            lines.append(maximum_line)
            lines.append(allowed_line)

        # Create legend and title
        self.fig.host.legend(lines, [l.get_label() for l in lines])
        self.fig.suptitle(it.name)
示例#30
0
 def plot_cv(self, th, fits, cv=Data()):
     #print('amw_3')
     # Plot capacitance data
     capacitance_line = self.fig.host.errorbar(
         x=cv.v_mean,
         y=cv.inverse_c_squared,
         yerr=cv.inverse_c_squared_error,
         fmt='r.',
         label='Capacitance')
     # Label axis
     self.fig.host.set_xlabel("Voltage (V)")
     self.fig.host.set_ylabel("$1/C^2$ ($1/pF^2$)")
     # List of plotted lines
     lines = [capacitance_line]
     # Tick size and y axis limit
     tkw = dict(size=4, width=1.5)
     self.fig.host.tick_params(axis='x', **tkw)
     self.fig.host.set_ylim([0, max(cv.inverse_c_squared) * 1.1])
     # If plotting temperature and humidity:
     if th:
         # Overall average temperature and humidity
         temp_averages = cv.average_temp()
         hum_averages = cv.average_hum()
         # Plot temperature and humidity
         temp_line, = self.fig.temp.plot(
             cv.v_mean,
             cv.temperature,
             color='b',
             alpha=0.4,
             label='Temperature, $T_{Av}$ = %s$^\circ$C' % temp_averages[0])
         hum_line, = self.fig.hum.plot(cv.v_mean,
                                       cv.humidity,
                                       color='g',
                                       alpha=0.4,
                                       label='Humidity, $H_{Av}$ = %s%%' %
                                       hum_averages[0])
         # Colour axis
         self.fig.temp.yaxis.label.set_color(temp_line.get_color())
         self.fig.hum.yaxis.label.set_color(hum_line.get_color())
         # Uncomment to constrain axis range
         #self.fig.hum.set_ylim([30, 50])
         #self.fig.temp.set_ylim([18, 22])
         # Label axis and tick parameters
         self.fig.temp.set_ylabel('Temperature ($^\circ$C)')
         self.fig.hum.set_ylabel('Humidity (%)')
         self.fig.temp.tick_params(axis='y',
                                   colors=temp_line.get_color(),
                                   **tkw)
         self.fig.hum.tick_params(axis='y',
                                  colors=hum_line.get_color(),
                                  **tkw)
         # Add to list of lines
         lines.append(temp_line)
         lines.append(hum_line)
     # if finding the depletion voltage
     if fits:
         # Get data
         fit_data = Fitting.cv_fits(cv)
         # Plot two linear lines
         self.fig.host.plot(cv.v_mean, fit_data[0], 'r')
         self.fig.host.plot(cv.v_mean, fit_data[1], 'r')
         # Plot line at full depletion
         full_depletion = self.fig.host.axvline(
             x=fit_data[2],
             linestyle='--',
             color='r',
             label='$V_{Full \: Depletion}$ = %s $\pm$ %sV' %
             (fit_data[2], fit_data[3]))
         # Add to list of lines
         lines.append(full_depletion)
     # Create legend and title
     self.fig.host.legend(lines, [l.get_label() for l in lines])
     self.fig.suptitle(cv.name)
示例#31
0
import pandas as pd
import numpy as np

import Clustering as trk
import Fitting as fit

N = 1000

if __name__ == '__main__':
    print("Reading file ...")

    fitter = fit.TrackFitter(B=1.)

    data_track = pd.DataFrame({
        'event': [0],
        'track': [0],
        'pt': [0.],
        'phi': [0.],
        'xVtx': [0.],
        'yVtx': [0.]
    })
    data_track = data_track.drop(data_track.index[[0]])

    df = pd.read_csv("hits_" + str(N) + ".csv")
    y_df = df[['particle']]
    #    X_df = df.drop(['hit','layer','particle','event'], axis=1)
    X_df = df.drop(['particle', 'layer', 'iphi'], axis=1)

    #replace particle with 100000000*event+particle

    y_train = df['particle'].values + df['event'].values * 1000
示例#32
0
文件: Fitting.py 项目: song9206/acq4
    stdFont = 'Arial'

    import matplotlib.pyplot as pylab
    pylab.rcParams['text.usetex'] = True
    pylab.rcParams['interactive'] = False
    pylab.rcParams['font.family'] = 'sans-serif'
    pylab.rcParams['font.sans-serif'] = 'Arial'
    pylab.rcParams['mathtext.default'] = 'sf'
    pylab.rcParams['figure.facecolor'] = 'white'
    # next setting allows pdf font to be readable in Adobe Illustrator
    pylab.rcParams['pdf.fonttype'] = 42
    pylab.rcParams['text.dvipnghack'] = True
    ##################### to here (matplotlib stuff - touchy!

    Fits = Fitting.Fitting()
    #    x = numpy.arange(0, 100.0, 0.1)
    #    y = 5.0-2.5*numpy.exp(-x/5.0)+0.5*numpy.random.randn(len(x))
    #    (dc, aFit,tauFit) = Fits.expfit(x,y)
    #    yf = dc + aFit*numpy.exp(-x/tauFit)
    #   pyplot.figure(1)
    #  pyplot.plot(x,y,'k')
    #  pyplot.hold(True)
    #  pyplot.plot(x, yf, 'r')
    #  pyplot.show()
    exploreError = False

    if exploreError is True:
        # explore the error surface for a function:

        func = 'exp1'
示例#33
0

# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
placelist = [
    'Global', 'Northern hemisphere', 'Southern hemisphere', 'Suzhou', 'Hawaii',
    'Arctic', 'Xinjiang'
]
placename = placelist[3]
filename = 'data' + os.sep + placename + '.csv'
dataframe = read_csv(filename, usecols=[1], engine='python', skipfooter=0)
dataset = dataframe.values
dataset = dataset.astype('float32')
dataset, fittingPlot, func, fittingFig = dataHandle(dataset)
fittingPredictPlot, fittingPredictFig = Fitting.predict(func, dataset)
#create file
if os.path.exists('result') == False:
    os.mkdir('result')
path = 'result' + os.sep + placename
if os.path.exists(path):
    shutil.rmtree(path)
os.mkdir(path)
#save log
sys.stdout = Logger(path + os.sep + placename + '.txt')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# set train and test
look_back = 40
train_size = int(len(dataset) * 1)
示例#34
0
文件: Base.py 项目: tubapala/peat
class EkinProject(object):
    """Ekin base class - capturing, storing and analysing biophysical data.
       This class provides the base functionality that can be called outside
       a GUI.
       Usage: E = EkinProject(data=ekindata)
    """

    fitmodels = Fitting.getCurrentModels()
    #modelsdict, modelsfile = Fitting.loadModelsFile()
    #fitmodels = sorted(modelsdict.keys())

    ekinfields = [
        '__datatabs_fits__', '__fit_matches__', '__Exp_Meta_Dat__',
        '__distance_matches__', '__datatab_structmapping__', '__meta_data__',
        '__plotopts__', '__currentdataset__', '__displaymultiple__'
    ]

    modes = [
        'General', 'Simple enzyme kinetic', 'Enzyme pH-activity',
        'pH-stability', 'NMR titration', 'Protein Stability',
        'Amyloid formation'
    ]
    mode_definition = {
        'General':
        fitmodels,
        'Simple enzyme kinetic': [
            'Michaelis-Menten', 'Michaelis-Menten-competitive-inhibition',
            'Competitive inhibition', 'Non-competitive inhibition'
        ],
        'Enzyme pH-activity': [
            'Bell-shaped pH-act profile (3 pKas)',
            'Bell-shaped pH-act profile (2 pKas)'
        ],
        'pH-stability': [
            'Bell-shaped pH-act profile (3 pKas)',
            'Bell-shaped pH-act profile (2 pKas)'
        ],
        'NMR titration': [
            'Linear', '1 pKa 2 Chemical shifts', '3 pKas, 2 Chemical shifts',
            '2 pKas, 3 Chemical shifts', '3 pKas, 4 Chemical shifts'
        ],
        'Protein Stability': ['Sigmoid'],
        'Amyloid formation': ['Linear', 'Amyloid Fibre Formation']
    }

    def __init__(self,
                 parent=None,
                 ekinproj=None,
                 data=None,
                 mode=None,
                 protein=None,
                 field=None):
        """Initialize the class"""

        #try to load pylab if present
        self.protein = protein
        self.parent = parent
        self.currentmode = mode
        #set a default model for fitting new datasets
        if self.currentmode != None:
            self.defaultmodel = self.mode_definition[self.currentmode][0]

        if self.parent and protein:
            self.allowreturntoDB = 1
            self.protein_name = self.parent.data['DBinstance'].DB[protein][
                'Name']
            #check for other open instances for the cell if called in PEAT
            if self.parent.ekin_instances.has_key(protein + field):
                if self.parent.ekin_instances[protein + field] > 1:
                    print 'EKIN ALREADY OPENED FOR THIS CELL'
                    self.allowreturntoDB = 0
        else:
            self.protein_name = protein
        self.field = field
        self.filename = None
        #create IO vars
        self.savedir = os.getcwd()

        #if ekinprojectfile provided we load it
        if ekinproj != None:
            self.openProject(ekinproj)
        else:
            #just load the data
            self.loadData(data)
        self.current = None  #track 'current' dataset
        self.length = 0
        return

    def loadData(self, data=None):
        """Load the data into self.data and update selector etc"""

        # Did we get data or should we just add an empty data window?
        if data == None or type(data) is types.StringType:
            self.data = {}
            self.datasets = []
            self.__datatabs_fits__ = {}
            M = MetaData()
            self.__meta_data__ = M.data
        else:
            self.data = copy.deepcopy(data)
            self.datasets = []
            # Set mode if present
            if self.data.has_key('mode'):
                self.currentmode = self.data['mode']
                del self.data['mode']
            #we now load the special dicts directly, so same names
            for name in self.ekinfields:
                if data.has_key(name):
                    self.__dict__[name] = copy.deepcopy(self.data[name])
                    del self.data[name]
            #populate list of datasets
            for dataset in self.data.keys():
                if dataset in self.ekinfields:
                    continue
                self.datasets.append(dataset)
        self.length = len(self.datasets)
        self.checkMeta()
        return

    def checkMeta(self):
        """Load old style meta data if present and then remove"""
        if not hasattr(self, '__meta_data__'):
            M = MetaData(project=self)
            if hasattr(self, '__Exp_Meta_Dat__'):
                print 'old meta data - converting'
                M.convertfromOld()
                del self.__Exp_Meta_Dat__
            self.setMetaData(M.data)
        return

    def addDataset(self, label=None, update=1):
        """Add a new empty dataset, can be combined with insert? """

        if label == None:
            return
        if label in self.datasets:
            print 'error, dataset present'
            return

        self.datasets.append(label)
        self.length = len(self.datasets)
        #here we need to add to self.data
        ek = self.data[label] = EkinDataset()
        for i in range(8):
            ek.add()
        self.__datatabs_fits__[label] = {}
        return

    def insertDataset(self,
                      newdata=None,
                      newname='data',
                      xydata=None,
                      fit=None,
                      replace=False,
                      update=1):
        """Insert pre-existing data as a dataset"""
        #print newdata
        if newdata == None:
            ek = EkinDataset(xy=xydata)
        else:
            #check for old or bad data
            ek = self.checkDataset(newdata)
            if ek == False:
                return

        if self.data.has_key(newname) and replace == False:
            newname = newname + 'a'
        self.data[newname] = ek
        self.datasets.append(newname)
        self.length = len(self.datasets)
        if fit == None:
            fit = ek.getFit()
        self.setFitData(newname, fit)
        return

    def readDataset(self, filename):
        """Get dataset from file"""
        newdata = {}
        if os.path.isfile(filename):
            fd = open(filename)
            import pickle
            newdata = pickle.load(fd)
            fd.close()
            # Add the data tab with a filename
            showname = os.path.split(filename)[1]
            showname = showname.replace('_', '-')
        return newdata

    def deleteDataset(self, name=None):
        """Delete a dataset"""
        if name == None:
            print 'error, no dataset given'
            return
        if not name in self.datasets:
            print 'error, no such dataset'
            return
        del self.data[name]
        self.datasets.remove(name)
        self.length = len(self.datasets)
        for f in self.ekinfields:
            if not self.__dict__.has_key(f) or type(
                    self.__dict__[f]) is not types.DictType:
                continue
            if self.__dict__[f].has_key(name):
                del self.__dict__[f][name]
        return

    def renameDataset(self, currname, newname):
        """Rename a dataset"""
        if newname == None or newname == currname:
            return
        self.copyDataset(currname, newname)
        self.deleteDataset(currname)
        return

    def batchRename(self, pattern='', replacement=''):
        """Rename all datasets by replacing a pattern"""
        if pattern == replacement:
            return
        count = 0
        for name in self.datasets[:]:
            newname = re.sub(pattern, replacement, name)
            if newname != name:
                self.renameDataset(name, newname)
                count += 1
        print 'renamed %s datasets' % count
        return

    def copyDataset(self, name, newname=None):
        """Copy a dataset"""

        if newname == None:
            newname = name + '_copy'
        elif newname in self.datasets:
            print 'error, new name exists'
            return
        else:
            self.data[newname] = copy.deepcopy(self.data[name])
            for f in self.ekinfields:
                if not self.__dict__.has_key(f) or type(
                        self.__dict__[f]) is not types.DictType:
                    continue
                if self.__dict__[f].has_key(name):
                    self.__dict__[f][newname] = copy.deepcopy(
                        self.__dict__[f][name])
            self.datasets.append(newname)
            self.length = len(self.datasets)
        return

    def getDataset(self, name):
        """Get a dataset"""
        data = self.data[name]
        ek = self.checkDataset(data)
        return ek

    def deleteAll(self):
        """Delete All"""
        self.data = {}
        self.datasets = []
        self.__datatabs_fits__ = {}
        self.length = 0
        return

    def saveDataset(self, name, filename=None):
        """Save single dataset to a file"""
        import tkFileDialog, os
        if filename == None:
            filename = name + '.ekindat'

        ek = self.data[name]
        fitdata = self.getFitData(name)
        ek.addFit(fitdata)
        fd = open(filename, 'w')
        import pickle
        pickle.dump(ek, fd)
        fd.close()
        return

    def checkDict(self, dataset, name):
        """Check for arbitrary fields stored in the old format dataset
           we move these to metadata"""
        if not type(dataset) is types.DictType:
            return None
        for k in dataset.keys():
            if not k in [0, 1, 2]:
                self.addMeta(name, k, dataset[k])
        #print self.getMetaData(name)
        return

    def checkDataset(self, data):
        """Check for old format or bad dataset"""

        if type(data) is types.DictType:  #old format
            x, y, a, xerr, yerr = EkinConvert.ekin2xy(data,
                                                      getall=1,
                                                      geterrors=True)
            return EkinDataset(x=x, y=y, active=a, xerrs=xerr, yerrs=yerr)
        elif not type(data) is EkinDataset:
            return None
        else:
            return data

    def checkDatasets(self):
        """Check corrupt datasets and remove"""
        #print 'checking for old or bad datasets'
        for d in self.datasets[:]:
            ek = self.checkDataset(self.data[d])
            if ek == False:
                print 'removed', d
                self.deleteDataset(d)
            else:
                other = self.checkDict(self.data[d], d)
                self.data[d] = ek
        return

    def importCSV(self,
                  filename=None,
                  text=None,
                  format=1,
                  sep=',',
                  mode=None,
                  truncate='end'):
        """
        Import csv file(s) to ekin data.
        This method should work without user intervention as much as possible.
        There are three options:
            1. Single file, curves per row, with ph values at the top
            2. Single file, curves per column, with ph values in
            the first column and curve names in first row
            3. Each curve in its own file, with 2 cols
        """
        import csv

        if filename != None:
            csvfile = open(filename)
            reader = csv.reader(csvfile, delimiter=sep)

        elif text != None:
            reader = csv.reader(text.split(os.linesep))

        x = reader.next()
        pts = len(x)
        print 'found csv data with %s points in header row' % pts
        datasets = {}
        for row in reader:
            if len(row) == 0:
                continue
            name = row[0]
            del row[0]
            xdata = x
            if len(row) < pts:
                print 'dataset is short %s points' % (pts - len(row))
                if truncate == 'end':
                    xdata = x[0:len(row)]
                else:
                    st = pts - len(row)
                    xdata = x[st:len(x)]
            elif len(row) > pts:
                row = row[0:pts]
                print 'dataset has more than %s points, truncating' % pts

            #datasets[name] = EkinConvert.xy2ekin([xdata,row])
            datasets[name] = EkinDataset(xy=[xdata, row])
        for d in datasets.keys():
            self.insertDataset(datasets[d], d)
        print 'imported %s datasets' % len(datasets)
        print '-----------------------'
        return

    def exportCSV(self, datasets='ALL', filename=None, sep=','):
        """Export dataset(s) to csv"""
        import csv
        if filename != None:
            csvfile = open(filename, 'w')
        writer = csv.writer(csvfile, delimiter=sep)
        datasets = self.checkDatasetParam(datasets)

        for d in self.datasets:
            writer.writerow([d])
            ek = self.getDataset(d)
            x, y = ek.getxy()
            for i in range(len(x)):
                writer.writerow([x[i], y[i]])
        return

    def openProject(self, filename=None):
        """Open an ekin project file"""
        import os
        if filename == None:
            return
        if os.path.splitext(filename)[1] == '':
            filename = filename + '.ekinprj'
        if not os.path.exists(filename):
            #print 'no such file'
            return None
        data = {}
        if filename != None:
            if os.path.isfile(filename):
                fd = open(filename)
                import pickle
                data = pickle.load(fd)
                fd.close()

        self.loadData(data)
        self.filename = filename
        return

    def saveProject(self, filename=None):
        """Save data to an ekin project file"""
        if filename == None:
            if not hasattr(self, 'filename') or self.filename == None:
                print 'no filename'
                return
            else:
                filename = self.filename
        self.filename = filename
        if os.path.splitext(filename)[1] != '.ekinprj':
            filename = filename + '.ekinprj'
        data = self.prepare_data()
        fd = open(filename, 'w')
        import pickle
        pickle.dump(data, fd)
        fd.close()
        return

    def prepare_data(self):
        """Prepare data for saving or return to another app ie. PEAT"""

        data = copy.deepcopy(self.data)
        data = self.data
        #print self.__meta_data__
        for name in self.ekinfields:
            if hasattr(self, name):
                data[name] = copy.deepcopy(self.__dict__[name])
        data['mode'] = self.currentmode

        return data

    def printData(self):
        """Print out a summary of all the datasets"""
        for name in self.datasets:
            fitdata = self.__datatabs_fits__[name]
            if fitdata.has_key('model'):
                fit = fitdata['model']
            else:
                fit = 'Not fitted'
            axes = self.getAxesLabels(name)
            print '%s: %s pts, fit: %s, axes: %s' % (
                name, len(self.data[name][0].keys()), fit, axes)

    def prettyPrint(self):
        """prints the entire dict"""
        import pprint
        pp = pprint.PrettyPrinter(indent=4)
        x = pp.pformat(self.data)
        print x
        return

    def getFitData(self, dataset):
        """Get the fit data for the dataset"""
        if self.__datatabs_fits__.has_key(dataset):
            return self.__datatabs_fits__[dataset]
        else:
            return None

    def setFitData(self, dataset, fitdata=None):
        """Set the fit data for the dataset"""
        if fitdata != None:
            self.__datatabs_fits__[dataset] = fitdata
        else:
            self.__datatabs_fits__[dataset] = {}
        return fitdata

    def addMeta(self, dataset, fieldname, data):
        """Set a field for this dataset in the meta data dict"""
        if not hasattr(self, '__meta_data__'):
            self.__meta_data__ = {}
            self.__meta_data__[dataset] = {}
        if not self.__meta_data__.has_key(dataset):
            self.__meta_data__[dataset] = {}
        self.__meta_data__[dataset][fieldname] = data
        return

    def getMetaData(self, dataset):
        """Return the fit data in a formatted dict with the variable
           names as keys, more intelligible than the simple fit data.
           We also include the experimental meta data here, if present
           and all fields which are special keys e.g. __enzyme_conc__"""
        mdata = {}
        fitdata = self.__datatabs_fits__[dataset]
        if fitdata != None and len(fitdata) != 0:
            keys = ['model', 'error', 'comment']
            #use fitter object for this model to get fit param names
            X = Fitting.makeFitter(fitdata)
            if X != None:
                mdata = X.getFitDict()
            else:
                mdata = {}
            for k in keys:
                if fitdata.has_key(k):
                    mdata[k] = fitdata[k]

        if hasattr(self, '__meta_data__'):
            if self.__meta_data__.has_key(dataset):
                for k in self.__meta_data__[dataset].keys():
                    mdata[k] = self.__meta_data__[dataset][k]
        return mdata

    def getMeta(self, dataset, field):
        """Get a field from the meta data"""
        if self.__meta_data__.has_key(
                dataset) and self.__meta_data__[dataset].has_key(field):
            return self.__meta_data__[dataset][field]
        else:
            return None

    def setMetaData(self, meta):
        self.__meta_data__ = meta
        return

    def printMeta(self):
        """print current meta data"""
        if not hasattr(self, '__meta_data__'):
            print 'No Meta data'
            return
        M = MetaData(data=self.__meta_data__)
        M.prettyPrint()
        return

    def getField(self, dataset, field):
        """Some fields might be in the ekin dataset dict itself"""
        #we should really use meta_data instead in future!
        if self.data[dataset].has_key(field):
            return self.data[dataset][field]
        else:
            return None

    def fitDataset(self,
                   dataset,
                   model=None,
                   update=True,
                   noiter=300,
                   conv=None,
                   grad=None,
                   damper=1.0,
                   silent=False,
                   guess=True,
                   startvalues=None,
                   changevars=None,
                   callback=None):
        """Calculate the fit for this current dataset, if a model
           is given we use that instead of the current one.
           update=True means that the dataset fit info will be overwritten"""

        #check case of model, as our names are stupidly kept case sensitive
        for m in Fitting.fitterClasses:
            if model == m or model == m.lower():
                model = m
        datatofit = self.data[dataset]
        if model == None:
            currfitdata = self.getFitData(dataset)
            if currfitdata.has_key('model'):
                model = currfitdata['model']
            else:
                model = self.defaultmodel
        else:
            currfitdata = None

        fitresult, X = Fitting.doFit(ekindata=datatofit,
                                     fitdata=currfitdata,
                                     model=model,
                                     noiter=noiter,
                                     conv=conv,
                                     grad=grad,
                                     LM_damper=damper,
                                     silent=silent,
                                     guess=guess,
                                     startvalues=startvalues,
                                     changevars=changevars,
                                     callback=callback)

        if fitresult == None:
            print 'Fitter returned None..'
            return

        if update == True:
            self.__datatabs_fits__[dataset] = fitresult

        return fitresult, X

    def fitDatasets(self,
                    datasets='ALL',
                    update=True,
                    findbest=False,
                    models=None,
                    noiter=300,
                    conv=1e-6,
                    grad=1e-6,
                    silent=False,
                    callback=None):
        """Fit multiple datasets in this project, if no datasets are given, we
           fit all of them.
           If findbest=True, finds the best fit model using f-testing for the given
           dataset.
           models is optional and provides a list of models to try for best fitting
        """
        datasets = self.checkDatasetParam(datasets)
        print 'fitting %s datasets..' % len(datasets)
        if findbest == True:
            for name in datasets:
                self.findBestModel(name, update=update, models=models)
        else:
            for name in datasets:
                self.fitDataset(name,
                                update=update,
                                model=models[0],
                                noiter=noiter,
                                conv=conv,
                                grad=grad,
                                silent=silent)
                if callback != None:
                    callback()
        return

    def findBestModel(self,
                      dataset,
                      update=True,
                      models=None,
                      mode=None,
                      checkfunc=None,
                      conv=None,
                      grad=None,
                      alpha=0.01,
                      silent=False):
        """Finds the best fit model using f-testing"""

        thisdata = self.data[dataset]
        #print 'models', models
        if models == None:
            models = copy.deepcopy(self.mode_definition[self.currentmode])

        fitdata, p = Fitting.findBestModel(thisdata,
                                           models,
                                           checkfunc=checkfunc,
                                           conv=conv,
                                           grad=grad,
                                           alpha=alpha,
                                           silent=silent)

        if update == True:
            if fitdata == None:
                self.setFitData(dataset)
            else:
                self.setFitData(dataset, fitdata)
        return fitdata, p

    def estimateExpUncertainty(self,
                               dataset,
                               xuncert=0.01,
                               yuncert=0.01,
                               runs=10,
                               doplots=False,
                               conv=1e-6,
                               grad=1e-6,
                               guess=True):
        """Chresten's method for estimating the error on each parameter due
           to the effect of the exp uncertainty, which is user provided """

        data = self.data[dataset]
        fitdata = self.__datatabs_fits__[dataset]
        #print 'estimating exp uncertainty for %s' %dataset
        fitstats = Fitting.estimateExpUncertainty(data,
                                                  fitdata,
                                                  xuncert=xuncert,
                                                  yuncert=yuncert,
                                                  runs=runs,
                                                  doplots=doplots,
                                                  conv=conv,
                                                  grad=grad,
                                                  guess=guess)

        return fitstats

    def setPylab(self, size=(6, 4)):
        """Setup plotting variables if pylab available"""
        try:
            fig = plt.figure()
        except:
            return False
        return True

    def doPlot(self, ek, fitdata, options):
        """Need a bare bones plotter that can plot any given ekin data
           Should then call this one in plotDatasets below and simplify"""
        x, y, a, xerr, yerr = ek.getAll()
        line = ax.scatter(x, y, marker='o')
        #plotting fit should also be seperate?
        f = fitdata
        d = []
        X = Fitting.makeFitter(f, zip(x, y))
        if X == None: return
        #we now use this model to draw the fit line
        fitclr = lineclr
        if plotoption != 3:
            fitclr = 'r'
        xx, fity = self.updateFit(X, ax, x, clr=fitclr, plotoption=plotoption)
        return

    def getGeometry(self, datasets, cols):
        if cols == 0 or cols == '':
            cols = int(math.ceil(math.sqrt(len(datasets))))
        if len(datasets) == 2:
            cols = 1
            dim = 2
        else:
            dim = int(math.ceil(len(datasets) / float(cols)))
        return cols, dim

    def plotDatasets(self,
                     datasets='ALL',
                     data=None,
                     fitdata=None,
                     filename=None,
                     plotoption=1,
                     cols=0,
                     size=(6, 4),
                     linecolor=None,
                     figure=None,
                     axis=None,
                     showfitvars=False,
                     dpi=80,
                     **kwargs):
        """Plot a dataset or list of datasets, if none given all are  plotted.
           plotoptions:
           1 - each dataset in one plot, different figures/images
           2 - each dataset in different axes but same figure - one image
           3 - all datasets in one plot, single figure
           Note: errorbars are drawn +/- value supplied"""

        status = self.setPylab(size)
        prms = Params(**kwargs)
        self.current = datasets
        shapes = Options.shapes

        plt.rc('font', family=prms.font)
        plt.rc('font', size=prms.fontsize)
        plt.rc('text', usetex=prms.usetex)

        if plotoption == 2 and cols >= 3:
            size = (3, 4)
        if status == False:
            print 'sorry, install pylab..'
            return
        if figure != None:
            fig = figure
        else:
            fig = plt.figure(figsize=size, dpi=80)
        #fig.clf()
        datasets = self.checkDatasetParam(datasets)
        if plotoption == 2:
            cols, dim = self.getGeometry(datasets, cols)
            n = 1
            plt.rc('font', size=9.0)
            if prms.title != None:
                fig.suptitle(prms.title, fontsize=prms.fontsize)
        else:
            if axis != None:
                ax = axis
            else:
                ax = fig.add_subplot(111)
            ax.cla()
            self.ax = ax

        legendlines = []
        a = numpy.arange(-4, 4, 0.5).tolist()
        #get ekin data into plot values
        xdata = {}
        ydata = {}
        adata = {}
        xerrors = {}
        yerrors = {}

        for name in datasets:
            self.fitline = None
            ek = self.data[name]
            xd, yd = xdata[name], ydata[name] = ek.getxy()
            adata[name] = ek.active
            xerrors[name], yerrors[name] = ek.getErrors()
            #if no error stored for points, try using global error if present
            if xerrors[name] == None and xerror != None:
                xerrors[name] = []
                for i in range(len(xd)):
                    xerrors[name].append(xerror)
            if yerrors[name] == None and yerror != None:
                yerrors[name] = []
                for i in range(len(yd)):
                    yerrors[name].append(yerror)

            if prms.xlabel != '':
                xlabel = prms.xlabel
            else:
                xlabel = ek.labels[0]
            if prms.ylabel != '':
                ylabel = prms.ylabel
            else:
                ylabel = ek.labels[1]

        i = 0
        cc = 0
        for name in datasets:
            if cc >= len(prms.colors):
                cc = 0
            if prms.varyshapes == True:
                if prms.markers != None and len(prms.markers) > cc:
                    prms.marker = prms.markers[cc]
                else:
                    prms.marker = shapes[cc]
            if plotoption == 2:
                ax = fig.add_subplot(int(dim), int(cols), n)
                ax.set_title(name, fontsize=prms.fontsize, fontweight='bold')
                if cols < 4:
                    ax.set_xlabel(xlabel, family=prms.font)
                    ax.set_ylabel(ylabel, family=prms.font)
                if prms.grid == True:
                    ax.grid(True)
                n = n + 1
                self.ax = ax

            x = xdata[name]
            y = ydata[name]
            act = adata[name]
            xerr = xerrors[name]
            yerr = yerrors[name]
            self.currxdata = x
            self.currydata = y
            if len(x) == 0:
                continue
            if prms.normalise == True:
                self.mindata = min(y)
                self.maxdata = max(y)
                y = normList(y, inputmin=self.mindata, inputmax=self.maxdata)
            if prms.graphtype == 'XY':
                if prms.logx == True and prms.logy == False:
                    line, = ax.semilogx(x, y, prms.marker, alpha=prms.alpha)
                elif prms.logy == True and prms.logx == False:
                    line, = ax.semilogy(x, y, prms.marker, alpha=prms.alpha)
                elif prms.logx == True and prms.logy == True:
                    line, = ax.loglog(x, y, prms.marker, alpha=prms.alpha)
                elif prms.marker in ['-', '--', ':', '.', ':.', ',', '|', '*']:
                    line, = ax.plot(x,
                                    y,
                                    prms.marker,
                                    linewidth=prms.linewidth,
                                    mew=prms.linewidth,
                                    alpha=prms.alpha,
                                    ms=prms.markersize)
                else:
                    ptcolors = []
                    if prms.grayscale == True:
                        actclr = '0.5'
                    else:
                        actclr = prms.colors[cc]
                    for i in range(len(x)):
                        if act[i] == 0:
                            ptcolors.append('0.8')
                        else:
                            ptcolors.append(actclr)

                    line = ax.scatter(x,
                                      y,
                                      marker=prms.marker,
                                      c=ptcolors,
                                      s=prms.markersize,
                                      lw=prms.linewidth,
                                      alpha=prms.alpha)
                cc += 1

                lineclr = self._getlineColor(line)
                if prms.showerrorbars == True and (yerr != None
                                                   or xerr != None):
                    #print xerr,yerr
                    errline = ax.errorbar(x,
                                          y,
                                          xerr=xerr,
                                          yerr=yerr,
                                          fmt=None,
                                          elinewidth=1.0,
                                          ecolor=lineclr,
                                          alpha=prms.alpha)
            elif prms.graphtype == 'bar':
                bars = ax.bar(x, y, linewidth=prms.linewidth, alpha=prms.alpha)
                line = bars[0]
                lineclr = line.get_facecolor()

            if len(xd) > 0:
                if abs(min(yd)) > 0.01 and max(yd) < 1e4:
                    major_formatter = plt.FormatStrFormatter('%2.2f')
                    ax.yaxis.set_major_formatter(major_formatter)

            legendlines.append(line)
            X = None
            if self.__datatabs_fits__.has_key(name):
                f = self.__datatabs_fits__[name]
                if f.has_key('model'):
                    #create the required fitter
                    d = []
                    X = Fitting.makeFitter(f, zip(x, y))
                    if X == None:
                        continue
                    #we now use this model to draw the fit line
                    fitclr = lineclr
                    if plotoption != 3:
                        fitclr = 'r'
                    if prms.grayscale == True:
                        fitclr = '0.4'
                    xx, fity = self.updateFit(X,
                                              ax,
                                              xdata[name],
                                              clr=fitclr,
                                              normalise=prms.normalise,
                                              plotoption=plotoption)

            if prms.xlim != 0:
                l = min(x) - prms.xlim
                u = max(y) + prms.xlim
                ax.set_xlim((l, u))
                #print l,u
            if prms.ylim != 0:
                l = min(y) - prms.ylim
                u = max(y) + prms.ylim
                ax.set_ylim((l, u))

        if plotoption != 2:
            if prms.title == None or prms.title == '':
                prms.title = name
            ax.set_title(prms.title, family=prms.font)
            ax.set_xlabel(xlabel, family=prms.font)
            ax.set_ylabel(ylabel, family=prms.font)

            if showfitvars == True:
                self.showfitResults(X, ax)
            if prms.grid == True:
                ax.grid(True)
            if prms.legend == True:
                l = ax.legend(legendlines,
                              datasets,
                              numpoints=1,
                              loc=prms.legendloc,
                              prop=FontProperties(size="smaller"))
                l.get_frame().set_alpha(0.8)
        else:
            fig.subplots_adjust(hspace=0.4, wspace=0.4)

        if filename != None:
            fig.savefig(filename, dpi=dpi)
            plt.close(fig)

        return ax

    def updateFit(self,
                  X,
                  ax=None,
                  xdata=None,
                  clr=None,
                  normalise=None,
                  plotoption=1,
                  showfitvars=False):
        """Get the fit data line for given fitter object and redraw"""
        if xdata == None:
            xdata = self.currxdata
        if ax == None:
            ax = self.ax
        if clr == None:
            clr = 'r'
        e = []
        for i in xdata:
            e.append(i)
        fity = []
        inc = (max(e) - min(e)) / 50
        xx = numpy.arange(min(e) - inc, max(e) + inc, inc).tolist()
        for i in xx:
            fity.append(X.evaluate(i))
        if normalise == True:
            fity = normList(fity, inputmin=self.mindata, inputmax=self.maxdata)
        if clr == None:
            clr = line.get_color()
        if self.fitline == None or plotoption != 1:
            self.fitline, = ax.plot(xx, fity, clr, linewidth=2.5, alpha=0.8)
        else:
            self.fitline.set_data(xx, fity)

        if showfitvars == True:
            self.showfitResults(X, ax)
        return xx, fity

    def _getlineColor(self, line):
        """Private method - get a lines' color"""
        clr = '#cccccc'
        try:
            clr = line.get_color()
        except:
            i = 0
            while clr == '#cccccc':
                clr = tuple(line.get_facecolor()[i][:3])
                clr = matplotlib.colors.rgb2hex(clr)
                i += 1
        return clr

    def showfitResults(self, X, ax):
        """Show fit vars in a plot"""
        if X == None:
            return
        info = X.getResult()
        text = ''
        bboxprops = {
            'facecolor': 'lightyellow',
            'alpha': 0.9,
            'pad': 10,
            'lw': 1
        }
        for i in X.varnames:
            text += i + '=' + str(round(info[i], 3)) + '\n'
        text += 'sse=' + str(round(info['error'], 3)) + '\n'
        text += 'rmse=' + str(round(info['rmse'], 3))
        if len(ax.texts) >= 1:
            ax.texts[0].set_text(text)
        else:
            ax.text(.1,
                    .7,
                    text,
                    transform=ax.transAxes,
                    size=12,
                    bbox=bboxprops)
        plt.draw()
        return

    def updatePlot(self):
        """Update current plot points"""
        curr = self.current
        if type(curr) is not types.StringType:
            return
        #data =
        l = self.ax.get_lines()
        #print l
        return

    def checkDatasetParam(self, datasets):
        """Check dataset parameter to see if it's a list or not, allows
           methods to be called with dataset as a string or list"""
        if datasets == 'ALL':
            return self.datasets
        else:
            import types
            if type(datasets) is types.StringType:
                s = datasets
                datasets = []
                datasets.append(s)
                return datasets
            elif type(datasets) is types.ListType:
                return datasets
            else:
                print 'unknown format for dataset names, provide a list'
                return None

    def __repr__(self):
        """Overload string repr of class"""
        l = len(self.datasets)
        return "Ekin project containing %s datasets" % l

    def __getitem__(self, key):
        return self.__dict__[key]

    def __setitem__(self, key, item):
        self.__dict__[key] = item

    def getFields(self):
        return self.__dict__.keys()

    def addProject(self, E, overwrite=False, label=''):
        """Combine another project with the current one, duplicates are renamed"""

        for d in E.datasets:
            edata = copy.deepcopy(E.getDataset(d))
            fitdata = E.getFitData(d)
            if label != '':
                d = str(d) + '_' + str(label)
            if d in self.datasets:
                name = str(d) + '_a'
            else:
                name = str(d)
            self.insertDataset(edata, name, fit=fitdata)
            for field in self.ekinfields:
                if not E.__dict__.has_key(field):
                    continue
                #if E.__dict__.has_key(field):
                #    print field, type(E.__dict__[field]), E.__dict__[field][name]
                if E.__dict__[field].has_key(name):
                    self.__dict__[field][name] = copy.deepcopy(
                        E.__dict__[field][name])
        return

    def createSampleData(self):

        #linear
        x = range(30)
        y = [i + numpy.random.normal(0, .6) for i in x]
        ek = EkinDataset(xy=[x, y])
        self.insertDataset(ek, 'testdata1')
        #sigmoid
        x = numpy.arange(0.1, 10, 0.1)
        y = [
            1 / (1 + math.exp((5 - i) / 0.8)) + numpy.random.normal(0, .02)
            for i in x
        ]
        ek = EkinDataset(xy=[x, y])
        self.insertDataset(ek, 'testdata2')
        #power law
        x = numpy.arange(0, 5, 0.1)
        y = [math.pow(i, 3) + numpy.random.normal(5) for i in x]
        ek = EkinDataset(xy=[x, y])
        self.insertDataset(ek, 'testdata3')
        #gaussian
        x = numpy.arange(0, 5, 0.1)
        y = [
            1 * math.exp(-(pow((i - 2), 2) / (pow(.5, 2)))) +
            numpy.random.normal(0, .02) for i in x
        ]
        ek = EkinDataset(xy=[x, y])
        self.insertDataset(ek, 'testdata4')
        #MM
        x = numpy.arange(0.1, 5, 0.2)
        y = [(2 * i) / (i + .6) + numpy.random.normal(0, .04) for i in x]
        ek = EkinDataset(xy=[x, y])
        self.insertDataset(ek, 'testdata5')
        return

    def exportDatasets(self, filename=None, format=None, append=False):
        """Export all datasets as csv
            formatted as x-y columns per dataset """
        if filename == None:
            return
        if os.path.splitext(filename) != '.csv':
            filename = os.path.splitext(filename)[0] + '.csv'
        if append == True:
            f = open(filename, 'a')
        else:
            f = open(filename, 'wb')
        import csv
        cw = csv.writer(f)
        for d in self.datasets:
            ek = self.getDataset(d)
            x, y = ek.getxy()
            cw.writerow([d])
            cw.writerows(sorted(zip(x, y)))
            cw.writerow('')
        return


#
# These methods are dataset based and could be handled by the ekindataset class
#

    def setAxesLabels(self, x, y):
        """Set axes labels for all datasets"""
        for name in self.datasets:
            self.data[name][0]['label'] = x
            self.data[name][1]['label'] = y
        return

    def getAxesLabels(self, dataset):
        """Get axes labels"""
        try:
            labels = (self.data[dataset][0]['label'],
                      self.data[dataset][1]['label'])
            return labels
        except:
            return None
示例#35
0
 def getCurrentFunction(self):
     currFunctionName = self.getCurrentFunctionName()
     import Fitting
     return Fitting.GetFunctionFromName(currFunctionName)