def test_absorbed_extrapolate(): ebl_model = "dominguez" z = 0.001 absorption = Absorption.read_builtin(ebl_model) model = absorption.table_model(z) assert_allclose(model(1 * u.TeV), 1)
def test_absorption_io(tmp_path): dominguez = Absorption.read_builtin("dominguez") model = AbsorbedSpectralModel( spectral_model=Model.create("PowerLawSpectralModel"), absorption=dominguez, parameter=0.5, parameter_name="redshift", ) assert len(model.parameters) == 5 model_dict = model.to_dict() parnames = [_["name"] for _ in model_dict["parameters"]] assert parnames == ["redshift", "alpha_norm"] new_model = AbsorbedSpectralModel.from_dict(model_dict) assert new_model.parameter == 0.5 assert new_model.parameter_name == "redshift" assert new_model.alpha_norm.name == "alpha_norm" assert new_model.alpha_norm.value == 1 assert new_model.spectral_model.tag == "PowerLawSpectralModel" assert_allclose(new_model.absorption.energy, dominguez.energy) assert_allclose(new_model.absorption.param, dominguez.param) assert len(new_model.parameters) == 5 test_absorption = Absorption( u.Quantity(range(3), "keV"), u.Quantity(range(2), ""), u.Quantity(np.ones((2, 3)), ""), ) model = AbsorbedSpectralModel( spectral_model=Model.create("PowerLawSpectralModel"), absorption=test_absorption, parameter=0.5, parameter_name="redshift", ) model_dict = model.to_dict() new_model = AbsorbedSpectralModel.from_dict(model_dict) assert_allclose(new_model.absorption.energy, test_absorption.energy) assert_allclose(new_model.absorption.param, test_absorption.param) write_yaml(model_dict, tmp_path / "tmp.yaml") read_yaml(tmp_path / "tmp.yaml")
def test_absorption(): # absorption values for given redshift redshift = 0.117 absorption = Absorption.read_builtin("dominguez") # Spectral model corresponding to PKS 2155-304 (quiescent state) index = 3.53 amplitude = 1.81 * 1e-12 * u.Unit("cm-2 s-1 TeV-1") reference = 1 * u.TeV pwl = PowerLawSpectralModel(index=index, amplitude=amplitude, reference=reference) # EBL + PWL model model = AbsorbedSpectralModel(spectral_model=pwl, absorption=absorption, parameter=redshift) desired = u.Quantity(5.140765e-13, "TeV-1 s-1 cm-2") assert_quantity_allclose(model(1 * u.TeV), desired, rtol=1e-3) assert model.alpha_norm.value == 1.0 # EBL + PWL model: test if norm of EBL=0: it mean model =pwl model = AbsorbedSpectralModel(spectral_model=pwl, absorption=absorption, alpha_norm=0, parameter=redshift) assert_quantity_allclose(model(1 * u.TeV), pwl(1 * u.TeV), rtol=1e-3) # EBL + PWL model: Test with a norm different of 1 model = AbsorbedSpectralModel(spectral_model=pwl, absorption=absorption, alpha_norm=1.5, parameter=redshift) desired = u.Quantity(2.739695e-13, "TeV-1 s-1 cm-2") assert model.alpha_norm.value == 1.5 assert_quantity_allclose(model(1 * u.TeV), desired, rtol=1e-3) # Test error propagation model.spectral_model.parameters.set_error( amplitude=0.1 * model.spectral_model.amplitude.value) dnde, dnde_err = model.evaluate_error(1 * u.TeV) assert_allclose(dnde_err / dnde, 0.1)
This model represents EBL absorption models. They are usually used as part of `~gammapy.modeling.models.AbsorbedSpectralModel` """ # %% # Example plot # ------------ # Here we illustrate how to create and plot EBL absorption models for a redshift of 0.5: from astropy import units as u import matplotlib.pyplot as plt from gammapy.modeling.models import Absorption redshift = 0.5 dominguez = Absorption.read_builtin("dominguez").table_model(redshift) franceschini = Absorption.read_builtin("franceschini").table_model(redshift) finke = Absorption.read_builtin("finke").table_model(redshift) plt.figure() energy_range = [0.08, 3] * u.TeV opts = dict(energy_range=energy_range, energy_unit="TeV", flux_unit="") franceschini.plot(label="Franceschini 2008", **opts) finke.plot(label="Finke 2010", **opts) dominguez.plot(label="Dominguez 2011", **opts) plt.ylabel(r"Absorption coefficient [$\exp{(-\tau(E))}$]") plt.xlim(energy_range.value) plt.ylim(1e-4, 2) plt.title(f"EBL models (z={redshift})") plt.grid(which="both")
# Example plot # ------------ # Here is an example plot of the model: from astropy import units as u import matplotlib.pyplot as plt from gammapy.modeling.models import ( AbsorbedSpectralModel, Absorption, Models, PowerLawSpectralModel, SkyModel, ) redshift = 0.117 absorption = Absorption.read_builtin("dominguez") # Spectral model corresponding to PKS 2155-304 (quiescent state) index = 3.53 amplitude = 1.81 * 1e-12 * u.Unit("cm-2 s-1 TeV-1") reference = 1 * u.TeV pwl = PowerLawSpectralModel(index=index, amplitude=amplitude, reference=reference) # EBL + PWL model model = AbsorbedSpectralModel(spectral_model=pwl, absorption=absorption, redshift=redshift) energy_range = [0.1, 100] * u.TeV
import matplotlib.pyplot as plt import astropy.units as u from gammapy.modeling.models import Absorption # Load tables for z=0.5 redshift = 0.5 dominguez = Absorption.read_builtin('dominguez').table_model(redshift) franceschini = Absorption.read_builtin('franceschini').table_model(redshift) finke = Absorption.read_builtin('finke').table_model(redshift) # start customised plot energy_range = [0.08, 3] * u.TeV ax = plt.gca() opts = dict(energy_range=energy_range, energy_unit='TeV', ax=ax, flux_unit='') franceschini.plot(label='Franceschini 2008', **opts) finke.plot(label='Finke 2010', **opts) dominguez.plot(label='Dominguez 2011', **opts) # tune plot ax.set_ylabel(r'Absorption coefficient [$\exp{(-\tau(E))}$]') ax.set_xlim(energy_range.value) # we get ride of units ax.set_ylim([1.e-4, 2.]) ax.set_yscale('log') ax.set_title('EBL models (z=' + str(redshift) + ')') plt.grid(which='both') plt.legend(loc='best') # legend # show plot plt.show()