Repository: gammapy/gammapy
Pull request #5656 (2025-01-02 10:32:12)

Implicit support of map dataset instead of observation in DatasetMaker
Implicit support of map dataset instead of observation in DatasetMaker.
Related to #5649 : If ones don't want to create an Observation object for Fermi-LAT data (and  in general data with DL4 irfs), we could instead support using a map dataset as input irfs container in makers.  This would require very little change to work as shown in this draft PR.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/gammapy/gammapy/pull/5656?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 94.60%. Comparing base [(`29b2fd5`)](https://app.codecov.io/gh/gammapy/gammapy/commit/29b2fd555eeeba0c56327ce084761a8bd2449753?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) to head [(`f2b143b`)](https://app.codecov.io/gh/gammapy/gammapy/commit/f2b143b1d091345c0363c66c9fecc39ef2a6f6bf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).
> Report is 36 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #5656      +/-   ##
==========================================
- Coverage   94.65%   94.60%   -0.06%     
==========================================
  Files         238      238              
  Lines       37052    37086      +34     
==========================================
+ Hits        35071    35084      +13     
- Misses       1981     2002      +21     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/gammapy/gammapy/pull/5656?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).


Patch:
diff --git a/gammapy/makers/map.py b/gammapy/makers/map.py
index aea3e79aa..e09e12694 100644
--- a/gammapy/makers/map.py
+++ b/gammapy/makers/map.py
@@ -5,6 +5,7 @@ from astropy.table import Table
 from regions import PointSkyRegion
 from gammapy.datasets import MapDatasetMetaData
 from gammapy.irf import EDispKernelMap, PSFMap
+from gammapy.data import Observation
 from gammapy.maps import Map
 from .core import Maker
 from .utils import (
@@ -165,6 +166,10 @@ class MapDatasetMaker(Maker):
         exposure : `~gammapy.maps.Map`
             Exposure map.
         """
+        if getattr(observation, "exposure", None):
+            return observation.exposure.interp_to_geom(
+                geom=geom,
+            )
         if isinstance(observation.aeff, Map):
             return observation.aeff.interp_to_geom(
                 geom=geom,
@@ -393,8 +398,11 @@ class MapDatasetMaker(Maker):
             Map dataset.
         """
         kwargs = {"gti": observation.gti}
-        kwargs["meta_table"] = self.make_meta_table(observation)
-        kwargs["meta"] = self._make_metadata(kwargs["meta_table"])
+        if isinstance(observation, Observation):
+            kwargs["meta_table"] = self.make_meta_table(observation)
+            kwargs["meta"] = self._make_metadata(kwargs["meta_table"])
+        elif getattr(observation, "meta"):
+            kwargs["meta"] = observation.meta
 
         mask_safe = Map.from_geom(dataset.counts.geom, dtype=bool)
         mask_safe.data[...] = True


Test patch:
diff --git a/gammapy/makers/tests/test_map.py b/gammapy/makers/tests/test_map.py
index 07b5088cf..30269dfe2 100644
--- a/gammapy/makers/tests/test_map.py
+++ b/gammapy/makers/tests/test_map.py
@@ -410,6 +410,10 @@ def test_interpolate_map_dataset():
     ).data
     assert_allclose(psfkernel_preinterp, psfkernel_postinterp, atol=1e-4)
 
+    # test running maker with dataset
+    maker = MapDatasetMaker(selection=["exposure", "edisp", "psf"])
+    dataset = maker.run(dataset, dataset)
+
 
 @requires_data()
 @pytest.mark.xfail