Skip to content

Commit

Permalink
Merge pull request #4734 from meeseeksmachine/auto-backport-of-pr-472…
Browse files Browse the repository at this point in the history
…1-on-v1.1.x

Backport PR #4721 on branch v1.1.x (Exposing NormSpectralModels)
  • Loading branch information
registerrier committed Sep 4, 2023
2 parents da56284 + 5e86164 commit 80b86aa
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/models/spectral/plot_exp_cutoff_powerlaw_norm_spectral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
r"""
.. _exp-cutoff-powerlaw-norm-spectral-model:
Exponential cutoff power law norm spectral model
========================
This model parametrises a cutoff power law spectral correction with a norm parameter.
"""

# %%
# 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 (
ExpCutoffPowerLawNormSpectralModel,
ExpCutoffPowerLawSpectralModel,
Models,
SkyModel,
TemplateSpectralModel,
)

energy_bounds = [0.1, 100] * u.TeV

energy = [0.3, 1, 3, 10, 30] * u.TeV
values = [40, 30, 20, 10, 1] * u.Unit("TeV-1 s-1 cm-2")
template = TemplateSpectralModel(energy, values)
norm = ExpCutoffPowerLawNormSpectralModel(
norm=2,
reference=1 * u.TeV,
)

template.plot(energy_bounds=energy_bounds, label="Template model")
ecpl_norm = template * norm
ecpl_norm.plot(
energy_bounds, label="Template model with ExpCutoffPowerLaw norm correction"
)
plt.legend(loc="best")
plt.grid(which="both")

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

model = SkyModel(spectral_model=ecpl_norm, name="exp-cutoff-power-law-norm-model")
models = Models([model])

print(models.to_yaml())
51 changes: 51 additions & 0 deletions examples/models/spectral/plot_logparabola_norm_spectral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
r"""
.. _logparabola-spectral-norm-model:
Log parabola spectral norm model
===========================
This model parametrises a log parabola spectral correction with a norm parameter.
"""

# %%
# 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 (
LogParabolaNormSpectralModel,
LogParabolaSpectralModel,
Models,
SkyModel,
TemplateSpectralModel,
)

energy_bounds = [0.1, 100] * u.TeV

energy = [0.3, 1, 3, 10, 30] * u.TeV
values = [40, 30, 20, 10, 1] * u.Unit("TeV-1 s-1 cm-2")
template = TemplateSpectralModel(energy, values)
norm = LogParabolaNormSpectralModel(
norm=1.5,
reference=1 * u.TeV,
)

template.plot(energy_bounds=energy_bounds, label="Template model")
lp_norm = template * norm
lp_norm.plot(energy_bounds, label="Template model with LogParabola norm correction")
plt.legend(loc="best")
plt.grid(which="both")


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

model = SkyModel(spectral_model=lp_norm, name="log-parabola-norm-model")
models = Models([model])

print(models.to_yaml())
50 changes: 50 additions & 0 deletions examples/models/spectral/plot_powerlaw_norm_spectral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
r"""
.. _powerlaw-spectral-norm-model:
Power law norm spectral model
========================
This model parametrises a power law spectral correction with a norm and tilt parameter.
"""

# %%
# 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,
PowerLawNormSpectralModel,
PowerLawSpectralModel,
SkyModel,
TemplateSpectralModel,
)

energy_bounds = [0.1, 100] * u.TeV

energy = [0.3, 1, 3, 10, 30] * u.TeV
values = [40, 30, 20, 10, 1] * u.Unit("TeV-1 s-1 cm-2")
template = TemplateSpectralModel(energy, values)
norm = PowerLawNormSpectralModel(
norm=5,
reference=1 * u.TeV,
)

template.plot(energy_bounds=energy_bounds, label="Template model")
pwl_norm = template * norm
pwl_norm.plot(energy_bounds, label="Template model with PowerLaw norm correction")
plt.legend(loc="best")
plt.grid(which="both")

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

model = SkyModel(spectral_model=pwl_norm, name="power-law-norm-model")
models = Models([model])

print(models.to_yaml())

0 comments on commit 80b86aa

Please sign in to comment.