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

Remove equidistant time-spacing assumption in computing approximate derivative #268

Closed
lochhh opened this issue Aug 9, 2024 · 6 comments · Fixed by #270
Closed

Remove equidistant time-spacing assumption in computing approximate derivative #268

lochhh opened this issue Aug 9, 2024 · 6 comments · Fixed by #270

Comments

@lochhh
Copy link
Collaborator

lochhh commented Aug 9, 2024

Currently when computing approximate derivatives, we assume equidistant time-spacing with a fixed dt:

dt = data["time"].values[1] - data["time"].values[0]
for _ in range(order):
result = xr.apply_ufunc(
np.gradient,
result,
dt,
kwargs={"axis": 0},
)
result = result.reindex_like(data)

We should instead provide the actual time axis values, i.e. data.coords["time"].values.
Since we do not need to compute derivatives along multiple axes (what np.gradient() can do and xr.differentiate() can't), an even simpler solution would be to use xr.differentiate() (as @sfmig has pointed out in #265), replacing the above lines with:

for _ in range(order): 
     result = result.differentiate("time")
@sfmig
Copy link
Contributor

sfmig commented Aug 9, 2024

I wonder if they differ in performance - maybe we could run a quick time test comparing our current implementation and differentiate 🤔

@lochhh
Copy link
Collaborator Author

lochhh commented Aug 9, 2024

I wonder if they differ in performance - maybe we could run a quick time test comparing our current implementation and differentiate 🤔

I think they use np.gradient().

@niksirbi
Copy link
Member

This sounds like an unambiguously good idea to me 👍🏼

@niksirbi
Copy link
Member

I'm also all in for using xr.differentiate().

@lochhh
Copy link
Collaborator Author

lochhh commented Aug 12, 2024

I wonder if they differ in performance - maybe we could run a quick time test comparing our current implementation and differentiate 🤔

# input:
dlc_ds.postion.shape = (59999, 2, 12, 2)

# xr.differentiate
%%timeit
dlc_ds.position.differentiate("time")
# 24.2 ms ± 1.56 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# numpy
result = xr.apply_ufunc(
    np.gradient,
    dlc_ds.position,
    dlc_ds.time.values,
    kwargs={"axis": 0},
)
# 23.4 ms ± 652 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

but because we need to reindex after applying np.gradient, this takes longer

%%timeit
result = xr.apply_ufunc(
    np.gradient,
    dlc_ds.position,
    dlc_ds.time.values,
    kwargs={"axis": 0},
)
result = result.reindex_like(dlc_ds.position)
# 32 ms ± 1.51 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

@lochhh lochhh mentioned this issue Aug 12, 2024
7 tasks
@sfmig
Copy link
Contributor

sfmig commented Aug 12, 2024

@lochhh thanks for timing it 🤩

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

Successfully merging a pull request may close this issue.

3 participants