Repository: gammapy/gammapy

Issue:
datasets.info_table() returns inconsistent values.
**Gammapy version**
Current master / 1.1
Probably its in all version.

**Bug description**
The code for `datasets.info_table()` returns a table with "wrong" values.
For the first dataset, `to_masked` is called before retrieving the info_dict, for all others it is not.

This produces inconsistent output if any masks are attached to the dataset.
Also seems to matter for an attached FoVBackgroundModel, but I don't see why right now.

https://github.com/gammapy/gammapy/blob/0b099a7c68b702ff6e0b5451abd4e5b155530f6f/gammapy/datasets/core.py#L502-L530

**Expected behavior**
Either use `to_masked` everywhere or never.




Discussion:

Resolving patch:
diff --git a/gammapy/datasets/core.py b/gammapy/datasets/core.py
--- a/gammapy/datasets/core.py
+++ b/gammapy/datasets/core.py
@@ -505,7 +505,8 @@ def info_table(self, cumulative=False):
         Parameters
         ----------
         cumulative : bool
-            Cumulate info across all observations. Default is False.
+            Cumulate information across all datasets. If True, all model-dependent
+            information will be lost. Default is False.
 
         Returns
         -------
@@ -515,19 +516,18 @@ def info_table(self, cumulative=False):
         if not self.is_all_same_type:
             raise ValueError("Info table not supported for mixed dataset type.")
 
-        name = "stacked" if cumulative else self[0].name
-        stacked = self[0].to_masked(name=name)
+        rows = []
 
-        rows = [stacked.info_dict()]
-
-        for dataset in self[1:]:
-            if cumulative:
+        if cumulative:
+            name = "stacked"
+            stacked = self[0].to_masked(name=name)
+            rows.append(stacked.info_dict())
+            for dataset in self[1:]:
                 stacked.stack(dataset)
-                row = stacked.info_dict()
-            else:
-                row = dataset.info_dict()
-
-            rows.append(row)
+                rows.append(stacked.info_dict())
+        else:
+            for dataset in self:
+                rows.append(dataset.info_dict())
 
         return Table(rows)
 


Test patch:
diff --git a/gammapy/datasets/tests/test_datasets.py b/gammapy/datasets/tests/test_datasets.py
--- a/gammapy/datasets/tests/test_datasets.py
+++ b/gammapy/datasets/tests/test_datasets.py
@@ -1,7 +1,8 @@
 # Licensed under a 3-clause BSD style license - see LICENSE.rst
 import os
 import pytest
-from numpy.testing import assert_allclose
+import numpy as np
+from numpy.testing import assert_allclose, assert_equal
 from astropy.coordinates import SkyCoord
 from gammapy.datasets import Datasets, SpectrumDatasetOnOff
 from gammapy.datasets.tests.test_map import get_map_dataset
@@ -88,10 +89,33 @@ def test_datasets_info_table():
     table = datasets_hess.info_table()
     assert table["name"][0] == "23523"
     assert table["name"][1] == "23526"
-
-    table = datasets_hess.info_table(cumulative=True)
-    assert table["name"][0] == "stacked"
-    assert table["name"][1] == "stacked"
+    assert np.isnan(table["npred_signal"][0])
+    assert_equal(table["counts"], [124, 126])
+    assert_allclose(table["background"], [7.666, 8.583], rtol=1e-3)
+
+    table_cumul = datasets_hess.info_table(cumulative=True)
+    assert table_cumul["name"][0] == "stacked"
+    assert table_cumul["name"][1] == "stacked"
+    assert np.isnan(table_cumul["npred_signal"][0])
+    assert table_cumul["alpha"][1] == table_cumul["alpha"][0]
+    assert_equal(table_cumul["counts"], [124, 250])
+    assert_allclose(table_cumul["background"], [7.666, 16.25], rtol=1e-3)
+
+    assert table["excess"].sum() == table_cumul["excess"][1]
+    assert table["counts"].sum() == table_cumul["counts"][1]
+    assert table["background"].sum() == table_cumul["background"][1]
+
+    datasets_hess[0].mask_safe.data = ~datasets_hess[0].mask_safe.data
+    assert datasets_hess.info_table()["counts"][0] == 65
+
+    datasets_hess[0].mask_safe.data = np.ones_like(
+        datasets_hess[0].mask_safe.data, dtype=bool
+    )
+    datasets_hess[1].mask_safe.data = np.ones_like(
+        datasets_hess[1].mask_safe.data, dtype=bool
+    )
+    assert_equal(datasets_hess.info_table()["counts"], [189, 199])
+    assert_equal(datasets_hess.info_table(cumulative=True)["counts"], [189, 388])
 
 
 @requires_data()