Example #1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-r', '--rthreshold', type=float, default=0.85)
    parser.add_argument('-rp',
                        '--regression-plot',
                        dest='regression_plot',
                        action='store_true')
    parser.add_argument('wind_csv', type=Path, default=False)
    params = parser.parse_args()

    matplotlib.rcParams.update({'font.size': 8.5})
    global_subplots_params = {
        'figsize': (4.69, 3.52),
        'constrained_layout': True
    }
    trials_result = dict()
    all_points = list()
    with open(params.wind_csv, 'r', newline='') as wind_csv:
        wind_reader = csv.reader(wind_csv)
        # skip the header row
        next(wind_reader)
        for trial in wind_reader:
            if trial[4].startswith('skip'):
                continue
            print('processing', trial[0])
            ddm_time_fit_file_prefix = Path(
                trial[1]) / 'auto_correlate_data_lin_diff_diff.npy'
            params_fit_path = \
                ddm_time_fit_file_prefix.with_stem(ddm_time_fit_file_prefix.stem + '.polar.params_fit')
            median_angle_path = \
                ddm_time_fit_file_prefix.with_stem(ddm_time_fit_file_prefix.stem + '.polar.median_angle')
            median_radius_path = \
                ddm_time_fit_file_prefix.with_stem(ddm_time_fit_file_prefix.stem + '.polar.median_radius')
            params_fit = np.load(params_fit_path)
            median_angle = np.load(median_angle_path)
            median_radius = np.load(median_radius_path)

            with open(trial[3]) as vid_info_file:
                vid_info = json.load(vid_info_file)
            with open(trial[2]) as calibration_file:
                calibration = json.load(calibration_file)
            frame_interval = vid_info['framerate'][1] / vid_info['framerate'][0]
            # freq_factor = np.pi / ((max_time_index - 1) * frame_interval)
            # freq_factor = 1 / frame_interval
            wavenumber_factor = 2 * np.pi / (vid_info['fft']['size'] *
                                             calibration['calibration_factor'])
            wavenumber_unit = '\\mathrm{{rad}}\\,\\mathrm{{{}}}^{{-1}}'.format(
                calibration['physical_unit'])
            # print(f'wavenumber_factor = {wavenumber_factor}')
            # print(f'frame_interval = {frame_interval} s')

            params_fit[..., 2] /= frame_interval
            freq_fit = params_fit[:, 1:, 2]
            angle_indices = (int(i)
                             for i in trial[4].split()) if trial[4] else range(
                                 freq_fit.shape[0])
            do_plot = trial[6].lower(
            )[0] == 't' if trial[6] else params.regression_plot
            slope_list = list()
            slopeerr_list = list()
            wavenumber_space = median_radius[1:] * wavenumber_factor
            fig_path_prefix = ddm_time_fit_file_prefix.with_suffix('.png')
            fig_stem_prefix = ddm_time_fit_file_prefix.stem + '.polar.freq_fit'
            for angle_index in angle_indices:
                finite_mark = np.isfinite(freq_fit[angle_index])
                regress = linregress(wavenumber_space[finite_mark],
                                     freq_fit[angle_index, finite_mark])
                # print(trial[0], angle_index, regress.slope, regress.intercept, regress.rvalue, regress.stderr)

                if regress.rvalue >= params.rthreshold:
                    print('using', angle_index)
                    slope_list.append(regress.slope)
                    slopeerr_list.append(regress.stderr)
                else:
                    print('skipping', angle_index)
                if do_plot:
                    lin_fit = regress.intercept + wavenumber_space * regress.slope
                    suptitle = 'Radial dependence of $\\Omega$ parameter as fitted along median ' + \
                        f'$q_\\theta={median_angle[angle_index]}\\degree$\n$R={regress.rvalue}$'
                    # fig, ax = plt.subplots(**global_subplots_params)
                    fig = matplotlib.figure.Figure(**global_subplots_params)
                    ax = fig.add_subplot()
                    ax: matplotlib.axes.Axes
                    ax.plot(wavenumber_space, freq_fit[angle_index])
                    ax.plot(wavenumber_space, lin_fit)
                    ax.set_xlabel(f'$q_r$ (${wavenumber_unit}$)')
                    ax.set_ylabel('$\\Omega$ (rad/s)')
                    fig.suptitle(suptitle)
                    fig.savefig(
                        fig_path_prefix.with_stem(fig_stem_prefix +
                                                  str(angle_index).zfill(2)),
                        dpi=300)

            slopes = np.array(slope_list)
            slope_av = np.mean(slopes)
            if len(slope_list) <= 1:
                slope_std = 0
            else:
                slope_std = np.std(slopes, ddof=1)
            slopes_err = np.array(slopeerr_list)
            slopes_err_av = np.mean(slopes_err)
            print(slope_std, slopes_err_av)
            slope_err = max(slope_std, slopes_err_av)

            entry = (float(trial[-2]), float(trial[-1]), slope_av, slope_err)
            print(trial[-3], *entry)
            trials_list = trials_result.setdefault(trial[-3], list())
            trials_list.append(entry)
            if not (trial[6] and trial[6].lower()[0] == 't'):
                all_points.append(entry)

        all_points = np.array(all_points)
        odr_data = odr.RealData(all_points[:, 0], all_points[:, -2],
                                all_points[:, 1], all_points[:, -1])
        linear_model = odr.polynomial(1)
        odr_obj = odr.ODR(odr_data, linear_model)
        odr_output = odr_obj.run()
        odr_output.pprint()
        fit_x = np.linspace(0, 3)
        fit_y = odr_output.beta[0] + odr_output.beta[1] * fit_x
        # fit_y_min = (odr_output.beta[0] + odr_output.sd_beta[0]) + \
        #     (odr_output.beta[1] - odr_output.sd_beta[1]) * fit_x
        # fit_y_max = (odr_output.beta[0] - odr_output.sd_beta[0]) + \
        #     (odr_output.beta[1] + odr_output.sd_beta[1]) * fit_x

        # regress_result = linregress(all_points[:, 0], all_points[:, -2])
        # fit_x = np.linspace(0, 3)
        # fit_y = regress_result.intercept + regress_result.slope * fit_x

        # colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22',
        #           '#17becf', '#1a55FF']
        colors = ['maroon', 'indianred', 'royalblue']
        fig, ax = plt.subplots(**global_subplots_params)
        fig = plt.Figure(**global_subplots_params)
        ax = fig.add_subplot()
        ax: matplotlib.axes.Axes
        ax.plot(fit_x,
                fit_y,
                color='slategrey',
                linewidth=.7,
                linestyle='-',
                label='Linear fit')
        # ax.plot(fit_x, fit_y_min)
        # ax.plot(fit_x, fit_y_max)
        for c, (wind_type, trials_list) in zip(colors, trials_result.items()):
            trials_array = np.array(trials_list)
            print(c, wind_type)
            ax.errorbar(trials_array[:, 0],
                        trials_array[:, -2],
                        trials_array[:, -1],
                        trials_array[:, 1],
                        linestyle='',
                        ecolor=c,
                        capsize=3,
                        capthick=1,
                        label=wind_type)
        ax.set_xlim(0, 3)
        ax.set_ylim(0)
        ax.set_xlabel(r'Wind speed ($\mathrm{m}\,\mathrm{s}^{-1}$)')
        ax.set_ylabel(r'Wave speed ($\mathrm{m}\,\mathrm{s}^{-1}$)')
        ax.legend()
        fig.savefig('fig.svg', dpi=300)
Example #2
0
def approximate(order_red, pos, dit, t_min, t_max, L, u_mean):
    t_ = np.array(sorted(list(order_red.keys())))
    dt_ = np.diff(t_)
    dt = dt_.max() * dit
    t_ = np.arange(t_min + dt, t_max - dt, dt)

    data = []
    for t in t_:
        ic_ = order_red[t]
        for i in range(1, len(ic_)):
            ic = ic_[i]
            ic_upst = ic_[i - 1]
            if t + dt in pos[ic]:
                x_this = pos[ic][t]
                x_upst = pos[ic_upst][t]
                x_next = pos[ic][t + dt]

                dx = x_next - x_this
                if dx < -L / 2:
                    dx += L
                elif dx > L / 2:
                    dx -= L
                dxdt = dx / dt
                l_upst = x_this - x_upst

                if np.isfinite(dxdt) and np.isfinite(l_upst):
                    data.append((dxdt, l_upst))

    print("number of data points:", len(data))

    data = np.array(data)
    ids = np.logical_and(np.isfinite(data[:, 0]), np.isfinite(data[:, 1]))
    data = data[ids]

    l_bs, dx_bs = bootstrap(data[:, 1], data[:, 0], nbins=200)

    odr_data = odr.RealData(l_bs[:, 0],
                            dx_bs[:, 0],
                            sx=l_bs[:, 1],
                            sy=dx_bs[:, 1])
    odr_obj = odr.ODR(odr_data,
                      odr.Model(func2),
                      beta0=(-u_mean, rates.v_0, 1. / rates.l_c))
    out = odr_obj.run()
    popt_odr = out.beta

    #popt, pcov = curve_fit(func, data[:, 1], data[:, 0],
    #                       p0=popt_odr)

    plt.figure()
    # plt.scatter(data[:, 1], data[:, 0])
    x = np.linspace(data[:, 1].min(), data[:, 1].max(), 1000)
    plt.plot(x, func(x, *popt_odr), 'r-')
    #plt.plot(x, func(x, *popt), 'y-')
    plt.plot(x, func(x, -u_mean, rates.v_0, 1. / rates.l_c), 'k')
    plt.errorbar(l_bs[:, 0],
                 dx_bs[:, 0],
                 xerr=l_bs[:, 1],
                 yerr=dx_bs[:, 1],
                 fmt='m')

    plt.figure()
    plt.hist2d(data[:, 1], data[:, 0], bins=(200, 200))
    plt.show()

    v_inf = popt_odr[0]
    v_0 = popt_odr[1]
    l_c = 1. / popt_odr[2]

    print("v_inf =", v_inf, "(compare with", u_mean, "?)")
    print("v_0   =", v_0)
    print("l_c   =", l_c)

    dz = data[:, 0] - func(data[:, 1], *popt_odr)

    mu, sigma = norm.fit(dz)
    print("mu    =", mu)
    print("sigma =", sigma)
    D = sigma**2 * dt
    print("D     =", D)

    print(dit, dt, v_inf, v_0, l_c, D)
Example #3
0
    def Multilinearfit(self, adddisp=False, PRINT=False):

        Fct = lambda B, X: self._lin_fcn(B, X)
        jac = lambda B, X: self._lin_fjb(B, X)
        jacd = lambda B, X: self._lin_fjd(B, X)

        if self.Beta00 is None:

            if type(self.X[0]) == np.float64:
                BETA0 = (1, 0)
            else:
                BETA0 = (len(self.X), )
                for i in range(len(self.X)):
                    BETA0 += (len(self.X) - i - 1, )

        else:
            BETA0 = self.Beta00

        if self.covx is not None:
            dataXZ = odrs.RealData(self.X,
                                   self.y,
                                   sy=self.yerr,
                                   covx=self.covx)
        else:
            dataXZ = odrs.RealData(self.X, self.y, sx=self.xerr, sy=self.yerr)
        estXZ = self._lin_est(dataXZ)

        MODELXZ = odrs.Model(Fct, fjacb=jac, fjacd=jacd, estimate=estXZ)
        odrXZ = odrs.ODR(dataXZ, MODELXZ, beta0=BETA0, maxit=1000, sstol=0.)
        output = odrXZ.run()
        BETA1 = (output.beta[0], )
        for i in range(len(output.beta) - 1):
            BETA1 += (output.beta[i + 1], )

        odrXZ = odrs.ODR(dataXZ, MODELXZ, beta0=BETA1, maxit=1000, sstol=0.)
        output = odrXZ.run()

        alpha = np.zeros(len(output.beta[1:]))
        for correction in range(len(output.beta[1:])):
            alpha[correction] = output.beta[correction + 1]
        M0 = output.beta[0]
        chi2_ODR = output.sum_square

        self.disp = 0.
        self.alpha = alpha
        self.M0 = output.beta[0]
        self.dof = len(self.y) - len(output.beta[1:]) - 1.

        if adddisp:
            calls = 0
            self.pouet = 0.
            if (chi2_ODR / (self.dof)) < 1.:
                if PRINT:
                    print 'ok'
            else:
                while abs((chi2_ODR / (self.dof)) - 1.) > 0.001:

                    if calls < 100:
                        if PRINT:
                            print 'search of dispertion : %i' % (calls + 1)
                        self._compute_dispertion()

                        if self.covx is not None:
                            dataXZ = odrs.RealData(self.X,
                                                   self.y,
                                                   sy=np.sqrt(self.yerr**2 +
                                                              self.disp**2),
                                                   covx=self.covx)
                        else:
                            dataXZ = odrs.RealData(self.X,
                                                   self.y,
                                                   sx=self.xerr,
                                                   sy=np.sqrt(self.yerr**2 +
                                                              self.disp**2))
                        estXZ = self._lin_est(dataXZ)

                        MODELXZ = odrs.Model(Fct,
                                             fjacb=jac,
                                             fjacd=jacd,
                                             estimate=estXZ)
                        odrXZ = odrs.ODR(dataXZ,
                                         MODELXZ,
                                         beta0=BETA0,
                                         maxit=1000,
                                         sstol=0.)
                        output = odrXZ.run()

                        chi2_ODR = output.sum_square
                        for correction in range(len(output.beta[1:])):
                            self.alpha[correction] = output.beta[correction +
                                                                 1]
                        self.M0 = output.beta[0]
                        calls += 1

                    else:
                        print 'error : calls limit are exceeded'
                        break

        self.output = output
Example #4
0
    def BestLocalFit(self,ch):
        
        nhits = 2*sum(self.dic_layer_cnt[ch])
                
        R = 100000
        best_parameters = []
        
        arr = np.empty(shape=(nhits,2))

        i=0
        for event in self.df.itertuples():
                    
            if event.chamber == ch:
                arr[i] = [event.xleft_g,event.z_g]
                arr[i+1] = [event.xright_g,event.z_g]
                i+=2    
            else:
                continue
        
        
        # to do fit for groups of 3 and 4 points (when is possible) 
        if 0 in self.dic_layer_cnt[ch]:
            k = 3
        else:
            k = 4
                                                
        p = np.empty(shape=(k,2))
        best_hits = np.empty(shape=(k,2))
        lst = list(range(nhits))    
        
        for case in combinations(lst,k):
            a=0
            for index in case:
                p[a] = arr[index]
                a+=1

                                   
            # to prevent fit on points in the same layer (same z)
            cnt_z = [0 for t in range(k)]
            for j in range(k):
                cnt_z[j] = list(p[:,1]).count(p[j,1])
            if cnt_z != [1 for l in range(k)]:
                continue
                
            
            linear_model = odr.Model(fitting_func)
            data = odr.RealData(p[:,0],p[:,1],sx=c.XERR)
            odr1 = odr.ODR(data=data,model=linear_model,beta0=[0,0])
            out = odr1.run()
            reduced_chisquare = out.res_var
            
            if reduced_chisquare < R:
                R = reduced_chisquare
                best_parameters = out.beta
                
                h=0
                for index in case:
                    best_hits[h] = arr[index]
                    h+=1                
                    
            else:
                continue                          
        
        results = (best_parameters[0],best_parameters[1],best_hits)
 
        return results
Example #5
0
#Model function and object
def fit_function(beta, x):
	A, B = beta
	return A * x + B

model = odr.Model(fit_function)

#Data and data object
w = np.array([0.8275625283588963, 1.2165534798712103, 1.4843383070656362, 1.5551328356806084, 1.6087827103854795, 1.6684484310595435, 1.713337888359156, 1.7482510850226676, 1.7592175396035283, 1.763938958791798, 1.7922753840328536, 1.8145424098679928, 1.820789825519454, 1.8115091076514478, 1.8231985764875462, 1.834114675420635, 1.8433173540544594, 1.851764009781818, 1.8595696920265226, 1.866827233319856, 1.8733018031301882, 1.8796769203234778, 1.8851913932652993, 1.8905831346167017, 1.8951944186701835, 1.8995589602623755, 1.9037034690349952, 1.907458995956483, 1.9082876254165595, 1.9122086557902023, 1.915674398397806, 1.9190086872068828, 1.9217207196496475])
N = np.arange(2, 100, 3)

x = np.sin(np.pi/N)
y = 2 / w - 1

#Exclude first 4 points
data = odr.RealData(x, y)

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

#Run regression
out = odrm.run()

#Extract parameters
betaout = out.beta
betaerr = out.sd_beta
print('Fit parameters and their std deviations')
print('-------------------')
for i in range(len(betaout)):
	print('Parameter {}: '.format(i + 1) + str(betaout[i]) + ' +- ' + str(betaerr[i]))
Example #6
0
    def init_gene_coverage_consistency_information(self):
        """ Perform orthogonal distance regression for each gene to determine coverage consistency.
            
            The question that we are trying to ask is:
                Do the non-outlier nt coverage of the gene in samlpes correlates to the non-outlier
                nt coverage of the genome in samples?

            The regression is performed only for positive samples.
            For each gene, the regression is performed only according to samples in which
            the gene is present (according to the detection critrea).
        """

        if not self.gene_presence_absence_in_samples_initiated:
            self.init_gene_presence_absence_in_samples()

        if not self.gene_coverage_stats_dict_of_dfs_initiated:
            self.init_gene_coverage_stats_dict_of_dfs()

        self.progress.new("Computing coverage consistency for all genes.")
        progress.update('...')
        num_genes, counter = len(
            self.profile_db.gene_level_coverage_stats_dict.keys()), 1
        for gene_id in self.profile_db.gene_level_coverage_stats_dict.keys():
            if num_genes > 100 and counter % 100 == 0:
                self.progress.update('%d of %d genes...' %
                                     (counter, num_genes))

            # samples in which the gene is present
            _samples = self.gene_presence_absence_in_samples.loc[
                gene_id, self.gene_presence_absence_in_samples.loc[
                    gene_id, ] == True].index
            # mean and std of non-outlier nt in each sample
            x = self.samples_coverage_stats_dicts.loc[
                _samples, 'non_outlier_mean_coverage']
            std_x = self.samples_coverage_stats_dicts.loc[
                _samples, 'non_outlier_coverage_std']
            if len(_samples) > 1:
                # mean and std of non-outlier nt in the gene (in each sample)
                y = self.gene_coverage_stats_dict_of_dfs[gene_id].loc[
                    _samples, 'non_outlier_mean_coverage']
                std_y = self.gene_coverage_stats_dict_of_dfs[gene_id].loc[
                    _samples, 'non_outlier_coverage_std']

                # performing the regression using ODR
                _data = odr.RealData(list(x.values), list(y.values),
                                     list(std_x.values), list(std_y.values))
                _model = lambda B, c: B[0] * c
                _odr = odr.ODR(_data, odr.Model(_model), beta0=[3])
                odr_output = _odr.run()

                # store results
                self.gene_coverage_consistency_dict[gene_id] = {}
                self.gene_coverage_consistency_dict[gene_id][
                    'slope'] = odr_output.beta[0]
                self.gene_coverage_consistency_dict[gene_id][
                    'slope_std'] = odr_output.sd_beta[0]
                self.gene_coverage_consistency_dict[gene_id][
                    'slope_precision'] = odr_output.sd_beta[
                        0] / odr_output.beta[0]

                # compute R squered
                f = lambda b: lambda _x: b * _x
                R_squered = 1 - sum(
                    (np.apply_along_axis(f(odr_output.beta[0]), 0, x) -
                     y.values)**2) / sum((y - np.mean(y.values))**2)

                # Check if converged
                self.gene_coverage_consistency_dict[gene_id][
                    'R_squered'] = R_squered
                if odr_output.stopreason[0] == 'Sum of squares convergence':
                    self.gene_coverage_consistency_dict[gene_id][
                        'converged'] = True
                else:
                    self.gene_coverage_consistency_dict[gene_id][
                        'converged'] = False

        self.gene_coverage_consistency_dict_initiated = True
        self.progress.end()
Example #7
0
        data = (x1, y1, sx1, sy1)
        sampler1 = MCMCfunc(data, bounds1,
            n_walkers=n_walkers,
            n_burn_in_steps=n_burn_in_steps,
            n_steps=n_steps)

        # Plot the posteriors to see if a reasonable result was obtained.
        plt.ion()
        plotSamples = sampler1.flatchain[:,plotInds]
        plotBounds  = np.array(bounds1)[plotInds]
        corner.corner(plotSamples, bins=100,
            range=plotBounds,
            labels=plotLabels)

        # Setup the orthogonal-distance-regression (ODR) for band 1
        data1 = odr.RealData(x1, y1, sx=sx1, sy=sy1)
        odr1  = odr.ODR(data1, lineModel, beta0=[0.0, np.median(y1)])
        out1  = odr1.run()

        plt.ion()
        plt.figure()
        plt.errorbar(x1, y1, xerr=sx1, yerr=sy1, linestyle='none')
        plt.scatter(x1, y1)
        xlims = np.array([0, 1.2])
        plt.plot(xlims, xlims*out1.beta[0] + out1.beta[1], color='b')

        out1b = np.median(plotSamples, axis=0)
        out1b[1] = np.max(plotSamples[:,1])
        plt.plot(xlims, xlims*out1b[0] + out1b[1], color='r')
        import pdb; pdb.set_trace()
        plt.close('all')
Example #8
0
def linefitting(x,
                y,
                xerr=None,
                yerr=None,
                xrange=[-5, 5],
                color='b',
                prob=.95,
                yoffset=0):
    # Initial guess from simple linear regression
    sorted = np.argsort(x)
    b, a, rval, pval, std_err = stats.linregress(x[sorted], y[sorted])
    print("\nLineregress parameters: ", a, b)
    # Run the fit
    fitobj = kmpfit.Fitter(residuals=residuals, data=(x, y, xerr, yerr))
    fitobj.fit(params0=[a, b])
    print("\n======== Results kmpfit with effective variance =========")
    print("Fitted parameters:      ", fitobj.params)
    print("Covariance errors:      ", fitobj.xerror)
    print("Standard errors:        ", fitobj.stderr)
    print("Chi^2 min:              ", fitobj.chi2_min)
    print("Reduced Chi^2:          ", fitobj.rchi2_min)
    print("Status Message:", fitobj.message)
    c, d = fitobj.params
    e, f = fitobj.stderr
    # Alternative method using Orthogonal Distance Regression
    linear = odr.Model(model)
    mydata = odr.RealData(x, y, sx=xerr, sy=yerr)
    myodr = odr.ODR(mydata, linear, beta0=[a, b])
    myoutput = myodr.run()
    myoutput.pprint()
    # Plot the results
    xmod = np.linspace(xrange[0], xrange[1], 50)
    #ymod0 = model([a, b], xmod)
    ymod = model([c, d], xmod)
    plt.plot(xmod, ymod, linestyle='--', color=color, zorder=1)
    axes = plt.gca()
    dfdp = [1, xmod]
    ydummy, upperband, lowerband = fitobj.confidence_band(
        xmod, dfdp, prob, model)
    verts = list(zip(xmod, lowerband)) + list(zip(xmod[::-1], upperband[::-1]))
    poly = Polygon(verts,
                   closed=True,
                   fc='c',
                   ec='c',
                   alpha=0.3,
                   label="{:g}% conf.".format(prob * 100))
    axes.add_patch(poly)
    # The slope
    axes.text(0.03,
              0.95 - yoffset,
              'a = $%4.2f$ ' % d + u'\u00B1' + ' $%4.2f$' % f,
              size=10,
              transform=axes.transAxes,
              color=color)
    # The intercept
    axes.text(0.03,
              0.90 - yoffset,
              'b = $%4.2f$ ' % c + u'\u00B1' + ' $%4.2f$' % e,
              size=10,
              transform=axes.transAxes,
              color=color)
    return
    ###############
    # Get PE value
    ###############
    # Close any remaining plots before proceeding to show the user the graphical
    # summary of the calibration data.
    plt.close('all')

    # Define the model to be used in the fitting
    def PE(slope, x):
         return slope*x

    # Set up ODR with the model and data.
    PEmodel = odr.Model(PE)
    data = odr.RealData(
        expectedPol,
        measuredPol,
        sx=uncertInExpectedPol,
        sy=uncertInMeasuredPol
    )

    # Initalize the full odr model object
    odrObj = odr.ODR(data, PEmodel, beta0=[1.])

    # Run the regression.
    PEout = odrObj.run()

    # Use the in-built pprint method to give us results.
    print(thisFilter + '-band PE fitting results')
    PEout.pprint()


    print('\n\nGenerating P plot')
Example #10
0
def D(A: list, t: float):
    return np.sqrt(A[0] + A[1] * t)


t = np.linspace(0, 9, 1000)
tmeet = np.array([0.25, 0.75, 1.25, 2.5, 3.0, 3.75, 4.5, 5.5, 7.0, 8.5])
dmeet = np.array([1.43, 2.06, 2.09, 2.66, 3.31, 3.52, 3.79, 4.12, 4.35, 4.95])
sig_dmeet = np.array(
    [0.20, 0.20, 0.20, 0.20, 0.20, 0.20, 0.20, 0.20, 0.20, 0.20])

astart = 1
bstart = 2

odr_model = odr.Model(D)
odr_data = odr.RealData(tmeet, dmeet, sy=sig_dmeet)
odr_obj = odr.ODR(odr_data, odr_model, beta0=[astart, bstart])
odr_obj.set_job(fit_type=2)

odr_res = odr_obj.run()

par_best = odr_res.beta
par_sig = odr_res.sd_beta
par_cov = odr_res.cov_beta
odr_res.pprint()

fig, ax = plt.subplots(num="figuur 1")

(line1, ) = ax.plot(t, D(par_best, t), color="blue",
                    zorder=5)  # we plotten de beste schatter volgens scipy.odr
Example #11
0
# on our slope and intercept.

# Here, in testing, we will have uniform errors to begin with. Then we will make a
# few have non-uniform errors afterwards and see how things differ.


# Define the function to fit against
def func(params, x):
    return params[0] * x + params[1]


# Create the model
linear = odr.Model(func)

# Create the data
mydata = odr.RealData(x_data, y_data, sx=x_data_err, sy=y_data_err)

# Setting up ODR
myodr = odr.ODR(mydata, linear, beta0=[1.0, 2.0])

# Run the fit
output = myodr.run()

# See the output
output.pprint()

# Get out the params
m_fit = output.beta[0]
c_fit = output.beta[1]

m_fiterr = output.sd_beta[0]
def fit(fname, save=False):
    from scipy import odr

    def red_chi_sq(f, B, x, y, sy):
        return np.sum((y - f(B, x))**2 / sy**2) / (len(y) - len(B))

    def r_sq(f, B, x, y):
        return 1. - np.sum((y - f(B, x))**2) / np.sum((y - y.mean())**2)

    def linear_model_func(B, x):
        return B[0] * x + B[1]

    linear_model = odr.Model(linear_model_func)

    fname = fname % ('moments', 'z')
    m_file = np.load(fname)
    m, d = m_file['m'], m_file['d']

    hi = 9e-2
    lo = 9e-3
    d = d / 1024
    domain = (lo < d) & (d < hi)

    d_m = (m[..., 1] / m[..., 0])[domain]
    l_m = np.log(m[..., 0][domain])
    l_d = np.log(d[domain])
    zeta_arr = np.empty((m.shape[1], 2))
    C_arr = np.empty_like(zeta_arr)
    d_z_arr = np.empty(m.shape[1])
    d_C_arr = np.empty_like(d_z_arr)
    r_sq_arr = np.empty_like(d_z_arr)

    print('\n\n\t[%s]\n' % fname)
    print('[p]\tzeta\t\t\t\t\tC\t\t\t\t\tred chi sq\tr^2')
    print('===\t===\t\t\t\t\t===\t\t\t\t\t===\t\t===')
    for i in range(l_m.shape[1]):
        data = odr.RealData(l_d, l_m[:, i], sy=d_m[:, i])
        out = odr.ODR(data, linear_model, beta0=[1., 0.]).run()
        B, sd_B = out.beta, out.sd_beta
        red_chi_sq_test = red_chi_sq(linear_model_func, B, l_d, l_m[:, i],
                                     d_m[:, i])
        r_sq_test = r_sq(linear_model_func, B, l_d, l_m[:, i])
        zeta_arr[i, 0], zeta_arr[i, 1] = B[0], sd_B[0]
        C_arr[i, 0], C_arr[i, 1] = B[1], sd_B[1]
        d_z_arr[i] = abs(sd_B[0] / B[0]) * 100
        d_C_arr[i] = abs(sd_B[1] / B[1]) * 100
        r_sq_arr[i] = r_sq_test
        print('[%2d]\t%f +- %f (%.2f%%)\t\t%f +- %f (%.2f%%)\t\t%f\t%f' \
              % (i, B[0], sd_B[0], d_z_arr[i],
                 B[1], sd_B[1], d_C_arr[i],
                 red_chi_sq_test, r_sq_test))

    if save:
        np.savetxt(os.path.splitext(fname)[0] + '.csv',
                   np.vstack(
                       (np.arange(1, 13), zeta_arr[:, 0], zeta_arr[:, 1],
                        d_z_arr, C_arr[:, 0], C_arr[:,
                                                    1], d_C_arr, r_sq_arr)).T,
                   fmt=['%d'] + 7 * ['%.3f'],
                   header='p,t,dt,dtr,c,dc,dcr,rsq',
                   comments='',
                   delimiter=',')

    return (zeta_arr, C_arr), (l_d, l_m)
Example #13
0
            textcoords='offset points',
        )

    else:
        plt.annotate(key, (x, y), xytext=(5, 10), textcoords='offset points')

ax.set_xlabel('Kd (nM)')
ax.set_ylabel('Prot BS z Covariance with Prot BS ')
#ax.vlines(np.mean([Kds[kk] for kk in to_exclude]), min_err, max_err, linestyles = 'dashed', color = 'firebrick', label = 'max error bar', linewidths = 2.)
ax.legend()
fig.savefig(save_path + 'corr_plot' + str(wtc7) + '_all.pdf', format='pdf')
plt.show()

#FACCIO E STAMPO IL FIT

mydata = odr.RealData(Kds[1:], correlators[1:], sy=err / 2, sx=Kds_errs[1:])
myodr = odr.ODR(mydata, linear, beta0=[1., 2.])
myoutput = myodr.run()

fig, ax = plt.subplots()
x = np.linspace(np.min(Kds), np.max(np.array(Kds) + np.array(Kds_errs)), 1000)
ax.set_title(r'$\bf{y = mx + q}$ fit' +
             '\nProtein BS Covariance with Protein BS vs Kd' +
             '\ncorrelation = {:3.2f} p-value = {:3.2f}'.format(
                 *pearsonr(Kds[1:], correlators[1:])))
ax.set_xlabel('Kd (nM)')
ax.set_ylabel('Prot BS z Covariance with Prot BS ')
ax.errorbar(Kds[1:],
            correlators[1:],
            fmt='o',
            xerr=Kds_errs[1:],
Example #14
0
File: fit.py Project: DavidT3/XGA
def scaling_relation_odr(model_func: FunctionType,
                         y_values: Quantity,
                         y_errs: Quantity,
                         x_values: Quantity,
                         x_errs: Quantity = None,
                         y_norm: Quantity = None,
                         x_norm: Quantity = None,
                         x_lims: Quantity = None,
                         start_pars: list = None,
                         y_name: str = 'Y',
                         x_name: str = 'X') -> ScalingRelation:
    """
    A function to fit a scaling relation with the scipy orthogonal distance regression implementation, generate
    an XGA ScalingRelation product, and return it.

    :param FunctionType model_func: The function object of the model you wish to fit. PLEASE NOTE, the function must
        be defined in the style used in xga.models.misc; i.e. powerlaw(x: np.ndarray, k: float, a: float), where
        the first argument is for x values, and the following arguments are all fit parameters. The scipy ODR
        implementation requires functions of a different style, and I try to automatically convert the input function
        to that style, but to help that please avoid using one letter parameter names in any custom function you might
        want to use.
    :param Quantity y_values: The y data values to fit to.
    :param Quantity y_errs: The y error values of the data. These should be supplied as either a 1D Quantity with
        length N (where N is the length of y_values), or an Nx2 Quantity with lower and upper errors.
    :param Quantity x_values: The x data values to fit to.
    :param Quantity x_errs: The x error values of the data. These should be supplied as either a 1D Quantity with
        length N (where N is the length of x_values), or an Nx2 Quantity with lower and upper errors.
    :param Quantity y_norm: Quantity to normalise the y data by.
    :param Quantity x_norm: Quantity to normalise the x data by.
    :param Quantity x_lims: The range of x values in which this relation is valid, default is None. If this
        information is supplied, please pass it as a Quantity array, with the first element being the lower
        bound and the second element being the upper bound.
    :param list start_pars: The start parameters for the ODR run, default is all ones.
    :param str y_name: The name to be used for the y-axis of the scaling relation (DON'T include the unit, that
        will be inferred from the astropy Quantity.
    :param str x_name: The name to be used for the x-axis of the scaling relation (DON'T include the unit, that
        will be inferred from the astropy Quantity.
    :return: An XGA ScalingRelation object with all the information about the data and fit, a view method, and a
        predict method.
    :rtype: ScalingRelation
    """
    if start_pars is None:
        # Setting up the start_pars, as ODR doesn't have a default value like curve_fit
        num_par = len(list(
            inspect.signature(model_func).parameters.keys())) - 1
        start_pars = np.ones(num_par)

    # Standard data preparation
    x_fit_data, x_fit_errs, y_fit_data, y_fit_errs, x_norm, y_norm = _fit_initialise(
        y_values, y_errs, x_values, x_errs, y_norm, x_norm)

    # Immediately faced with a problem, because scipy's ODR is naff and wants functions defined like
    #  blah(par_vector, x_values), which is completely different to my standard definition of models in this module.
    # I don't want the user to have to define things differently for this fit function, so I'm gonna try and
    #  dynamically redefine function models passed in here...
    converted_model_func = convert_to_odr_compatible(model_func)

    # The first thing I want to do is check that the newly converted function gives the same result for a
    #  simple test as the original model
    if model_func(5, *start_pars) != converted_model_func(start_pars, 5):
        raise XGAFunctionConversionError(
            'I attempted to convert the input model function to the standard'
            ' required by ODR, but it is not returning the same value as the '
            'original for this test case.')

    # Then we define a scipy odr model
    odr_model = odr.Model(converted_model_func)

    # And a RealData (which takes the errors as the proper standard deviations of the data)
    odr_data = odr.RealData(x_fit_data, y_fit_data, x_fit_errs, y_fit_errs)

    # Now we instantiate the ODR class with our model and data. This is currently basically ripped from the example
    #  given in the scipy ODR documentation
    odr_obj = odr.ODR(odr_data, odr_model, beta0=start_pars)

    # Actually run the fit, and grab the output
    fit_results = odr_obj.run()

    # And from here, with this output object, I just read out the parameter values, along with the standard dev
    fit_par = fit_results.beta
    fit_par_err = fit_results.sd_beta

    sr = ScalingRelation(fit_par,
                         fit_par_err,
                         model_func,
                         x_norm,
                         y_norm,
                         x_name,
                         y_name,
                         'ODR',
                         x_fit_data * x_norm,
                         y_fit_data * y_norm,
                         x_fit_errs * x_norm,
                         y_fit_errs * y_norm,
                         odr_output=fit_results,
                         x_lims=x_lims)

    return sr
Example #15
0
                fillstyle=None,
                lw=3,
                label='excluded from fit')
        ax.vlines(1340,
                  min_err,
                  max_err,
                  linestyles='dashed',
                  color='yellowgreen',
                  label='max error bar',
                  linewidths=2.)
        ax.legend()
        plt.show()

        mydata = odr.RealData(
            new_Kds,
            new_correlators,
            sy=err / 2,
        )
        myodr = odr.ODR(mydata, linear, beta0=[1., 2.])
        myoutput = myodr.run()

        fig, ax = plt.subplots()
        x = np.linspace(np.min(new_Kds), np.max(new_Kds), 1000)
        ax.set_title(
            r'$\bf{y = mx + q}$' +
            ' fit for correlation of {:3.2f} \n treshold = {} Ang step = {}\n {} excluded'
            .format(new_correlations[0], treshold, ii, WTC_identifier[kk]),
            fontsize=16)
        ax.set_xlabel('Kds (nM)')
        ax.set_ylabel('z Covariance')
        ax.errorbar(new_Kds,
Example #16
0
    def fit(self, fitter = defaults.fitter, method = 'ls', grad = False, 
    weights = None, ext_bounding = False, ascombe = True, update_plot = False, 
    **kwargs):
        """
        Fits the model to the experimental data using the fitter e
        The covariance matrix calculated by the 'leastsq' fitter is not always
        reliable
        """
        switch_aap = (update_plot != self.auto_update_plot)
        if switch_aap is True:
            self.set_auto_update_plot(update_plot)
        self.p_std = None
        self._set_p0()
        if ext_bounding:
            self._enable_ext_bounding()
        if grad is False :
            approx_grad = True
            jacobian = None
            odr_jacobian = None
            grad_ml = None
            grad_ls = None
        else :
            approx_grad = False
            jacobian = self._jacobian
            odr_jacobian = self._jacobian4odr
            grad_ml = self._gradient_ml
            grad_ls = self._gradient_ls
        if method == 'ml':
            weights = None
        if weights is True:
            if self.spectrum.variance is None:
                self.spectrum.estimate_variance()
            weights = 1. / np.sqrt(self.spectrum.variance.__getitem__(
            self.axes_manager._getitem_tuple)[self.channel_switches])
        elif weights is not None:
            weights = weights.__getitem__(self.axes_manager._getitem_tuple)[
            self.channel_switches]
        args = (self.spectrum()[self.channel_switches], 
        weights)
        
        # Least squares "dedicated" fitters
        if fitter == "leastsq":
            output = \
            leastsq(self._errfunc, self.p0[:], Dfun = jacobian,
            col_deriv=1, args = args, full_output = True, **kwargs)
            
            self.p0 = output[0]
            var_matrix = output[1]
            # In Scipy 0.7 sometimes the variance matrix is None (maybe a 
            # bug?) so...
            if var_matrix is not None:
                self.p_std = np.sqrt(np.diag(var_matrix))
        
        elif fitter == "odr":
            modelo = odr.Model(fcn = self._function4odr, 
            fjacb = odr_jacobian)
            mydata = odr.RealData(self.axis.axis[self.channel_switches],
            self.spectrum()[self.channel_switches],
            sx = None,
            sy = (1/weights if weights is not None else None))
            myodr = odr.ODR(mydata, modelo, beta0=self.p0[:])
            myoutput = myodr.run()
            result = myoutput.beta
            self.p_std = myoutput.sd_beta
            self.p0 = result
        else:          
        # General optimizers (incluiding constrained ones(tnc,l_bfgs_b)
        # Least squares or maximum likelihood
            if method == 'ml':
                tominimize = self._poisson_likelihood_function
                fprime = grad_ml
            elif method == 'ls':
                tominimize = self._errfunc2
                fprime = grad_ls
                        
            # OPTIMIZERS
            
            # Simple (don't use gradient)
            if fitter == "fmin" :
                self.p0 = fmin(tominimize, self.p0, args = args, **kwargs)
            elif fitter == "powell" :
                self.p0 = fmin_powell(tominimize, self.p0, args = args, 
                **kwargs)
            
            # Make use of the gradient
            elif fitter == "cg" :
                self.p0 = fmin_cg(tominimize, self.p0, fprime = fprime,
                args= args, **kwargs)
            elif fitter == "ncg" :
                self.p0 = fmin_ncg(tominimize, self.p0, fprime = fprime,
                args = args, **kwargs)
            elif fitter == "bfgs" :
                self.p0 = fmin_bfgs(tominimize, self.p0, fprime = fprime,
                args = args, **kwargs)
            
            # Constrainded optimizers
            
            # Use gradient
            elif fitter == "tnc" :
                self.p0 = fmin_tnc(tominimize, self.p0, fprime = fprime,
                args = args, bounds = self.free_parameters_boundaries, 
                approx_grad = approx_grad, **kwargs)[0]
            elif fitter == "l_bfgs_b" :
                self.p0 = fmin_l_bfgs_b(tominimize, self.p0, fprime = fprime, 
                args =  args,  bounds = self.free_parameters_boundaries, 
                approx_grad = approx_grad, **kwargs)[0]
            else:
                print \
                """
                The %s optimizer is not available.

                Available optimizers:
                Unconstrained:
                --------------
                Only least Squares: leastsq and odr
                General: fmin, powell, cg, ncg, bfgs

                Cosntrained:
                ------------
                tnc and l_bfgs_b
                """ % fitter
                
        
        if np.iterable(self.p0) == 0:
            self.p0 = (self.p0,)
#        if self.p_std is None:
#            self.p_std = self.calculate_p_std(self.p0, method, *args)
        self._charge_p0(p_std = self.p_std)
        self.set()
#        self.model_cube[self.channel_switches, 
#                        self.coordinates.ix, self.coordinates.iy] = \
#                        self.__call__(not self.convolved, onlyactive = True)
        if ext_bounding is True:
            self._disable_ext_bounding()
        if switch_aap is True:
            self.set_auto_update_plot(not update_plot)
            if not update_plot and self.spectrum._plot is not None:
                self.update_plot()
Example #17
0
], [
    lamda_error[1], lamda_error[4], lamda_error[7], lamda_error[8],
    lamda_error[9]
])
lamda_H_value = unp.nominal_values(lamda_H)
lamda_H_error = unp.std_devs(lamda_H)
plt.errorbar(x=n**2,
             y=lamda_H_value * 1e-5,
             xerr=0,
             yerr=lamda_H_error * 1e-5,
             marker='.',
             linestyle='None')

x_fit = np.linspace(n[0]**2, n[-1]**2, num=100)
model = sodr.Model(lin_func)
fit_data = sodr.RealData(n**2, lamda_H_value, sy=lamda_H_error)
odr = sodr.ODR(fit_data, model, beta0=[0., 1.])
out = odr.run()
a = out.beta[0]
b = out.beta[1]
err_a = out.sd_beta[0]
err_b = out.sd_beta[1]
print(a)
print(err_a)
y_fit = a * x_fit + b
plt.plot(
    x_fit,
    y_fit * 1e-5,
    '-',
    label=
    '$\\frac{1}{\lambda}(n)=R_{\infty}\\left(\\frac{1}{2^2}-\\frac{1}{n^2}\\right)$\n $R_\infty=(%.3f+%.3f)m^{-1}$'
if (Final_plot == True):
    f, ax = plt.subplots(1, figsize=(12, 8))
    ax.set_xlabel("$\lambda$ (m)", fontsize=fts)
    ax.set_ylabel("$f$ (s$^-$$^1$)", fontsize=fts)
    ax.tick_params(axis="both", labelsize=24)
    ax.errorbar(x=x, y=y, xerr=sig_x, yerr=sig_y, fmt='k.', label="Meetpunten")


def f(B, x):
    return np.sqrt(((g / ((2 * np.pi) / x)) * np.tanh(
        (2 * np.pi / x) * B[0])) / x**2)


odr_model = odr.Model(f)
odr_data = odr.RealData(x, y, sy=sig_y, sx=sig_x)
odr_obj = odr.ODR(odr_data, odr_model, beta0=[H])
odr_obj.set_job(fit_type=0)  # 0 door onzekerheden in x en y
odr_res = odr_obj.run()

par_best = odr_res.beta  # vraag de beste schatter op
par_sig_ext = odr_res.sd_beta  # de externe onzerheden
chi2red = odr_res.res_var

print("\n######### Govonden waarden #########")
print("H = {:.3f}±{:.3f} m".format(par_best[0], par_sig_ext[0]))
#print("\N{greek small letter tau} = {:.3f}±{:.3f} N/m".format(par_best[1], par_sig_ext[1]))
print(" Reduced chi-squared = {:.3f}".format(chi2red))

if (Final_plot == True):
    xplot = np.linspace(0.07, 1, num=100)

def lin_func(p, x):
    m, c = p
    return m * x + c


file = sys.argv[1]
data = genfromtxt(file + '.csv', delimiter=',')
x = data[1:, 0]
x_err = data[1:, 1]
y = data[1:, 2]
y_err = data[1:, 3]

model = sodr.Model(lin_func)
fit_data = sodr.RealData(x, y, sx=x_err, sy=y_err)
odr = sodr.ODR(fit_data, model, beta0=[0., 1.])
out = odr.run()

a = out.beta[0]
b = out.beta[1]
err_a = out.sd_beta[0]
err_b = out.sd_beta[1]

print("Fitergebnis:\n")
print("y = a * x + b mit\n")
print("a = {:.5f} +/- {:.5f}".format(a, err_a))
print("b = {:.5f} +/- {:.5f}".format(b, err_b))

y_fit = a * x + b
Example #20
0
#remember, everything is in log, so when we are converting back we will have 
#to convert the linear functio too. 
#again:, 
#https://stackoverflow.com/questions/22670057/linear-fitting-in-python-with-uncertainty-in-both-x-and-y-coordinates


#WE HAVE TO CHANGE THIS TO MULTIPLE LINEAR REGRESSION


def linear(p, x):
    m, c = p
    return m*x + c

linear_model = o.Model(linear)

data = o.RealData(L, t, sx = L_err, sy = t_err)

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

output = odr.run() 

m = output.beta[0]
c = output.beta[1]


output.pprint()

print output.beta

############## PLOT-ATION ################
Example #21
0
def polyfit(inputX, inputY, order, iterMax=25):
	"""
	Outlier resistance two-variable polynomial function fitter.
	
	Based on the robust_poly_fit routine in the AstroIDL User's 
	Library.
	
	Unlike robust_poly_fit, two different polynomial fitters are used
	because numpy.polyfit does not support non-uniform weighting of the
	data.  For the weighted fitting, the SciPy Orthogonal Distance
	Regression module (scipy.odr) is used.
	"""
	
	from scipy import odr
	
	def polyFunc(B, x, order=order):
		out = x * 0.0
		for i in xrange(order + 1): out = out + B[i] * x ** i
	
	model = odr.Model(polyFunc)
	
	x = inputX.ravel()
	y = inputY.ravel()
	if type(y).__name__ == "MaskedArray":
		x = x.compress(numpy.logical_not(y.mask))
		y = y.compressed()
	n = len(x)
	
	x0 = x.sum() / n
	y0 = y.sum() / n
	u = x
	v = y
		
	nSeg = order + 2
	if (nSeg / 2) * 2 == nSeg:
		nSeg = nSeg + 1
	minPts = nSeg * 3
	if n < 1000:
		lsqFit = 1
		cc = numpy.polyfit(u, v, order)
		yFit = numpy.polyval(cc, u)
	else:
		lsqfit = 0
		q = numpy.argsort(u)
		u = u[q]
		v = v[q]
		nPerSeg = numpy.zeros(nSeg) + n / nSeg
		nLeft = n - nPerSeg[0] * nSeg
		nPerSeg[nSeg / 2] = nPerSeg[nSeg / 2] + nLeft
		r = numpy.zeros(nSeg)
		s = numpy.zeros(nSeg)
		r[0] = numpy.median(u[0:nPerSeg[0]])
		s[0] = numpy.median(v[0:nPerSeg[0]])
		i2 = nPerSeg[0] - 1
		for i in xrange(1, nSeg):
			i1 = i2
			i2 = i1 + nPerSeg[i]
			r[i] = numpy.median(u[i1:i2])
			s[i] = numpy.median(v[i1:i2])
		cc = numpy.polyfit(r, s, order)
		yFit = numpy.polyval(cc, u)
		
	sigma, fracDev, nGood, biweights, scaledResids = checkfit(v, yFit, __epsilon, __delta)
	if nGood == 0:
		return cc
	if nGood < minPts:
		if lsqFit == 0:
			cc = numpy.polyfit(u, v, order)
			yFit = numpy.polyval(cc, u)
			sigma, fracDev, nGood, biweights, scaledResids = checkfit(yp, yFit, __epsilon, __delta)  # @UndefinedVariable
			if nGood == 0:
				return __processPoly(x0, y0, order, cc)  # @UndefinedVariable
			nGood = n - nGood
		if nGood < minPts:
			return 0
		
	closeEnough = 0.03 * numpy.sqrt(0.5 / (n - 1))
	if closeEnough < __delta:
		closeEnough = __delta
	diff = 1.0e10
	sigma1 = 100.0 * sigma
	nIter = 0
	while diff > closeEnough:
		nIter = nIter + 1
		if nIter > iterMax:
			break
		sigma2 = sigma1
		sigma1 = sigma
		g = (numpy.where(biweights > 0))[0]
		if len(g) < len(biweights):
			u = u[g]
			v = v[g]
			biweights = biweights[g]
		data = odr.RealData(u, v, sy=1.0 / biweights)
		fit = odr.ODR(data, model, beta0=cc[::-1])
		out = fit.run()
		cc = out.beta[::-1]
		yFit = numpy.polyval(cc, u)
		sigma, fracDev, nGood, biweights, scaledResids = checkfit(v, yFit, __epsilon, __delta)
		if nGood < minPts:
			return cc
		diff1 = numpy.abs(sigma1 - sigma) / sigma
		diff2 = numpy.abs(sigma2 - sigma) / sigma
		if diff1 < diff2:
			diff = diff1
		else:
			diff = diff2
	return cc
Example #22
0
def nonlinear_odr(x, y, dx, dy, func, params_init, **kwargs):
    """Perform a non-linear orthogonal distance regression, return the results as
    ErrorValue() instances.

    Inputs:
        x: one-dimensional numpy array of the independent variable
        y: one-dimensional numpy array of the dependent variable
        dx: absolute error (square root of the variance) of the independent
            variable. Either a one-dimensional numpy array or None. If None,
            weighting is disabled. Non-finite (NaN or inf) elements signify
            that the corresponding element in x is to be treated as fixed by
            ODRPACK.
        dy: absolute error (square root of the variance) of the dependent
            variable. Either a one-dimensional numpy array or None. If None,
            weighting is disabled.
        func: a callable with the signature
            func(x,par1,par2,par3,...)
        params_init: list or tuple of the first estimates of the
            parameters par1, par2, par3 etc. to be fitted

        other optional keyword arguments will be passed to leastsq().

    Outputs: par1, par2, par3, ... , statdict
        par1, par2, par3, ...: fitted values of par1, par2, par3 etc
            as instances of ErrorValue.
        statdict: dictionary of various statistical parameters:
            'DoF': Degrees of freedom
            'Chi2': Chi-squared
            'Chi2_reduced': Reduced Chi-squared
            'num_func_eval': number of function evaluations during fit.
            'func_value': the function evaluated in the best fitting parameters
            'message': status message from leastsq()
            'error_flag': integer status flag from leastsq() ('ier')
            'Covariance': covariance matrix (variances in the diagonal)
            'Correlation_coeffs': Pearson's correlation coefficients (usually
                denoted by 'r') in a matrix. The diagonal is unity.

    Notes:
        for the actual fitting, the module scipy.odr is used.
    """
    odrmodel = odr.Model(lambda pars, x: func(x, *pars))
    if dx is not None:
        # treat non-finite values as fixed
        xfixed = np.isfinite(dx)
    else:
        xfixed = None

    odrdata = odr.RealData(x, y, sx=dx, sy=dy, fix=xfixed)
    odrodr = odr.ODR(
        odrdata,
        odrmodel,
        params_init,
        ifixb=[not (isinstance(p, FixedParameter)) for p in params_init],
        **kwargs)
    odroutput = odrodr.run()
    statdict = odroutput.__dict__.copy()
    statdict['Covariance'] = odroutput.cov_beta
    statdict['Correlation_coeffs'] = odroutput.cov_beta / np.outer(
        odroutput.sd_beta, odroutput.sd_beta)
    statdict['DoF'] = len(x) - len(odroutput.beta)
    statdict['Chi2_reduced'] = statdict['res_var']
    statdict['func_value'] = statdict['y']
    statdict['Chi2'] = statdict['sum_square']

    def convert(p_, dp_, pi):
        if isinstance(pi, FixedParameter):
            return FixedParameter(p_)
        else:
            return ErrorValue(p_, dp_)

    return tuple([
        convert(p_, dp_, pi)
        for (p_, dp_,
             pi) in zip(odroutput.beta, odroutput.sd_beta, params_init)
    ] + [statdict])
Example #23
0
    return m * x + n


def OneOrGreaterReturnSqrt(arg):
    if arg > 0.0:
        return np.sqrt(arg)
    else:
        return 1.0


#%%------------------------------------------------------------------------------

# Apply a gaussian fit to the channel spectrum of the $E$ detector, to calculate the peak position, start at channel number 163:
model = odr.Model(Gaussian)
data = odr.RealData(channel[163:],
                    countsE[163:],
                    sx=np.repeat(0.5 / np.sqrt(3), int(n - 163)),
                    sy=list(map(OneOrGreaterReturnSqrt, countsE[163:])))
ODR = odr.ODR(data, model, beta0=[450., 170., 5., 0.05])
output = ODR.run()

# Apply the calibration fit for the $E$ detector in MeV:
EchannelCal = np.linspace(0., output.beta[1], 2)
EchannelCalUnc = np.linspace(0.5 / np.sqrt(3), output.beta[2], 2)
EenergyCal = np.linspace(0., E_0, 2)
EenergyCalUnc = np.linspace(1., E_0_Unc, 2)

model1 = odr.Model(line)
data1 = odr.RealData(EchannelCal,
                     EenergyCal,
                     sx=EchannelCalUnc,
                     sy=EenergyCalUnc)
dxs = (unc_chi1/chi1*np.ones(shape=x.shape), # frac. uncert. on chi
       (VSF_interp1_sd/VSF_interp1).ravel(), # frac. uncert. based on vertical var. within the density bin
       cf.unc_sw*(VSF_interp1_sw/VSF_interp1).ravel()) # frac. uncert. of seawater contribution
dx_tot = np.sqrt(np.sum(np.array([x**2*d**2 for d in dxs]),axis=0))

dys = (unc_chi2/chi2*np.ones(shape=y.shape), # frac. uncert. on chi
       (VSF_interp2_sd/VSF_interp2).ravel(), # frac. uncert. based on vertical var. within the density bin
       cf.unc_sw*(VSF_interp2_sw/VSF_interp2).ravel()) # frac. uncert. of seawater contribution
dy_tot = np.sqrt(np.sum(np.array([y**2*d**2 for d in dys]),axis=0))

inds = np.isfinite(x*y*dx_tot*dy_tot) & (dsalt<cf.DSAL_THRES) & (dtemp<cf.DTEMP_THRES)
TSoutliers = np.isfinite(x*y*dx_tot*dy_tot) & ((dsalt>=cf.DSAL_THRES) | (dtemp>=cf.DTEMP_THRES))

## Do a first fit to find outliers
data = odr.RealData(x[inds], y[inds], sx=dx_tot[inds], sy=dy_tot[inds])
odr_line = odr.ODR(data, odr.unilinear, beta0=[1,0])
fit_line = odr_line.run()
odr_offset = odr.ODR(data, get_bestfit.offset_model(), beta0=[0])
fit_offset = odr_offset.run()
outliers = get_bestfit.find_outliers(fit_line.beta,x,y,dx_tot,dy_tot,inds) & inds
iinds = inds & ~outliers
r = stats.pearsonr(x[iinds],y[iinds])[0]
if cf.OUTPUT_INFO:
    print('n=%d (%d outliers removed), r^2=%.3f'%(np.sum(iinds),np.sum(outliers),r**2))

## Do second fit without the outliers
data = odr.RealData(x[iinds], y[iinds], sx=dx_tot[iinds], sy=dy_tot[iinds])
odr_line = odr.ODR(data, odr.unilinear, beta0=[1,0])
fit_line = odr_line.run()
odr_offset = odr.ODR(data, get_bestfit.offset_model(), beta0=[0])
Example #25
0
                      skip_header=1)

#for example
x = table['wmax']
y = table['M']
x_err = table['wmax_err']
y_err = table['M_err']


def func(p, x):
    m, c = p  #p lists the constants in whatever function you want to use to fit
    return m * x + c


quad_model = odr.Model(func)
data = odr.RealData(x, y, sx=x_err, sy=y_err)
odr = odr.ODR(data, quad_model, beta0=[1., 1.])
#beta0 is an array of best first-guesses to the fit line. The number of
#elements in the array is equal to the number of parameters in p
out = odr.run()

popt = out.beta
perr = out.sd_beta
print('--------------------------------------')
print('Slope:')
print(str(popt[0]) + ' +/- ' + str(perr[0]))
print('Intercept:')
print(str(popt[1]) + ' +/- ' + str(perr[1]))
#for i in range(len(popt)):
#    print(str(popt[i])+' +- '+str(perr[i]))
print('--------------------------------------')
Example #26
0
                if fitdata is None:
                    fitdata = [np.expand_dims(a, axis=1) for a in xys[i]]
                else:
                    fitdata = [
                        np.concatenate(
                            [fitdata[j],
                             np.expand_dims(xys[i][j], axis=1)],
                            axis=0) for j in range(len(xys[i]))
                    ]

            #print(fitdata)
            fitdata = [np.squeeze(a, axis=1) for a in fitdata]
            #fit linear
            m = odr.Model(linear)
            mydata = odr.RealData(fitdata[0],
                                  fitdata[1],
                                  sx=fitdata[2],
                                  sy=fitdata[3])
            myodr = odr.ODR(mydata, m, beta0=[7.])
            out = myodr.run()
            print(out.beta)
            print(out.sd_beta)

            alpha = round(out.beta[0], 2)
            alphaerr = round(out.sd_beta[0], 2)

            if alphaerr:
                allalphas.append(alpha)
                allalphaerrs.append(alphaerr)
                allts.append(t)
                allterr.append(t * terr)
#plt.figure()
#plt.plot(t,d, label='.')
#plt.errorbar(t,d,xerr=0.0,yerr=sig_d,fmt='y.', label='.')
#plt.legend()

f,ax = plt.subplots(1)
ax.errorbar(t,d,xerr=0.0,yerr=sig_d,fmt='y.')
#ax.show()
A_start=2.6
B_start=1.5

def d(B, t):
    return np.sqrt(B[0] + B[1]*t)

odr_model = odr.Model(d)
odr_data  = odr.RealData(t,d,sy=sig_d)

## (2) Definieer het model-object om te gebruiken in odr
odr_model = odr.Model(d)

odr_obj   = odr.ODR(odr_data,odr_model,beta0=[A_start,B_start])
odr_obj.set_job(fit_type=2)

odr_res   = odr_obj.run()

par_best = odr_res.beta
par_sig_ext = odr_res.sd_beta
par_cov = odr_res.cov_beta 
print(" De (INTERNE!) covariantiematrix  = \n", par_cov)
chi2 = odr_res.sum_square
print("\n Chi-squared         = ", chi2)
Example #28
0
def odr_fit(func, x, y, sx, sy, beta0=None):
    """Fitting using ODR."""
    model = odr.Model(func)
    data = odr.RealData(x, y, sx=sx, sy=sy)
    odr_inst = odr.ODR(data, model, beta0)
    return odr_inst.run()
Example #29
0
def FitterPlotter(fit_function, init_params, x, y, sx=None, sy=None, nstd=1.0,
	title=None, xlabel=None, ylabel=None, xlim=None, ylim=None, datalabel='',
	showfig=True, color=None, savepath=None, textbox=None, figax=None,
	ret_fig=False):

	# Plotting setup
	rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
	rc('text', usetex=True)

	# Model function
	if fit_function == 'lin':
		def fit_function(beta, x):
			A, B = beta
			return A * x + B
	elif fit_function == 'exp':
		def fit_function(beta, x):
			A, B = beta
			return A * np.exp(B * x)
	elif not callable(fit_function):
		raise TypeError('fit_function is not callable.')

	# Turn fit_function into an odr model
	model = odr.Model(fit_function)

	# Create data object
	if sx == None and sy is None:
		data = odr.RealData(x, y)
	elif sx == None:
		data = odr.RealData(x, y, sy=sy)
	elif sy == None:
		data = odr.RealData(x, y, sx=sx)
	else:
		data = odr.RealData(x, y, sx=sx, sy=sy)

	# Set up ODR with model and data
	odrm = odr.ODR(data, model, beta0=init_params)

	# Run regression
	out = odrm.run()

	# Extract parameters
	betaout = out.beta
	betaerr = out.sd_beta
	print('Fit parameters and their std deviations')
	print('-------------------')
	for i in range(len(betaout)):
		print('Parameter {}: '.format(i + 1) + str(betaout[i])
			+ ' +- ' + str(betaerr[i]))

	# Fit curve and confidence intervals
	betaout_up = betaout + nstd * betaerr
	betaout_dw = betaout - nstd * betaerr

	x_dummy = np.linspace(min(x), max(x), 1000)
	fit = fit_function(betaout, x_dummy)
	fit_up = fit_function(betaout_up, x_dummy)
	fit_dw = fit_function(betaout_dw, x_dummy)

	# Plotting
	if figax is None: fig, ax = plt.subplots(1)
	else: fig, ax = figax

	if title is not None: ax.set_title(title)
	if xlabel is not None: ax.set_xlabel(xlabel)
	if ylabel is not None: ax.set_ylabel(ylabel)
	if xlim is not None: ax.set_xlim(xlim[0], xlim[1])
	if ylim is not None: ax.set_ylim(ylim[0], ylim[1])
	ax.grid()

	if color is not None:
		points, = ax.plot(x, y, 'o', color=color, label=('Data ' + datalabel))
	else:
		points, = ax.plot(x, y, 'o', label=('Data ' + datalabel))
	ax.plot(x_dummy, fit, color=points.get_color())
	ax.fill_between(x_dummy, fit_up, fit_dw, color=points.get_color(),
		alpha=0.25, label='Confidence interval')

	# Plot textbox if necessary
	if textbox is not None:
		text = ''
		for i in textbox['params']:
			if textbox['error']:
				form = f'{int(np.log10(betaerr[i]))}' # WIP
				text += (textbox['symbols'][i] + fr'$={betaout[i]:0.3f}$' + '\n') #  \pm {betaerr[i]:0.3f}
		text = text[:-1]
		boxprops = dict(boxstyle='round', facecolor='ivory', alpha=0.5)
		ax.text(textbox['coords'][0], textbox['coords'][1], text,
			transform=ax.transAxes, fontsize=14, verticalalignment='top',
			bbox=boxprops)

	# Add legend, save and show
	ax.legend(loc='best')
	if savepath is not None: plt.savefig(savepath)
	if showfig: plt.show()

	# return figure and axes if necessary
	if ret_fig: return fig, ax
full1['dis_FTab_averr'] = full1.apply(lambda row: dis_FTab_averr(row), axis=1)


#see https://www.wikiwand.com/en/Log%E2%80%93log_plot
def regression(x, m, b):
    y = x**m + 10**b
    return y


####linear regression code that isnt done yet####

log_log_model = o.Model(regression)

data = o.RealData([full['z'], full1['redshift']],
                  [full['dis_mpc'], full1['dis_mpc']],
                  sx=0,
                  sy=[full['dis_Ftab_err'], full1['dis_FTab_averr']])
"""
odr_output = o.ODR(data, log_log_model, beta0=[0.,1.])

out = odr_output.run()

out.print()
"""

####closeness of fit to model####


#sums the individual values of Chi from the two tables and devides by the number of points
def x_sum(tables, column):
    data = []