Ejemplo n.º 1
0
def fix_plots(filename):

        fit_instructions = {}
        galaxy, model_components = import_spectrum(filename)
        # Calculate redshift constraints.
        z_low, z_high = model_components["redshift"] - 0.001,  model_components["redshift"] + 0.001

        fit = pipes.fit(galaxy, fit_instructions, run="r4_exponential_burst")
        fit.fit(verbose=False)
        fig, ax = fit.plot_sfh_posterior(save=False, show=False)
        plt.savefig("pipes/plots/r4_exponential_burst/"+filename+"_sfh.pdf", bbox_inches = 'tight')

        fit = pipes.fit(galaxy, fit_instructions, run="r4_dblplaw_burst")
        fit.fit(verbose=True)
        fig, ax = fit.plot_sfh_posterior(save=False, show=False)
        plt.savefig("pipes/plots/r4_dblplaw_burst/"+filename+"_sfh.pdf", bbox_inches = 'tight')

        fit = pipes.fit(galaxy, fit_instructions, run="r4_delayed_burst")
        fit.fit(verbose=False)
        fig, ax = fit.plot_sfh_posterior(save=False, show=False)
        plt.savefig("pipes/plots/r4_delayed_burst/"+filename+"_sfh.pdf", bbox_inches = 'tight')

        fit = pipes.fit(galaxy, fit_instructions, run="r4_lognormal_burst")
        fit.fit(verbose=False)
        fig, ax = fit.plot_sfh_posterior(save=False, show=False)
        fig.set_size_inches(12,4)
        plt.savefig("pipes/plots/r4_lognormal_burst/"+filename+"_sfh.pdf", bbox_inches = 'tight')
Ejemplo n.º 2
0
def get_best_fit():

    for filename in datafiles:

        fit_instructions = {}
        chi_squ_vals = {}

        galaxy, model_components = import_spectrum(filename)
        # Calculate redshift constraints.
        z_low, z_high = model_components["redshift"] - 0.001,  model_components["redshift"] + 0.001

        fit = pipes.fit(galaxy, fit_instructions, run="r4_exponential_burst")
        fit.fit(verbose=False)
        chi_squ_vals = {"r4_exponential_burst" : chi_squared(galaxy, fit)}

        fit = pipes.fit(galaxy, fit_instructions, run="r4_dblplaw_burst")
        fit.fit(verbose=True)
        chi_squ_vals["r4_dblplaw_burst"] = chi_squared(galaxy, fit)

        fit = pipes.fit(galaxy, fit_instructions, run="r4_delayed_burst")
        fit.fit(verbose=False)
        chi_squ_vals["r4_delayed_burst"] = chi_squared(galaxy, fit)

        fit = pipes.fit(galaxy, fit_instructions, run="r4_lognormal_burst")
        fit.fit(verbose=False)
        chi_squ_vals["r4_lognormal_burst"] = chi_squared(galaxy, fit)

        # Get the functional form with the lowest chi-squared value.
        best_func = min(chi_squ_vals, key=lambda k: chi_squ_vals[k])
        # Select the fit with lowest chi-squared value and plot it.
        print(filename)
        print(best_func)
        print(chi_squ_vals[best_func])
Ejemplo n.º 3
0
def plot_model_posterior_quantities(filename):

    fit_instructions = {}
    chi_squ_vals = {}

    galaxy, model_components = import_spectrum(filename)
    # Calculate redshift constraints.
    z_low, z_high = model_components["redshift"] - 0.001,  model_components["redshift"] + 0.001

    fit = pipes.fit(galaxy, fit_instructions, run="r4_exponential_burst")
    fit.fit(verbose=False)
    chi_squ_vals = {"r4_exponential_burst" : chi_squared(galaxy, fit)}

    fit = pipes.fit(galaxy, fit_instructions, run="r4_dblplaw_burst")
    fit.fit(verbose=True)
    chi_squ_vals["r4_dblplaw_burst"] = chi_squared(galaxy, fit)

    fit = pipes.fit(galaxy, fit_instructions, run="r4_delayed_burst")
    fit.fit(verbose=False)
    chi_squ_vals["r4_delayed_burst"] = chi_squared(galaxy, fit)

    fit = pipes.fit(galaxy, fit_instructions, run="r4_lognormal_burst")
    fit.fit(verbose=False)
    chi_squ_vals["r4_lognormal_burst"] = chi_squared(galaxy, fit)

    # Get the functional form with the lowest chi-squared value.
    best_func = min(chi_squ_vals, key=lambda k: chi_squ_vals[k])
    fit = pipes.fit(galaxy, fit_instructions, run=best_func)
    # Select the fit with lowest chi-squared value and plot it.
    print(filename)
    print(best_func)
    for key in chi_squ_vals.keys():
        print(key, chi_squ_vals[key])

    if best_func == "r4_exponential_burst":
        labels = ["exponential2:age", "exponential2:massformed", "exponential1:age", "exponential1:massformed"]
    if best_func == "r4_delayed_burst":
        labels = ["exponential2:age", "exponential2:massformed", "delayed:age", "delayed:massformed"]
    if best_func == "r4_lognormal_burst":
        labels = ["exponential2:age", "exponential2:massformed", "lognormal:tmax", "lognormal:massformed"]
    if best_func == "r4_dblplaw_burst":
        labels = ["exponential2:age", "exponential2:massformed", "dblplaw:tau", "dblplaw:massformed"]

    post_quantities = dict(zip(labels, [fit.posterior.samples[l] for l in labels]))

    tex_on = True
    update_rcParams()

    fig, axes = plt.subplots(1, 4)

    for i in range(4):
        hist1d(post_quantities[labels[i]], axes[i], smooth=True, label=labels[i])
        fixed_aspect_ratio(1,axes[i])

    fig.set_figwidth(6.75)
    plt.savefig("report/img/"+filename+"_posterior.pdf", bbox_inches = 'tight', pad_inches = 0)
Ejemplo n.º 4
0
def corner():
    # from matplotlib import rcParams
    # rcParams.update({'figure.autolayout': True})
    galaxy, model_components = import_spectrum("phil_model_4")
    fit_instructions = {}
    fit = pipes.fit(galaxy, fit_instructions, run="r4_exponential_burst")
    fit.fit(verbose=False)
    plot_corner(fit, names=["exponential2:massformed", "exponential1:tau"], save=True)
Ejemplo n.º 5
0
def print_posterior_file(filename, run_name):

    fit_instructions = {}
    chi_squ_vals = {}

    galaxy, model_components = import_spectrum(filename)
    # Calculate redshift constraints.
    z_low, z_high = model_components["redshift"] - 0.001,  model_components["redshift"] + 0.001

    fit = pipes.fit(galaxy, fit_instructions, run=run_name)
    fit.fit(verbose=False)
    # chi_squ_vals = {"r4_exponential_burst" : chi_squared(galaxy, fit)}
    print_posterior(fit)
Ejemplo n.º 6
0
def fix_h5_enumeration():
    for oldname, newname in zip(oldfiles, datafiles):
        for run in runs:
            try:
                oldpath = "pipes/posterior/" + run + "/" + oldname + ".h5"
                newpath = "pipes/posterior/" + run + "/" + newname + ".h5"
                os.rename(oldpath, newpath)
            except FileNotFoundError:
                pass

            if os.path.isfile(newpath):
                galaxy, model_components = import_spectrum(newname)
                fit_instructions = {}
                fit = pipes.fit(galaxy, fit_instructions, run=run)
                fit.fit(verbose=False)
                fit.plot_sfh_posterior()
            else:
                pass
Ejemplo n.º 7
0
def fitmodel(ID, saveplot=False):
    filt_list = np.loadtxt("filters/goodss_filt_list.txt", dtype="str")

    galaxy = pipes.galaxy(ID,
                          load_goodss,
                          spectrum_exists=False,
                          filt_list=filt_list)

    exp = {}
    exp["age"] = (0.1, 15.)
    exp["tau"] = (0.3, 12.)
    exp["massformed"] = (2., 15.)
    exp["metallicity"] = (0., 2.5)

    dust = {}
    dust["type"] = "Cardelli"
    dust["Av"] = (0., 2.)

    fit_instructions = {}
    fit_instructions["redshift"] = (0., 10.)
    fit_instructions["exponential"] = exp
    fit_instructions["dust"] = dust

    fit = pipes.fit(galaxy, fit_instructions)
    fit.fit(verbose=False)

    if saveplot == True:
        fig = fit.plot_spectrum_posterior(
            save=True, show=False)  #####doesnt work, unclear why
        fig = fit.plot_sfh_posterior(save=True, show=False)
        fig = fit.plot_corner(save=True, show=False)
    else:
        fig = fit.plot_spectrum_posterior(
            save=False, show=True)  #####doesnt work, unclear why
        fig = fit.plot_sfh_posterior(save=False, show=True)
        fig = fit.plot_corner(save=False, show=True)
Ejemplo n.º 8
0
    z_low, z_high = redshift - 0.001,  redshift + 0.001

    try:
        f = open("pipes/posterior/exponential_burst_r4/"+filename+".h5")
        f.close()
        fit_instructions = {
        "redshift"     : (z_low, z_high), # z varies tight_layout around z_obs.
        "t_bc"         : (0.013, 0.021),  # Constraints from Murray 2011.
        "veldisp"      : (50.0, 450.0),   # Constrained by Faber-Jackson. TODO: Lookup Minkowski 1962!
        "exponential1" : exponential1,    # Add the exp SFH component.
        "exponential2" : exponential2,    # Add the burst component.
        "dust"         : dust,
        "nebular"      : nebular
        }
        # Do a fit with both an old exponential component and recent burst.
        fit = pipes.fit(galaxy, fit_instructions, run="exponential_burst_r4")
        fit.fit(verbose=False)
        # Create a dictionary for storying posterior sample distribution widths.
        chi_squ_vals["exponential_burst_r4"] = chi_squared(galaxy, fit)
        # Select the fit with lowest chi-ssquared value and plot it.
        plt.tight_layout()
        fig = fit.plot_sfh_posterior(save=True, show=False)
    except IOError:
        print("Run not exponential_burst_r4 not accessible for "+filename)

    try:
        f = open("pipes/posterior/dblplaw_burst_r4/"+filename+".h5")
        f.close()
        fit_instructions = {
        "redshift"     : (z_low, z_high), # z varies tight_layout around z_obs.
        "t_bc"         : (0.013, 0.021),  # Constraints from Murray 2011.
Ejemplo n.º 9
0
def main(segid_to_test):

    print("Running BAGPIPES on galaxy SegID:", segid_to_test)
    galaxy = pipes.galaxy(str(segid_to_test),
                          load_data,
                          photometry_exists=False)

    galaxy.plot()

    dblplaw = {}
    dblplaw["tau"] = (0., 15.)
    dblplaw["alpha"] = (0.01, 1000.)
    dblplaw["beta"] = (0.01, 1000.)
    dblplaw["alpha_prior"] = "log_10"
    dblplaw["beta_prior"] = "log_10"
    dblplaw["massformed"] = (1., 15.)
    dblplaw["metallicity"] = (0.1, 2.)
    dblplaw["metallicity_prior"] = "log_10"

    nebular = {}
    nebular["logU"] = -3.

    dust = {}
    dust["type"] = "CF00"
    dust["eta"] = 2.
    dust["Av"] = (0., 2.0)
    dust["n"] = (0.3, 2.5)
    dust["n_prior"] = "Gaussian"
    dust["n_prior_mu"] = 0.7
    dust["n_prior_sigma"] = 0.3

    fit_instructions = {}
    fit_instructions["redshift"] = (0.1, 2.5)

    fit_instructions["t_bc"] = 0.01
    #fit_instructions["redshift_prior"] = "Gaussian"
    #fit_instructions["redshift_prior_mu"] = 0.5
    #fit_instructions["redshift_prior_sigma"] = 0.2
    fit_instructions["dblplaw"] = dblplaw
    fit_instructions["nebular"] = nebular
    fit_instructions["dust"] = dust

    fit_instructions["veldisp"] = (1., 1000.)  #km/s
    fit_instructions["veldisp_prior"] = "log_10"

    calib = {}
    calib["type"] = "polynomial_bayesian"

    calib["0"] = (0.5, 1.5)
    # Zero order is centred on 1,
    # at which point there is no change to the spectrum.
    calib["0_prior"] = "Gaussian"
    calib["0_prior_mu"] = 1.0
    calib["0_prior_sigma"] = 0.25

    calib["1"] = (-0.5, 0.5)  # Subsequent orders are centred on zero.
    calib["1_prior"] = "Gaussian"
    calib["1_prior_mu"] = 0.
    calib["1_prior_sigma"] = 0.25

    calib["2"] = (-0.5, 0.5)
    calib["2_prior"] = "Gaussian"
    calib["2_prior_mu"] = 0.
    calib["2_prior_sigma"] = 0.25

    fit_instructions["calib"] = calib

    noise = {}
    noise["type"] = "white_scaled"
    noise["scaling"] = (1., 10.)
    noise["scaling_prior"] = "log_10"
    fit_instructions["noise"] = noise

    fit = pipes.fit(galaxy, fit_instructions, run="spectroscopy")

    fit.fit(verbose=False)

    fig = fit.plot_spectrum_posterior(save=True, show=True)
    fig = fit.plot_calibration(save=True, show=True)
    fig = fit.plot_sfh_posterior(save=True, show=True)
    fig = fit.plot_corner(save=True, show=True)

    print("\a    \a    \a")

    return None
Ejemplo n.º 10
0
    print("Running initial exponential fit for {}...".format(filename)),

    # Create (or reset) the fit instructions dictionary.
    fit_instructions = {
        "redshift": (0.0, 0.1),  # Obs. redshift from 0-10.
        "t_bc": (0.005, 0.015),
        "veldisp": (150.0, 200.0),
        "exponential1": exponential1,  # Add the exp SFH component.
        "exponential2": exponential2,  # Add the burst component.
        "dust": dust
    }

    # Do an initial fit with only an exponential compontent.
    galaxy, model_components = import_spectrum(filename)
    fit = pipes.fit(galaxy, fit_instructions, run="r2_exponential_burst")
    fit.fit(verbose=False)

    # Create a dictionary for storying posterior sample distribution widths.
    chi_squ_vals = {"r2_exponential_burst": chi_squared(galaxy, fit)}

    fit_instructions.pop("exponential1")
    fit_instructions["dblplaw"] = dblplaw
    fit = pipes.fit(galaxy, fit_instructions, run="r2_dblplaw_burst")
    fit.fit(verbose=True)
    chi_squ_vals["r2_dblplaw_burst"] = chi_squared(galaxy, fit)

    fit_instructions.pop("dblplaw", None)
    fit_instructions["delayed"] = delayed
    fit = pipes.fit(galaxy, fit_instructions, run="r2_delayed_burst")
    fit.fit(verbose=False)
Ejemplo n.º 11
0
for filename in datafiles:

    print("Running initial exponential fit for {}...".format(filename)),

    # Create (or reset) the fit instructions dictionary.
    fit_instructions = {
        "redshift": (0.0, 0.1),  # Obs. redshift from 0-10.
        "t_bc": (0.005, 0.015),
        "veldisp": (150.0, 200.0),
        "exponential": exponential,  # Add the exp SFH component.
        "dust": dust
    }

    # Do an initial fit with only an exponential compontent, over a large parameter space.
    galaxy, model_components = import_spectrum(filename)
    fit = pipes.fit(galaxy, fit_instructions, run="r1_exponential_noburst")
    fit.fit(verbose=False)

    # Now run a new fit with other functional components with age constrained to one standard deviation around the exponential age.
    age_lower_bound = np.percentile(fit_instructions["exponential"]["age"], 16)
    age_upper_bound = np.percentile(fit_instructions["exponential"]["age"], 84)

    # Create a dictionary for storying posterior sample distribution widths.
    chi_squ_vals = {"exponential": chi_squared(galaxy, fit)}

    fit_instructions.pop("exponential", None)
    delayed["age"] = (age_lower_bound, age_upper_bound)
    fit_instructions["delayed"] = delayed
    fit = pipes.fit(galaxy, fit_instructions, run="r1_delayed_noburst")
    fit.fit(verbose=False)
    chi_squ_vals["delayed"] = chi_squared(galaxy, fit)
from loaddata import load_goodss
from loaddata import load_vandels_spec
import bagpipes as pipes
import numpy as np

filt_list = np.loadtxt("filters/goodss_filt_list.txt", dtype="str")

galaxy = pipes.galaxy(14697,load_goodss,spectrum_exists=False,filt_list=filt_list)

dust = {}
dust["type"] = "Cardelli"
dust["Av"] = (0.,2.)

power = {}
power["tau"] = (0., 15.)
power["alpha"] = (.1, 1000.)
power["beta"] = (.1, 1000.)
power["alpha_prior"] = "log_10"
power["beta_prior"] = "log_10"
power["massformed"] = (2., 15.)
power["metallicity"] = (0., 2.5)

secondfit = {}
secondfit["redshift"] = (0.,10.)
secondfit["dblplaw"] = power
secondfit["dust"] = dust
fit = pipes.fit(galaxy, secondfit, run="dblplaw_sfh")
fit.fit(verbose=False)
fig = fit.plot_sfh_posterior(save=False, show=True)
fig = fit.plot_corner(save=False, show=True)
Ejemplo n.º 13
0
def fit_various_models(galaxy, redshift, fit_instructions_dict, catalog):
    """
    This is the  function  to  fit  different models to the
    galaxy object. It will create the following directories:

      results_dir/
                 catalog/
                         results/

    And for every run (model) a different directory will be
    made to store the results.

    FUNCTION:
    =========
              It will save a .h5 file with all the SED fitting results
              information for every galaxy and they are as follows:
               (for more info look at Bagpipes)

            fit.posterior.get_basic_quantities()
            fit.posterior.get_advanced_quantities()
            results = fit.results
            added_results = fit.posterior.samples

            results["samples"] = added_results
            results["samples"]["ages"] = fit.posterior.sfh.ages
            results["filters_list"] = galaxy.filt_list
            results["model_components"] = fit.fitted_model.model_components



    INPUT
    ======
            galaxy (Galaxy obj) : galaxy obj from bagpipes
            redshift (float): redshift of the Galaxy
            fit_instructions_dict (dict): A dictionary of --->
                                          {run (model) : fit_instructions}
            catalog (string) : catalog string like GOODSS, GOODSN, UDS, ...

    OUTPUT
    ======
            None
    """
    # Create the main results directory if not exists
    if not os.path.isdir(res):
        os.mkdir(res)

    results_dir = res + catalog + "/"

    if not os.path.isdir(results_dir):
        os.mkdir(results_dir)

    if not os.path.isdir(results_dir + "results"):
        os.mkdir(results_dir + "results")

    # Create the error log file if not exists
    log_error = results_dir + "results/.no_fitting"
    if not os.path.exists(log_error):
        with open(log_error, "w+") as f:
            f.close()

    # Create directories for every models if not exists
    for run in fit_instructions_dict:
        if not os.path.isdir(results_dir + "results/" + run):
            os.mkdir(results_dir + "results/" + run)


#         print(run)

    for run, fit_info in fit_instructions_dict.items():
        run = run + "_" + catalog
        try:
            fit_info["redshift"] = redshift
            fit = pipes.fit(galaxy=galaxy, fit_instructions=fit_info, run=run)
            fit.fit(verbose=False)

            fit.posterior.get_basic_quantities()
            fit.posterior.get_advanced_quantities()
            results = fit.results
            added_results = fit.posterior.samples

            results["samples"] = added_results
            results["samples"]["ages"] = fit.posterior.sfh.ages
            results["filters_list"] = galaxy.filt_list
            results["model_components"] = fit.fitted_model.model_components
            # Saving the results of the SED fitting with all of the main
            # physical quantities.
            path_to_save = results_dir + "results/" + run \
                           + "/" + galaxy.ID + ".h5"
            if not os.path.exists(path_to_save):
                dd.io.save(path_to_save, results)
        except:
            with open(log_error, "a") as f:
                f.write("No fitting for " + run + "  " + fit.galaxy.ID)

    return None