Ejemplo n.º 1
0
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)

nwv = 4861
m = abs(spectrum.wavelengths - nwv) == min(abs(spectrum.wavelengths - nwv))
BCflux /= BCflux[m]

plt.plot(spectrum.wavelengths, BCflux, 'k|')
plt.plot([1000, 5000], [norm, norm], 'k--')
plt.plot([3646, 3646], [0, norm * 1.3], 'r--')

x2, y2 = sp.genfromtxt(
Ejemplo n.º 2
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))
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)