Repository: gammapy/gammapy

Issue:
Livetime information messed up in `Datasets.info_table()`
Livetime information is messed up in `Datasets.info_table()` in the latest dev, see: https://docs.gammapy.org/dev/tutorials/spectrum_analysis.html#Source-statistic
This must be fixed before 0.18 release
Livetime information messed up in `Datasets.info_table()`
Livetime information is messed up in `Datasets.info_table()` in the latest dev, see: https://docs.gammapy.org/dev/tutorials/spectrum_analysis.html#Source-statistic
This must be fixed before 0.18 release


Discussion:

Resolving patch:
diff --git a/gammapy/datasets/map.py b/gammapy/datasets/map.py
--- a/gammapy/datasets/map.py
+++ b/gammapy/datasets/map.py
@@ -1105,7 +1105,7 @@ def info_dict(self, in_safe_data_range=True):
 
             exposure_min = np.min(self.exposure.quantity[mask_exposure])
             exposure_max = np.max(self.exposure.quantity[mask_exposure])
-            livetime = self.exposure.meta.get("livetime", np.nan)
+            livetime = self.exposure.meta.get("livetime", np.nan * u.s).copy()
 
         info["exposure_min"] = exposure_min
         info["exposure_max"] = exposure_max
@@ -1719,7 +1719,9 @@ def from_geoms(
         for key in ["counts", "counts_off", "acceptance", "acceptance_off"]:
             kwargs[key] = Map.from_geom(geom, unit="")
 
-        kwargs["exposure"] = Map.from_geom(geom_exposure, unit="m2 s")
+        kwargs["exposure"] = Map.from_geom(
+            geom_exposure, unit="m2 s", meta={"livetime": 0 * u.s}
+        )
 
         if geom_edisp.axes[0].name.lower() == "energy":
             kwargs["edisp"] = EDispKernelMap.from_geom(geom_edisp)
diff --git a/gammapy/datasets/spectrum.py b/gammapy/datasets/spectrum.py
--- a/gammapy/datasets/spectrum.py
+++ b/gammapy/datasets/spectrum.py
@@ -306,7 +306,9 @@ def create(
         name = make_name(name)
         counts = RegionNDMap.create(region=region, axes=[e_reco])
         background = RegionNDMap.create(region=region, axes=[e_reco])
-        exposure = RegionNDMap.create(region=region, axes=[e_true], unit="cm2 s")
+        exposure = RegionNDMap.create(
+            region=region, axes=[e_true], unit="cm2 s", meta={"livetime": 0 * u.s}
+        )
         edisp = EDispKernelMap.from_diagonal_response(e_reco, e_true, geom=counts.geom)
         mask_safe = RegionNDMap.from_geom(counts.geom, dtype="bool")
         gti = GTI.create(u.Quantity([], "s"), u.Quantity([], "s"), reference_time)


Test patch:
diff --git a/gammapy/datasets/tests/test_spectrum.py b/gammapy/datasets/tests/test_spectrum.py
--- a/gammapy/datasets/tests/test_spectrum.py
+++ b/gammapy/datasets/tests/test_spectrum.py
@@ -905,6 +905,26 @@ def test_datasets_stack_reduce():
     assert stacked.name == "stacked"
 
 
+@requires_data("gammapy-data")
+def test_stack_livetime():
+    dataset_ref = SpectrumDatasetOnOff.from_ogip_files(
+        "$GAMMAPY_DATA/joint-crab/spectra/hess/pha_obs23523.fits"
+    )
+
+    energy_axis = dataset_ref.counts.geom.axes["energy"]
+    energy_axis_true = dataset_ref.exposure.geom.axes["energy_true"]
+
+    dataset = SpectrumDatasetOnOff.create(
+        e_reco=energy_axis, e_true=energy_axis_true
+    )
+
+    dataset.stack(dataset_ref)
+    assert_allclose(dataset.exposure.meta["livetime"], 1581.736758 * u.s)
+
+    dataset.stack(dataset_ref)
+    assert_allclose(dataset.exposure.meta["livetime"], 2 * 1581.736758 * u.s)
+
+
 def test_spectrum_dataset_on_off_to_yaml(tmpdir):
     spectrum_datasets_on_off = make_observation_list()
     datasets = Datasets(spectrum_datasets_on_off)