files.new_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  """Built-in models in Gammapy."""
  from gammapy.utils.registry import Registry
  from .core import *
  from .cube import *
  from .spatial import *
  from .spectral import *
  from .spectral_cosmic_ray import *
  from .spectral_crab import *
  from .temporal import *
  
  SPATIAL_MODEL_REGISTRY = Registry(
      [
          ConstantSpatialModel,
          TemplateSpatialModel,
          DiskSpatialModel,
          GaussianSpatialModel,
          GeneralizedGaussianSpatialModel,
          PointSpatialModel,
          ShellSpatialModel,
      ]
  )
  """Registry of spatial model classes."""
  
  SPECTRAL_MODEL_REGISTRY = Registry(
      [
          ConstantSpectralModel,
          CompoundSpectralModel,
          PowerLawSpectralModel,
          PowerLaw2SpectralModel,
          BrokenPowerLawSpectralModel,
          SmoothBrokenPowerLawSpectralModel,
          PiecewiseBrokenPowerLawSpectralModel,
          ExpCutoffPowerLawSpectralModel,
          ExpCutoffPowerLaw3FGLSpectralModel,
          SuperExpCutoffPowerLaw3FGLSpectralModel,
          SuperExpCutoffPowerLaw4FGLSpectralModel,
          LogParabolaSpectralModel,
          TemplateSpectralModel,
          GaussianSpectralModel,
          AbsorbedSpectralModel,
          NaimaSpectralModel,
          ScaleSpectralModel,
          PowerLawNormSpectralModel,
          LogParabolaNormSpectralModel,
          ExpCutoffPowerLawNormSpectralModel,
      ]
  )
  """Registry of spectral model classes."""
  
  TEMPORAL_MODEL_REGISTRY = Registry(
      [
          ConstantTemporalModel,
          LightCurveTemplateTemporalModel,
          ExpDecayTemporalModel,
          GaussianTemporalModel,
      ]
  )
  """Registry of temporal models classes."""
  
  MODEL_REGISTRY = Registry([SkyModel, BackgroundModel])
  """Registry of model classes"""
  
  
  __all__ = [
      "MODEL_REGISTRY",
      "SPATIAL_MODEL_REGISTRY",
      "TEMPORAL_MODEL_REGISTRY",
      "SPECTRAL_MODEL_REGISTRY",
      "SkyModelBase",
      "Models",
      "SkyModel",
      "BackgroundModel",
  
  
  [... the rest is omitted ...]
files.new_path: gammapy/modeling/models/__init__.py
files.old_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  """Built-in models in Gammapy."""
  from gammapy.utils.registry import Registry
  from .core import *
  from .cube import *
  from .spatial import *
  from .spectral import *
  from .spectral_cosmic_ray import *
  from .spectral_crab import *
  from .temporal import *
  
  SPATIAL_MODEL_REGISTRY = Registry(
      [
          ConstantSpatialModel,
          TemplateSpatialModel,
          DiskSpatialModel,
          GaussianSpatialModel,
          GeneralizedGaussianSpatialModel,
          PointSpatialModel,
          ShellSpatialModel,
      ]
  )
  """Registry of spatial model classes."""
  
  SPECTRAL_MODEL_REGISTRY = Registry(
      [
          ConstantSpectralModel,
          CompoundSpectralModel,
          PowerLawSpectralModel,
          PowerLaw2SpectralModel,
          BrokenPowerLawSpectralModel,
          SmoothBrokenPowerLawSpectralModel,
          ExpCutoffPowerLawSpectralModel,
          ExpCutoffPowerLaw3FGLSpectralModel,
          SuperExpCutoffPowerLaw3FGLSpectralModel,
          SuperExpCutoffPowerLaw4FGLSpectralModel,
          LogParabolaSpectralModel,
          TemplateSpectralModel,
          GaussianSpectralModel,
          AbsorbedSpectralModel,
          NaimaSpectralModel,
          ScaleSpectralModel,
          PowerLawNormSpectralModel,
          LogParabolaNormSpectralModel,
          ExpCutoffPowerLawNormSpectralModel,
      ]
  )
  """Registry of spectral model classes."""
  
  TEMPORAL_MODEL_REGISTRY = Registry(
      [
          ConstantTemporalModel,
          LightCurveTemplateTemporalModel,
          ExpDecayTemporalModel,
          GaussianTemporalModel,
      ]
  )
  """Registry of temporal models classes."""
  
  MODEL_REGISTRY = Registry([SkyModel, BackgroundModel])
  """Registry of model classes"""
  
  
  __all__ = [
      "MODEL_REGISTRY",
      "SPATIAL_MODEL_REGISTRY",
      "TEMPORAL_MODEL_REGISTRY",
      "SPECTRAL_MODEL_REGISTRY",
      "SkyModelBase",
      "Models",
      "SkyModel",
      "BackgroundModel",
      "create_crab_spectral_model",
     
  
  [... the rest is omitted ...]
files.old_path: gammapy/modeling/models/__init__.py
files.new_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  """Spectral models for Gammapy."""
  import operator
  import numpy as np
  import scipy.optimize
  import scipy.special
  import astropy.units as u
  from astropy import constants as const
  from astropy.table import Table
  from gammapy.maps import MapAxis
  from gammapy.maps.utils import edges_from_lo_hi
  from gammapy.modeling import Covariance, Parameter, Parameters
  from gammapy.utils.integrate import trapz_loglog
  from gammapy.utils.interpolation import ScaledRegularGridInterpolator
  from gammapy.utils.scripts import make_path
  from .core import Model
  
  
  def integrate_spectrum(func, emin, emax, ndecade=100, intervals=False):
      """Integrate 1d function using the log-log trapezoidal rule.
  
      If scalar values for xmin and xmax are passed an oversampled grid is generated using the
      ``ndecade`` keyword argument. If xmin and xmax arrays are passed, no
      oversampling is performed and the integral is computed in the provided
      grid.
  
      Parameters
      ----------
      func : callable
          Function to integrate.
      emin : `~astropy.units.Quantity`
          Integration range minimum
      emax : `~astropy.units.Quantity`
          Integration range minimum
      ndecade : int, optional
          Number of grid points per decade used for the integration.
          Default : 100.
      intervals : bool, optional
          Return integrals in the grid not the sum, default: False
      """
      if emin.isscalar and emax.isscalar:
          energies = MapAxis.from_energy_bounds(
              emin=emin, emax=emax, nbin=ndecade, per_decade=True
          ).edges
      else:
          energies = edges_from_lo_hi(emin, emax)
  
      values = func(energies)
  
      integral = trapz_loglog(values, energies)
  
      if intervals:
          return integral
  
      return integral.sum()
  
  
  class SpectralModel(Model):
      """Spectral model base class."""
      _type = "spectral"
  
      def __call__(self, energy):
  
  
  [... the rest is omitted ...]
files.new_path: gammapy/modeling/models/spectral.py
files.old_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  """Spectral models for Gammapy."""
  import operator
  import numpy as np
  import scipy.optimize
  import scipy.special
  import astropy.units as u
  from astropy import constants as const
  from astropy.table import Table
  from gammapy.maps import MapAxis
  from gammapy.maps.utils import edges_from_lo_hi
  from gammapy.modeling import Covariance, Parameter, Parameters
  from gammapy.utils.integrate import trapz_loglog
  from gammapy.utils.interpolation import ScaledRegularGridInterpolator
  from gammapy.utils.scripts import make_path
  from .core import Model
  
  
  def integrate_spectrum(func, emin, emax, ndecade=100, intervals=False):
      """Integrate 1d function using the log-log trapezoidal rule.
  
      If scalar values for xmin and xmax are passed an oversampled grid is generated using the
      ``ndecade`` keyword argument. If xmin and xmax arrays are passed, no
      oversampling is performed and the integral is computed in the provided
      grid.
  
      Parameters
      ----------
      func : callable
          Function to integrate.
      emin : `~astropy.units.Quantity`
          Integration range minimum
      emax : `~astropy.units.Quantity`
          Integration range minimum
      ndecade : int, optional
          Number of grid points per decade used for the integration.
          Default : 100.
      intervals : bool, optional
          Return integrals in the grid not the sum, default: False
      """
      if emin.isscalar and emax.isscalar:
          energies = MapAxis.from_energy_bounds(
              emin=emin, emax=emax, nbin=ndecade, per_decade=True
          ).edges
      else:
          energies = edges_from_lo_hi(emin, emax)
  
      values = func(energies)
  
      integral = trapz_loglog(values, energies)
  
      if intervals:
          return integral
  
      return integral.sum()
  
  
  class SpectralModel(Model):
      """Spectral model base class."""
      _type = "spectral"
  
      def __call__(self, energy):
  
  
  [... the rest is omitted ...]
files.old_path: gammapy/modeling/models/spectral.py
files.new_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  import pytest
  import numpy as np
  from numpy.testing import assert_allclose
  import astropy.units as u
  from astropy.table import Table
  from astropy.utils.data import get_pkg_data_filename
  from gammapy.maps import Map, MapAxis
  from gammapy.modeling.models import (
      MODEL_REGISTRY,
      AbsorbedSpectralModel,
      Absorption,
      BackgroundModel,
      Model,
      Models,
      PiecewiseBrokenPowerLawSpectralModel,
  )
  from gammapy.utils.scripts import read_yaml, write_yaml
  from gammapy.utils.testing import requires_data
  
  
  @requires_data()
  def test_dict_to_skymodels():
      filename = get_pkg_data_filename("data/examples.yaml")
      models_data = read_yaml(filename)
      models = Models.from_dict(models_data)
  
      assert len(models) == 5
  
      model0 = models[0]
      assert isinstance(model0, BackgroundModel)
      assert model0.name == "background_irf"
  
      model0 = models[1]
      assert "ExpCutoffPowerLawSpectralModel" in model0.spectral_model.tag
      assert "PointSpatialModel" in model0.spatial_model.tag
  
      pars0 = model0.parameters
      assert pars0["index"].value == 2.1
      assert pars0["index"].unit == ""
      assert np.isnan(pars0["index"].max)
      assert np.isnan(pars0["index"].min)
      assert not pars0["index"].frozen
  
      assert pars0["lon_0"].value == -0.5
      assert pars0["lon_0"].unit == "deg"
      assert pars0["lon_0"].max == 180.0
      assert pars0["lon_0"].min == -180.0
      assert pars0["lon_0"].frozen
  
      assert pars0["lat_0"].value == -0.0005
      assert pars0["lat_0"].unit == "deg"
      assert pars0["lat_0"].max == 90.0
      assert pars0["lat_0"].min == -90.0
      assert pars0["lat_0"].
  
  [... the rest is omitted ...]
files.new_path: gammapy/modeling/models/tests/test_io.py
files.old_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  import pytest
  import numpy as np
  from numpy.testing import assert_allclose
  import astropy.units as u
  from astropy.table import Table
  from astropy.utils.data import get_pkg_data_filename
  from gammapy.maps import Map, MapAxis
  from gammapy.modeling.models import (
      MODEL_REGISTRY,
      AbsorbedSpectralModel,
      Absorption,
      BackgroundModel,
      Model,
      Models,
  )
  from gammapy.utils.scripts import read_yaml, write_yaml
  from gammapy.utils.testing import requires_data
  
  
  @requires_data()
  def test_dict_to_skymodels():
      filename = get_pkg_data_filename("data/examples.yaml")
      models_data = read_yaml(filename)
      models = Models.from_dict(models_data)
  
      assert len(models) == 5
  
      model0 = models[0]
      assert isinstance(model0, BackgroundModel)
      assert model0.name == "background_irf"
  
      model0 = models[1]
      assert "ExpCutoffPowerLawSpectralModel" in model0.spectral_model.tag
      assert "PointSpatialModel" in model0.spatial_model.tag
  
      pars0 = model0.parameters
      assert pars0["index"].value == 2.1
      assert pars0["index"].unit == ""
      assert np.isnan(pars0["index"].max)
      assert np.isnan(pars0["index"].min)
      assert not pars0["index"].frozen
  
      assert pars0["lon_0"].value == -0.5
      assert pars0["lon_0"].unit == "deg"
      assert pars0["lon_0"].max == 180.0
      assert pars0["lon_0"].min == -180.0
      assert pars0["lon_0"].frozen
  
      assert pars0["lat_0"].value == -0.0005
      assert pars0["lat_0"].unit == "deg"
      assert pars0["lat_0"].max == 90.0
      assert pars0["lat_0"].min == -90.0
      assert pars0["lat_0"].frozen
  
      assert pars0["lambda_"].
  
  [... the rest is omitted ...]
files.old_path: gammapy/modeling/models/tests/test_io.py
files.new_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  import pytest
  import numpy as np
  import operator
  from numpy.testing import assert_allclose
  import astropy.units as u
  from gammapy.maps import MapAxis
  from gammapy.modeling.models import (
      SPECTRAL_MODEL_REGISTRY,
      AbsorbedSpectralModel,
      Absorption,
      ConstantSpectralModel,
      CompoundSpectralModel,
      ExpCutoffPowerLaw3FGLSpectralModel,
      ExpCutoffPowerLawSpectralModel,
      ExpCutoffPowerLawNormSpectralModel,
      GaussianSpectralModel,
      LogParabolaSpectralModel,
      LogParabolaNormSpectralModel,
      NaimaSpectralModel,
      PowerLaw2SpectralModel,
      PowerLawSpectralModel,
      PowerLawNormSpectralModel,
      BrokenPowerLawSpectralModel,
      SmoothBrokenPowerLawSpectralModel,
      SuperExpCutoffPowerLaw4FGLSpectralModel,
      TemplateSpectralModel,
      PiecewiseBrokenPowerLawSpectralModel,
  )
  from gammapy.utils.testing import (
      assert_quantity_allclose,
      mpl_plot_check,
      requires_data,
      requires_dependency,
  )
  
  
  def table_model():
      energy = MapAxis.from_energy_bounds(0.1 * u.TeV, 100 * u.TeV, 1000).center
  
      model = PowerLawSpectralModel(
          index=2.3, amplitude="4 cm-2 s-1 TeV-1", reference="1 TeV"
      )
      dnde = model(energy)
  
      return TemplateSpectralModel(energy, dnde)
  
  
  TEST_MODELS = [
      dict(
          name="powerlaw",
          model=PowerLawSpectralModel(
              index=2.3 * u.Unit(""),
              amplitude=4 / u.cm ** 2 / u.s / u.TeV,
              reference=1 * u.TeV,
          ),
          val_at_2TeV=u.Quantity(4 * 2.0 ** (-2.3), "cm-2 s-1 TeV-1"),
          integral_1_10TeV=u.Quantity(2.9227116204223784, "cm-2 s-1"),
          eflux_
  
  [... the rest is omitted ...]
files.new_path: gammapy/modeling/models/tests/test_spectral.py
files.old_contents: # Licensed under a 3-clause BSD style license - see LICENSE.rst
  import pytest
  import numpy as np
  import operator
  from numpy.testing import assert_allclose
  import astropy.units as u
  from gammapy.maps import MapAxis
  from gammapy.modeling.models import (
      SPECTRAL_MODEL_REGISTRY,
      AbsorbedSpectralModel,
      Absorption,
      ConstantSpectralModel,
      CompoundSpectralModel,
      ExpCutoffPowerLaw3FGLSpectralModel,
      ExpCutoffPowerLawSpectralModel,
      ExpCutoffPowerLawNormSpectralModel,
      GaussianSpectralModel,
      LogParabolaSpectralModel,
      LogParabolaNormSpectralModel,
      NaimaSpectralModel,
      PowerLaw2SpectralModel,
      PowerLawSpectralModel,
      PowerLawNormSpectralModel,
      BrokenPowerLawSpectralModel,
      SmoothBrokenPowerLawSpectralModel,
      SuperExpCutoffPowerLaw4FGLSpectralModel,
      TemplateSpectralModel,
  )
  from gammapy.utils.testing import (
      assert_quantity_allclose,
      mpl_plot_check,
      requires_data,
      requires_dependency,
  )
  
  
  def table_model():
      energy = MapAxis.from_energy_bounds(0.1 * u.TeV, 100 * u.TeV, 1000).center
  
      model = PowerLawSpectralModel(
          index=2.3, amplitude="4 cm-2 s-1 TeV-1", reference="1 TeV"
      )
      dnde = model(energy)
  
      return TemplateSpectralModel(energy, dnde)
  
  
  TEST_MODELS = [
      dict(
          name="powerlaw",
          model=PowerLawSpectralModel(
              index=2.3 * u.Unit(""),
              amplitude=4 / u.cm ** 2 / u.s / u.TeV,
              reference=1 * u.TeV,
          ),
          val_at_2TeV=u.Quantity(4 * 2.0 ** (-2.3), "cm-2 s-1 TeV-1"),
          integral_1_10TeV=u.Quantity(2.9227116204223784, "cm-2 s-1"),
          eflux_1_10TeV=u.Quantity(6
  
  [... the rest is omitted ...]
files.old_path: gammapy/modeling/models/tests/test_spectral.py