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

Extrapolate points in interpolate_over_time? #379

Closed
lochhh opened this issue Jan 20, 2025 · 3 comments · Fixed by #383
Closed

Extrapolate points in interpolate_over_time? #379

lochhh opened this issue Jan 20, 2025 · 3 comments · Fixed by #383
Labels
question Further information is requested

Comments

@lochhh
Copy link
Collaborator

lochhh commented Jan 20, 2025

MWE:

import numpy as np
import xarray as xr
from movement.filtering import interpolate_over_time

vals = [np.nan, 2, 3, np.nan, np.nan, 6, np.nan, np.nan, 9, np.nan]
da = xr.DataArray(
    vals, dims="time", coords={"time": range(len(vals))}
)
interpolate_over_time(da, max_gap=0, print_report=False).values

Using interpolate_over_time, the expectation for max_gap=0 is that all NaNs are retained.
However, the above outputs the following, with the first and last NaNs are filled:

array([ 1.,  2.,  3., nan, nan,  6., nan, nan,  9., 10.])

The following excerpt from PR #116 explains why:

  • I added fill_value="extrapolate" to the interpolate_na method, to also take care of nan values at the start and end of the array (if they are smaller than the max_gap).

Originally posted by @niksirbi in #116 (review)

@lochhh lochhh added the question Further information is requested label Jan 20, 2025
@niksirbi
Copy link
Member

Thanks for raising this issue @lochhh, indeed extrapolation leads to some weird behaviours. One of them is just what you pointed out. Another I've witnessed occasionally is some really wild values being introduced at the edges (including negative pixel values).

I think we should move away of hard-coding fill_value="extrapolate" and instead leave that option to the user. I was thinking of exposing fill_value as an argument, either explicitly or via **kwargs passed verbatim to the underlying interpolation functions.

What do you think we should do?

@lochhh
Copy link
Collaborator Author

lochhh commented Jan 21, 2025

Thanks for raising this issue @lochhh, indeed extrapolation leads to some weird behaviours. One of them is just what you pointed out. Another I've witnessed occasionally is some really wild values being introduced at the edges (including negative pixel values).

I think we should move away of hard-coding fill_value="extrapolate" and instead leave that option to the user. I was thinking of exposing fill_value as an argument, either explicitly or via **kwargs passed verbatim to the underlying interpolation functions.

What do you think we should do?

I would opt for **kwargs to match the underlying xarray.DataArray.interpolate_na and redirect users to xarray docs.

@niksirbi
Copy link
Member

Alright, I'll have a go at implementing a fix and tag you as reviewer.

@niksirbi niksirbi moved this from 🤔 Triage to 🚧 In Progress in movement progress tracker Jan 21, 2025
@niksirbi niksirbi linked a pull request Jan 21, 2025 that will close this issue
7 tasks
@github-project-automation github-project-automation bot moved this from 🚧 In Progress to ✅ Done in movement progress tracker Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Development

Successfully merging a pull request may close this issue.

2 participants