Repository: StingraySoftware/stingray (python)

Issue:
LightCurve.baseline(offset_correction=True) throws and exception with to much GTIs
When calling LightCurve.baseline(offset_correction=True) an exception is thrown:

Message: Improper input: N=1 must not exceed M=0

Dave get_lightcurve parameters:
-"filename":"/Users/ricardo/TimeLab/PN_evt.fits" ->
 Download it here: (https://drive.google.com/file/d/1BWx598pXEBvcG_RYY7FnJRcEkI7EI9Aq/view?usp=sharing)

-"meanflux_opts":{"niter":10,"lam":1000,"p":0.01},
-"filters": {"table":"EVENTS","column":"TIME","from":9053,"to":9468}, this filters is not necessary, the exception is thrown every time that we use multiple GTIs 
-"dt":1 second

StackTrace
File "/Users/ricardo/TimeLab/dave/src/main/python/stingray/lightcurve.py", line 477, in baseline
Python:  File "/Users/ricardo/TimeLab/dave/src/main/python/stingray/utils.py", line 510, in baseline_als
Python:  File "/Users/ricardo/TimeLab/dave/src/main/python/stingray/utils.py", line 402, in offset_fit
Python:  File "/Users/ricardo/TimeLab/dave/work/miniconda/envs/dave/lib/python3.5/site-packages/scipy/optimize/minpack.py", line 742, in curve_fit
Python:  File "/Users/ricardo/TimeLab/dave/work/miniconda/envs/dave/lib/python3.5/site-packages/scipy/optimize/minpack.py", line 380, in leastsq
Python:  raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))


Pull request description:
Fix offset correction error
Resolves #292, throws a warning when data points are too few to perform offset correction with some precision.

Patch:
diff --git a/stingray/utils.py b/stingray/utils.py
index 996da68f..8ec495a9 100644
--- a/stingray/utils.py
+++ b/stingray/utils.py
@@ -532,9 +532,11 @@ def baseline_als(x, y, lam=None, p=None, niter=10, return_baseline=False,
     offset = 0
     if offset_correction:
         std = mad(ysub)
-
         good = np.abs(ysub) < 10 * std
-
+        if len(x[good]) < 10:
+            good = np.ones(len(x), dtype=bool)
+            warnings.warn('Too few bins to perform baseline offset correction'
+                          ' precisely. Beware of results')
         offset = offset_fit(x[good], ysub[good], 0)
 
     if return_baseline:


Test patch:
diff --git a/stingray/tests/test_lightcurve.py b/stingray/tests/test_lightcurve.py
index 09c84e02..fb83defd 100644
--- a/stingray/tests/test_lightcurve.py
+++ b/stingray/tests/test_lightcurve.py
@@ -822,6 +822,19 @@ class TestLightcurveRebin(object):
         baseline = lc.baseline(10000, 0.01, offset_correction=True)
         assert np.isclose(np.std(lc.counts - baseline), input_stdev, rtol=0.1)
 
+    def test_lc_baseline_offset_fewbins(self):
+        times = np.arange(0, 4, 1)
+        input_stdev = 0.1
+        counts = np.random.normal(100, input_stdev, len(times)) + \
+            0.001 * times
+        gti = [[-0.005, 4.005]]
+        lc = Lightcurve(times, counts, gti=gti)
+        with pytest.warns(UserWarning) as record:
+            lc.baseline(10000, 0.01, offset_correction=True)
+
+        assert np.any(["Too few bins to perform baseline offset correction"
+                       in r.message.args[0] for r in record])
+
     def test_change_mjdref(self):
         lc_new = self.lc.change_mjdref(57000)
         assert lc_new.mjdref == 57000