Exemplo n.º 1
0
def ws_correlation_orthoginal_distance_model(data, ref_ws_col='ref', site_ws_col='site', force_through_origin=False):
    """Calculate the slope and offset between two wind speed columns using orthoganal distance regression.

    https://docs.scipy.org/doc/scipy-0.18.1/reference/odr.html

    :Parameters:

    data: DataFrame
        DataFrame with wind speed columns ref and site, and direction data dir

    ref_ws_col: string, default None (primary anemometer assumed)
        Reference anemometer data to use. Extracted from MetMast.data

    site_ws_col: string, default None (primary anemometer assumed)
        Site anemometer data to use. Extracted from MetMast.data

    force_through_origin: boolean, default False
        Force the correlation through the origin (offset equal to zero)

    :Returns:

    out: DataFrame
        slope, offset, R2, uncert, points

    """
    data = data.loc[:, [ref_ws_col, site_ws_col]].dropna().astype(np.float)
    results = return_correlation_results_frame(ref_label=ref_ws_col, site_label=site_ws_col)

    if not valid_ws_correlation_data(data=data, ref_ws_col=ref_ws_col, site_ws_col=site_ws_col):
        return results

    points = data.shape[0]
    R2 = calculate_R2(data=data, ref_ws_col=ref_ws_col, site_ws_col=site_ws_col)
    uncert = calculate_IEC_uncertainty(data=data, ref_ws_col=ref_ws_col, site_ws_col=site_ws_col)

    X = data.loc[:, ref_ws_col].values
    Y = data.loc[:, site_ws_col].values

    data_mean = data.mean()
    slope_estimate_via_ratio = data_mean[site_ws_col] / data_mean[ref_ws_col]

    realdata = odrpack.RealData(X, Y)

    if force_through_origin:
        linear = odrpack.Model(f_without_offset)
        odr = odrpack.ODR(realdata, linear, beta0=[slope_estimate_via_ratio])
        slope = odr.run().beta[0]
        offset = 0
    else:
        linear = odrpack.Model(f_with_offset)
        odr = odrpack.ODR(realdata, linear, beta0=[slope_estimate_via_ratio, 0.0])
        slope, offset = odr.run().beta[0], odr.run().beta[1]

    results.loc[pd.IndexSlice[ref_ws_col, site_ws_col], ['slope', 'offset', 'R2', 'uncert', 'points']] = np.array(
        [slope, offset, R2, uncert, points])
    return results
Exemplo n.º 2
0
def poly_lsq(x, y, n, verbose=False, itmax=200):
    ''' Performs a polynomial least squares fit to the data,
    with errors! Uses scipy odrpack, but for least squares.

    IN:
       x,y (arrays) - data to fit
       n (int)      - polinomial order
       verbose      - can be 0,1,2 for different levels of output
                      (False or True are the same as 0 or 1)
       itmax (int)  - optional maximum number of iterations

    OUT:
       coeff -  polynomial coefficients, lowest order first
       err   - standard error (1-sigma) on the coefficients

    --Tiago, 20071114
    '''
    func = models.polynomial(n)
    mydata = odr.Data(x, y)
    myodr = odr.ODR(mydata, func, maxit=itmax)
    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)
    fit = myodr.run()
    # Display results:
    if verbose:
        fit.pprint()
    if fit.stopreason[0] == 'Iteration limit reached':
        print('(WWW) poly_lsq: Iteration limit reached, result not reliable!')
    # Results and errors
    coeff = fit.beta
    err = fit.sd_beta
    return coeff, err
Exemplo n.º 3
0
def bilinear_Q_regression(freqs_log, Q_list_log):
    freqs_log = array(freqs_log)

    #data = odrpack.RealData(x[12:], y[12:])
    data = odrpack.RealData(freqs_log, Q_list_log)

    #x_range = arange(-0.5, 1.0, step=0.01) # x range

    bilin_reg = odrpack.Model(bilinear_reg_free)
    odr = odrpack.ODR(data, bilin_reg, beta0=[0.4, 3.0, 0.3, -0.5])

    odr.set_job(
        fit_type=2)  #if set fit_type=2, returns the same as least squares
    out = odr.run()
    #print('\nbilinear auto\n')
    #out.pprint()

    a = out.beta[0]
    b = out.beta[1]
    c = out.beta[2]
    hx = out.beta[3]  # x hinge point

    log_q_fit = b + a * freqs_log  # get y values from bilinear
    yhinge = b + a * hx
    idx = freqs_log > hx
    log_q_fit[idx] = c * (freqs_log[idx] - hx) + yhinge

    return log_q_fit
Exemplo n.º 4
0
def bilinear_regression(x, y):

    data = odrpack.RealData(x[12:], y[12:])

    x_range = np.arange(-0.5, 1.0, step=0.01) # x range 

    bilin_reg = odrpack.Model(bilinear_reg_free)
    odr = odrpack.ODR(data, bilin_reg, beta0=[0.4, 3.0, 0.3, -0.5])

    odr.set_job(fit_type=2) #if set fit_type=2, returns the same as least squares
    out = odr.run()
    print('\nbilinear auto\n')
    out.pprint()

    a = out.beta[0]
    b = out.beta[1]
    c = out.beta[2]
    hx = out.beta[3] # x hinge point

    y_range = b + a * x_range # get y values from bilinear
    yhinge = b + a * hx
    idx = x_range > hx
    y_range[idx] = c * (x_range[idx]-hx) + yhinge

    return y_range, x_range
Exemplo n.º 5
0
def gaussian_lsq(x, y, verbose=False, itmax=200, iparams=[]):
    ''' Performs a gaussian least squares fit to the data,
    with errors! Uses scipy odrpack, but for least squares.'''
    def _gauss_fjd(B, x):
        # Analytical derivative of gaussian with respect to x
        return 2 * (x - B[0]) * gaussian(B, x)

    # these derivatives need to be fixed!
    def _gauss_fjb(B, x):
        # Analytical derivatives of gaussian with respect to parameters
        _ret = np.concatenate(
            (-2 * (x - B[0]) * gaussian(B, x),
             ((x - B[0])**2 / (2 * B[1]**2) - 1) / B[1] * gaussian(B, x),
             gaussian(B, x) / B[2], np.ones(x.shape, float)))
        _ret.shape = (4, ) + x.shape
        return _ret

    # Centre data in mean(x) (makes better conditioned matrix)
    mx = np.mean(x)
    x2 = x - mx

    if not any(iparams):
        # automatic guessing of gaussian's initial parameters (saves
        # iterations)
        iparams = np.array([
            x2[np.argmax(y)],
            np.std(y),
            math.sqrt(2 * math.pi) * np.std(y) * np.max(y), 1.
        ])

    gauss = odr.Model(gaussian, fjacd=_gauss_fjd, fjacb=_gauss_fjb)

    mydata = odr.Data(x2, y)
    myodr = odr.ODR(mydata, gauss, beta0=iparams, maxit=itmax)

    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)

    fit = myodr.run()

    # Display results:
    if verbose:
        fit.pprint()
        print('Re-centered Beta: [%f  %f  %f %f]' %
              (fit.beta[0] + mx, fit.beta[1], fit.beta[2], fit.beta[3]))

    itlim = False
    if fit.stopreason[0] == 'Iteration limit reached':
        itlim = True
        print("(WWW) gauss_lsq: Iteration limit reached, result not reliable!")

    # Results and errors
    coeff = fit.beta
    coeff[0] += mx  # Recentre in original axis
    err = fit.sd_beta

    return coeff, err, itlim
Exemplo n.º 6
0
def least(func, x, y, sy=None, beta0=None, ifixb=None):
    linear = odrpack.Model(func)
    #mydata = odrpack.Data(x, z[1], wd=1./np.power(z[2],2), we=1./np.power(sy,2))
    mydata = odrpack.RealData(x, y, sy=sy)
    myodr = odrpack.ODR(mydata, linear, beta0=beta0, ifixb=ifixb)
    myodr.set_job(fit_type=2)
    myoutput = myodr.run()
    #    myoutput.pprint()
    return myodr.output
Exemplo n.º 7
0
def double_gauss_lsq(x, y, verbose=False, itmax=200, iparams=[]):
    ''' Performs a double gaussian least squares fit to the data,
    with errors! Uses scipy odrpack, but for least squares.'''
    def _dgauss_fjd(B, x):
        # Analytical derivative of gaussian with respect to x
        return (B[0] - x) / B[1]**2 * gaussian(np.concatenate((B[:3], [0.])), x) + \
               (B[3] - x) / B[4]**2 * gaussian(np.concatenate((B[3:6], [0.])), x)

    def _dgauss_fjb(B, x):
        # Analytical derivatives of gaussian with respect to parameters
        gauss1 = gaussian(np.concatenate((B[:3], [0.])), x)
        gauss2 = gaussian(np.concatenate((B[3:6], [0.])), x)
        _ret = np.concatenate(
            ((x - B[0]) / B[1]**2 * gauss1,
             ((B[0] - x)**2 - B[1]**2) / B[1]**3 * gauss1, gauss1 / B[2],
             (x - B[3]) / B[4]**2 * gauss2,
             ((B[3] - x)**2 - B[4]**2) / B[4]**3 * gauss2, gauss2 / B[5],
             np.ones(x.shape, float)))
        _ret.shape = (7, ) + x.shape
        return _ret

    # Centre data in mean(x) (makes better conditioned matrix)
    mx = mean(x)
    x2 = x - mx
    if not any(iparams):
        iparams = array([
            x2[np.argmax(y)],
            np.std(y),
            np.sqrt(2 * np.pi) * np.std(y) * max(y), x2[np.argmax(y)],
            np.std(y),
            np.sqrt(2 * np.pi) * np.std(y) * max(y), 1.
        ])
    dgauss = odr.Model(double_gaussian, fjacd=_dgauss_fjd, fjacb=_dgauss_fjb)
    mydata = odr.Data(x2, y)
    myodr = odr.ODR(mydata, dgauss, beta0=iparams, maxit=itmax)
    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)
    fit = myodr.run()
    # Display results:
    if verbose:
        fit.pprint()
        print('Re-centered Beta: [%f  %f  %f  %f  %f  %f  %f]' %
              (fit.beta[0] + mx, fit.beta[1], fit.beta[2], fit.beta[3] + mx,
               fit.beta[4], fit.beta[5], fit.beta[6]))
    itlim = False
    if fit.stopreason[0] == 'Iteration limit reached':
        itlim = True
        print('(WWW) gauss_lsq: Iteration limit reached, result not reliable!')
    # Results and errors
    coeff = fit.beta
    coeff[[0, 3]] += mx  # Recentre in original axis
    err = fit.sd_beta
    return coeff, err, itlim
Exemplo n.º 8
0
 def fit(self, num_cuts, truncate=True, withSpreadterm=False):
     x, y, xaxis, m, s, cnt = CMECostModel.qc(self, num_cuts, truncate)
     try:
         popt, pcov = curve_fit(self.model_curve_guess, x, y)
     except RuntimeError:
         popt = [1.0] * CMECostModel.num_free_params
     func = odr.Model(self.model_curve_ODR)
     odrdata = odr.Data(x, y)
     odrmodel = odr.ODR(odrdata, func, beta0=popt, maxit=500, ifixx=[0])
     o = odrmodel.run()
     return o, x, y, xaxis, m, s, cnt
Exemplo n.º 9
0
def least(func, x, y, sy=None, beta0=[]):
    linear = odrpack.Model(func)
    #mydata = odrpack.Data(x, z[1], wd=1./np.power(z[2],2), we=1./np.power(sy,2))
    if not sy == None:
        mydata = odrpack.RealData(x, y, sy=sy)
    else:
        mydata = odrpack.RealData(x, y)
    myodr = odrpack.ODR(mydata, linear, beta0=beta0, maxit=500)
    myodr.set_job(fit_type=2)
    myoutput = myodr.run()
    myoutput.pprint()
    return myodr.output
Exemplo n.º 10
0
def fitScipyODR(xdata, ydata, xerr, yerr, func, pInit):
    """ Fit a series of data points with ODR (function must be in from f(beta[n], x))
    """
    model = odrpack.Model(func)
    data = odrpack.RealData(xdata, ydata, sx=xerr, sy=yerr)
    odr = odrpack.ODR(data, model, beta0=pInit)
    out = odr.run()
    popt = out.beta
    pcov = out.cov_beta
    out.pprint()
    print('Chi square = %f' % out.sum_square)
    return popt, pcov
Exemplo n.º 11
0
def do_odr(f, x, xe, y, ye, estimates):

    model = odrpack.Model(f)

    # sx and sy should be the covariances
    data = odrpack.RealData(x, y, sx=xe, sy=ye)

    # need to hard-code in estimates for parameters
    odr = odrpack.ODR(data, model, estimates)

    output = odr.run()

    return output
Exemplo n.º 12
0
    def estimate_spectra(self):
        from scipy.odr import odrpack as odr

        def ofunc(pars, x, sp_sens, nu, el):
            a, b, dkalpha, tele = pars
            spectra = a * Kalpha_profile(el, nu, dkalpha) + b * ff_profile(
                nu, tele)
            spectra /= np.trapz(spectra, nu)

            spectra = np.tile(spectra, (sp_sens.shape[0], 1))
            comp_tr = np.trapz(sp_sens * spectra, nu, axis=-1)
            return comp_tr

        def ofunc2(pars, thick, nu, el):
            spectra = compute_spectra(pars, nu)

            sp_tr_filters = np.ones(nu.shape)
            for filt_el, filt_thick in self.filters.iteritems():
                sp_tr_filters *= np.exp(-cold_opacity(filt_el, nu=nu) *
                                        filt_thick)
            ip_sens = ip_sensitivity(nu)
            comp_tr = []
            for this_thick in thick:
                sp_sens = np.exp(-cold_opacity(el, nu=nu)*this_thick)\
                        * sp_tr_filters * ip_sens * spectra
                comp_tr.append(np.trapz(sp_sens, nu, axis=0))

            return np.array(comp_tr).reshape((1, -1))
            #return ((comp_tr - exp_tr)**2).sum()

        beta0 = [1, 1, 100, 1000]
        my_data = odr.RealData(self.thick,
                               self.exp_tr,
                               sy=0.05 * np.ones(self.exp_tr.shape))
        my_model = odr.Model(ofunc2, extra_args=(self.nu, 'polystyrene'))
        my_odr = odr.ODR(my_data, my_model, beta0=beta0)
        my_odr.set_job(fit_type=2)
        my_odr.set_iprint(final=2, iter=1, iter_step=1)
        fit = my_odr.run()
        self.beta = fit.beta  #[::-1]
        self.sdbeta = fit.sd_beta  #[::-1]

        print(ofunc2(self.beta, self.thick, self.nu, 'polystyrene'))
        print(self.exp_tr)

        print('\n')
        print(beta0)
        print(self.beta)
def fitTrack(trackArray):
    results = []
    x = []
    y = []
    energy = []
    for i in trackArray:
        x.append(i[0])
        y.append(i[1])
        energy.append(i[2])
    slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
    mydata = odrpack.Data(x, y, energy, energy)
    linear = odrpack.Model(f)
    myodr = odrpack.ODR(mydata, linear, beta0=[slope, intercept])
    myoutput = myodr.run()
    results.append(myoutput.beta)
    return results
Exemplo n.º 14
0
    def nominal_AB(self, K, N, T, explore_mag=10, res_time=100):
        '''
        Estimates matrices A and B
        Args:
            K: The controller gain
            N: number of iterations to be consistent with iterative methods
            T: Rollout length
            explore_mag: noise magnitude for excitement
            res_time: when to reset the dynamics to its initial condition

        Returns: A_nom, B_nom
        '''
        Lin_gain = LinK(K)
        Lin_gain.make_sampling_on(explore_mag)
        if T >= res_time:
            N = int(np.ceil(N * T / res_time))
            T = res_time
        A_nom = np.zeros((self.n, self.n))
        B_nom = np.zeros((self.n, self.m))

        # storage matrices
        states_batch = np.zeros((self.n, N, T))
        next_states_batch = np.zeros((self.n, N, T))
        actions_batch = np.zeros((self.m, N, T))

        # simulate
        for k in range(N):
            # Do one rollout to save data for  model estimation
            states, actions, _, next_states = self.dyn.one_rollout(Lin_gain.sample_lin_policy, T)
            states_batch[:, k, :] = states.T
            actions_batch[:, k, :] = actions.T
            next_states_batch[:, k, :] = next_states.T

        for i in range(self.n):
            linear = odrpack.Model(linear_func)
            data = odrpack.RealData(
                x=np.vstack((states_batch.reshape(self.n, N * T), actions_batch.reshape(self.m, N * T))),
                y=next_states_batch[i, :, :].reshape(1, N * T))
            Myodr = odrpack.ODR(data, linear, np.zeros(self.n + self.m))
            out = Myodr.run()
            tmp = out.beta
            A_nom[i, :] = tmp[0:self.n]
            B_nom[i, :] = tmp[self.n:]

        return A_nom, B_nom
def minThis(vertex, trackArray):
    sumOfSquares = 0
    for i in range(len(trackArray)):
        x = []
        y = []
        energy = []
        #print trackArray
        for i in trackArray[i]:
            x.append(i[0])
            y.append(i[1])
            energy.append(i[2])
        slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
        mydata = odrpack.Data(x, y, energy, energy)
        linear = odrpack.Model(fv, extra_args=vertex)
        myodr = odrpack.ODR(mydata, linear, beta0=[slope, intercept])
        myoutput = myodr.run()
        sumOfSquares += myoutput.sum_square
    return sumOfSquares
Exemplo n.º 16
0
    def fitSingleCrystalBallPeak(self):
        params = np.array([
            self.peak1_paramsEdits[0].value(),
            self.peak1_paramsEdits[1].value(),
            self.peak1_paramsEdits[2].value(),
            self.peak1_paramsEdits[3].value(), 0.9, 4
        ])

        model = odr.Model(lineshape.Single_Crystalball)
        odr_data = odr.RealData(self.x_cut,
                                self.y_cut)  #, sy = np.sqrt(self.y_cut))
        myodr = odr.ODR(odr_data, model, beta0=params)
        myodr.set_job(fit_type=0)
        myoutput = myodr.run()

        self.params = myoutput.beta
        params_cov = myoutput.cov_beta

        params_output = []
        params_red_chi2 = myoutput.res_var

        content, error = integrate.quad(lineshape.Single_Crystalball_integrand,
                                        self.x_fit[0],
                                        self.x_fit[-1],
                                        args=tuple(self.params))

        params_output.append(content)
        params_output.append(params_red_chi2)
        self.params_outputs = np.array(params_output)

        for i in range(len(self.peak1_paramsEdits)):
            self.peak1_paramsEdits[i].setValue(self.params[i])
        self.peak1_contentEdit.setText(str(self.params_outputs[0]))
        self.peak1_chi2Edit.setText(str(self.params_outputs[1]))

        self.plotWidget.plot(clear=True)
        self.plotWidget.plot(self.x, self.y[:-1], stepMode=True)
        fitPen = pg.mkPen(color=(204, 0, 0), width=2)
        self.plotWidget.plot(self.x_fit,
                             lineshape.Single_Crystalball(
                                 self.params, self.x_fit),
                             pen=fitPen)
        self.plotWidget.addItem(self.vLine, ignoreBounds=True)
        self.plotWidget.addItem(self.hLine, ignoreBounds=True)
Exemplo n.º 17
0
def fit2(data,interval,init):
    interval.sort()
    nData = data[(data[:,0] > interval[0])&(data[:,0] < interval[1])]
    x=nData[:,0]
    y=nData[:,1]
    sx=None
    sy=None
    try:
        sx = nData[:,2]
        sy = nData[:,3]
    except:
        pass
    def func(B,x):
        return (1/B[0]) * x
    linear = odrpack.Model(func)
    mydata = odrpack.RealData(x, y, sx=sx, sy=sy)
    myodr = odrpack.ODR(mydata, linear, beta0=[init])
    myoutput = myodr.run()
    myoutput.pprint()
    return [myoutput.beta[0],myoutput.sd_beta[0]]
Exemplo n.º 18
0
def poly_lsq(x, y, n, verbose=False, itmax=200):
    """
    Performs a polynomial least squares fit to the data,
    with errors! Uses scipy odrpack, but for least squares.

    Parameters
    ----------
    x, y : 1-D arrays
        Data to fit.
    n : int
        Polynomial order
    verbose : bool or int, optional
        Can be 0,1,2 for different levels of output (False or True
        are the same as 0 or 1)
    itmax : int, optional
        Maximum number of iterations.

    Returns
    -------
    coeff :  1-D array
        Polynomial coefficients, lowest order first.
    err :  1-D array
        Standard error (1-sigma) on the coefficients.
    """
    func = models.polynomial(n)
    mydata = odr.Data(x, y)
    myodr = odr.ODR(mydata, func, maxit=itmax)
    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)
    fit = myodr.run()
    # Display results:
    if verbose:
        fit.pprint()
    if fit.stopreason[0] == 'Iteration limit reached':
        print('(WWW) poly_lsq: Iteration limit reached, result not reliable!')
    # Results and errors
    coeff = fit.beta
    err = fit.sd_beta
    return coeff, err
Exemplo n.º 19
0
def fit_tls(x: np.ndarray, y: np.ndarray) -> float:
	"""Fit total least squares model to data
	This function fits a total least squares (also known as orthogonal distance regression) model to 2-dimensional data.
	The model has the form `y = m * x`, where m is the "slope". (It has an intercept at y==0).
	See Also:
			At its core, the function uses scipy's `Orthogonal Distance Regression`_ module
			.. _Orthogonal Distance Regression:
					https://docs.scipy.org/doc/scipy/reference/odr.html
			Args:
					x: 1-dimensional Numpy array
					y: 1-dimensional Numpy array of the same size as `x`

			Returns:
					The slope of the fitted model

			"""
	odr_data = odrpack.RealData(x, y)
	model = odrpack.Model(lambda beta, x_: beta[0] * x_)
	odr_container = odrpack.ODR(odr_data, model, beta0=[1.0])
	slope = odr_container.run().beta[0]
	#TODO PLOT SECTOR 0 some pairs (weird results)
	return slope
Exemplo n.º 20
0
def ODRfit(model, X, Y, p0, verbose=False):

    if type(p0) != 'numpy.ndarray':
        try:
            p0, up0 = unpack_unarray(p0)
        except TypeError:
            pass

    x, ux = unpack_unarray(X)
    y, uy = unpack_unarray(Y)

    modello = odrpack.Model(model)
    data = odrpack.RealData(x, y, sx=ux, sy=uy)
    engine = odrpack.ODR(data, modello, beta0=p0)
    output = engine.run()

    P = unc.correlated_values(output.beta, output.cov_beta)

    if verbose:
        output.pprint()

    return P, output.sum_square
Exemplo n.º 21
0
def estimate_spatial_resolution(x,
                                y,
                                ofunc=blurred_step_function,
                                beta0=None,
                                verbose=True):
    """
    Estimate the spatial resolution of an X-ray image from a lineout of an edge.
    It assumes that if the spatial resolution was ideal, that should produce a sharp step.
    Instead we have a step convolved with a gaussian function. This function then estimates 
    the standard deviation of this gaussian. 

    Parameters
    ----------
        x : ndarray : position in physical units [µm, cm, etc]
        y : ndarray : signal on the IP
        ofunc: objective function, here the convolution of a step with a gaussian
        beta0 : initial estimate of parameters. A good estimate could be for instance, 
                   beta0 = [y.mean(), x.mean(), 20, y.mean(), 0]
        verbose: true


    Returns
    -------
      tuple:  (std, std_err)
    """
    from scipy import odr
    mmodel = odr.Model(ofunc)
    mdata = odr.Data(x, y)

    fit = odr.ODR(mdata, mmodel, beta0, maxit=1000)
    fit.set_job(fit_type=2)  # least squares
    fit.set_iprint(final=verbose)
    fit.run()
    beta = fit.output.beta
    error = fit.output.sd_beta

    return beta, error
Exemplo n.º 22
0
def circle_lsq(x, y, up=True, verbose=False, itmax=200, iparams=[]):
    ''' Method to compute a circle fit, It fits for circle
    parameters in the form y = B_2 +/- sqrt(B_0^2-(x-B_1)^2), the sign
    is negative if up is False.

    IN:
       x,y (arr)     - data to fit
       n (int)       - polinomial order
       verbose       - can be 0,1,2 for different levels of output
                         (False or True are the same as 0 or 1)
       itmax (int)   - optional maximum number of iterations.
       iparams (arr) - optional initial parameters b0,b1,b2

    OUT:
       coeff -  polynomial coefficients, lowest order first
       err   - standard error (1-sigma) on the coefficients

    --Tiago, 20080120
    '''

    # circle functions for B=r,x0,y0
    def circle_up(B, x):
        return B[2] + sqrt(B[0]**2 - (x - B[1])**2)

    def circle_dn(B, x):
        return B[2] - sqrt(B[0]**2 - (x - B[1])**2)

    # Derivative of function in respect to x
    def circle_fjd_up(B, x):
        return -(x - B[1]) / (sqrt(B[0]**2 - (x - B[1])**2))

    def circle_fjd_dn(B, x):
        return (x - B[1]) / (sqrt(B[0]**2 - (x - B[1])**2))

    # Derivative of function in respect to B[i]
    def circle_fjb_up(B, x):
        _ret = np.concatenate((
            B[0] / (sqrt(B[0]**2 - (x - B[1])**2)),
            -circle_fjd_up(B, x),
            np.ones(x.shape, float),
        ))
        _ret.shape = (3, ) + x.shape
        return _ret

    def circle_fjb_dn(B, x):
        _ret = np.concatenate((
            B[0] / (sqrt(B[0]**2 - (x - B[1])**2)),
            -circle_fjd_dn(B, x),
            np.ones(x.shape, float),
        ))
        _ret.shape = (3, ) + x.shape
        return _ret

    if any(iparams):

        def circle_est(data):
            return tuple(iparams)
    else:

        def circle_est(data):
            return (1., 1., 1.)

    if up:
        circle_fit = odr.Model(circle_up,
                               fjacd=circle_fjd_up,
                               fjacb=circle_fjb_up,
                               estimate=circle_est)
    else:
        circle_fit = odr.Model(circle_dn,
                               fjacd=circle_fjd_dn,
                               fjacb=circle_fjb_dn,
                               estimate=circle_est)
    mydata = odr.Data(x, y)
    myodr = odr.ODR(mydata, circle_fit, maxit=itmax)
    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)
    fit = myodr.run()
    # Display results:
    if verbose:
        fit.pprint()
    if fit.stopreason[0] != 'Sum of squares convergence':
        if verbose:
            print('(WWW): circle_lsq: fit result not reliable')
        success = 0
    else:
        success = 1
    # Results and errors
    coeff = fit.beta
    err = fit.sd_beta
    return coeff, success, err
Exemplo n.º 23
0
##    Test orth. regression
##
################################################################################

def f(B, theta1):
    return B[0] * (np.cos((2*np.pi)/360*theta1))**2  + B[1]

x = theta1
y = a1
sx = theta2
sy = a2

linear = odrpack.Model(f)

myData = odrpack.RealData(x, y, sx=sx, sy=sy)
myOdr = odrpack.ODR(myData, linear , beta0=[1,1])
myOdr.set_job(fit_type=2) #if set fit_type=2, returns the same as leastsq
out = myOdr.run()
#out.pprint()

coeff = out.beta[::-1]
err   = out.sd_beta[::-1]

print(coeff[0], err[0])
print(coeff[1], err[1])

################################################################################



#print(np.cos(np.deg2rad(theta2))**2)
Exemplo n.º 24
0
import scipy.odr.odrpack as odrpack
filename1 = "LN_data.pkl"
filename2 = "RT_data.pkl"

dataframe1 = pd.read_pickle(filename1)
dataframe2 = pd.read_pickle(filename2)
currents = [0.00, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75]

R_S = "Sample Resistance, R_S (Ohmns)"
R_S_E = "Error in R_S, alpha_R_S (Ohms)"
A = "Angle, theta (degrees)"
A_E = "Error in theta, alpha_theta (degrees)"

slice = dataframe1.loc[dataframe1["current"] == 0.05]
x = slice[A]
y = slice[R_S]
sx = slice[A_E]
sy = slice[R_S_E]

print(x, y, sx, sy)

def f(params, x):
    return params[0]*np.sin(params[1]*x + params[2]) + params[3]

linear = odrpack.Model(f)
mydata = odrpack.RealData(x, y, sx=sx, sy=sy)
myodr = odrpack.ODR(mydata, linear, beta0=[0., 2., 3., 1.])
myoutput = myodr.run()
myoutput.pprint()
#print(myoutput.beta)
Exemplo n.º 25
0
infile = sys.argv[1]
outfile = sys.argv[2]

dataframe = pd.read_pickle(infile)

def f(params, x):
    rho_per, rho_par, shift = params
    return rho_per + (rho_par - rho_per) * np.cos(x - shift)**2

slice = dataframe.loc[dataframe.current == CURRENT]
R_S = "Sample Resistance, R_S (Ohmns)"
R_S_E = "Error in R_S, alpha_R_S (Ohms)"
A = "Angle, theta (degrees)"
A_E = "Error in theta, alpha_theta (degrees)"
xs = slice[A] * np.pi / 180
ys = slice[R_S]
sx = slice[A_E] * np.pi / 180
sy = slice[R_S_E]
beta0 = [0., 2., 0.]
linear = odrpack.Model(f)
mydata = odrpack.RealData(xs, ys, sx=sx, sy=sy)
myodr = odrpack.ODR(mydata, linear, beta0, maxit=100)
myoutput = myodr.run()

fig, ax = plt.subplots()

ax.scatter(xs, ys, marker="+")
ax.plot(xs, f(myoutput.beta, xs))

fig.savefig(outfile)
Exemplo n.º 26
0
linear = odrpack.Model(f)

#for i in range(0):
mydata1 = odrpack.RealData(druck[0],
                           brechungsindex[0],
                           sx=Er_druck[0],
                           sy=Er_brech[0])
mydata2 = odrpack.RealData(druck[1],
                           brechungsindex[1],
                           sx=Er_druck[1],
                           sy=Er_brech[1])
mydata3 = odrpack.RealData(druck[2],
                           brechungsindex[2],
                           sx=Er_druck[2],
                           sy=Er_brech[2])
myodr1 = odrpack.ODR(mydata1, linear, beta0=[1., 2.])
myodr2 = odrpack.ODR(mydata2, linear, beta0=[1., 2.])
myodr3 = odrpack.ODR(mydata3, linear, beta0=[1., 2.])
myoutput1 = myodr1.run()
myoutput2 = myodr2.run()
myoutput3 = myodr3.run()
myoutput1.pprint()

#print(myoutput1.beta[0])
linfit1 = ufloat(myoutput1.beta[0], myoutput1.sd_beta[0])
linfit2 = ufloat(myoutput2.beta[0], myoutput2.sd_beta[0])
linfit3 = ufloat(myoutput3.beta[0], myoutput3.sd_beta[0])
offset1 = myoutput1.beta[1]
offset2 = myoutput2.beta[1]
offset3 = myoutput3.beta[1]
Exemplo n.º 27
0
# Reshape variables into a single column
mag = np.reshape(
    mag, (-1, nt))  # Reshapes variable intro 2D matrix of voxels x timepoints
ph = np.reshape(ph, (-1, nt))
stdm = np.reshape(
    stdm, (-1, ))  # Reshapes variable intro array of length = number of voxels
stdp = np.reshape(stdp, (-1, ))
mask = np.reshape(mask, (-1, ))

for x in range(mag.shape[0]):
    if mask[x]:
        design = ph[x, :]
        ests = [stdm[x] / stdp[x], 1.0]
        mydata = odr.RealData(design, mag[x, :], sx=stdp[x], sy=stdm[x])
        odr_obj = odr.ODR(mydata, linearfit, beta0=ests, maxit=600)
        res = odr_obj.run()
        est = res.y

        r2[x] = 1.0 - (np.sum((mag[x, :] - est)**2) / np.sum((mag[x, :])**2))

        # take out scaled phase signal and re-mean may need correction
        sim[x, :] = ph[x, :] * res.beta[0]
        filt[x, :] = mag[x, :] - est
        # estimate residuals
        residuals[x, :] = np.sign(mag[x, :] - est) * (
            np.sum(res.delta**2, axis=0) + res.eps**2)
        delta[x, :] = np.sum(
            res.delta, axis=0
        )  # res.delta --> Array of estimated errors in input variables (same shape as x)
        eps[x, :] = res.eps  # res.eps --> Array of estimated errors in response variables (same shape as y)
Exemplo n.º 28
0
 stdbin = delete(stdbin, delidx)
 
 
 '''
 if mb == 4.25:
     print(len(mmi[idx]))
     print(log10(rrup[idx]))
     print(medbin, binstrp)
     print(eqname[idx])
     crash
 '''
 if len(medbin < 2) and mb != 4.25:    
     # get intercept
     data = odrpack.RealData(binstrp, medbin)
     intfit = odrpack.Model(linear_fixed_slope)
     odr = odrpack.ODR(data, intfit, beta0=[5.0])
     
     #data = odrpack.RealData(10**binstrp, medbin, meta={'mag':mb})
     #intfit = odrpack.Model(near_src_trunc_fixed_slope)
     #odr = odrpack.ODR(data, intfit, beta0=[5.0])
     
     odr.set_job(fit_type=2) #if set fit_type=2, returns the same as leastsq, 0=ODR
     out = odr.run()
     intercept = out.beta
     intercepts.append(intercept[0])
     meanmagbin.append(mean(mmi[idx]))
     meanmws.append(mean(mw[idx]))
 else:
     intercepts.append(nan)
     meanmagbin.append(nan)
     meanmws.append(nan)
Exemplo n.º 29
0
def gauss_lsq(x,
              y,
              weight_x=1.,
              weight_y=1.,
              verbose=False,
              itmax=200,
              iparams=[]):
    """
    Performs a Gaussian least squares fit to the data,
    with errors! Uses scipy odrpack, but for least squares.

    Parameters
    ----------
    x : 1D array-like
        Observed data, independent variable
    y : 1D array-like
        Observed data, dependent variable
    weight_x: array-like, optional
        Weights for independent variable. This is typically based on the errors,
        if any. With errors, normal weights should be 1/err**2. If weight is a
        scalar, the same weight will be used for all points and therefore its
        value is irrelevant.
    weight_y: array-like, optional.
        Weights for independent variable. This is typically based on the errors,
        if any. With errors, normal weights should be 1/err**2. For Poisson
        weighing, should be 1/y.
    verbose: boolean
        If True, will print out more detailed information about the result.
    itmax: integer, Optional
        Maximum number of iterations, default is 200.
    iparams: list, optional
        Starting guess of Gaussian parameters. Optional but highly recommended
        to use realistic values!

    Returns
    -------
    output: tuple
        Tuple with containing (coeff, err, itlim), where coeff are the fit
        resulting coefficients (same order as Gaussian function above), err are
        the errors on each coefficient, and itlim is the number of iterations.

    Notes
    -----
    See documentation of scipy.odr.ordpack for more information.
    """
    def _gauss_fjd(B, x):
        # Analytical derivative of gaussian with respect to x
        return (B[0] - x) / B[1]**2 * gaussian(np.concatenate(
            (B[:3], [0.])), x)

    def _gauss_fjb(B, x):
        gauss1 = gaussian(np.concatenate((B[:3], [0.])), x)
        # Analytical derivatives of gaussian with respect to parameters
        _ret = np.concatenate(
            ((x - B[0]) / B[1]**2 * gauss1,
             ((B[0] - x)**2 - B[1]**2) / B[1]**3 * gauss1, gauss1 / B[2],
             np.ones(x.shape, float)))
        _ret.shape = (4, ) + x.shape
        return _ret

    # Centre data in mean(x) (makes better conditioned matrix)
    mx = np.mean(x)
    x2 = x - mx
    if not any(iparams):
        iparams = np.array([
            x2[np.argmax(y)],
            np.std(y),
            np.sqrt(2 * np.pi) * np.std(y) * max(y), 1.
        ])
    gauss = odr.Model(gaussian, fjacd=_gauss_fjd, fjacb=_gauss_fjb)
    mydata = odr.Data(x2, y, wd=weight_x, we=weight_y)
    myodr = odr.ODR(mydata, gauss, beta0=iparams, maxit=itmax)
    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)
    fit = myodr.run()
    # Display results:
    if verbose:
        fit.pprint()
        print('Re-centered Beta: [%f  %f  %f %f]' %
              (fit.beta[0] + mx, fit.beta[1], fit.beta[2], fit.beta[3]))
    itlim = False
    if fit.stopreason[0] == 'Iteration limit reached':
        itlim = True
        print('(WWW) gauss_lsq: Iteration limit reached, result not reliable!')
    # Results and errors
    coeff = fit.beta
    coeff[0] += mx  # Recentre in original axis
    err = fit.sd_beta
    return coeff, err, itlim
Exemplo n.º 30
0
def quad_lsq(x, y, verbose=False, itmax=200, iparams=[]):
    ''' Method to compute a parabola fit, more handy as it fits for
    parabola parameters in the form y = B_0 * (x - B_1)**2 + B_2.
    This is computationally slower than poly_lsq, so beware of its usage
    for time consuming operations.

    IN:
       x,y (arr)     - data to fit
       n (int)       - polinomial order
       verbose       - can be 0,1,2 for different levels of output
                         (False or True are the same as 0 or 1)
       itmax (int)   - optional maximum number of iterations.
       iparams (arr) - optional initial parameters b0,b1,b2

    OUT:
       coeff -  polynomial coefficients, lowest order first
       err   - standard error (1-sigma) on the coefficients


    --Tiago, 20071115
    '''

    # Tiago's internal new definition of quadratic
    def _quadratic(B, x):
        return B[0] * (x - B[1]) * (x - B[1]) + B[2]

    def _quad_fjd(B, x):
        return 2 * B[0] * (x - B[1])

    def _quad_fjb(B, x):
        _ret = np.concatenate((
            np.ones(x.shape, float),
            2 * B[0] * (B[1] - x),
            x * x - 2 * B[1] * x + B[1] * B[1],
        ))
        _ret.shape = (3, ) + x.shape
        return _ret

    if any(iparams):

        def _quad_est(data):
            return tuple(iparams)
    else:

        def _quad_est(data):
            return (1., 1., 1.)

    quadratic = odr.Model(_quadratic,
                          fjacd=_quad_fjd,
                          fjacb=_quad_fjb,
                          estimate=_quad_est)
    mydata = odr.Data(x, y)
    myodr = odr.ODR(mydata, quadratic, maxit=itmax)
    # Set type of fit to least-squares:
    myodr.set_job(fit_type=2)
    if verbose == 2:
        myodr.set_iprint(final=2)
    fit = myodr.run()
    # Display results:
    if verbose:
        fit.pprint()
    if fit.stopreason[0] == 'Iteration limit reached':
        print('(WWW) quad_lsq: iteration limit reached, result not reliable!')
    # Results and errors
    coeff = fit.beta
    err = fit.sd_beta
    return coeff, err