Пример #1
0
def sgr_rv_fits():
    data = np.loadtxt("/home/newbym2/Dropbox/Research/sgrLetter/sgr_spec.csv", delimiter=",")
    #dered_g,dered_r,l,b,ELODIERVFINAL,ELODIERVFINALERR
    g0, r0 = data[:,0], data[:,1]
    l, b = data[:,2], data[:,3]
    rv, rv_err = data[:,4], data[:,5]
    d = ac.getr(g0)
    # Transform to vgsr from Yanny+ 2009
    vgsr = ac.rv_to_vgsr(rv,l,b)
    X,Y,Z, lsgr, bsgr, r_sgr = ac.lb2sgr(l, b, d)
    for w in [0.5, 1.0, 2.5, 5.0, 7.5, 10.0, 15.0, 20.0, 25.0, 30.0, 50.0]:
        keep = []
        for i in range(len(data[:,0])):
            if abs(bsgr[i]) < w:  
                if g0[i] < 20.0:  continue
                if g0[i] > 23.0:  continue
                keep.append(vgsr[i])
        hist, edges = np.histogram(np.array(keep), bins=60, range=(-300.0, 300.0))
        y, x = hist, edges[:-1] 
        e = func.poisson_errors(y)
        fitter = fit.ToFit(x,y,e)
        fitter.function=func.double_gaussian_one_fixed
        fitter.update_params([3.0, -120.0, 30.0, 2.0, 0.0, 120.0])
        fitter.step = [1.0, 10.0, 1.0, 1.0, 0.0, 0.0]
        fitter.param_names = ["amp", "mu", "sigma", "amp", "mu", "sigma"]
        path1 = fit.gradient_descent(fitter, its=10000, line_search=0)
        path2 = fit.MCMC(fitter)
        new_params=fitter.params
        xx = sc.arange(-300.0, 300.0, 1.0)
        yy = func.double_gaussian_one_fixed(xx, new_params)
        y1 = func.gaussian_function(xx, new_params[:3])
        y2 = func.gaussian_function(xx, new_params[3:])
        fig = plt.figure()
        plt.bar(edges[:-1], hist, width=10.0, color="white")
        plt.plot(xx,yy, "k-")
        plt.plot(xx,y1, "k--")
        plt.plot(xx,y2, "k--")
        plt.title("cut width = "+str(w))
        plt.xlabel(r"$v_{\rm gsr}$", fontsize=16)
        plt.ylabel(r"Counts")
        #plt.ylim(0.0, 60.0)
        #plt.show()
        plt.savefig("/home/newbym2/Dropbox/Research/sgrLetter/sgr_spec/r_cut_relative"+str(w)+".png")
        plt.close()    
Пример #2
0
     i += 1
 if eval(name) > 291:  i = -20 #-20
 elif eval(name) > 274:  i = -40
 elif eval(name) < 201:  i = -40
 else:  i = -1
 while len(x1) < 5:
     if y[i] > minbin:  x1.append(x[i]);  y1.append(y[i])
     i -= 1
 xi, yi = np.mean(x0), np.mean(y0)
 xf, yf = np.mean(x1), np.mean(y1)
 #xi, yi = np.mean(x[2:6]), np.mean(y[2:6])
 #xf, yf = np.mean(x[-7:-3]), np.mean(y[-7:-3])
 aa = (yf-yi)/(xf-xi)
 bb = yf - (aa*xf)
 #fit it
 fitter = fit.ToFit(x,y,e)
 """
 fitter.function=func.double_gauss_line
 fitter.update_params([6.0, 0.0, 5.0, 6.0, -6.0, 5.0, aa, bb])
 fitter.step = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]
 fitter.param_names = ["amp", "mu", "sigma", "amp", "mu", "sigma", "slope", "intercept"]
 """
 fitter.function=func.quad_fat_gauss_line
 fitter.update_params([6.0, 5.0, 1.0, 6.0, -10.0, 1.0, 5.0, 10.0, 5.0, 10.0, aa, bb])
 fitter.step = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]
 fitter.param_names = ["amp", "mu", "sigma", "amp", "mu", "sigma", "amp","sigma", "amp","sigma","slope","intercept"]
 """
 fitter.function=func.double_gaussian
 fitter.update_params([20.0, 0.0, 5.0, 20.0, 20.0, 15.0])
 fitter.step = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
 fitter.param_names = ["amp", "mu", "sigma", "amp", "mu", "sigma"]
Пример #3
0
for i in run:
    print "# --- Fitting region {0}".format(i)
    data = fi.read_data("/home/newbym2/Dropbox/Research/Cetus_Polar/bhb_dmod" +
                        str(i) + ".txt",
                        skip=1)
    print "# --- l-coordinate mean, std: {0}, {1}".format(
        np.mean(data[:, 0]), np.std(data[:, 0]))
    b_hist, b_edges = np.histogram(data[:, 1], bins, range=[80.0, 180.0])
    b_err = func.poisson_errors(b_hist)
    b_wid = (np.ma.max(data[:, 1]) - np.ma.min(data[:, 1])) / bins
    d_hist, d_edges = np.histogram(data[:, 2], bins, range=[16.0, 19.0])
    d_err = func.poisson_errors(d_hist)
    d_wid = (np.ma.max(data[:, 2]) - np.ma.min(data[:, 2])) / bins
    # Initialize
    fitter = fit.ToFit(d_edges[:-1] + (d_wid / 2.0), d_hist, d_err)
    fitter.function = func.gauss_plus_floor
    fitter.update_params(start_params)
    fitter.step = [0.01, 0.01, 0.01, 0.01]
    fitter.param_names = ["amp", "mu", "sigma", "floor"]
    fitter.min = [None, None, 0.0, None]
    fitter.max = [None, None, None, None]
    print "# - Starting from:", fitter.params, fitter.RR
    print "# - With steps:", fitter.step
    # Fit
    path1 = fit.gradient_descent(fitter, its=10000, line_search=0)
    errors = fit.get_Hessian_errors(fitter)
    gd_params = fitter.params, fitter.RR
    #fitter.update_params(start_params)
    print "# - Starting from:", fitter.params, fitter.RR
    path2 = fit.MCMC(fitter, iters=10000, annealing=0, verbose=0)
Пример #4
0
import numpy as np
import scipy as sc
import functions as func
import files as fi
import fit as fit
import plane_fit as pf
import astro_coordinates as co

#data = fi.read_data("../sgrnorth_paper/Newberg2007_Sgr.txt", ",")
#data = fi.read_data("../sgrnorth_paper/Koposov2012_Sgr.txt", ",")
data = fi.read_data("../sgrnorth_paper/Koposov2012_2nd.txt", ",")
# In fit parameter format:
#data = fi.read_data("../sgrnorth_paper/sgr_params_south.txt", ",")
#data = fi.read_data("../sgrnorth_paper/sgr_params_north.txt", ",")

fitter=fit.ToFit(data[:,0], data[:,1], "1.0")
"""
# gots to be tricksy, yes...  plane fitting requires dark magiks...
#wedges = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
#wedges = [79, 82, 86]
# errors from sgr_north_utils.py
#errors = [9.31738638849, 9.1020519394, 19.7856077035, 20.8318952656, 9.39314333747,
#          26.9966491404, 40.6343350862, 15.3631542798, 11.270906999, 10.3242787823,
#          10.290985838, 8.75140991672, 14.6281676367, 16.0532641683, 6.69586402716]
#errors = [10.3012569286, 9.26151305927, 10.4476312495]  # Errors are wrong!!
# These are right?
# 7.57640162772, 7.67810980299, 11.5363484879, 25.6937588232, 8.75918164562,
# 37.6255221984, 54.5484852169, 13.1237001504, 12.2803690866, 11.9300143607,
# 12.2150368604, 13.3084651939, 14.9866923184, 15.7846605126, 27.9365850481, 
# 11.2583497941, 10.1111224829, 9.12819009006
to_x = sc.zeros((len(data[:,0]), 3) )  # RAISE THIS TO 4? WHEN ERRORS ARE USED
Пример #5
0
def fit_hists(x_raw,
              y_raw,
              name,
              outfile=None,
              outpath="",
              fit_type='double',
              cuts=None,
              background="common",
              binsize=0.5,
              lam_offset=0.0):
    run = eval(name.split("_")[-1])
    # clean data;  don't fit bins with certain criteria
    if cuts != None:
        mask = sc.ones(len(y_raw), dtype=bool)
        for i in range(cuts.shape[0]):
            if cuts[i, 0] + lam_offset == run:  #for offset Lambda wedges
                if binsize == 0.5:
                    for j in range(cuts[i, 1], cuts[i, 2]):
                        mask[(j - 1)] = cuts[i, 3]
                if binsize == 1.0:
                    for j in range(cuts[i, 1] / 2, cuts[i, 2] / 2):
                        mask[(j - 1)] = cuts[i, 3]
    else:
        mask = sc.zeros(len(y_raw), dtype=bool)
        for i in range(len(y_raw)):
            if y_raw[i] <= 0.0: mask[i] = 1  #flag zeros
        #if y_raw[i] > 1000.0:  mask[i] = 1  #flag globular clusters
    # make new array that contains only mask==0
    x, y = [], []
    for i in range(len(y_raw)):
        if mask[i] == 0:
            x.append(x_raw[i])
            y.append(y_raw[i])
    # Set up data
    x, y = np.array(x), np.array(y)
    e = func.poisson_errors(y)
    if background == "common":
        # Make a flat background line set to most common bin height
        hline, hedges = np.histogram(y, bins=40, range=(0.0, 1000.0))
        best = 25.0 * np.argmax(hline) - 12.5
        aa, bb = 0.0, best
    elif background == "least":
        ids = np.argsort(y)[:10]
        aa, bb = 0.0, np.mean(y[ids])
    elif background == "fitline":
        # make a line, using the average of 10 bins on each end as the anchor points
        x0, y0, x1, y1 = [], [], [], []
        minbin = 1.0
        if run < 120: i = 10
        else: i = 1
        while len(x0) < 10:
            if y[i] > minbin:
                x0.append(x[i])
                y0.append(y[i])
            i += 1
        if run < 120: i = (len(y) / 2.0) - 5
        elif run > 254: i = 65
        else: i = -1
        while len(x1) < 10:
            if y[i] > minbin:
                x1.append(x[i])
                y1.append(y[i])
            i -= 1
        xi, yi = np.mean(x0), np.mean(y0)
        xf, yf = np.mean(x1), np.mean(y1)
        #xi, yi = np.mean(x[2:6]), np.mean(y[2:6])
        #xf, yf = np.mean(x[-7:-3]), np.mean(y[-7:-3])
        aa = (yf - yi) / (xf - xi)
        bb = yf - (aa * xf)
    else:
        # null line
        aa, bb = 0.0, 0.0
    #  fit it
    fitter = fit.ToFit(x, y, e)
    if fit_type == "double":
        fitter.function = func.double_gauss_line
        fitter.update_params([6.0, 0.0, 5.0, 6.0, -6.0, 5.0, aa, bb])
        fitter.step = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
        fitter.param_names = [
            "amp", "mu", "sigma", "amp", "mu", "sigma", "slope", "intercept"
        ]
    elif fit_type == "quad":
        fitter.function = func.quad_fat_gauss_line
        fitter.update_params(
            [6.0, 5.0, 1.0, 6.0, -10.0, 1.0, 5.0, 10.0, 5.0, 10.0, aa, bb])
        fitter.step = [
            1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0
        ]
        fitter.param_names = [
            "amp", "mu", "sigma", "amp", "mu", "sigma", "amp", "sigma", "amp",
            "sigma", "slope", "intercept"
        ]
    elif fit_type == "triple":
        if run < 120: tf, tm = 200.0, -50.0
        if run > 120: tf, tm = 150.0, 40.0
        fitter.function = func.triple_gauss_floor
        fitter.update_params(
            [6.0, 5.0, 1.0, 6.0, -10.0, 1.0, 2.0, tm, 40.0, tf])
        fitter.step = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 5.0, 10.0]
        fitter.param_names = [
            "amp", "mu", "sigma", "amp", "mu", "sigma", "amp", "mu", "sigma",
            "floor"
        ]
    print "\n {0}:".format(name)
    print "# - Starting from:", fitter.params, fitter.RR
    print "# - With steps:", fitter.step
    path1 = fit.gradient_descent(fitter, its=10000, line_search=0)
    path2 = fit.MCMC(fitter)
    #errors = fit.get_Hessian_errors(fitter)
    # cut from ToFit
    new_params = fitter.params
    xx = np.arange(-70.0, 40.0, 0.1)
    plt.figure(1)
    plt.bar(x_raw, y_raw, binsize, align='center', color='white', zorder=1)
    plt.bar(fitter.x,
            fitter.y,
            binsize,
            align='center',
            color='grey',
            zorder=2)
    plt.plot(xx, fitter.function(xx, new_params), 'k-')
    print new_params[:4]
    plt.plot(xx, func.gaussian_function(xx, new_params[:3]), 'k--')
    plt.plot(xx, func.gaussian_function(xx, new_params[3:6]), 'k--')
    if fit_type == "triple":
        plt.plot(
            xx,
            func.gaussian_function(
                xx, [new_params[6], new_params[7], new_params[8]]), 'k--')
        plt.plot(xx, new_params[9] * sc.ones(len(xx)), "k--")
    if fit_type == "quad":
        plt.plot(
            xx,
            func.gaussian_function(
                xx, [new_params[6], new_params[1], new_params[7]]), 'k--')
        plt.plot(
            xx,
            func.gaussian_function(
                xx, [new_params[8], new_params[4], new_params[9]]), 'k--')
    plt.xlabel("B")
    plt.ylabel("Counts")
    plt.xlim(-70.0, 40.0)
    plt.ylim(0.0, 700.0)
    #plt.text(-65.0, 1100.0, name, fontsize=8 )
    out_name = outpath + name + "_plot" + ".png"
    #plt.show()
    plt.savefig(out_name)
    plt.close('all')
    return np.insert(np.concatenate((fitter.params, fitter.error)), -1,
                     fitter.RR)