Ejemplo n.º 1
0
def test_piecewise_norm_spectral_model_init():
    with pytest.raises(ValueError):
        PiecewiseNormSpectralModel(energy=[
            1,
        ] * u.TeV, norms=[1, 5])

    with pytest.raises(ValueError):
        PiecewiseNormSpectralModel(energy=[
            1,
        ] * u.TeV, norms=[
            1,
        ])
Ejemplo n.º 2
0
def test_piecewise_norm_spectral_model_io():
    energy = [1, 3, 7, 10] * u.TeV
    norms = [1, 5, 3, 0.5] * u.Unit("")

    model = PiecewiseNormSpectralModel(energy=energy, norms=norms)
    model.parameters[0].value = 2

    model_dict = model.to_dict()

    parnames = [_["name"] for _ in model_dict["parameters"]]
    for k, parname in enumerate(parnames):
        assert parname == f"norm_{k}"

    new_model = PiecewiseNormSpectralModel.from_dict(model_dict)

    assert_allclose(new_model.parameters[0].value, 2)
    assert_allclose(new_model.energy, energy)
    assert_allclose(new_model.norms, [2, 5, 3, 0.5])
# 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 (
    Models,
    PiecewiseNormSpectralModel,
    PowerLawSpectralModel,
    SkyModel,
)

energy_range = [0.1, 100] * u.TeV
model = PiecewiseNormSpectralModel(
    energy=[0.1, 1, 3, 10, 30, 100] * u.TeV,
    norms=[1, 3, 8, 10, 8, 2],
)
model.plot(energy_range, flux_unit="")
plt.grid(which="both")

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

model = model * PowerLawSpectralModel()
model = SkyModel(spectral_model=model, name="piecewise-norm-model")
models = Models([model])

print(models.to_yaml())
Ejemplo n.º 4
0
        model=SmoothBrokenPowerLawSpectralModel(
            index1=2.5 * u.Unit(""),
            index2=1.5 * u.Unit(""),
            amplitude=4 / u.cm ** 2 / u.s / u.TeV,
            ebreak=0.5 * u.TeV,
            reference=1 * u.TeV,
            beta=1,
        ),
        val_at_2TeV=u.Quantity(3.5355339059327378, "cm-2 s-1 TeV-1"),
        integral_1_10TeV=u.Quantity(13.522782989735022, "cm-2 s-1"),
        eflux_1_10TeV=u.Quantity(40.06681812966845, "TeV cm-2 s-1"),
    ),
    dict(
        name="pbpl",
        model=PiecewiseNormSpectralModel(
            energy=[1, 3, 7, 10] * u.TeV,
            norms=[1, 5, 3, 0.5] * u.Unit(""),
        ),
        val_at_2TeV=u.Quantity(2.76058404, ""),
        integral_1_10TeV=u.Quantity(24.758255, "TeV"),
        eflux_1_10TeV=u.Quantity(117.745068, "TeV2"),
    ),
]

# Add compound models

TEST_MODELS.append(
    dict(
        name="compound3",
        model=TEST_MODELS[0]["model"] + TEST_MODELS[0]["model"],
        val_at_2TeV=TEST_MODELS[0]["val_at_2TeV"] * 2,
        integral_1_10TeV=TEST_MODELS[0]["integral_1_10TeV"] * 2,
# 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 (
    Models,
    PiecewiseNormSpectralModel,
    PowerLawSpectralModel,
    SkyModel,
)

energy_bounds = [0.1, 100] * u.TeV
model = PiecewiseNormSpectralModel(
    energy=[0.1, 1, 3, 10, 30, 100] * u.TeV,
    norms=[1, 3, 8, 10, 8, 2],
)
model.plot(energy_bounds, yunits=u.Unit(""))
plt.grid(which="both")

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

model = model * PowerLawSpectralModel()
model = SkyModel(spectral_model=model, name="piecewise-norm-model")
models = Models([model])

print(models.to_yaml())