added caplog for test_fov
# Licensed under a 3-clause BSD style license - see LICENSE.rst
+from typing import List, Dict
+
import pytest
+import logging
+
from numpy.testing import assert_allclose
from astropy.coordinates import Angle, SkyCoord
from regions import CircleSkyRegion
@@ -69,10 +73,10 @@ def test_fov_bkg_maker_incorrect_method():
with pytest.raises(ValueError):
FoVBackgroundMaker(method="bad")
-
@requires_data()
-def test_fov_bkg_maker_scale(obs_dataset, exclusion_mask):
- fov_bkg_maker = FoVBackgroundMaker(method="scale", exclusion_mask=exclusion_mask)
+@requires_dependency("iminuit")
+def test_fov_bkg_maker_fit(obs_dataset, exclusion_mask):
+ fov_bkg_maker = FoVBackgroundMaker(method="fit", exclusion_mask=exclusion_mask)
test_dataset = obs_dataset.copy(name="test-fov")
dataset = fov_bkg_maker.run(test_dataset)
@@ -82,6 +86,23 @@ def test_fov_bkg_maker_scale(obs_dataset, exclusion_mask):
assert_allclose(model.tilt.value, 0.0, rtol=1e-4)
+
+@requires_data()
+def test_fov_bkg_maker_scale_nocountsnobackground(obs_dataset, exclusion_mask, caplog):
+ fov_bkg_maker = FoVBackgroundMaker(method="scale", exclusion_mask=exclusion_mask)
+
+ test_dataset = obs_dataset.copy(name="test-fov")
+ test_dataset.counts *= 0
+ test_dataset.background *= 0
+ dataset = fov_bkg_maker.run(test_dataset)
+
+ model = dataset.models[f"{dataset.name}-bkg"].spectral_model
+ assert_allclose(model.norm.value, 1, rtol=1e-4)
+ assert_allclose(model.tilt.value, 0.0, rtol=1e-4)
+ assert caplog.records[-1].levelname == "WARNING"
+ assert "No counts found outside exclusion mask for test-fov" in caplog.records[-1].message
+ assert "FoVBackgroundMaker failed" in caplog.records[-1].message
+
@requires_data()
@requires_dependency("iminuit")
def test_fov_bkg_maker_fit(obs_dataset, exclusion_mask):
@@ -94,6 +115,24 @@ def test_fov_bkg_maker_fit(obs_dataset, exclusion_mask):
assert_allclose(model.norm.value, 0.830789, rtol=1e-4)
assert_allclose(model.tilt.value, 0.0, rtol=1e-4)
+@requires_data()
+@requires_dependency("iminuit")
+def test_fov_bkg_maker_fit_nocountsnobackground(obs_dataset, exclusion_mask, caplog):
+ fov_bkg_maker = FoVBackgroundMaker(method="fit", exclusion_mask=exclusion_mask)
+
+ test_dataset = obs_dataset.copy(name="test-fov")
+ test_dataset.counts *=0
+ test_dataset.background *=0
+ dataset = fov_bkg_maker.run(test_dataset)
+
+ model = dataset.models[f"{dataset.name}-bkg"].spectral_model
+ assert_allclose(model.norm.value, 1, rtol=1e-4)
+ assert_allclose(model.tilt.value, 0.0, rtol=1e-4)
+
+ assert caplog.records[-1].levelname == "WARNING"
+ assert "Fit did not converge for test-fov" in caplog.records[-1].message
+ assert "Background model parameters might be unphysical for test-fov" in caplog.records[-1].message
+
@requires_data()
@requires_dependency("iminuit")
