Пример #1
0
    def run_mcmc(self):
        """
        Runs the MCMC fit
        """
        if self.sampler is not None:
            instance = None
            prev_samples = self.sampler.iterations
        else:
            instance = self._setup_instance()
            if self.fit_type == "both":
                optres = self.run_downhill(instance)
                if optres.success:
                    self.guess = list(optres.x)
            prev_samples = 0

        self._write_log_pre()

        fitter = fit.MCMC(priors=self.priors,
                          data=self.y,
                          quantity=self.quantity,
                          constraints=self.constraints,
                          sigma=self.sigma,
                          guess=self.guess,
                          blobs=self.blobs,
                          verbose=self.verbose,
                          relax=self.relax)

        start = time.time()
        if self.chunks == 0:
            self.chunks = self.nsamples - prev_samples
        nchunks = np.ceil((self.nsamples - prev_samples) / float(self.chunks))
        for i, s in enumerate(
                fitter.fit(self.sampler, instance, self.nwalkers,
                           self.nsamples - prev_samples, self.burnin,
                           self.nthreads, self.chunks)):
            # Write out files
            self.write_iter_pickle(s)
            print "Done {0}%. Time per sample: {1}".format(
                100 * float(i + 1) / nchunks, (time.time() - start) /
                ((i + 1) * self.chunks * self.nwalkers))

        total_time = time.time() - start

        self._write_log_post(s, total_time)
        self._write_data(s)
Пример #2
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()    
Пример #3
0
 """
 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"]
 
 fitter.function=func.quad_fat_gaussians
 fitter.update_params([6.0, 10.0, 1.0, 6.0, -10.0, 1.0, 5.0, 10.0, 5.0, 10.0])
 fitter.step = [1.0, 1.0, 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", "amp","sigma", "amp","sigma"]
 """
 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
 plt.figure(1)
 plt.bar(fitter.x, fitter.y, ((np.ma.max(fitter.x)-np.ma.min(fitter.x))/len(fitter.x)), align='center', color='w')
 plt.plot(fitter.x, fitter.function(fitter.x, new_params), 'k-')
 print new_params[:4]
 plt.plot(fitter.x, func.gaussian_function(fitter.x, new_params[:3]), 'k--')
 plt.plot(fitter.x, func.gaussian_function(fitter.x, new_params[3:6]), 'k--')
 plt.plot(fitter.x, func.gaussian_function(fitter.x, [new_params[6], new_params[1], new_params[7]]), 'k--')
 plt.plot(fitter.x, func.gaussian_function(fitter.x, [new_params[8], new_params[4], new_params[9]]), 'k--')
 plt.xlabel("B")
 plt.ylabel("counts")
 plt.title(name, fontsize=8 )
 out_name = name+"_plot"+".png"
Пример #4
0
print to_x


params = pf.plane_OLS(to_x[:,0], to_x[:,1], to_x[:,2])
print "#- Distance array:", pf.plane_dist(to_x[:,0], to_x[:,1], to_x[:,2], params)

fitter.x = to_x
fitter.y = sc.zeros(len(data[:,0]) )
fitter.function=func.plane_fit  #func.R_squared_plane
fitter.update_params([3.0*ma.pi/2.0, ma.pi/2.0, 1.0]) #(params)
fitter.range = [(0, 0.0, 2.0*ma.pi, ma.pi/10.0), (1, 0.0, ma.pi, ma.pi/20.0), (2, -20.0, 20.0, 1.0)]
print fitter.params, fitter.RR

fitter.step = [0.1, 0.1, 0.1]
path1 = fit.MCMC(fitter, iters=10000, annealing=0, verbose=0)

fitter.update_params([3.0*ma.pi/2.0, ma.pi/2.0, 1.0])
fitter.step = [0.75, 0.75, 0.75]
print "# - Steps: ", fitter.step
path2 = fit.gradient_descent(fitter, its=10000, line_search=0)
errors = fit.get_Hessian_errors(fitter)

#fitter.plot()
yi, xi = 1,0
surface = fit.get_surface(fitter, yi, xi, plot=1, path=(path1[:,yi], path1[:,xi]))
surface = fit.get_surface(fitter, yi, xi, plot=1, path=(path2[:,yi], path2[:,xi]))
#print fitter.params, fitter.RR

t, p, d = fitter.params
dt, dp, dd = errors
Пример #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)