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

Support __getitem__ on the bins property for more concise value/interval extraction #2831

Merged
merged 6 commits into from
Oct 11, 2022

Conversation

SimonHeybrock
Copy link
Member

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.

dim, index = key
if isinstance(index, _cpp.Variable):
if index.ndim == 0:
return self._obj.group(concat([index], dim)).squeeze(dim)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member Author

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?

Copy link
Member Author

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

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.")
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

@SimonHeybrock SimonHeybrock merged commit 7f8dad2 into main Oct 11, 2022
@SimonHeybrock SimonHeybrock deleted the bins-getitem branch October 11, 2022 07:20
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 this pull request may close these issues.

2 participants