Repository: gammapy/gammapy

Issue:
stat_null is not available on FluxPoints objects created with gammapy
**Gammapy version**
present dev, 1.1


**Bug description**
For `FluxPoints` created with gammapy estimators, the quantity `stat_null` is not available.


**To Reproduce**
1. Run the `spectral analysis notebook`. Then, accessing `flux_points.stat_null` gives an error
```
File ~/anaconda3/envs/gammapy-1.1/lib/python3.9/site-packages/gammapy/estimators/map/core.py:493, in FluxMaps.stat_null(self)
    490 @property
    491 def stat_null(self):
    492     """Fit statistic value for the null hypothesis"""
--> 493     self._check_quantity("stat_null")
    494     return self._data["stat_null"]

File ~/anaconda3/envs/gammapy-1.1/lib/python3.9/site-packages/gammapy/estimators/map/core.py:222, in FluxMaps._check_quantity(self, quantity)
    220 def _check_quantity(self, quantity):
    221     if quantity not in self.available_quantities:
--> 222         raise AttributeError(
    223             f"Quantity '{quantity}' is not defined on current flux estimate."
    224         )

AttributeError: Quantity 'stat_null' is not defined on current flux estimate.
```

Similarly for the PKS2155 lightcurve in gammapy-ata


**Other information**
This blocks the TS computation for #5007



Discussion:

Resolving patch:
diff --git a/gammapy/estimators/parameter.py b/gammapy/estimators/parameter.py
--- a/gammapy/estimators/parameter.py
+++ b/gammapy/estimators/parameter.py
@@ -114,6 +114,7 @@ def estimate_ts(self, datasets, parameter):
 
                 * "ts" : fit statistic difference with null hypothesis.
                 * "npred" : predicted number of counts per dataset.
+                * "stat_null" : total stat corresponding to the null hypothesis
         """
         npred = self.estimate_npred(datasets=datasets)
 
@@ -132,11 +133,9 @@ def estimate_ts(self, datasets, parameter):
                 _ = self.fit.optimize(datasets=datasets)
 
             ts = datasets.stat_sum() - stat
+            stat_null = datasets.stat_sum()
 
-        return {
-            "ts": ts,
-            "npred": npred["npred"],
-        }
+        return {"ts": ts, "npred": npred["npred"], "stat_null": stat_null}
 
     def estimate_errn_errp(self, datasets, parameter):
         """Estimate parameter asymmetric errors.


Test patch:
diff --git a/gammapy/estimators/points/tests/test_sed.py b/gammapy/estimators/points/tests/test_sed.py
--- a/gammapy/estimators/points/tests/test_sed.py
+++ b/gammapy/estimators/points/tests/test_sed.py
@@ -247,6 +247,15 @@ def test_run_pwl(fpe_pwl, tmpdir):
     actual = table["sqrt_ts"].data
     assert_allclose(actual, [18.568429, 18.054651, 7.057121], rtol=1e-2)
 
+    actual = table["ts"].data
+    assert_allclose(actual, [344.7866, 325.9704, 49.8029], rtol=1e-2)
+
+    actual = table["stat"].data
+    assert_allclose(actual, [2.76495, 13.11912, 3.70128], rtol=1e-2)
+
+    actual = table["stat_null"].data
+    assert_allclose(actual, [347.55159, 339.08952, 53.50424], rtol=1e-2)
+
     actual = table["norm_scan"][0][[0, 5, -1]]
     assert_allclose(actual, [0.2, 1.0, 5.0])
 
diff --git a/gammapy/estimators/tests/test_parameter_estimator.py b/gammapy/estimators/tests/test_parameter_estimator.py
--- a/gammapy/estimators/tests/test_parameter_estimator.py
+++ b/gammapy/estimators/tests/test_parameter_estimator.py
@@ -46,6 +46,8 @@ def test_parameter_estimator_1d(crab_datasets_1d, pwl_model):
     assert_allclose(result["amplitude"], 5.1428e-11, rtol=1e-3)
     assert_allclose(result["amplitude_err"], 6.42467e-12, rtol=1e-3)
     assert_allclose(result["ts"], 353.2092, rtol=1e-3)
+    assert_allclose(result["stat"], 38.3435, rtol=1e-3)
+    assert_allclose(result["stat_null"], 391.5527, rtol=1e-3)
     assert_allclose(result["amplitude_errp"], 6.703e-12, rtol=5e-3)
     assert_allclose(result["amplitude_errn"], 6.152e-12, rtol=5e-3)