-
Notifications
You must be signed in to change notification settings - Fork 21
Document behavior of min/max/nanmin/nanmax for empty inputs #3287
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scipp returns DBL_MAX for empty inputs, while NumPy returns NaN. For integer inputs, Scipp returns INT_MAX, while NumPy raises.
It is not really correct to say that Numpy returns Nan when the input is empty, or at least it is a bit unclear. Both of the below examples raises ValueError: zero-size array to reduction operation minimum which has no identity independent of the data-type.
np.min(np.ones(0, dtype=np.int64))
np.min(np.ones(0, dtype=np.float64))However, Numpy.min does returns nan if the input contains nan.
As a side note that probably should be the topic of another issue: When looking into this I noticed that sc.nanmin and sc.min does the same thing
# Both return -1
sc.array(dims=['x'], values=[1, float('nan'), -1]).min()
sc.array(dims=['x'], values=[1, float('nan'), -1]).nanmin()while
# Only nanmin returns -1
np.min([1, float('nan'), -1])
np.nanmin([1, float('nan'), -1])I think the reason is that in the implementation of sc.min we use std::min(a, b) equivalent to (b < a) ? b : a; and if b=nan we get a.
|
Hmm, and from import numpy as np
import numpy.ma as ma
ma.masked_array(np.ones(1), mask=[True]).min() # masked |
src/scipp/core/reduction.py
Outdated
| inputs, Scipp returns INT_MAX, while NumPy raises. Note that in the case of | ||
| :py:class:`DataArray`, inputs can also be "empty" if all elements contributing | ||
| to an output element are masked. | ||
| Scipp returns DBL_MAX and INT_MAX for empty inputs of float or int dtype, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Scipp returns DBL_MAX and INT_MAX for empty inputs of float or int dtype, | |
| Scipp returns DBL_MAX or INT_MAX for empty inputs of float or int dtype, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in the other docstrings, too.
src/scipp/core/reduction.py
Outdated
| :py:class:`DataArray`, inputs can also be "empty" if all elements contributing | ||
| to an output element are masked. | ||
| Scipp returns DBL_MAX and INT_MAX for empty inputs of float or int dtype, | ||
| respectively, while NumPy rases. Note that in the case of :py:class:`DataArray`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| respectively, while NumPy rases. Note that in the case of :py:class:`DataArray`, | |
| respectively, while NumPy raises. Note that in the case of :py:class:`DataArray`, |
Fixes #3241.