Repository: gammapy/gammapy

Issue:
Error in executing `FluxPoints.to_sed_type()`
**Gammapy version**

Present dev

**Bug description**
Calling `FluxPoints.to_sed_type()` returns an error

```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-225-40a5362800d4> in <module>
----> 1 flux_points_fermi = FluxPoints(fgl_src.flux_points.table).to_sed_type("dnde", model=fgl_src.sky_model().spectral_model)
      2 fpds_fermi = FluxPointsDataset(
      3     data=flux_points_fermi, models=fgl_src.sky_model()
      4 )

~/Gammapy-dev/gammapy/gammapy/estimators/flux_point.py in to_sed_type(self, sed_type, method, model, pwl_approx)
    380             else:
    381                 raise ValueError(f"Invalid method: {method}")
--> 382             table = self._flux_to_dnde(e_ref, table, model, pwl_approx)
    383 
    384         elif self.sed_type == "dnde" and sed_type == "e2dnde":

~/Gammapy-dev/gammapy/gammapy/estimators/flux_point.py in _flux_to_dnde(self, e_ref, table, model, pwl_approx)
    277 
    278         flux = table["flux"].quantity
--> 279         dnde = self._dnde_from_flux(flux, model, e_ref, e_min, e_max, pwl_approx)
    280 
    281         # Add to result table

~/Gammapy-dev/gammapy/gammapy/estimators/flux_point.py in _dnde_from_flux(flux, model, e_ref, e_min, e_max, pwl_approx)
    436             )
    437         else:
--> 438             flux_model = model.integral(e_min, e_max, intervals=True)
    439 
    440         return dnde_model * (flux / flux_model)

~/Gammapy-dev/gammapy/gammapy/modeling/models/spectral.py in integral(self, emin, emax, **kwargs)
    167             return self.evaluate_integral(emin, emax, **kwargs)
    168         else:
--> 169             return integrate_spectrum(self, emin, emax, **kwargs)
    170 
    171     def integral_error(self, emin, emax):

TypeError: integrate_spectrum() got an unexpected keyword argument 'intervals'

```

**To Reproduce**

```
from gammapy.catalog import CATALOG_REGISTRY
catalog_4fgl = CATALOG_REGISTRY.get_cls("4fgl")()
fgl_src = catalog_4fgl["FGES J1553.8-5325"]
flux_points_fermi = FluxPoints(fgl_src.flux_points.table).to_sed_type("dnde", 
                                                                      model=fgl_src.sky_model().spectral_model)
```
Error in executing `FluxPoints.to_sed_type()`
**Gammapy version**

Present dev

**Bug description**
Calling `FluxPoints.to_sed_type()` returns an error

```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-225-40a5362800d4> in <module>
----> 1 flux_points_fermi = FluxPoints(fgl_src.flux_points.table).to_sed_type("dnde", model=fgl_src.sky_model().spectral_model)
      2 fpds_fermi = FluxPointsDataset(
      3     data=flux_points_fermi, models=fgl_src.sky_model()
      4 )

~/Gammapy-dev/gammapy/gammapy/estimators/flux_point.py in to_sed_type(self, sed_type, method, model, pwl_approx)
    380             else:
    381                 raise ValueError(f"Invalid method: {method}")
--> 382             table = self._flux_to_dnde(e_ref, table, model, pwl_approx)
    383 
    384         elif self.sed_type == "dnde" and sed_type == "e2dnde":

~/Gammapy-dev/gammapy/gammapy/estimators/flux_point.py in _flux_to_dnde(self, e_ref, table, model, pwl_approx)
    277 
    278         flux = table["flux"].quantity
--> 279         dnde = self._dnde_from_flux(flux, model, e_ref, e_min, e_max, pwl_approx)
    280 
    281         # Add to result table

~/Gammapy-dev/gammapy/gammapy/estimators/flux_point.py in _dnde_from_flux(flux, model, e_ref, e_min, e_max, pwl_approx)
    436             )
    437         else:
--> 438             flux_model = model.integral(e_min, e_max, intervals=True)
    439 
    440         return dnde_model * (flux / flux_model)

~/Gammapy-dev/gammapy/gammapy/modeling/models/spectral.py in integral(self, emin, emax, **kwargs)
    167             return self.evaluate_integral(emin, emax, **kwargs)
    168         else:
--> 169             return integrate_spectrum(self, emin, emax, **kwargs)
    170 
    171     def integral_error(self, emin, emax):

TypeError: integrate_spectrum() got an unexpected keyword argument 'intervals'

```

**To Reproduce**

```
from gammapy.catalog import CATALOG_REGISTRY
catalog_4fgl = CATALOG_REGISTRY.get_cls("4fgl")()
fgl_src = catalog_4fgl["FGES J1553.8-5325"]
flux_points_fermi = FluxPoints(fgl_src.flux_points.table).to_sed_type("dnde", 
                                                                      model=fgl_src.sky_model().spectral_model)
```


Discussion:

Resolving patch:
diff --git a/gammapy/estimators/flux_point.py b/gammapy/estimators/flux_point.py
--- a/gammapy/estimators/flux_point.py
+++ b/gammapy/estimators/flux_point.py
@@ -435,7 +435,7 @@ def _dnde_from_flux(flux, model, e_ref, e_min, e_max, pwl_approx):
                 amplitude=dnde_model,
             )
         else:
-            flux_model = model.integral(e_min, e_max, intervals=True)
+            flux_model = model.integral(e_min, e_max)
 
         return dnde_model * (flux / flux_model)
 


Test patch:
diff --git a/gammapy/estimators/tests/test_flux_point.py b/gammapy/estimators/tests/test_flux_point.py
--- a/gammapy/estimators/tests/test_flux_point.py
+++ b/gammapy/estimators/tests/test_flux_point.py
@@ -139,6 +139,16 @@ def test_compute_flux_points_dnde_exp(method):
     assert_quantity_allclose(actual, desired, rtol=1e-8)
 
 
+@requires_data()
+def test_fermi_to_dnde():
+    from gammapy.catalog import CATALOG_REGISTRY
+    catalog_4fgl = CATALOG_REGISTRY.get_cls("4fgl")()
+    src = catalog_4fgl["FGES J1553.8-5325"]
+    fp_dnde = src.flux_points.to_sed_type("dnde", model=src.spectral_model())
+
+    assert_allclose(fp_dnde.table["dnde"].quantity[1], 4.567393e-10 * u.Unit("cm-2 s-1 MeV-1"), rtol=1e-5)
+
+
 @pytest.fixture(params=FLUX_POINTS_FILES, scope="session")
 def flux_points(request):
     path = "$GAMMAPY_DATA/tests/spectrum/flux_points/" + request.param
diff --git a/gammapy/modeling/models/tests/test_cube.py b/gammapy/modeling/models/tests/test_cube.py
--- a/gammapy/modeling/models/tests/test_cube.py
+++ b/gammapy/modeling/models/tests/test_cube.py
@@ -23,7 +23,7 @@
     SkyModel,
     create_fermi_isotropic_diffuse_model,
 )
-from gammapy.utils.testing import requires_data, mpl_plot_check
+from gammapy.utils.testing import requires_data, mpl_plot_check, requires_dependency
 from gammapy.modeling import Parameter
 from astropy.coordinates.angle_utilities import angular_separation
 
@@ -612,6 +612,7 @@ def test_energy_dependent_model(geom_true):
     assert_allclose(model.data.sum(), 1.678314e-14, rtol=1e-3)
 
 
+@requires_dependency("matplotlib")
 def test_plot_grid(geom_true):
     spatial_model = MyCustomGaussianModel(frame="galactic")
     with mpl_plot_check():
diff --git a/gammapy/modeling/models/tests/test_spatial.py b/gammapy/modeling/models/tests/test_spatial.py
--- a/gammapy/modeling/models/tests/test_spatial.py
+++ b/gammapy/modeling/models/tests/test_spatial.py
@@ -235,6 +235,7 @@ def test_sky_diffuse_constant():
     assert isinstance(model.to_region(), EllipseSkyRegion)
 
 
+@requires_dependency("matplotlib")
 @requires_data()
 def test_sky_diffuse_map():
     filename = "$GAMMAPY_DATA/catalogs/fermi/Extended_archive_v18/Templates/RXJ1713_2016_250GeV.fits"
@@ -242,18 +243,23 @@ def test_sky_diffuse_map():
     lon = [258.5, 0] * u.deg
     lat = -39.8 * u.deg
     val = model(lon, lat)
+
     assert val.unit == "sr-1"
     desired = [3269.178107, 0]
     assert_allclose(val.value, desired)
+
     res = model.evaluate_geom(model.map.geom)
     assert_allclose(np.sum(res.value), 32816514.42078349)
     radius = model.evaluation_radius
+
     assert radius.unit == "deg"
     assert_allclose(radius.value, 0.64, rtol=1.0e-2)
     assert model.frame == "fk5"
     assert isinstance(model.to_region(), PolygonSkyRegion)
+
     with pytest.raises(TypeError):
         model.plot_interative()
+
     with pytest.raises(TypeError):
         model.plot_grid()
 
@@ -266,13 +272,17 @@ def test_sky_diffuse_map_3d():
     lat = -39.8 * u.deg
     energy = 1 * u.GeV
     val = model(lon, lat, energy)
+
     with pytest.raises(ValueError):
         model(lon, lat)
     assert model.map.unit == "cm-2 s-1 MeV-1 sr-1"
+
     val = model(lon, lat, energy)
     assert val.unit == "cm-2 s-1 MeV-1 sr-1"
+
     res = model.evaluate_geom(model.map.geom)
     assert_allclose(np.sum(res.value), 0.11803847221522712)
+
     with pytest.raises(TypeError):
         model.plot()