ekaterinailin / AltaiPony

Find flares in Kepler and TESS light curves. Notebooks for quickstart inside.
https://altaipony.readthedocs.io
MIT License
26 stars 10 forks source link

pos must be nonnegative and less than window_length #78

Open retifrav opened 1 year ago

retifrav commented 1 year ago

This might be a duplicate of #76.

There is a problem in detrend_savgol() function on the line wl = max(wl, 5). Or at least I think it is a problem, because in my script it leads to the following error:

ValueError: pos must be nonnegative and less than window_length
Full error output, just in case ``` sh Detrending fake LC: /usr/local/lib/python3.11/site-packages/numpy/lib/nanfunctions.py:1215: RuntimeWarning: Mean of empty slice return np.nanmean(a, axis, out=out, keepdims=keepdims) Traceback (most recent call last): File "/path/to/some.py", line 23, in flcd, fakeflc = flcd.sample_flare_recovery( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/AltaiPony/altaipony/flarelc.py", line 519, in sample_flare_recovery fake_lc = fake_lc.detrend(mode, func=func, **detrend_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/AltaiPony/altaipony/flarelc.py", line 342, in detrend new_lc = detrend_savgol(new_lc, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/AltaiPony/altaipony/altai.py", line 282, in detrend_savgol flux_model_i = savgol_filter(flux, wl, 3, mode='nearest') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/scipy/signal/_savitzky_golay.py", line 341, in savgol_filter coeffs = savgol_coeffs(window_length, polyorder, deriv=deriv, delta=delta) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/scipy/signal/_savitzky_golay.py", line 112, in savgol_coeffs raise ValueError("pos must be nonnegative and less than " ValueError: pos must be nonnegative and less than window_length. ```

So wl can be NaN - value that is bigger than 5 - which is apparently what is causing the problem.

A script to reproduce the problem ``` py from altaipony.lcio import from_mast import pandas flc = from_mast( "TRAPPIST-1", mode="LC", cadence="short", mission="K2" ) for j in range(len(flc)): print(f"\n--- {j} ---\n") # detrend curve flcd = flc[j].detrend("savgol") # find flares flcd = flcd.find_flares(N1=3, N2=1, N3=3, minsep=3) flcdpanda = flcd.flares # print(flcdpanda) if not flcdpanda.empty: # here it fails with the error "pos must be nonnegative" flcd, fakeflc = flcd.sample_flare_recovery( inject_before_detrending=True, mode="savgol", iterations=20, fakefreq=2, ampl=[1e-4, 0.5], dur=[.001/6., 0.1/6.] ) else: print("DataFrame is empty, no flares") ```

To fix this I applied the following patch:

diff --git a/altaipony/altai.py b/altaipony/altai.py
index 1d73d47..78ebb12 100755
--- a/altaipony/altai.py
+++ b/altaipony/altai.py
@@ -274,6 +274,12 @@ def detrend_savgol(lc, window_length=None, pad=3, printwl=False, **kwargs):
             wl = np.floor(.1 / dt)
             if wl % 2 == 0:
                 wl = wl + 1
+
+        # don't know what is a proper fallback in this situation,
+        # but since later it takes maximum of this value and 5,
+        # then 0 seems to be okay
+        if np.isnan(wl):
+            wl = 0

         # args are flux, window_length, polyorder, mode is 
         wl = max(wl, 5) #wl must be larger than polyorder

But I am not sure whether this is a correct was to fix it or not.

Tested with aa6ba8d202566d1b69d8b7744eba39617056bbb7 revision.