Esempio n. 1
0
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)
Esempio n. 2
0
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")
Esempio n. 3
0
    def _set_model_dataset(self, ch, mass):
        """Attach to loop dataset the model to fit."""

        jfactor = self.flux_model.parameters[
            "jfactor"].value * self.flux_model.parameters["jfactor"].unit
        flux_model = DarkMatterAnnihilationSpectralModel(mass=mass,
                                                         channel=ch,
                                                         sv=1,
                                                         jfactor=jfactor,
                                                         z=self.flux_model.z,
                                                         k=self.flux_model.k)
        if isinstance(self.dataset.models[0].spectral_model,
                      AbsorbedSpectralModel):
            flux_model = AbsorbedSpectralModel(
                flux_model, self.dataset.models[0].spectral_model.absorption,
                self.flux_model.z)
        ds = self.dataset.copy()
        ds.models[0].spectral_model = flux_model
        return ds
Esempio n. 4
0
)

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
model.plot(energy_range)
plt.grid(which="both")
plt.ylim(1e-24, 1e-8)

# %%
# YAML representation
# -------------------
# Here is an example YAML file using the model:

model = SkyModel(spectral_model=model, name="absorbed-model")
models = Models([model])