Exemplo n.º 1
0
    def test_pearson(self):
        p_x = np.array([0.0, 0.9, 1.8, 2.6, 3.3, 4.4, 5.2, 6.1, 6.5, 7.4])
        p_y = np.array([5.9, 5.4, 4.4, 4.6, 3.5, 3.7, 2.8, 2.8, 2.4, 1.5])
        p_sx = np.array([0.03, 0.03, 0.04, 0.035, 0.07, 0.11, 0.13, 0.22, 0.74, 1.0])
        p_sy = np.array([1.0, 0.74, 0.5, 0.35, 0.22, 0.22, 0.12, 0.12, 0.1, 0.04])

        p_dat = RealData(p_x, p_y, sx=p_sx, sy=p_sy)

        # Reverse the data to test invariance of results
        pr_dat = RealData(p_y, p_x, sx=p_sy, sy=p_sx)

        p_mod = Model(self.pearson_fcn, meta=dict(name="Uni-linear Fit"))

        p_odr = ODR(p_dat, p_mod, beta0=[1.0, 1.0])
        pr_odr = ODR(pr_dat, p_mod, beta0=[1.0, 1.0])

        out = p_odr.run()
        assert_array_almost_equal(out.beta, np.array([5.4767400299231674, -0.4796082367610305]))
        assert_array_almost_equal(out.sd_beta, np.array([0.3590121690702467, 0.0706291186037444]))
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[0.0854275622946333, -0.0161807025443155], [-0.0161807025443155, 0.003306337993922]]),
        )

        rout = pr_odr.run()
        assert_array_almost_equal(rout.beta, np.array([11.4192022410781231, -2.0850374506165474]))
        assert_array_almost_equal(rout.sd_beta, np.array([0.9820231665657161, 0.3070515616198911]))
        assert_array_almost_equal(
            rout.cov_beta,
            np.array([[0.6391799462548782, -0.1955657291119177], [-0.1955657291119177, 0.0624888159223392]]),
        )
Exemplo n.º 2
0
def graph(a,toc): #a:odr with xerrors or curvefit without? toc: number of the figure
    plt.figure(toc,figsize=(15,10))
    for tickLabel in plt.gca().get_xticklabels()+plt.gca().get_yticklabels():
        tickLabel.set_fontsize(15)
    plt.title("Number of coincidence counts in the peak of $Na^{22}$ wrt angle",fontsize=17)
    plt.xlabel(r'Angle $\theta$ (°)',fontsize=17)
    plt.ylabel('# coincidence events',fontsize=17)
    plt.scatter(Ld,res,label='\n Data with parameters'+\
               ' \n $16$ $cm$ source-detectors'+\
               ' \n $150$ $s/points$')
    if a==False:
        par, par_va = curve_fit(simplegaussian, Ld, res, p0=[70000, 180, 10,1000],sigma=err_res,absolute_sigma=True)
        chi2=round(sum(((res - simplegaussian(Ld,*par) )/ err_res) ** 2)/(len(Ld)-3),2)
        plt.plot(Lc,simplegaussian(Lc, *par),color='gold',label='Fit with '+r'$A\exp\{\frac{-(\theta-\mu)^2}{2\sigma^2}\}+Cst$'+\
                  ' \n $A =$%s'%int(par[0])+' $ \pm$ %s'%int(np.sqrt(np.diag(par_va)[0]))+' #'+\
                  ' \n $\mu =$ %s'%round(par[1],1)+' $\pm$ %s'%round(np.sqrt(np.diag(par_va)[1]),1)+'°'+\
                  ' \n $\sigma =$ %s'%round(par[2],1)+ '$\pm$ %s'%round(np.sqrt(np.diag(par_va)[2]),1)+'°'+\
                  ' \n $Cst=$ %s'%int(par[3])+' $\pm$ %s'%int(np.sqrt(np.diag(par_va)[3]))+' #'+\
                  ' \n $\chi^2/dof = $ %s'%chi2)
        plt.errorbar(Ld, res, err_res,fmt='.',label=r'$y=\sqrt{counts}$ '+\
                     '\n $x=0°$', color='black',ecolor='lightgray', elinewidth=3, capsize=0)

    else:
        data = RealData(Ld,res,err_resx,err_res)
        model = Model(simplegaussianl)

        odr = ODR(data, model, [73021, 183, 11,1208])
        odr.set_job(fit_type=2)
        output = odr.run()
        
        xn = Lc
        yn = simplegaussianl(output.beta, xn)
        
        #pl.hold(True)
        #plot(Ld,res,'ro')
        #print(x,y)
        plt.plot(xn,yn,'k-',label=' ODR leastsq fit'+\
            ' \n $\chi^2/dof = $ %s'%round(output.sum_square/(len(Ld)-3),2)+'\n')
        
        odr.set_job(fit_type=0)
        output = odr.run()
        par,par_va=output.beta,output.cov_beta
        yn = simplegaussianl(output.beta, xn)
        plt.plot(xn,yn,color='gold',label='ODR fit '+r'$A\exp\{\frac{-(\theta-\mu)^2}{2\sigma^2}\}+Cst$'+\
          ' \n $A =$%s'%int(par[0])+' $ \pm$ %s'%int(np.sqrt(np.diag(par_va)[0]))+' #'+\
          ' \n $\mu =$ %s'%round(par[1],1)+' $\pm$ %s'%round(np.sqrt(np.diag(par_va)[1]),1)+'°'+\
          ' \n $\sigma =$ %s'%round(par[2],1)+ '$\pm$ %s'%round(np.sqrt(np.diag(par_va)[2]),1)+'°'+\
          ' \n $Cst=$ %s'%int(par[3])+' $\pm$ %s'%int(np.sqrt(np.diag(par_va)[3]))+' #'+\
          ' \n Sum of squares/dof $= $ %s'%round(output.sum_square/(len(Ld)-3),2))
        plt.legend(loc=0)
        plt.errorbar(Ld, res, err_res,err_resx,label=r'$y=\sqrt{counts}$ '+\
                     '\n $x=$%s'%errx+'°',fmt='.', color='black',ecolor='lightgray', elinewidth=3, capsize=0)


    plt.gca().set_xlim(140,220)
    plt.legend(bbox_to_anchor=(0.68, 0.58), loc=1, borderaxespad=0.,prop={'size':14})
    plt.yscale('log')
    plt.ylim(1e2,1e5)
Exemplo n.º 3
0
    def test_ifixx(self):
        x1 = [-2.01, -0.99, -0.001, 1.02, 1.98]
        x2 = [3.98, 1.01, 0.001, 0.998, 4.01]
        fix = np.vstack((np.zeros_like(x1, dtype=int), np.ones_like(x2, dtype=int)))
        data = Data(np.vstack((x1, x2)), y=1, fix=fix)
        model = Model(lambda beta, x: x[1, :] - beta[0] * x[0, :]**2., implicit=True)

        odr1 = ODR(data, model, beta0=np.array([1.]))
        sol1 = odr1.run()
        odr2 = ODR(data, model, beta0=np.array([1.]), ifixx=fix)
        sol2 = odr2.run()
        assert_equal(sol1.beta, sol2.beta)
Exemplo n.º 4
0
    def test_ifixx(self):
        x1 = [-2.01, -0.99, -0.001, 1.02, 1.98]
        x2 = [3.98, 1.01, 0.001, 0.998, 4.01]
        fix = np.vstack((np.zeros_like(x1, dtype=int), np.ones_like(x2, dtype=int)))
        data = Data(np.vstack((x1, x2)), y=1, fix=fix)
        model = Model(lambda beta, x: x[1, :] - beta[0] * x[0, :]**2., implicit=True)

        odr1 = ODR(data, model, beta0=np.array([1.]))
        sol1 = odr1.run()
        odr2 = ODR(data, model, beta0=np.array([1.]), ifixx=fix)
        sol2 = odr2.run()
        assert_equal(sol1.beta, sol2.beta)
def chi2_iterative(k, k_nn, Foward=True):
    """
    Esta funcion agarra un eje X (k), un eje Y (k_nn) y busca los parametros para
    ajustar la mejor recta, buscando el regimen lineal de la curva. Esto lo hace
    sacando puntos de la curva, ajustando la curva resultante, y luego comparando 
    los parametros de los distintos ajustes, seleccionando el de menor chi2.
    
    Si Foward=True entonces la funcion va a ir sacando puntos del final para
    encontrar kmax. Si Foward=False, la funcion va a sacar puntos del principio para
    calcular kmin. El punto va a estar dado por k[index].
    
    Returns: m, b, chi2_stat, index
    
    m: pendiente de la recta resultante
    b: ordenada de la recta resultante
    chi2: estadistico de chi2 de la recta resultante
    index: indice del elemento donde empieza/termina el regimen lineal.
    .
    .
    """
    chi2_list = []
    m_list = []
    b_list = []
    if Foward == True:
        for j in range(0, len(k) - 3):
            k_nn_temp = k_nn[:len(k_nn) - j]
            k_temp = k[:len(k) - j]
            linear_model = Model(linear)
            data = RealData(k_temp, k_nn_temp)
            odr = ODR(data, linear_model, beta0=[0., 1.])
            out = odr.run()
            chi2_list.append(out.res_var)
            m_list.append(out.beta[0])
            b_list.append(out.beta[1])
    else:
        for j in range(0, len(k) - 3):
            k_nn_temp = k_nn[j:]
            k_temp = k[j:]
            linear_model = Model(linear)
            data = RealData(k_temp, k_nn_temp)
            odr = ODR(data, linear_model, beta0=[0., 1.])
            out = odr.run()
            chi2_list.append(out.res_var)
            m_list.append(out.beta[0])
            b_list.append(out.beta[1])
    #index = ClosestToOne(chi2_list)
    index = chi2_list.index(min(chi2_list))
    m = m_list[index]
    b = b_list[index]
    chi2 = chi2_list[index]

    return m, b, chi2, index
Exemplo n.º 6
0
 def do_bestfit(self):
     """
     do bestfit using scipy.odr
     """
     self.check_important_variables()
     x = np.array(self.args["x"])
     y = np.array(self.args["y"])
     if self.args.get("use_RealData", True):
         realdata_kwargs = self.args.get("RealData_kwargs", {})
         data = RealData(x, y, **realdata_kwargs)
     else:
         data_kwargs = self.args.get("Data_kwargs", {})
         data = Data(x, y, **data_kwargs)
     model = self.args.get("Model", None)
     if model is None:
         if "func" not in self.args.keys():
             raise KeyError("Need fitting function")
         model_kwargs = self.args.get("Model_kwargs", {})
         model = Model(self.args["func"], **model_kwargs)
     odr_kwargs = self.args.get("ODR_kwargs", {})
     odr = ODR(data, model, **odr_kwargs)
     self.output = odr.run()
     if self.args.get("pprint", False):
         self.output.pprint()
     self.fit_args = self.output.beta
     return self.fit_args
Exemplo n.º 7
0
 def roda(self, dados):
     x, ux = Variáveis[self.x], Variáveis[self.ux]
     y, uy = Variáveis[self.y], Variáveis[self.uy]
     if self.tipo == 'odr':
         data = RealData(x, y, ux, uy)
         odr = ODR(data, linear, beta0=[1, 0], ndigit=16, maxit=100)
         DadosAjuste = odr.run()
         p = DadosAjuste.beta
         u = sqrt(diag(DadosAjuste.cov_beta))
     elif self.tipo == 'cfit':
         p, cov = curve_fit(flinear,
                            x,
                            y,
                            sigma=uy,
                            absolute_sigma=True,
                            method='trf')
         u = sqrt(diag(cov))
     elif self.tipo == 'cfitse':
         p, cov = curve_fit(flinear, x, y, method='trf')
         u = sqrt(diag(cov))
     print(p, u)
     Ajustes[self.ref] = ({
         'a': p[0],
         'ua': u[0],
         'b': p[1],
         'ub': u[1]
     }, x, ux, y, uy)
Exemplo n.º 8
0
def fit_slope(X, Y, eX=None, eY=None):
    """Will fit a Y vs X with errors using the scipy
    ODR functionalities and a linear fitting function

    Args:
        X:
        Y: input arrays
        eX:
        eY: input uncertaintites [None]

    Returns:
        odr output
    """

    # First filtering the Nan
    good = ~np.isnan(X)
    if eX is None: eX_good = None
    else: eX_good = eX[good]
    if eY is None: eY_good = None
    else: eY_good = eY[good]

    linear = Model(linear_fit)
    odr_data = RealData(X[good], Y[good], sx=eX_good, sy=eY_good)
    odr_run = ODR(odr_data, linear, beta0=[2., 1.])
    return odr_run.run()
Exemplo n.º 9
0
    def test_explicit(self):
        explicit_mod = Model(
            self.explicit_fcn,
            fjacb=self.explicit_fjb,
            fjacd=self.explicit_fjd,
            meta=dict(name='Sample Explicit Model',
                      ref='ODRPACK UG, pg. 39'),
        )
        explicit_dat = Data([0.,0.,5.,7.,7.5,10.,16.,26.,30.,34.,34.5,100.],
                        [1265.,1263.6,1258.,1254.,1253.,1249.8,1237.,1218.,1220.6,
                         1213.8,1215.5,1212.])
        explicit_odr = ODR(explicit_dat, explicit_mod, beta0=[1500.0, -50.0, -0.1],
                       ifixx=[0,0,1,1,1,1,1,1,1,1,1,0])
        explicit_odr.set_job(deriv=2)
        explicit_odr.set_iprint(init=0, iter=0, final=0)

        out = explicit_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([1.2646548050648876e+03, -5.4018409956678255e+01,
                -8.7849712165253724e-02]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([1.0349270280543437, 1.583997785262061, 0.0063321988657267]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[4.4949592379003039e-01, -3.7421976890364739e-01,
                 -8.0978217468468912e-04],
               [-3.7421976890364739e-01, 1.0529686462751804e+00,
                 -1.9453521827942002e-03],
               [-8.0978217468468912e-04, -1.9453521827942002e-03,
                  1.6827336938454476e-05]]),
        )
Exemplo n.º 10
0
def ajuste_odr_u(x, y):
    data = RealData(unp.nominal_values(x), unp.nominal_values(y), sx=unp.std_devs(x), sy=unp.std_devs(y))
    odr = ODR(data, mlinear, beta0=[1., 1.], ndigit=20)
    ajuste = odr.run()
    a, b = ajuste.beta
    ua, ub = np.sqrt(np.diag(ajuste.cov_beta))
    return a, ua, b, ub
Exemplo n.º 11
0
def regress_odr(x, y, sx, sy, beta0=[0., 1.]):
    """Return an ODR linear fit
    """
    linear = Model(my_linear)
    mydata = RealData(x.ravel(), y.ravel(), sx=sx.ravel(), sy=sy.ravel())
    myodr = ODR(mydata, linear, beta0=beta0)
    return myodr.run()
def voltage_current_regression():
    # read data from datafile
    df = pd.read_table(os.path.join(os.path.dirname(__file__), DATA_PATH,
                                    '1_a_soneloid.dat'),
                       delim_whitespace=True,
                       names=['current', 'voltage_min', 'voltage_max'],
                       decimal=',',
                       comment='#')

    # add columns for voltage mean and error
    df['voltage_mean'] = 0.5 * (df['voltage_min'] + df['voltage_max'])
    df['voltage_error'] = df['voltage_max'] - df['voltage_min']

    # Create a model for fitting.
    linear_model = Model(regression_func)

    x = np.array(df['current'])
    y = np.array(df['voltage_mean'])
    sy = np.array(df['voltage_error'])

    data = RealData(x, y, sy=sy)

    odr = ODR(data, linear_model, beta0=[0., 1.])
    out = odr.run()

    return out
Exemplo n.º 13
0
def orthoregress(x, y):
    """Perform an Orthogonal Distance Regression on the given data,
    using the same interface as the standard scipy.stats.linregress function.
    Adapted from https://gist.github.com/robintw/d94eb527c44966fbc8b9#file-orthoregress-py
    
    Arguments:
    x: x data
    y: y data

    Returns:
    [slope, intercept, residual]

    Uses standard ordinary least squares to estimate the starting parameters
    then uses the scipy.odr interface to the ODRPACK Fortran code to do the
    orthogonal distance calculations.
    """
    def f(p, x):
        """Basic linear regression 'model' for use with ODR"""
        return (p[0] * x) + p[1]

    linreg = stats.linregress(x, y)
    mod = Model(f)
    dat = Data(x, y)
    od = ODR(dat, mod, beta0=linreg[0:2])
    out = od.run()

    return list(out.beta) + [out.res_var]
Exemplo n.º 14
0
def meanResiduals(pts):
    f = lambda B, x: B[0] * x + B[1]
    model = Model(f)
    data = RealData(list(p.x() for p in pts), list(p.y() for p in pts))
    odr = ODR(data, model, beta0=[0., 1.])
    out = odr.run()
    return out.sum_square / len(pts)
Exemplo n.º 15
0
 def test_quadratic_model(self):
     x = np.linspace(0.0, 5.0)
     y = 1.0 * x**2 + 2.0 * x + 3.0
     data = Data(x, y)
     odr_obj = ODR(data, quadratic)
     output = odr_obj.run()
     assert_array_almost_equal(output.beta, [1.0, 2.0, 3.0])
Exemplo n.º 16
0
    def test_explicit(self):
        explicit_mod = Model(
            self.explicit_fcn,
            fjacb=self.explicit_fjb,
            fjacd=self.explicit_fjd,
            meta=dict(name='Sample Explicit Model',
                      ref='ODRPACK UG, pg. 39'),
        )
        explicit_dat = Data([0.,0.,5.,7.,7.5,10.,16.,26.,30.,34.,34.5,100.],
                        [1265.,1263.6,1258.,1254.,1253.,1249.8,1237.,1218.,1220.6,
                         1213.8,1215.5,1212.])
        explicit_odr = ODR(explicit_dat, explicit_mod, beta0=[1500.0, -50.0, -0.1],
                       ifixx=[0,0,1,1,1,1,1,1,1,1,1,0])
        explicit_odr.set_job(deriv=2)
        explicit_odr.set_iprint(init=0, iter=0, final=0)

        out = explicit_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([1.2646548050648876e+03, -5.4018409956678255e+01,
                -8.7849712165253724e-02]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([1.0349270280543437, 1.583997785262061, 0.0063321988657267]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[4.4949592379003039e-01, -3.7421976890364739e-01,
                 -8.0978217468468912e-04],
               [-3.7421976890364739e-01, 1.0529686462751804e+00,
                 -1.9453521827942002e-03],
               [-8.0978217468468912e-04, -1.9453521827942002e-03,
                  1.6827336938454476e-05]]),
        )
Exemplo n.º 17
0
 def test_multilinear_model(self):
     x = np.linspace(0.0, 5.0)
     y = 10.0 + 5.0 * x
     data = Data(x, y)
     odr_obj = ODR(data, multilinear)
     output = odr_obj.run()
     assert_array_almost_equal(output.beta, [10.0, 5.0])
Exemplo n.º 18
0
 def test_exponential_model(self):
     x = np.linspace(0.0, 5.0)
     y = -10.0 + np.exp(0.5 * x)
     data = Data(x, y)
     odr_obj = ODR(data, exponential)
     output = odr_obj.run()
     assert_array_almost_equal(output.beta, [-10.0, 0.5])
Exemplo n.º 19
0
    def find_c13(self):
        '''
        Find the c13 parameter by fitting a curve to 
        the measured group velocities using the scipy
        ODR library. Since c13 is par tof the equation
        for the curve, the c13 value corresponding to
        the best fit curve is the result of the inversion.
        '''

        self.c13_max = np.sqrt(
            self.c33 *
            (self.c11))  #The most physically reasonable maximum for c13
        self.c13_min = np.sqrt(self.c33 * (self.c11 - 2 * self.c66) +
                               self.c66**2) - self.c66
        self.c13_0 = self.c13_min * 1.2

        model = Model(self.forward_model,
                      estimate=[self.c13_0, self.theta0, self.c11, self.c33])
        data = RealData(self.group_angles,
                        self.group_vels,
                        sy=self.group_vel_err)
        fit = ODR(data,
                  model,
                  beta0=[self.c13_0, self.theta0, self.c11, self.c33],
                  ifixb=[1, 1, 1, 1])
        output = fit.run()

        best_fit = output.beta
        errors = output.sd_beta

        self.c13 = best_fit[0]
        self.c13_err = errors[0]
        self.theta0 = best_fit[1]
Exemplo n.º 20
0
 def test_unilinear_model(self):
     x = np.linspace(0.0, 5.0)
     y = 1.0 * x + 2.0
     data = Data(x, y)
     odr_obj = ODR(data, unilinear)
     output = odr_obj.run()
     assert_array_almost_equal(output.beta, [1.0, 2.0])
Exemplo n.º 21
0
    def do_the_fit(obs, **kwargs):

        global print_output, beta0

        func = kwargs.get('function')
        yerr = kwargs.get('yerr')
        length = len(yerr)

        xerr = kwargs.get('xerr')

        if length == len(obs):
            assert 'x_constants' in kwargs
            data = RealData(kwargs.get('x_constants'), obs, sy=yerr)
            fit_type = 2
        elif length == len(obs) // 2:
            data = RealData(obs[:length], obs[length:], sx=xerr, sy=yerr)
            fit_type = 0
        else:
            raise Exception('x and y do not fit together.')

        model = Model(func)

        odr = ODR(data, model, beta0, partol=np.finfo(np.float64).eps)
        odr.set_job(fit_type=fit_type, deriv=1)
        output = odr.run()
        if print_output and not silent:
            print(*output.stopreason)
            print('chisquare/d.o.f.:', output.res_var)
            print_output = 0
        beta0 = output.beta
        return output.beta[kwargs.get('n')]
Exemplo n.º 22
0
 def test_polynomial_model(self):
     x = np.linspace(0.0, 5.0)
     y = 1.0 + 2.0 * x + 3.0 * x**2 + 4.0 * x**3
     poly_model = polynomial(3)
     data = Data(x, y)
     odr_obj = ODR(data, poly_model)
     output = odr_obj.run()
     assert_array_almost_equal(output.beta, [1.0, 2.0, 3.0, 4.0])
Exemplo n.º 23
0
def ajuste_odr(x, y, ux, uy):
    data = RealData(x, y, sx=ux, sy=uy)
    odr = ODR(data, mlinear, beta0=[-1000., 1.], ndigit=20)
    ajuste = odr.run()
    a, b = ajuste.beta
    ua, ub = np.sqrt(np.diag(ajuste.cov_beta))
    ajuste.pprint()
    return a, ua, b, ub
Exemplo n.º 24
0
    def test_pearson(self):
        p_x = np.array([0., .9, 1.8, 2.6, 3.3, 4.4, 5.2, 6.1, 6.5, 7.4])
        p_y = np.array([5.9, 5.4, 4.4, 4.6, 3.5, 3.7, 2.8, 2.8, 2.4, 1.5])
        p_sx = np.array([.03, .03, .04, .035, .07, .11, .13, .22, .74, 1.])
        p_sy = np.array([1., .74, .5, .35, .22, .22, .12, .12, .1, .04])

        p_dat = RealData(p_x, p_y, sx=p_sx, sy=p_sy)

        # Reverse the data to test invariance of results
        pr_dat = RealData(p_y, p_x, sx=p_sy, sy=p_sx)

        p_mod = Model(self.pearson_fcn, meta=dict(name='Uni-linear Fit'))

        p_odr = ODR(p_dat, p_mod, beta0=[1., 1.])
        pr_odr = ODR(pr_dat, p_mod, beta0=[1., 1.])

        out = p_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([5.4767400299231674, -0.4796082367610305]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([0.3590121690702467, 0.0706291186037444]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[0.0854275622946333, -0.0161807025443155],
                      [-0.0161807025443155, 0.003306337993922]]),
        )

        rout = pr_odr.run()
        assert_array_almost_equal(
            rout.beta,
            np.array([11.4192022410781231, -2.0850374506165474]),
        )
        assert_array_almost_equal(
            rout.sd_beta,
            np.array([0.9820231665657161, 0.3070515616198911]),
        )
        assert_array_almost_equal(
            rout.cov_beta,
            np.array([[0.6391799462548782, -0.1955657291119177],
                      [-0.1955657291119177, 0.0624888159223392]]),
        )
Exemplo n.º 25
0
def ortho_regress(x, y):
    linreg = linregress(x, y)
    mod = Model(f)
    dat = Data(x, y)
    od = ODR(dat, mod, beta0=linreg[0:2])
    out = od.run()
    #print(list(out.beta))
    #return list(out.beta) + [np.nan, np.nan, np.nan]
    return(list(out.beta))
Exemplo n.º 26
0
    def orthoregress(x, y, xerr, yerr):

        linreg = linregress(x, y)
        mod = Model(f)

        dat = RealData(x, y, sx=xerr, sy=yerr)

        od = ODR(dat, mod, beta0=linreg[0:2])
        out = od.run()
        return list(out.beta)
Exemplo n.º 27
0
def sigma(X, Y, X_err, Y_err):
    linear_model = Model(Linear)
    data = RealData(X, Y, sx=X_err, sy=Y_err)
    odr = ODR(data, linear_model, beta0=[0., 1.])
    out = odr.run()

    m = out.beta[0]
    b = out.beta[1]
    recta = [(I[i] - (V[i] * m + b)) for i in range(len(I))]
    return len(recta), f.dispersion(recta)
Exemplo n.º 28
0
    def fit(self, x, y):
        # Initial estimate of betas
        linreg = linregress(x, y)

        linear = Model(self.model)
        mydata = Data(x, y)
        myodr = ODR(mydata, linear, beta0=linreg[0:2])
        myoutput = myodr.run()

        self.betas = myoutput.beta
Exemplo n.º 29
0
def orthogonal_distance_regression(func, data, err, beta0=None):
    """Fit the function using scipy.odr and return fit output."""
    if not beta0:
        beta0 = _start_values(data)
    model = Model(func)
    rdata = _real_data(data, err)
    fit = ODR(rdata, model, beta0=beta0)
    output = fit.run()
    print(output.beta)
    return output
 def orth_regression(model,obs):
     linear = Model(f)
     mydata = RealData(obs, model)
     myodr = ODR(mydata, linear, beta0=[1., 0.])
     myoutput = myodr.run()
     params = myoutput.beta
     gradient = params[0]
     y_intercept = params[1]
     res_var = myoutput.res_var
     return np.around(gradient,2), np.around(y_intercept,2), np.around(res_var,2)
Exemplo n.º 31
0
    def test_lorentz(self):
        l_sy = np.array([.29] * 18)
        l_sx = np.array([
            .000972971, .000948268, .000707632, .000706679, .000706074,
            .000703918, .000698955, .000456856, .000455207, .000662717,
            .000654619, .000652694, .000000859202, .00106589, .00106378,
            .00125483, .00140818, .00241839
        ])

        l_dat = RealData(
            [
                3.9094, 3.85945, 3.84976, 3.84716, 3.84551, 3.83964, 3.82608,
                3.78847, 3.78163, 3.72558, 3.70274, 3.6973, 3.67373, 3.65982,
                3.6562, 3.62498, 3.55525, 3.41886
            ],
            [
                652, 910.5, 984, 1000, 1007.5, 1053, 1160.5, 1409.5, 1430,
                1122, 957.5, 920, 777.5, 709.5, 698, 578.5, 418.5, 275.5
            ],
            sx=l_sx,
            sy=l_sy,
        )
        l_mod = Model(self.lorentz, meta=dict(name='Lorentz Peak'))
        l_odr = ODR(l_dat, l_mod, beta0=(1000., .1, 3.8))

        out = l_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([
                1.4306780846149925e+03, 1.3390509034538309e-01,
                3.7798193600109009e+00
            ]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([
                7.3621186811330963e-01, 3.5068899941471650e-04,
                2.4451209281408992e-04
            ]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[
                2.4714409064597873e-01, -6.9067261911110836e-05,
                -3.1236953270424990e-05
            ],
                      [
                          -6.9067261911110836e-05, 5.6077531517333009e-08,
                          3.6133261832722601e-08
                      ],
                      [
                          -3.1236953270424990e-05, 3.6133261832722601e-08,
                          2.7261220025171730e-08
                      ]]),
        )
Exemplo n.º 32
0
def ODR_fitting(xdata, ydata, fitfunction, beta, fix):
    bpl_all = Model(fitfunction)
    data_all = RealData(xdata,
                        ydata,
                        sx=np.cov([xdata, ydata])[0][1],
                        sy=np.cov([xdata, ydata])[0][1])
    odr_all = ODR(data_all, bpl_all, beta0=beta, ifixb=fix)
    odr_all.set_job(fit_type=0)
    output_all = odr_all.run()
    #output_all.pprint()
    return (output_all.beta, output_all.sd_beta)
Exemplo n.º 33
0
def orth_regression(obs, model):
    linear = Model(f)
    mydata = RealData(obs, model)
    myodr = ODR(mydata, linear, beta0=[1., 0.])
    myoutput = myodr.run()
    params = myoutput.beta
    gradient = params[0]
    y_intercept = params[1]
    res_var = myoutput.res_var
    return np.around(gradient, 2), np.around(y_intercept,
                                             2), np.around(res_var, 2)
Exemplo n.º 34
0
    def _run_odr(self):
        """Run an ODR regression"""
        linear = Model(self._modelODR)
        mydata = Data(ravel(self._datax), ravel(self._datay), 1)
        myodr = ODR(mydata, linear, beta0=self._guess, maxit=10000)

        myoutput = myodr.run()

        self._result = myoutput.beta
        self._stdev = myoutput.sd_beta
        self._covar = myoutput.cov_beta
        self._odr = myoutput
Exemplo n.º 35
0
def odrlin(x, y, sx, sy):
    """
    Linear fit of 2-D data set made with Orthogonal Distance Regression
    @params x, y: data to fit
    @param sx, sy: respective errors of data to fit
    """
    model = models.unilinear  # defines model as beta[0]*x + beta[1]
    data = RealData(x, y, sx=sx, sy=sy)
    kinit = (y[-1] - y[0]) / (x[-1] - x[0])
    init = (kinit, y[0] - kinit * x[0])
    linodr = ODR(data, model, init)
    return linodr.run()
Exemplo n.º 36
0
    def _run_odr(self):
        """Run an ODR regression"""
        linear = Model(self._modelODR)
        mydata = Data(ravel(self._datax), ravel(self._datay), 1)
        myodr = ODR(mydata, linear, beta0=self._guess, maxit=10000)

        myoutput = myodr.run()

        self._result = myoutput.beta
        self._stdev = myoutput.sd_beta
        self._covar = myoutput.cov_beta
        self._odr = myoutput
Exemplo n.º 37
0
def odrlin(x,y, sx, sy):
    """
    Linear fit of 2-D data set made with Orthogonal Distance Regression
    @params x, y: data to fit
    @param sx, sy: respective errors of data to fit
    """
    model = models.unilinear  # defines model as beta[0]*x + beta[1]
    data = RealData(x,y,sx=sx,sy=sy)
    kinit = (y[-1]-y[0])/(x[-1]-x[0])
    init = (kinit, y[0]-kinit*x[0])
    linodr = ODR(data, model, init)
    return linodr.run()
Exemplo n.º 38
0
    def test_ticket_1253(self):
        def linear(c, x):
            return c[0]*x+c[1]

        c = [2.0, 3.0]
        x = np.linspace(0, 10)
        y = linear(c, x)

        model = Model(linear)
        data = Data(x, y, wd=1.0, we=1.0)
        job = ODR(data, model, beta0=[1.0, 1.0])
        result = job.run()
        assert_equal(result.info, 2)
Exemplo n.º 39
0
def slope(x, y, xerr, yerr, verbose=True, null=np.double(-9.99999488e+08)):
    """
    Calculates the slope of a color trajectory.

    Uses the scipy ODRpack and total least squares algorithm.

    Parameters
    ----------
    x, y : array-like
        Colors or magnitudes to calculate slopes of.
    xerr, yerr : array-like
        Corresponding errors (stdev) on `x` and `y`.
        Be sure to make sure that the HMKPNT errors are properly adjusted!
    verbose : bool. optional
        Whether to print a verbose output. Default true.

    Returns
    -------
    slope : float
        Slope (in rise/run) of the linear fit.
    intercept : float
        Y-value where the linear fit intercepts the Y-axis.
    slope_error : float
        The standard error on the fitted slope: an indication of fit quality.

    Notes
    -----
    Much of this code is borrowed from the dosctring example found
    at https://github.com/scipy/scipy/blob/master/scipy/odr/odrpack.py#L27 .
    
    See http://docs.scipy.org/doc/scipy/reference/odr.html and
    http://stackoverflow.com/questions/9376886/orthogonal-regression-fitting-in-scipy-least-squares-method
    for further discussion.

    """
    
    if (len(x) != len(y)) or len(x) == 0 or len(y) == 0:
        return null, null, null
    
    mydata = RealData(x, y, sx=xerr, sy=yerr)

    # Someday, we may want to improve the "initial guess" with 
    # a leastsq first-pass.
    myodr = ODR(mydata, linear, beta0=[1.,2.])

    myoutput = myodr.run()

    if verbose:
        print myoutput.pprint()

    return myoutput.beta[0], myoutput.beta[1], myoutput.sd_beta[0]
Exemplo n.º 40
0
    def fit_odr(self, t_scale, xmin=-sys.maxint, xmax=sys.maxint):
        # initial guess for the parameters
        params_initial = [1e-4, 1.0] #np.array([1e-4, 1.0]) #a, b
        # function to fit
        def f(B, x):
            return B[0]*pow(x/t_scale, -B[1])
        powerlaw = Model(f)

        def powerlaw_err(x, a, b, aerr, berr, cov):
            val = f([a,b], x)#f(x, a, b)
            err = np.sqrt(pow(aerr/a, 2)+pow(np.log(x/t_scale), 2)*cov)#*val
            return err

        xdata = []
        ydata = []
        yerr = []
        for ipoint, xpoint in enumerate(self.get_xdata()):
            if xpoint>=xmin and xpoint<xmax:
                xdata.append(xpoint)
                ydata.append(self.get_ydata()[ipoint])
                yerr.append(np.sqrt(self.get_yerr()[0][ipoint]*self.get_yerr()[1][ipoint]))
        xdata = np.array(xdata)
        ydata = np.array(ydata)
        yerr = np.array(yerr)
        if len(xdata)<3:
            logging.info('Only {0} data points. Fitting is skipped.'.format(len(xdata)))
            return 1
        mydata = RealData(x=xdata, y=ydata, sy=yerr)
        myodr = ODR(mydata, powerlaw, beta0=params_initial)
        myoutput = myodr.run()
        myoutput.pprint()
        params_optimal = myoutput.beta
        cov = myoutput.cov_beta
        params_err = myoutput.sd_beta

        #logging.info("""Opimized parameters: {0}
#Error: {1}""".format(params_optimal, params_err))
        #params_err = np.sqrt(np.diag(cov))
        for iparam, param, in enumerate(params_optimal):
            logging.info("""Parameter {0}: {1} +/- {2}""".format(iparam, params_optimal[iparam], params_err[iparam]))

        x_draw = np.linspace(min(xdata), max(xdata), 100)
        y_draw = np.zeros_like(x_draw)
        yerr_draw = np.zeros_like(y_draw)
        for ix, x in enumerate(x_draw):
            y_draw[ix] = f(params_optimal, x) #f(x, params_optimal[0], params_optimal[1])
            yerr_draw[ix] = powerlaw_err(x, params_optimal[0], params_optimal[1], params_err[0], params_err[1], cov[1][1])

        return ((params_optimal, params_err), (x_draw, y_draw, yerr_draw))
Exemplo n.º 41
0
def calculate_ortho_regression(x, y, samples):
    sx = numpy.cov(x, y)[0][0]
    sy = numpy.cov(x, y)[1][1]

    
    linear = Model(func)
    # data = Data(x, y, wd=1./pow(sx, 2), we=1./pow(sy, 2))
    data = Data(x, y)
    odr = ODR(data, linear, beta0=[1., 2.])
    out = odr.run()

    print '\n'
    out.pprint()

    return (out.beta[0], out.beta[1], out.res_var)
Exemplo n.º 42
0
def scipyODR(recipe, *args, **kwargs):
    from scipy.odr import Data, Model, ODR, RealData, odr_stop

    # FIXME
    # temporarily change _weights to _weights**2 to fit the ODR fits
    a = [w ** 2 for w in recipe._weights]
    recipe._weights = a

    model = Model(recipe.evaluateODR,
                  # implicit=1,
                  meta=dict(name='ODR fit'),
                  )
    x = [recipe._contributions.values()[0].profile.x]
    y = [recipe._contributions.values()[0].profile.y]
    dy = [recipe._contributions.values()[0].profile.dy]

    cont = recipe._contributions.values()
    for i in range(1, len(cont)):
        xplus = x[-1][-1] - x[-1][-2] + x[-1][-1]
        x.append(cont[i].profile.x + x[-1][-1] + xplus)
        y.append(cont[i].profile.y)
        dy.append(cont[i].profile.dy)
    x.append(np.arange(len(recipe._restraintlist))
             * (x[-1][-1] - x[-1][-2]) + x[-1][-1])
    y.append(np.zeros_like(recipe._restraintlist))
    dy = np.concatenate(dy)
    dy = np.concatenate(
        [dy, np.ones_like(recipe._restraintlist) + np.average(dy)])
    data = RealData(x=np.concatenate(x), y=np.concatenate(y), sy=dy)

    odr_kwargs = {}
    if kwargs.has_key('maxiter'):
        odr_kwargs['maxit'] = kwargs['maxiter']
    odr = ODR(data, model, beta0=recipe.getValues(), **odr_kwargs)
    odr.set_job(deriv=1)
    out = odr.run()
    # out.pprint()

    # FIXME
    # revert back
    a = [np.sqrt(w) for w in recipe._weights]
    recipe._weights = a

    return {'x': out.beta,
            'esd': out.sd_beta,
            'cov': out.cov_beta,
            'raw': out,
            }
Exemplo n.º 43
0
    def test_implicit(self):
        implicit_mod = Model(
            self.implicit_fcn,
            implicit=1,
            meta=dict(name='Sample Implicit Model',
                      ref='ODRPACK UG, pg. 49'),
        )
        implicit_dat = Data([
            [0.5,1.2,1.6,1.86,2.12,2.36,2.44,2.36,2.06,1.74,1.34,0.9,-0.28,
             -0.78,-1.36,-1.9,-2.5,-2.88,-3.18,-3.44],
            [-0.12,-0.6,-1.,-1.4,-2.54,-3.36,-4.,-4.75,-5.25,-5.64,-5.97,-6.32,
             -6.44,-6.44,-6.41,-6.25,-5.88,-5.5,-5.24,-4.86]],
            1,
        )
        implicit_odr = ODR(implicit_dat, implicit_mod,
            beta0=[-1.0, -3.0, 0.09, 0.02, 0.08])

        out = implicit_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([-0.9993809167281279, -2.9310484652026476, 0.0875730502693354,
                0.0162299708984738, 0.0797537982976416]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([0.1113840353364371, 0.1097673310686467, 0.0041060738314314,
                0.0027500347539902, 0.0034962501532468]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[2.1089274602333052e+00, -1.9437686411979040e+00,
                  7.0263550868344446e-02, -4.7175267373474862e-02,
                  5.2515575927380355e-02],
               [-1.9437686411979040e+00, 2.0481509222414456e+00,
                 -6.1600515853057307e-02, 4.6268827806232933e-02,
                 -5.8822307501391467e-02],
               [7.0263550868344446e-02, -6.1600515853057307e-02,
                  2.8659542561579308e-03, -1.4628662260014491e-03,
                  1.4528860663055824e-03],
               [-4.7175267373474862e-02, 4.6268827806232933e-02,
                 -1.4628662260014491e-03, 1.2855592885514335e-03,
                 -1.2692942951415293e-03],
               [5.2515575927380355e-02, -5.8822307501391467e-02,
                  1.4528860663055824e-03, -1.2692942951415293e-03,
                  2.0778813389755596e-03]]),
        )
Exemplo n.º 44
0
    def test_lorentz(self):
        l_sy = np.array([.29]*18)
        l_sx = np.array([.000972971,.000948268,.000707632,.000706679,
            .000706074, .000703918,.000698955,.000456856,
            .000455207,.000662717,.000654619,.000652694,
            .000000859202,.00106589,.00106378,.00125483, .00140818,.00241839])

        l_dat = RealData(
            [3.9094, 3.85945, 3.84976, 3.84716, 3.84551, 3.83964, 3.82608,
             3.78847, 3.78163, 3.72558, 3.70274, 3.6973, 3.67373, 3.65982,
             3.6562, 3.62498, 3.55525, 3.41886],
            [652, 910.5, 984, 1000, 1007.5, 1053, 1160.5, 1409.5, 1430, 1122,
             957.5, 920, 777.5, 709.5, 698, 578.5, 418.5, 275.5],
            sx=l_sx,
            sy=l_sy,
        )
        l_mod = Model(self.lorentz, meta=dict(name='Lorentz Peak'))
        l_odr = ODR(l_dat, l_mod, beta0=(1000., .1, 3.8))

        out = l_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([1.4306780846149925e+03, 1.3390509034538309e-01,
                 3.7798193600109009e+00]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([7.3621186811330963e-01, 3.5068899941471650e-04,
                 2.4451209281408992e-04]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[2.4714409064597873e-01, -6.9067261911110836e-05,
                 -3.1236953270424990e-05],
               [-6.9067261911110836e-05, 5.6077531517333009e-08,
                  3.6133261832722601e-08],
               [-3.1236953270424990e-05, 3.6133261832722601e-08,
                  2.7261220025171730e-08]]),
        )
def size_linewidth_slope(catalog):
    """
    Measures a catalog's slope of sizes and linewidths using scipy.odr.

    ODR means "orthogonal distance regression", and is a way of fitting
    models to data where both the x and y values have scatter.

    Parameters
    ----------
    catalog : astropy.table.table.Table
        Table containing columns for 'size' and 'v_rms'.

    Returns
    -------
    odr_output : scipy.odr.odrpack.Output
        Output of the scipy ODR routine. The fit parameters
        are stored in odr_output.beta .

    """

    if 'size' not in catalog.colnames or 'v_rms' not in catalog.colnames:
        raise ValueError("'size' and 'v_rms' must be columns in `catalog`!")

    if 'error_size_plus' not in catalog.colnames or 'error_size_minus' not in catalog.colnames:
        mean_size_error = None
    else:
        mean_size_error = (catalog['error_size_plus']*catalog['error_size_minus'])**(1/2)
        # if any clouds have no error in their size, the procedure will puke unless we do this:
        mean_size_error[mean_size_error==0] = 0.01

    size_linewidth_data = RealData(catalog['size'], catalog['v_rms'], sx=mean_size_error)

    powerlaw_model = Model(powerlaw_function)
    # The provided `beta0` is a throwaway guess that shouldn't change the outcome.
    odr_object = ODR(size_linewidth_data, powerlaw_model, beta0=[1., 1.])
    odr_output = odr_object.run()

    return odr_output
Exemplo n.º 46
0
    def test_multi(self):
        multi_mod = Model(
            self.multi_fcn,
            meta=dict(name='Sample Multi-Response Model',
                      ref='ODRPACK UG, pg. 56'),
        )

        multi_x = np.array([30.0, 50.0, 70.0, 100.0, 150.0, 200.0, 300.0, 500.0,
            700.0, 1000.0, 1500.0, 2000.0, 3000.0, 5000.0, 7000.0, 10000.0,
            15000.0, 20000.0, 30000.0, 50000.0, 70000.0, 100000.0, 150000.0])
        multi_y = np.array([
            [4.22, 4.167, 4.132, 4.038, 4.019, 3.956, 3.884, 3.784, 3.713,
             3.633, 3.54, 3.433, 3.358, 3.258, 3.193, 3.128, 3.059, 2.984,
             2.934, 2.876, 2.838, 2.798, 2.759],
            [0.136, 0.167, 0.188, 0.212, 0.236, 0.257, 0.276, 0.297, 0.309,
             0.311, 0.314, 0.311, 0.305, 0.289, 0.277, 0.255, 0.24, 0.218,
             0.202, 0.182, 0.168, 0.153, 0.139],
        ])
        n = len(multi_x)
        multi_we = np.zeros((2, 2, n), dtype=float)
        multi_ifixx = np.ones(n, dtype=int)
        multi_delta = np.zeros(n, dtype=float)

        multi_we[0,0,:] = 559.6
        multi_we[1,0,:] = multi_we[0,1,:] = -1634.0
        multi_we[1,1,:] = 8397.0

        for i in range(n):
            if multi_x[i] < 100.0:
                multi_ifixx[i] = 0
            elif multi_x[i] <= 150.0:
                pass  # defaults are fine
            elif multi_x[i] <= 1000.0:
                multi_delta[i] = 25.0
            elif multi_x[i] <= 10000.0:
                multi_delta[i] = 560.0
            elif multi_x[i] <= 100000.0:
                multi_delta[i] = 9500.0
            else:
                multi_delta[i] = 144000.0
            if multi_x[i] == 100.0 or multi_x[i] == 150.0:
                multi_we[:,:,i] = 0.0

        multi_dat = Data(multi_x, multi_y, wd=1e-4/np.power(multi_x, 2),
            we=multi_we)
        multi_odr = ODR(multi_dat, multi_mod, beta0=[4.,2.,7.,.4,.5],
            delta0=multi_delta, ifixx=multi_ifixx)
        multi_odr.set_job(deriv=1, del_init=1)

        out = multi_odr.run()
        assert_array_almost_equal(
            out.beta,
            np.array([4.3799880305938963, 2.4333057577497703, 8.0028845899503978,
                0.5101147161764654, 0.5173902330489161]),
        )
        assert_array_almost_equal(
            out.sd_beta,
            np.array([0.0130625231081944, 0.0130499785273277, 0.1167085962217757,
                0.0132642749596149, 0.0288529201353984]),
        )
        assert_array_almost_equal(
            out.cov_beta,
            np.array([[0.0064918418231375, 0.0036159705923791, 0.0438637051470406,
                -0.0058700836512467, 0.011281212888768],
               [0.0036159705923791, 0.0064793789429006, 0.0517610978353126,
                -0.0051181304940204, 0.0130726943624117],
               [0.0438637051470406, 0.0517610978353126, 0.5182263323095322,
                -0.0563083340093696, 0.1269490939468611],
               [-0.0058700836512467, -0.0051181304940204, -0.0563083340093696,
                 0.0066939246261263, -0.0140184391377962],
               [0.011281212888768, 0.0130726943624117, 0.1269490939468611,
                -0.0140184391377962, 0.0316733013820852]]),
        )
Exemplo n.º 47
0
def total_least_squares(data1, data2, data1err=None, data2err=None,
        print_results=False, ignore_nans=True, intercept=True,
        return_error=False, inf=1e10):
    """
    Use Singular Value Decomposition to determine the Total Least Squares linear fit to the data.
    (e.g. http://en.wikipedia.org/wiki/Total_least_squares)
    data1 - x array
    data2 - y array

    if intercept:
        returns m,b in the equation y = m x + b
    else:
        returns m

    print tells you some information about what fraction of the variance is accounted for

    ignore_nans will remove NAN values from BOTH arrays before computing

    Parameters
    ----------
    data1,data2 : np.ndarray
        Vectors of the same length indicating the 'x' and 'y' vectors to fit
    data1err,data2err : np.ndarray or None
        Vectors of the same length as data1,data2 holding the 1-sigma error values

    """

    if ignore_nans:
        badvals = numpy.isnan(data1) + numpy.isnan(data2)
        if data1err is not None:
            badvals += numpy.isnan(data1err)
        if data2err is not None:
            badvals += numpy.isnan(data2err)
        goodvals = True-badvals
        if goodvals.sum() < 2:
            if intercept:
                return 0,0
            else:
                return 0
        if badvals.sum():
            data1 = data1[goodvals]
            data2 = data2[goodvals]

    
    if intercept:
        dm1 = data1.mean()
        dm2 = data2.mean()
    else:
        dm1,dm2 = 0,0

    arr = numpy.array([data1-dm1,data2-dm2]).T

    U,S,V = numpy.linalg.svd(arr, full_matrices=False)

    # v should be sorted.  
    # this solution should be equivalent to v[1,0] / -v[1,1]
    # but I'm using this: http://stackoverflow.com/questions/5879986/pseudo-inverse-of-sparse-matrix-in-python
    M = V[-1,0]/-V[-1,-1]

    varfrac = S[0]/S.sum()*100
    if varfrac < 50:
        raise ValueError("ERROR: SVD/TLS Linear Fit accounts for less than half the variance; this is impossible by definition.")

    # this is performed after so that TLS gives a "guess"
    if data1err is not None or data2err is not None:
        try:
            from scipy.odr import RealData,Model,ODR
        except ImportError:
            raise ImportError("Could not import scipy; cannot run Total Least Squares")

        def linmodel(B,x):
            if intercept:
                return B[0]*x + B[1]
            else:
                return B[0]*x 

        if data1err is not None:
            data1err = data1err[goodvals]
            data1err[data1err<=0] = inf
        if data2err is not None:
            data2err = data2err[goodvals]
            data2err[data2err<=0] = inf

        if any([data1.shape != other.shape for other in (data2,data1err,data2err)]):
            raise ValueError("Data shapes do not match")

        linear = Model(linmodel)
        data = RealData(data1,data2,sx=data1err,sy=data2err)
        B = data2.mean() - M*data1.mean()
        beta0 = [M,B] if intercept else [M]
        myodr = ODR(data,linear,beta0=beta0)
        output = myodr.run()

        if print_results:
            output.pprint()

        if return_error:
            return numpy.concatenate([output.beta,output.sd_beta])
        else:
            return output.beta



    if intercept:
        B = data2.mean() - M*data1.mean()
        if print_results:
            print "TLS Best fit y = %g x + %g" % (M,B)
            print "The fit accounts for %0.3g%% of the variance." % (varfrac)
            print "Chi^2 = %g, N = %i" % (((data2-(data1*M+B))**2).sum(),data1.shape[0]-2)
        return M,B
    else:
        if print_results:
            print "TLS Best fit y = %g x" % (M)
            print "The fit accounts for %0.3g%% of the variance." % (varfrac)
            print "Chi^2 = %g, N = %i" % (((data2-(data1*M))**2).sum(),data1.shape[0]-1)
        return M
def show():
    data_filename = 'number.txt'
    data = np.loadtxt(data_filename,skiprows=0)
    number = np.reshape(data,(41,3))
    
    #emperical error    
    xerr = np.sqrt(number[:,0])/3
    yerr = number[:,2]
    data = Data(number[:,0].T, number[:,1].T, we = 1/(np.power(xerr.T,2)+np.spacing(1)), wd = 1/(np.power(yerr.T,2)+np.spacing(1)))
    
    model = Model(ord_function)
    odr = ODR(data, model, beta0=[0, 0])
    odr.set_job(fit_type=2)
    output = odr.run()
    
    popt = output.beta
    perr = output.sd_beta

    output.pprint()

    fitting_error = np.mean(np.sqrt(np.power(popt[0]*number[:,0]+popt[1] - number[:,1],2)))
    
    
    labels = np.array([[1,1,1,1,1,1,\
           2,2,2,2,2,2,\
           3,3,3,3,3,3,\
           4,4,4,4,4,4,\
           5,5,5,5,5,5,\
           6,6,6,6,6,\
           7,7,7,7,7,7],
          [0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,
           0,1,2,3,4,5]])
    
    fig, ax = plt.subplots(ncols = 1)
    ax.errorbar(number[:,0], number[:,1], xerr = xerr, yerr = yerr, fmt='o')

    ax.plot(number[:,0], popt[0]*number[:,0]+popt[1], '-r')
    
    bbox_props = dict(boxstyle="square,pad=0.3", fc="white", ec="black", lw=2)

    annotation_text = "function:  y = kx + b \n" \
        "k = %.2f" % popt[0] + " +/- %.2f" % perr[0] + '\n' \
        "b = %.2f" % popt[1] + " +/- %.2f" % perr[1] + '\n' \
        "Error: %.2f" % fitting_error
    ax.text(10, np.amax(number[:,1])+10, annotation_text, ha="left", va="top", rotation=0,
    size=15, bbox=bbox_props)
    
    for i in range(0, len(number[:,0])):            # <--
        ax.annotate('(%s, %s)' % (labels[0,i], labels[1,i]),\
        (number[i,0]+1, number[i,1]-1)) #
    
    
    ax.set_title('Algorithom Performance')
    ax.set_xlabel('Bubble Number Counted Manually')
    ax.set_ylabel('Bubbble Number Counted by Algorithom')
    plt.grid()
    plt.xlim((np.amin(number[:,0])-5,np.amax(number[:,0])+5))
    plt.ylim((0,np.amax(number[:,1])+20))
    plt.show()
def main():
    try:
        if (len(sys.argv) > 1 and sys.argv[1] == '1'):
            data_filename = 'number.txt'
            start_time = time.time()
            number = np.zeros((41,3))
            index = -1
            for det in range(1,8):
                if (det!=6):
                    num_n = 6
                else:
                    num_n = 5
                for n in range(0,num_n):
                    index = index + 1
                    temp = np.zeros(3)
                    for angle in range(1,4):
                        filename = 'detector_' + str(det) + '_no_' + str(n) \
                                        + '_angle_' + str(angle) + '.jpg'
                        refname = 'detector_' + str(det) + '_no_' + str(n) \
                                        + '_background.jpg'
    
                        elapse_time = (time.time()-start_time)
                        if(elapse_time >= 1):
                            remain_time = elapse_time/(index*3+angle-1)*41*3-elapse_time
                            print 'Processing .. ' + filename \
                                    +  time.strftime(" %H:%M:%S", time.gmtime(elapse_time)) \
                                    + ' has past. ' + 'Remaining time: ' \
                                    +  time.strftime(" %H:%M:%S", time.gmtime(remain_time))
                        else:
                            print 'Processing .. ' + filename \
                                    +  time.strftime(" %H:%M:%S", time.gmtime(elapse_time)) \
                                    + ' has past'
            
                        image = rgb2gray(io.imread(filename))
                        ref = rgb2gray(io.imread(refname))
            
                        temp[angle-1] = ellipse.count_bubble(image,ref)
                        #temp[angle-1] = ellipse.count_bubble(image)
                        
                    number[index,1] = np.mean(temp)
                    number[index,2] = np.std(temp)
        
            manual_count = np.array([1,27,40,79,122,160,1,18,28,42,121,223,0,11,24,46,\
                    142,173,3,19,23,76,191,197,0,15,24,45,91,152,0,\
                    16,27,34,88,0,9,12,69,104,123]) 
            number[:,0] = manual_count.T
            number.tofile(data_filename,sep=" ")
        elif(len(sys.argv) > 1):
            data_filename = sys.argv[1]
            data = np.loadtxt(data_filename,skiprows=0)
            number = np.reshape(data,(41,3))
        else:
            data_filename = 'number.txt'
            data = np.loadtxt(data_filename,skiprows=0)
            number = np.reshape(data,(41,3))
    
        #emperical error    
        xerr = np.sqrt(number[:,0])/3
        yerr = number[:,2]
        data = Data(number[:,0].T, number[:,1].T, we = 1/(np.power(xerr.T,2)+np.spacing(1)), wd = 1/(np.power(yerr.T,2)+np.spacing(1)))
    
        model = Model(ord_function)
        odr = ODR(data, model, beta0=[0, 0])
        odr.set_job(fit_type=2)
        output = odr.run()
    
        popt = output.beta
        perr = output.sd_beta

        output.pprint()
    
#       popt, pcov = curve_fit(linear_fit_function, number[:,0], number[:,1], [0, 0], number[:, 2])
#       perr = np.sqrt(np.diag(pcov))
        fitting_error = np.mean(np.sqrt(np.power(popt[0]*number[:,0]+popt[1] - number[:,1],2)))
    
    
        labels = np.array([[1,1,1,1,1,1,\
           2,2,2,2,2,2,\
           3,3,3,3,3,3,\
           4,4,4,4,4,4,\
           5,5,5,5,5,5,\
           6,6,6,6,6,\
           7,7,7,7,7,7],
          [0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,5,\
           0,1,2,3,4,
           0,1,2,3,4,5]])
    
        fig, ax = plt.subplots(ncols = 1)
        ax.errorbar(number[:,0], number[:,1], xerr = xerr, yerr = yerr, fmt='o')

        ax.plot(number[:,0], popt[0]*number[:,0]+popt[1], '-r')
    
        bbox_props = dict(boxstyle="square,pad=0.3", fc="white", ec="black", lw=2)

        annotation_text = "function:  y = kx + b \n" \
        "k = %.2f" % popt[0] + " +/- %.2f" % perr[0] + '\n' \
        "b = %.2f" % popt[1] + " +/- %.2f" % perr[1] + '\n' \
        "Error: %.2f" % fitting_error
        ax.text(10, np.amax(number[:,1])+10, annotation_text, ha="left", va="top", rotation=0,
    size=15, bbox=bbox_props)
    
        for i in range(0, len(number[:,0])):            # <--
            ax.annotate('(%s, %s)' % (labels[0,i], labels[1,i]),\
                (number[i,0]+1, number[i,1]-1)) #
    
    
        ax.set_title('Algorithom Performance')
        ax.set_xlabel('Bubble Number Counted Manually')
        ax.set_ylabel('Bubbble Number Counted by Algorithom')
        plt.grid()
        plt.xlim((np.amin(number[:,0])-5,np.amax(number[:,0])+5))
        plt.ylim((0,np.amax(number[:,1])+20))
        plt.show()
  
    except KeyboardInterrupt:
        print "Shutdown requested... exiting"
    except Exception:
        traceback.print_exc(file=sys.stdout)
        sys.exit(0)
Exemplo n.º 50
0
d_orion, catalog_orion, header, metadata, template_cube, template_header, new_selection_dictionary, selection_ID_dictionary = orion_output

# the fit

selection = new_selection_dictionary['Mon R2'] + new_selection_dictionary['Orion A'] + new_selection_dictionary['Orion B']
selection_ids = [struct.idx for struct in selection]

s_size = catalog_orion[selection_ids]['size']
s_linewidth = catalog_orion[selection_ids]['v_rms']

mydata = RealData(s_size, s_linewidth)
def powerlaw(B, x):
    return B[0] * (x**B[1])
powerlaw_model = Model(powerlaw)
myodr = ODR(mydata, powerlaw_model, beta0=[1.,1.])
myoutput = myodr.run()

orion_fit_constant = myoutput.beta[0]
orion_fit_power = myoutput.beta[1]

# the plot

xs = np.logspace(-1,4,20)
ys = orion_fit_constant * xs**orion_fit_power

dv_orion = d_orion.viewer()

dv_orion.hub.select(1, selection)

dsl_orion = Scatter(d_orion, dv_orion.hub, catalog_orion, 'size', 'v_rms')
dsl_orion.set_loglog()
Exemplo n.º 51
0
def main():
    try:

        legend = ['curvature', 'neural-network', 'hough transform', 'SVM-Preliminary']
	data_filename = ['number.txt', 'skflow.txt', 'curvature.txt', 'SVM.txt']
        colors = ['blue', 'red', 'black', 'green']

        annotation_text = "function:  y = kx + b";
        fig, ax = plt.subplots(ncols = 1)
        for i in range(len(data_filename)):
            temp = np.loadtxt(data_filename[i],skiprows=0)
            data = np.reshape(temp,(41,len(temp)/41))
            #print data
            data[:,1:3] = np.true_divide(data[:,1:3], np.amax(data[:,1]))
            data[:,1] = data[:,1]+i
            #print data
            #emperical error 
            xerr = np.sqrt(data[:,0])/3
            yerr = data[:,2]
            data_fit = Data(data[:,0].T, data[:,1].T, \
                          we = 1/(np.power(xerr.T,2)+np.spacing(1)),\
                          wd = 1/(np.power(yerr.T,2)+np.spacing(1)))

            model = Model(ord_function)
            odr = ODR(data_fit, model, beta0=[0, 0])
	    odr.set_job(fit_type=2)
	    output = odr.run()
            popt = output.beta
            perr= output.sd_beta
            popt, pcov = curve_fit(linear_fit_function, 
                             data[:,0], data[:,1], [1, 0], data[:, 2])
            perr = np.sqrt(np.diag(pcov))

	    A = popt[0]/np.sqrt(popt[0]*popt[0]+1)
	    B = -1/np.sqrt(popt[0]*popt[0]+1)
	    C = popt[1]/np.sqrt(popt[0]*popt[0]+1)
            fitting_error= np.mean(np.abs(A*data[:,0]+B*data[:,1]+C))

            ax.errorbar(data[:,0], data[:,1], xerr = xerr,\
                        yerr = yerr, fmt='o',color=colors[i])
            ### not using error bar in fitting #########
            popt[0],popt[1] = np.polyfit(data[:,0], data[:,1], 1)
            ###
            ax.plot(data[:,0], popt[0]*data[:,0]+popt[1], colors[i], linewidth = 2)
            annotation_text = annotation_text + "\n" +\
                              legend[i] + "(" + \
                              colors[i] + ") \n" + \
                              "k = %.2f b = %.2f Error = %.2f"%( \
                              popt[0], popt[1], fitting_error) 
                              
       
        bbox_props = dict(boxstyle="square,pad=0.3", fc="white", ec="black", lw=2)
	#ax.text(0, 4.15, annotation_text, ha="left", va="top", \
        #        rotation=0, size=14, bbox=bbox_props)
   
        ax.set_title('Algorithom Performance')
        ax.set_xlabel('Bubble Number Counted Manually')
        ax.set_ylabel('Normalized Bubbble Number Counted by Algorithom')
        plt.grid()
        plt.xlim((np.amin(data[:,0])-5,np.amax(data[:,0])+5))
        plt.ylim((0,np.amax(data[:,1])+0.2))
        plt.show()
  
    except KeyboardInterrupt:
        print "Shutdown requested... exiting"
    except Exception:
        traceback.print_exc(file=sys.stdout)
        sys.exit(0)
Exemplo n.º 52
0
Arquivo: ccd.py Projeto: vsilv/fp
def linear_beta(x, beta_0, beta_1):
    return beta_0 + beta_1 * x
intensity = np.array(As).reshape(9, 5).T[[0, 3, 4, 1, 2]].T # move peak 2 & 3 to the correct position
A_lambda = intensity[:-1].T # entries of each peak put together
peaks = ['558.3', '563.5', '564.7', '570.8', '576.7']
intersects = []
x = np.array(concentration[:-1])
sx = 4
for i, peak, A in enum(peaks, A_lambda):
    y = un.nominal_values(A)
    sy = un.std_devs(A)
    data = RealData(x, y, sx=sx, sy=sy)
    model = Model(func)
    odr = ODR(data, model, [6, 0])
    odr.set_job(fit_type=0)
    output = odr.run()
    beta = uc.correlated_values(output.beta, output.cov_beta)
    fit = linear_beta(x_fit, *beta)
    #Intersects
    y0 = intensity[-1][i] # peak intensities of the original sample
    x0 = (y0 - beta[0]) / beta[1]
    intersects.append(x0)
lamb_all = np.array(x0s).reshape(9, 5)
d_lin = np.load(npy_dir + 'ccd_calibration.npy') 
lamb_all = linear(lamb_all, *d_lin) # Integrate error on wavelength of CCD
lamb_mean = np.mean(lamb_all, 0) 
lamb_laser = np.load(npy_dir + 'ccd_lamb_laser.npy')[0]
dnu = (1 / lamb_mean - 1 / lamb_laser) * 10**7
lamb_mean = np.sort(lamb_mean)
lits = np.array([888, 1028, 1091, 1274, 1464]) 
for i, x0, dnu_cm, lit,intersect in enum(lamb_mean, lamb_to_cm(lamb_mean), lits, intersects):
Exemplo n.º 53
0
def quad_func(p, x):
    m, c = p
    return m*x + c

# Create a model for fitting.
quad_model = Model(quad_func)

# Create a RealData object using our initiated data from above.
data = RealData(x_obs, y_obs, sx=x_error, sy=y_error)

# Set up ODR with the model and data.
odr = ODR(data, quad_model, beta0=[0.15, 1.0])

# Run the regression.
out = odr.run()

# define the model/function to be fitted.
def model(x_obs, y_obs): 
    m       = pm.Uniform('m', 0, 10, value= 0.15)
    n       = pm.Uniform('n', -10, 10, value= 1.0)
    x_pred  = pm.Normal('x_true', mu=x_obs, tau=(x_error)**-2)           # this allows error in x_obs

    #Theoretical values
    @pm.deterministic(plot=False)
    def linearD(x_true=x_pred, m=m, n=n):
        return m * x_true + n

    @pm.deterministic
    def random_operation_on_observable(obs_values=y_obs, m=m):
        return obs_values + 0 * m
Exemplo n.º 54
0
def zeroth_fit_2var(Nml,Eth,nu_des_ini,t_start,t_stop):
	
	deca=0
	sizeOfFont = 16
	fontProperties = {'family':'sans-serif',
	    'weight' : 'normal', 'size' : sizeOfFont}
	#rc('text', usetex=True)
	rc('font',**fontProperties)
	ticks_font = font_manager.FontProperties(family='cm', style='normal',
	    size=sizeOfFont, weight='normal', stretch='normal')
	filenm='12CO_13CO_1_1_fit.txt'


	#if molec =='n2':
	#	m_mol=30.0e-3
	#	xtex=0.042

	#if molec =='co':
	m_mol=28.0e-3
	xtex=0.0375

	#if molec =='co2':
	#	m_mol=44.0e-3
	#if molec =='h2o':
	#	m_mol=18.0e-3

	errort=0.1

	T_tot, N_tot,N_13co = np.loadtxt(filenm,unpack=True,skiprows=1)
	N_tot=N_13co
	T_ini=T_tot
	T_tot=T_tot
	T_tot_err=T_tot*0.0+errort
	N_tot=np.clip(N_tot,1e-12,1e30)
	N_tot_err=1e12+N_tot*0

	h_rate=1/60.0 #Kmin-1

	T=T_tot[np.where((T_tot>t_start) & (T_tot<t_stop))]
	N=N_tot[np.where((T_tot>t_start) & (T_tot<t_stop))]
	N_err=1e10+N*0
	T_err=T*0.0+errort
	
	x=1.0/T

	x_err=T_err/(T**2)
	
	#print x_err
	y=np.log(N/(1e15))
	y_err=N_err/N
	#y=np.log(N)
	p=[0.,0.]
	#nu_des_ini=7e11#np.sqrt(2.0*1e19*8.31*Eth/(np.pi*np.pi*m_mol))

	#slope, intercept, r_value, p_value, std_err=scipy.stats.linregress(x, y)
	def func(p, t):
		return p[1] +t*p[0]
	#def func (p,t):
	#	return np.log(1e12/h_rate)+t*p[0]
	#def func(p, t):
	#	return np.log(np.sqrt(2.0*1e19*8.31*-1*p[0]/(np.pi*np.pi*m_mol))/h_rate) +t*p[0]
	#p[1]=np.log(1e12/h_rate)
	
	#p,pcov=curve_fit(func, x, y)
	#err = np.sqrt(np.diag(pcov))
	data = RealData(x, y, sx=x_err,sy=y_err)#, sy=np.log(0.001))
	
	model = Model(func)

	#odr = ODR(data, model, beta0=[-800,1e12])
	odr = ODR(data, model, beta0=[-750,7e11])
	odr.set_job(fit_type=2)
	output = odr.run()

	p=output.beta
	err=output.sd_beta

	E_des=-p[0]
	nu_des=h_rate*np.exp(p[1])
	nu_theo=h_rate*np.exp(np.log(np.sqrt(2.0*1e19*8.31*-1*p[0]/(np.pi*np.pi*m_mol))/h_rate))
	#p[1]=np.log(np.sqrt(2.0*1e19*8.31*-1*p[0]/(np.pi*np.pi*m_mol))/h_rate)
	#err[1]=0.0
	
	#nu_theo=np.sqrt(2.0*1e19*8.31*Eth/(np.pi*np.pi*m_mol))
	print 'E_des fit', p[0]*-1, '+-',err[0]
	print 'E_des litt', Eth
	print 'nu fit e12', 1e-12*h_rate*np.exp(p[1]), '+-',err[1]*1e-12*h_rate*np.exp(p[1])

	print 'nu litt e12', 1e-12*nu_des_ini
	print 'nu theo e12', 1e-12*nu_theo


	#nu_des=2e11
	#print "nu e12n", nu_des*1e-12
	# def funct(x,t):
	# 	if x[0]>0.0:
	#   		f_ml = -(nu_des/h_rate)*np.exp(-E_des/t)
	#   		f_g= f_ml-x[1]
	#   	if x[0]<0.0:
	#   		f_ml=0
	#   		f_g= f_ml-x[1]
	#   	return [f_ml,f_g]
		
	# F_pure = odeint(funct,[Nml,0.00],T_tot)
	# N_ml = np.clip(F_pure[:,0],0.0,10)
	# N_g = -F_pure[:,1]

	# k_d_ml = (nu_des/h_rate)*np.exp(-E_des/T_tot)
	

	# k_d_mode=(nu_des_ini/h_rate)*np.exp(-Eth/T_tot)
		#print "Ns",N_ml
		  	#if order==0:
		  	#	k_d_ml[np.where(k_d_ml>np.max(N))]=0.0
		#A[:,i] = k_d_ml
                #A[:,i] = np.clip(k_d_ml,-1e15,1e15)
        
		#x,resid = optimize.nnls(A,b)

	#slope, intercept, r_value, p_value, std_err=scipy.stats.linregress(x, y)

	plot_tpd='12CO_reglinfit_2var.pdf'
	plt.errorbar(x,y,xerr=x_err,yerr=y_err,color='k')
	#plt.errorbar(a,b,yerr=c, linestyle="None")
	plt.plot(x,p[1]+p[0]*x,color='r',linestyle='--',linewidth=3.0)
	#plt.plot(x,p[1]+p[0]*x,color='r',linestyle='--',linewidth=3.0)

	plt.text(xtex,0.0,'-Slope: '+str(round(E_des))+' +- '+str(round(err[0])))
	plt.text(xtex,-0.5,'exp(Intercept)*h_rate: '+ str(round(1e-12*h_rate*np.exp(p[1]),2))+ '+/-'+str(round(err[1]*1e-12*h_rate*np.exp(p[1]),2))+ 'e12')
	#plt.plot(x,p[0]+(p[1]-err[1])*x,color='r',linestyle='--',linewidth=3.0)
	#plt.plot(x,p[0]+(p[1]+err[1])*x,color='r',linestyle='--',linewidth=3.0)
	#plt.plot(x,np.log(1e12/h_rate)-Eth*x,color='b',linestyle='--',linewidth=3.0)
	#plt.xlim(1/(t_stop+5),1/(t_start-5))
	plt.savefig(plot_tpd)
	plt.close('all')
	
	#ind=np.where(N_tot<np.max(N_tot))

	#plot_tpd='12CO_tpdfit_2var.pdf'
	#plt.errorbar(T_tot[ind],N_tot[ind]/(1e15),xerr=T_tot_err[ind],yerr=N_tot_err[ind]/1e15,color='k')
	#plt.plot([t_start-deca,t_start-deca],[0,np.max(N_tot)/1e15],linestyle='--',color='g')
	#plt.plot([t_stop-deca,t_stop-deca],[0,np.max(N_tot)/1e15],linestyle='--',color='g')
	#plt.plot(T,-np.exp(intercept)*np.exp(-1*slope/T),color='r',linestyle='--',linewidth=3.0)
	#plt.plot(T,(60*1.5e12/5.3e15)*np.exp(-950.0/T),color='b',linestyle='--',linewidth=3.0)
	#plt.plot(T_tot,np.clip(k_d_ml,0,2*np.max(N_tot/1e15)),color='r')
	#plt.plot(T_tot,np.clip(k_d_mode,0,2*np.max(N_tot/1e15)),color='r')
	
	#plt.xlim(15,50)
	#plt.ylim(-0.005,np.max(N_tot)/1e15+0.1)
	#plt.savefig(plot_tpd)
	plt.close('all')
Exemplo n.º 55
0
x = numpy.linspace(-4, 3.0, N)
y = model((a0,b0,c0),x) + normal(0.0, 0.5, N)  # Mean 0, sigma 1
errx = normal(0.3, 0.3, N) 
erry = normal(0.2, 0.4, N) 
print("\nTrue model values [a0,b0,c0]:", [a0,b0,c0])

beta0 = [1,1,1]
#beta0 = [1.8,-0.5,0.1]
print("\nODR:")
print("==========")
from scipy.odr import Data, Model, ODR, RealData, odr_stop
linear = Model(model)
mydata = RealData(x, y, sx=errx, sy=erry)
myodr = ODR(mydata, linear, beta0=beta0, maxit=5000)
#myodr.set_job(2)
myoutput = myodr.run()
print("Fitted parameters:      ", myoutput.beta)
print("Covariance errors:      ", numpy.sqrt(myoutput.cov_beta.diagonal()))
print("Standard errors:        ", myoutput.sd_beta)
print("Minimum chi^2:          ", myoutput.sum_square)
print("Minimum (reduced)chi^2: ", myoutput.res_var)
beta = myoutput.beta


# Prepare fit routine
fitobj = kmpfit.Fitter(residuals=residuals, data=(x, y, errx, erry), 
                       xtol=1e-12, gtol=1e-12)
fitobj.fit(params0=beta0)
print("\n\n======== Results kmpfit with effective variance =========")
print("Fitted parameters:      ", fitobj.params)
print("Covariance errors:      ", fitobj.xerror)
Exemplo n.º 56
0
def fitDoubleGaussians(charges, charge_err, data, data_err):

    # some simple peakfinding to find the first and second peaks
    peak1_value = np.max(data)
    peak1_bin = [x for x in range(len(data)) if data[x]==peak1_value][0]
    peak1_charge = charges[peak1_bin]

    # Preliminary fits to get better estimates of parameters...
    first_peak_data = RealData(charges[peak1_bin-30:peak1_bin+30],
                               data[peak1_bin-30:peak1_bin+30],
                               charge_err[peak1_bin-30:peak1_bin+30],
                               data_err[peak1_bin-30:peak1_bin+30],)
    first_peak_model = Model(gauss_fit)
    first_peak_odr = ODR(first_peak_data, first_peak_model,
                         [peak1_value, peak1_charge, 0.1*peak1_charge])
    first_peak_odr.set_job(fit_type=2)
    first_peak_output = first_peak_odr.run()
    first_peak_params = first_peak_output.beta

    #second_peak_params, covariance = curve_fit(gauss_fit2,
    #                                           data[peak1_bin-30:peak1_bin+30],
    #                                           data[peak1_bin-30:peak1_bin+30],
    #                                           p0 = [peak1_value, peak1_charge, 0.1*peak1_charge])


    # subtract the largest peak so we can search for the other one
    updated_data = data-gauss_fit(first_peak_params, charges)
    #updated_data = data[:int(len(data)*2.0/3.0)]

    peak2_value = np.max(updated_data)
    peak2_bin = [x for x in range(len(updated_data)) if updated_data[x]==peak2_value][0]
    peak2_charge = charges[peak2_bin]

    #first_peak_params, covariance = curve_fit(gauss_fit2,
    #                                          updated_data[peak2_bin-30:peak2_bin+30],
    #                                          updated_data[peak2_bin-30:peak2_bin+30],
    #                                          p0 = [peak2_value, peak2_charge, 0.1*peak2_charge])

    # and the second peak...
    second_peak_data = RealData(charges[peak2_bin-30:peak2_bin+30],
                               data[peak2_bin-30:peak2_bin+30],
                               charge_err[peak2_bin-30:peak2_bin+30],
                               data_err[peak2_bin-30:peak2_bin+30],)
    second_peak_model = Model(gauss_fit)
    second_peak_odr = ODR(second_peak_data, second_peak_model,
                         [peak2_value, peak2_charge, first_peak_params[2]])
    second_peak_odr.set_job(fit_type=2)
    second_peak_output = second_peak_odr.run()
    second_peak_params = second_peak_output.beta

    # Now fit both gaussians simultaneously
    double_peak_data = RealData(charges, data,
                                charge_err, data_err)
    double_peak_model = Model(double_gauss_fit)
    double_peak_odr = ODR(double_peak_data, double_peak_model,
                          [first_peak_params[0], first_peak_params[1], first_peak_params[2],
                           second_peak_params[0], second_peak_params[1], second_peak_params[2]])
    double_peak_odr.set_job(fit_type=0)
    double_peak_output = double_peak_odr.run()
    double_peak_params = double_peak_output.beta
    double_peak_sigmas = double_peak_output.sd_beta

    #double_peak_params = [first_peak_params[0], first_peak_params[1], first_peak_params[2],
    #                       second_peak_params[0], second_peak_params[1], second_peak_params[2]]

    return double_peak_params, double_peak_sigmas