-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
rolling argmin() or argmax() gives the wrong result (are they inverted?) #8541
Comments
This seems to be something to do with our implementation of bottleneck: [nav] In [9]: with xr.set_options(use_bottleneck=False):
...: print(data.rolling(time=3).argmax())
<xarray.DataArray (time: 10)>
array([nan, nan, 2., 2., 2., 2., 2., 2., 2., 2.])
Coordinates:
* time (time) int64 0 1 2 3 4 5 6 7 8 9
[nav] In [10]: with xr.set_options(use_bottleneck=True):
...: print(data.rolling(time=3).argmax())
<xarray.DataArray (time: 10)>
array([nan, nan, 0., 0., 0., 0., 0., 0., 0., 0.])
Coordinates:
* time (time) int64 0 1 2 3 4 5 6 7 8 9 ...but not bottleneck itself —
FWIW numbagg doesn't have these functions. @JulianGiles do you know whether this previously worked differently? |
@max-sixty thanks for looking into it. I do not know if this used to worked correctly, is the first time I try to combine rolling with argmax/argmin |
OK I think this is actually a bottleneck issue: import bottleneck as bn
data = xr.DataArray(np.arange(10), dims='time', coords={'time': np.arange(10)})
bn.move_argmax(data.data, window=3, axis=0)
I'm a bit surprised this didn't get spotted earlier. It's quite possible I'm missing something / misunderstanding what these should be returning. (But the cases above are different whether bottleneck is enabled, so I'm really confident something is wrong somewhere!) Do you want to put an issue in upstream @JulianGiles ? That would be helpful. We could also remove them from xarray if others confirm this is wrong and a fix in bottleneck isn't forthcoming. (Unfortunately writing these in numbagg isn't easy, since numba doesn't support the data structures that are required for implementing these; or I'd get onto that...) |
@max-sixty @JulianGiles It looks like that this is bottleneck's standard behaviour. Index 0 is at the rightmost edge of the window See https://kwgoodman.github.io/bottleneck-doc/reference.html#bottleneck.move_argmax Or did I get this totally wrong? |
Ah! OK. Thanks for finding that. I do find that very surprising! We should really strive to have consistent numerical output regardless of the xarray settings & dependencies. So if we can align the cases with & without bottleneck, that would be better. We could do |
I guess just doing |
Yup. Contributions welcome... |
I looked into the code but I am completely lost on how xarray works internally 😢 . I see no direct call to bn.move_argmax or something similar. |
@JulianGiles I think I've found the right location. You might check #8552. This should fix the issue for bottleneck. |
Thanks a lot @kmuehlbauer ! Looks good to me (with my limited knowledge) 😄 |
What happened?
I was trying to compute a rolling argmax and argmin on a DataArray, something like:
data.rolling(time=3).argmax()
What did you expect to happen?
The results of the operations look flipped, the result of rolling argmax looks like what a rolling argmin should give, and viceversa.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.12.0 | packaged by conda-forge | (main, Oct 3 2023, 08:43:22) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 5.14.21-150400.24.81-default
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: ('de_DE', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.2
xarray: 2023.11.0
pandas: 2.1.3
numpy: 1.26.2
scipy: 1.11.4
netCDF4: 1.6.5
pydap: None
h5netcdf: 1.3.0
h5py: 3.10.0
Nio: None
zarr: None
cftime: 1.6.3
nc_time_axis: None
iris: None
bottleneck: 1.3.7
dask: 2023.12.0
distributed: 2023.12.0
matplotlib: 3.8.2
cartopy: 0.22.0
seaborn: None
numbagg: None
fsspec: 2023.12.1
cupy: None
pint: 0.22
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.2.2
pip: 23.3.1
conda: None
pytest: None
mypy: None
IPython: 8.18.1
sphinx: 7.2.6
The text was updated successfully, but these errors were encountered: