-
Notifications
You must be signed in to change notification settings - Fork 16
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
Support __getitem__
on the bins
property for more concise value/interval extraction
#2831
Conversation
src/scipp/core/bins.py
Outdated
dim, index = key | ||
if isinstance(index, _cpp.Variable): | ||
if index.ndim == 0: | ||
return self._obj.group(concat([index], dim)).squeeze(dim) |
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.
I think this would be clearer if it used broadcast
instead of concat
.
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.
I had an issue with that as for some reason the C++ bin implementation does not work as it thinks it is operating on a non-contiguous object.
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.
ok
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.
Hmm, I thought I should be able to use flatten
, but for 0-D it falls back to broadcast
, which means it will also think it is non-contiguous (i.e., has a stride of 0). Maybe we should fix flatten
to return something with stride 1 (of length 1) instead?
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.
Done, please have another look @jl-wynen
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.
This now means that the variable can have any dimensions as long as it has only a single element. This is inconsistent with value based slicing on the data array itself and I don't think we should allow it.
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.
Which variable? The key is checked for ndim==0.
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.
Missed the if
, sorry
raise ValueError( | ||
f"Unsupported key '{key}'. Expected a dimension label and " | ||
"a 0-D variable or a dimension label and a slice object with start " | ||
"and stop given by a 0-D variable.") |
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.
You can add a type annotation for key
to make this a bit clearer.
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.
Good idea!
This addresses an aspect of the long standing issue scipp/scippneutron#237 (comment), in particular cases a) and c). After this change, I believe we are in a position to update/rewrite the filtering documentation and finally close that issue.