Repository: StingraySoftware/stingray

Issue:
Leahy normalization for err_dist == "gauss" and 0 error bars should just break

Discussion:

Patch:
diff --git a/stingray/fourier.py b/stingray/fourier.py
index 27b09bf7..5b1dda05 100644
--- a/stingray/fourier.py
+++ b/stingray/fourier.py
@@ -355,7 +355,16 @@ def normalize_leahy_from_variance(unnorm_power, variance, n_bin):
     True
     >>> np.isclose(pdsnorm[1:n_bin//2].mean(), poisson_level(norm="leahy"), rtol=0.01)
     True
+
+    If the variance is zero, it will fail:
+    >>> pdsnorm = normalize_leahy_from_variance(pds, 0., lc.size)
+    Traceback (most recent call last):
+    ...
+    ValueError: The variance used to normalize the ...
     """
+    if variance == 0.:
+        raise ValueError(
+            "The variance used to normalize the periodogram is 0.")
     return unnorm_power * 2. / (variance * n_bin)
 
 
diff --git a/tox.ini b/tox.ini
index c37f4235..a860e2f3 100644
--- a/tox.ini
+++ b/tox.ini
@@ -51,6 +51,8 @@ deps =
     numpy117: numpy==1.17.*
     numpy118: numpy==1.18.*
 
+    py36: pytest==6.2.5
+
     cov: coverage
 
     astropy3: astropy>=3.0.0,<4.0.0


Test patch:
diff --git a/stingray/tests/test_fourier.py b/stingray/tests/test_fourier.py
index c65fb04d..bb3c2292 100644
--- a/stingray/tests/test_fourier.py
+++ b/stingray/tests/test_fourier.py
@@ -442,6 +442,12 @@ class TestNorms(object):
         )
         assert np.isclose(pdsnorm.mean(), 2, rtol=0.01)
 
+    def test_normalize_with_variance_fails_if_variance_zero(self):
+        # If the variance is zero, it will fail:
+        with pytest.raises(ValueError) as excinfo:
+            pdsnorm = normalize_leahy_from_variance(self.pds, 0., self.N)
+        assert "The variance used to normalize the" in str(excinfo.value)
+
     def test_normalize_none(self):
         pdsnorm = normalize_periodograms(self.pds, self.dt, self.N,
                                          self.mean, n_ph=self.nph, norm="none")