Example #1
0
def create_hg(hg_params=None):
    """
    Args:
        hg_params (dictionary): Host galaxy component parameters. Required keys are:
            - no_templates (number of templates)
            - wl (wavelength range of HG model, redshift must be accounted for already)
            - hg_norm_{} (1,2,3 depending on number of templates)
    """

    if hg_params is None:
        hg_params = {"no_templates": 3, "wl": WL}
        max_template_flux = 6e-12
        samples = draw_from_sample.gaussian(PARS["hg_norm_min"],
                                            max_template_flux, 3)
        hg_params["hg_norm_1"] = samples[0]
        hg_params["hg_norm_2"] = samples[1]
        hg_params["hg_norm_3"] = samples[2]
        hg_params["hg_stellar_disp"] = draw_from_sample.gaussian(
            PARS["hg_stellar_disp_min"], PARS["hg_stellar_disp_max"])

    print("HG params: {}".format(hg_params))
    hg = HostGalaxyComponent()
    # Make a Spectrum object with dummy flux
    spectrum = Spectrum(hg_params["wl"])
    spectrum.dispersion = hg_params["wl"]
    hg.initialize(spectrum)
    comp_params = [
        hg_params["hg_norm_{}".format(x)]
        for x in range(1, hg_params["no_templates"] + 1)
    ] + [hg_params["hg_stellar_disp"]]
    hg_flux = HostGalaxyComponent.flux(hg, spectrum, comp_params)
    hg_err = hg_flux * 0.05

    #    pl.errorbar(hg_params["wl"], hg_flux, hg_err)
    #    pl.savefig("hg_data.png")

    return hg_params["wl"], hg_flux, hg_err, hg_params
Example #2
0
    model.components.append(fe_comp)

if False:
    nuclear_comp = NuclearContinuumComponent()

    datafile = "../Data/FakeData/PLcompOnly/fakepowlaw1_werr.dat"
    wavelengths, flux, flux_err = np.loadtxt(datafile, unpack=True)
    spectrum = Spectrum()
    spectrum.wavelengths = wavelengths
    spectrum.flux = flux
    spectrum.flux_error = flux_err

    model.components.append(nuclear_comp)

if False:
    host_galaxy_comp = HostGalaxyComponent()

    datafile = "../Data/FakeData/for_gisella/fake_host_spectrum.dat"
    wavelengths, flux, flux_err = np.loadtxt(datafile, unpack=True)
    spectrum = Spectrum()
    spectrum.wavelengths = wavelengths
    spectrum.flux = flux
    spectrum.flux_error = flux_err

    model.components.append(host_galaxy_comp)

model.data_spectrum = spectrum # add data

# ------------
# Run MCMC
# ------------
Example #3
0
def spamm_wlflux(components,
                 wl,
                 flux,
                 flux_err=None,
                 n_walkers=30,
                 n_iterations=500,
                 pname=None,
                 comp_params=None):
    """
    Args:
        components (dictionary): A dictionary with at least one component to
            model, e.g. {"FE": True}. Accepted key values are:
                - PL
                - FE
                - HOST
                - BC
                - BpC
                - Calzetti_ext
                - SMC_ext
                - MW_ext
                - AGN_ext
                - LMC_ext
        wl (array-like): Wavelength of input data spectrum.
        flux (array-like): Flux of input data spectrum.
        flux_err (array-like): Error on flux measurement.
        n_walkers (int): Number of walkers, or chains, to use in emcee.
        n_iterations (int): Number of iterations for each walker/chain.
        pname (str): Name of output pickle file. If None, name will be
            determined based on current run time.
        comp_params : dictionary
            Contains the known values of component parameters, with keys
            defined in each of the individual run scripts (run_XX.py).
            If None, the actual values of parameters will not be plotted.
    """

    t1 = datetime.datetime.now()
    for c in [
            "PL", "FE", "HOST", "BC", "BpC", "Calzetti_ext", "SMC_ext",
            "MW_ext", "AGN_ext", "LMC_ext"
    ]:
        if c not in components:
            components[c] = False

    if flux_err is None:
        flux_err = flux * 0.05

    spectrum = Spectrum.from_array(flux, uncertainty=flux_err)
    spectrum.dispersion = wl  #*units.angstrom
    spectrum.flux_error = flux_err

    if comp_params is None:
        comp_params = {}
    for k, v in zip(("wl", "flux", "err", "components"),
                    (wl, flux, flux_err, components)):
        if k not in comp_params:
            comp_params[k] = v

    # ------------
    # Initialize model
    # ------------
    model = Model()
    model.print_parameters = False

    # -----------------
    # Initialize components
    # -----------------
    if components["PL"]:
        try:
            if comp_params["broken_pl"] is True:
                brokenPL = True
            else:
                brokenPL = False
        except:
            brokenPL = False
        finally:
            nuclear_comp = NuclearContinuumComponent(broken=brokenPL)
            model.components.append(nuclear_comp)
    if components["FE"]:
        fe_comp = FeComponent()
        model.components.append(fe_comp)
    if components["HOST"]:
        host_galaxy_comp = HostGalaxyComponent()
        model.components.append(host_galaxy_comp)
    if components["BC"] or components["BpC"]:
        balmer_comp = BalmerCombined(BalmerContinuum=components["BC"],
                                     BalmerPseudocContinuum=components["BpC"])
        model.components.append(balmer_comp)
    if components["Calzetti_ext"] or components["SMC_ext"] or components[
            "MW_ext"] or components["AGN_ext"] or components["LMC_ext"]:
        ext_comp = Extinction(MW=MW_ext,
                              AGN=AGN_ext,
                              LMC=LMC_ext,
                              SMC=SMC_ext,
                              Calzetti=Calzetti_ext)
        model.components.append(ext_comp)

    model.data_spectrum = spectrum  # add data

    # ------------
    # Run MCMC
    # ------------
    model.run_mcmc(n_walkers=n_walkers, n_iterations=n_iterations)
    print("Mean acceptance fraction: {0:.3f}".format(
        np.mean(model.sampler.acceptance_fraction)))

    # -------------
    # save chains & model
    # ------------
    p_data = {"model": model, "comp_params": comp_params}

    if pname is None:
        now = datetime.datetime.now()
        pname = "model_{0}.pickle.gz".format(now.strftime("%Y%m%d_%M%S"))
    with gzip.open(pname, "wb") as model_output:
        model_output.write(pickle.dumps(p_data))
        print("Saved pickle file {0}".format(pname))
    make_chain_plots(pname)

    t2 = datetime.datetime.now()
    print("executed in {}".format(t2 - t1))
Example #4
0
def perform_test(components, datafile=None, comp_params=None):
    """
    Args:
        components : dictionary
            A dictionary with at least one component to model, e.g. {"FE": True}
        datafile : str
            Pathname of the datafile to be used as input.
        comp_params : dictionary
            If None, the component parameters (at minimum- wavelength, flux, flux error)
            will be determined from the datafile. If defined, comp_params *MUST* contain:
                - wl
                - flux
                - err
                - pname (can be None)
                - broken_pl (if component=PL)
    """
    # eventually need to update params to work with multiple components i.e. params["PL"]...
    redshift = False
    template_data = False
    change_wl = False

    for c in [
            "PL", "FE", "HOST", "BC", "BpC", "Calzetti_ext", "SMC_ext",
            "MW_ext", "AGN_ext", "LMC_ext"
    ]:
        if c not in components:
            components[c] = False
    if "n_walkers" not in components:
        components["n_walkers"] = 30
    if "n_iterations" not in components:
        components["n_iterations"] = 500

    if comp_params is None:
        keys = list(components.keys())
        i = 0
        while datafile is None:
            if components[keys[i]] is True:
                datafile = component_data[keys[i]]
                print("Using data file {0}".format(datafile))
            i += 1
        try:
            wavelengths, flux, flux_err = np.loadtxt(datafile, unpack=True)
        except ValueError:
            wavelengths, flux = np.loadtxt(datafile, unpack=True)
            flux_err = flux * 0.05
        finally:
            pname = None
            flux = np.where(flux < 0, 1e-19, flux)
    else:
        wavelengths = comp_params["wl"]
        flux = comp_params["flux"]
        flux_err = comp_params["err"]
        pname = comp_params["pname"]

    if redshift:
        print("Accounting for redshift")
        wavelengths /= 1.5

    if template_data:
        print("Scaling flux and error")
        flux *= 0.5
        flux_err *= 0.5

    if change_wl:
        print("Changing the wavelength range")
        idx = len(flux) // 2
        wavelengths = wavelengths[:idx]
        flux = flux[:idx]
        flux_err = flux_err[:idx]

#    mask = Mask(wavelengths=wavelengths,maskType=maskType)
#    spectrum.mask=mask
    spectrum = Spectrum.from_array(flux, uncertainty=flux_err)
    spectrum.dispersion = wavelengths  #*units.angstrom
    spectrum.flux_error = flux_err

    # ------------
    # Initialize model
    # ------------
    model = Model()
    model.print_parameters = False  #True#False#

    # -----------------
    # Initialize components
    # -----------------
    if components["PL"]:
        if comp_params:
            nuclear_comp = NuclearContinuumComponent(comp_params["broken_pl"])
        else:
            nuclear_comp = NuclearContinuumComponent()
        model.components.append(nuclear_comp)
    if components["FE"]:
        fe_comp = FeComponent()
        model.components.append(fe_comp)
    if components["HOST"]:
        host_galaxy_comp = HostGalaxyComponent()
        model.components.append(host_galaxy_comp)
    if components["BC"] or components["BpC"]:
        balmer_comp = BalmerCombined(BalmerContinuum=BC,
                                     BalmerPseudocContinuum=BpC)
        model.components.append(balmer_comp)
    if components["Calzetti_ext"] or components["SMC_ext"] or components[
            "MW_ext"] or components["AGN_ext"] or components["LMC_ext"]:
        ext_comp = Extinction(MW=MW_ext,
                              AGN=AGN_ext,
                              LMC=LMC_ext,
                              SMC=SMC_ext,
                              Calzetti=Calzetti_ext)
        model.components.append(ext_comp)

    if not comp_params:
        comp_params = {}
    comp_params["datafile"] = datafile

    model.data_spectrum = spectrum  # add data

    # ------------
    # Run MCMC
    # ------------
    model.run_mcmc(n_walkers=int(components["n_walkers"]),
                   n_iterations=int(components["n_iterations"]))
    print("Mean acceptance fraction: {0:.3f}".format(
        np.mean(model.sampler.acceptance_fraction)))

    # -------------
    # save chains & model
    # ------------
    p_data = {"model": model, "comp_params": comp_params}

    if pname is None:
        now = datetime.datetime.now()
        pname = "model_{0}.pickle.gz".format(now.strftime("%Y%m%d_%M%S"))
    with gzip.open(pname, "wb") as model_output:
        model_output.write(pickle.dumps(p_data))
        print("Saved pickle file {0}".format(pname))
    make_chain_plots(pname)
Example #5
0
def perform_test(components, datafile=None, params=None):

    if datafile:
        try:
            wavelengths, flux, flux_err = np.loadtxt(datafile, unpack=True)
        except ValueError:
            wavelengths, flux = np.loadtxt(datafile, unpack=True)
            flux_err = flux * 0.05
        finally:
            pname = None
    else:
        wavelengths = params["wl"]
        flux = params["flux"]
        flux_err = params["err"]
        pname = params["pname"]

#    mask = Mask(wavelengths=wavelengths,maskType=maskType)
    spectrum = Spectrum.from_array(flux, uncertainty=flux_err)
    #    spectrum.mask=mask
    spectrum.dispersion = wavelengths  #*units.angstrom
    spectrum.flux_error = flux_err

    # ------------
    # Initialize model
    # ------------
    model = Model()
    model.print_parameters = False  #True#False#

    # -----------------
    # Initialize components
    # -----------------
    if PL:
        nuclear_comp = NuclearContinuumComponent(params["broken_pl"])
        model.components.append(nuclear_comp)

    if FE:
        fe_comp = FeComponent()
        model.components.append(fe_comp)

    if HOST:
        host_galaxy_comp = HostGalaxyComponent()
        model.components.append(host_galaxy_comp)

    if BC or BpC:
        balmer_comp = BalmerCombined(BalmerContinuum=BC,
                                     BalmerPseudocContinuum=BpC)
        model.components.append(balmer_comp)

    if Calzetti_ext or SMC_ext or MW_ext or AGN_ext or LMC_ext:
        ext_comp = Extinction(MW=MW_ext,
                              AGN=AGN_ext,
                              LMC=LMC_ext,
                              SMC=SMC_ext,
                              Calzetti=Calzetti_ext)
        model.components.append(ext_comp)

    model.data_spectrum = spectrum  # add data
    # ------------
    # Run MCMC
    # ------------
    model.run_mcmc(n_walkers=n_walkers, n_iterations=n_iterations)
    print("Mean acceptance fraction: {0:.3f}".format(
        np.mean(model.sampler.acceptance_fraction)))

    # -------------
    # save chains & model
    # ------------
    p_data = {"model": model, "params": params}

    if pname is None:
        now = datetime.datetime.now()
        pname = "model_{0}.pickle.gz".format(now.strftime("%Y%m%d_%M%S"))
    with gzip.open(pname, "wb") as model_output:
        model_output.write(pickle.dumps(p_data))
        print("Saved pickle file {0}".format(pname))
    make_chain_plots(pname)