Beispiel #1
0
def create_nc(nc_params=None):
    if nc_params is None:
        nc_params = {"wl": WL}
        nc_params["broken_pl"] = False
        nc_params["slope1"] = draw_from_sample.gaussian(
            PARS["pl_slope_min"], PARS["pl_slope_max"])
        max_template_flux = 1e-13
        nc_params["norm_PL"] = draw_from_sample.gaussian(
            PARS["pl_norm_min"], max_template_flux)
    print("NC params: {}".format(nc_params))
    nc = NuclearContinuumComponent(nc_params["broken_pl"])

    # Make a Spectrum object with dummy flux
    spectrum = Spectrum(nc_params["wl"])
    spectrum.dispersion = nc_params["wl"]
    nc.initialize(spectrum)
    if nc_params["broken_pl"] == True:
        comp_params = [
            nc_params["wave_break"], nc_params["norm_PL"], nc_params["slope1"],
            nc_params["slope2"]
        ]
    else:
        comp_params = [nc_params["norm_PL"], nc_params["slope1"]]

    nc_flux = NuclearContinuumComponent.flux(nc, spectrum, comp_params)
    nc_err = nc_flux * 0.05

    return nc_params["wl"], nc_flux, nc_err, nc_params
Beispiel #2
0
def create_fe(fe_params=None):
    """
    Args:
        fe_params (dictionary): Iron component parameters. Required keys are:
            - no_templates (number of templates)
            - wl (wavelength range of Fe model, redshift must be accounted for already)
            - fe_width (in km/s)
            - fe_norm_{} (1,2,3 depending on number of templates)
    """

    if fe_params is None:
        fe_params = {"no_templates": 3, "wl": WL}
        max_template_flux = 1.8119e-14
        samples = draw_from_sample.gaussian(PARS["fe_norm_min"],
                                            max_template_flux, 3)
        fe_params["fe_norm_1"] = samples[0]
        fe_params["fe_norm_2"] = samples[1]
        fe_params["fe_norm_3"] = samples[2]
        sample = draw_from_sample.gaussian(PARS["fe_width_min"],
                                           PARS["fe_width_max"])
        fe_params["fe_width"] = sample

    print("Fe params: {}".format(fe_params))
    fe = FeComponent()
    # Make a Spectrum object with dummy flux
    spectrum = Spectrum(fe_params["wl"])
    spectrum.dispersion = fe_params["wl"]
    fe.initialize(spectrum)
    comp_params = [
        fe_params["fe_norm_{}".format(x)]
        for x in range(1, fe_params["no_templates"] + 1)
    ] + [fe_params["fe_width"]]
    fe_flux = FeComponent.flux(fe, spectrum, comp_params)
    fe_err = fe_flux * 0.05

    #    pl.errorbar(fe_params["wl"], fe_flux, fe_err)
    #    pl.savefig("fe_data.png")

    return fe_params["wl"], fe_flux, fe_err, fe_params
Beispiel #3
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
Beispiel #4
0
import matplotlib.pyplot as pl

import triangle

sys.path.append(os.path.abspath("../source"))

from spamm.Spectrum import Spectrum
from spamm.Model import Model
from spamm.components.NuclearContinuumComponent import NuclearContinuumComponent
from spamm.components.HostGalaxyComponent import HostGalaxyComponent
from spamm.components.FeComponent import FeComponent
from spamm.components.BalmerContinuum import BalmerContinuum

datafile = 'testLBT.dat'
wavelengths, flux, flux_err = np.loadtxt(datafile, unpack=True)
spectrum = Spectrum()
spectrum.wavelengths = sp.r_[1000:5200:10000j]
spectrum.flux = sp.ones(10000)
spectrum.flux_error = sp.ones(10000)

model = Model()
bc_comp = BalmerContinuum()
model.components.append(bc_comp)

model.data_spectrum = spectrum  # add data

norm = 1.e-15
pvector = [norm, 0.5e4, 0.1, 0.0, 0.5e8]

BCflux = model.model_flux(pvector)
Beispiel #5
0
# Initialize model
# ------------
model = Model()
model.print_parameters = False

# -----------------
# Initialize components
# -----------------
if True:
    fe_comp = FeComponent()

    #datafile = "../Data/FakeData/for_gisella/fake_host_spectrum.dat"
    #datafile = "../Data/FakeData/Iron_comp/fakeFe1_deg.dat"
    datafile = "../Fe_templates/FeSimdata_BevWills_0p05.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(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
Beispiel #6
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)
Beispiel #7
0
            #print('size',np.shape(sorted))
            #print('sorted',sorted[1])
            var_arr[jj, xx] = flux_errresize[sorted[1]]**2

flux = np.sum(fluxes_arr, axis=0)
var = np.sum(var_arr, axis=0)
flux_err = np.sqrt(var)

print('sizes', np.shape(flux), np.shape(wavelengths))

#spectrum = Spectrum()#maskType="Emission lines reduced")#"Cont+Fe")#
#spectrum.wavelengths = wavelengths
#spectrum.flux = flux
#spectrum.flux_error = flux_err
mask = Mask(wavelengths=wavelengths, maskType=maskType)
spectrum = Spectrum(flux)
spectrum.mask = mask
spectrum.dispersion = wavelengths
spectrum.flux_error = flux_err

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

# -----------------
# Initialize components
# -----------------
if PL:
    nuclear_comp = NuclearContinuumComponent()
Beispiel #8
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))
Beispiel #9
0
    datafile = "../Data/FakeData/fake_host_spectrum.dat"

if FE:
    #datafile = "../Data/FakeData/for_gisella/fake_host_spectrum.dat"
    datafile = "../Data/FakeData/Iron_comp/fakeFe1_deg.dat"
    #datafile = "../Fe_templates/FeSimdata_BevWills_0p05.dat"

if BC:
    datafile = "../Data/FakeData/BaC_comp/FakeBac01_deg.dat"
if BC and BpC:
    datafile = "../Data/FakeData/BaC_comp/FakeBac_lines01_deg.dat"

# do you think there will be any way to open generic fits file and you specify hdu, npix, midpix, wavelength stuff
wavelengths, flux, flux_err = np.loadtxt(datafile, unpack=True)
mask = Mask(wavelengths=wavelengths, maskType=maskType)
spectrum = Spectrum.from_array(flux, uncertainty=flux_err, mask=mask)
#spectrum = Spectrum(maskType="Emission lines reduced")#"Cont+Fe")#
spectrum.mask = mask
spectrum.dispersion = wavelengths  #*units.angstrom
spectrum.flux_error = flux_err
pl.plot(spectrum.wavelengths, spectrum.flux)
pl.show()
#exit()

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

# -----------------
Beispiel #10
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)