Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pos must be nonnegative and less than window_length #78

Open
retifrav opened this issue Aug 5, 2023 · 0 comments
Open

pos must be nonnegative and less than window_length #78

retifrav opened this issue Aug 5, 2023 · 0 comments

Comments

@retifrav
Copy link

retifrav commented Aug 5, 2023

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
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 <module>
    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
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 aa6ba8d revision.

@retifrav retifrav changed the title pos must be nonnegative and less than window_length Error pos must be nonnegative and less than window_length Aug 5, 2023
@retifrav retifrav changed the title Error pos must be nonnegative and less than window_length pos must be nonnegative and less than window_length Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant