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

How to reverse the order of an array along one or several dimensions? #3447

Open
jokasimr opened this issue May 8, 2024 · 6 comments
Open
Labels
enhancement New feature or request question Ask a question on how to use scipp

Comments

@jokasimr
Copy link
Contributor

jokasimr commented May 8, 2024

What is the equivalent of np.flip(...) or arr[::-1] in scipp?

Maybe we should make this easier to find in the docs.

@jokasimr jokasimr added the docs label May 8, 2024
@YooSunYoung
Copy link
Member

Is sort enough for your use-case?

import scipp as sc

da = sc.data.data_xy()
sc.sort(da, 'x', order='descending')

@jokasimr
Copy link
Contributor Author

jokasimr commented May 8, 2024

Is sort enough...

No not really. I don't want the entries sorted, I just want them in revered order.
Like this: [1, 3, 2] -> [2, 3, 1]

@jl-wynen
Copy link
Member

jl-wynen commented May 8, 2024

What is the utility of adding a named function? As I see it, the only reason to have np.flip is to specify an axis by rank programmatically. E.g., np.flip(a, n) where n is not a constant cannot easily be done using slicing syntax. But in Scipp, we require dimension labels when slicing. So v[d, ::-1] works when d is not a constant.

So I would stick with the (almost) standard Python syntax of [::-1].

We talked about collecting usage patterns at some point. This could be added there if we actually write them down.

But can I ask you, why did this come up?

@jokasimr
Copy link
Contributor Author

jokasimr commented May 8, 2024

What is the utility of adding a named function? As I see it, the only reason to have np.flip is to specify an axis by rank programmatically. E.g., np.flip(a, n) where n is not a constant cannot easily be done using slicing syntax. But in Scipp, we require dimension labels when slicing. So v[d, ::-1] works when d is not a constant.

Sure, but:

sc.arange('x', 0, 10)['x', ::-1]
# IndexError: negative slice step support not implemented

It came up when I translated some numpy code to scipp.
I'm not exactly sure why the reverse was required.

@nvaytet
Copy link
Member

nvaytet commented May 8, 2024

But can I ask you, why did this come up?

Isn't it because we don't support negative strides?

@SimonHeybrock
Copy link
Member

Supporting negative strides should be the way to go. Note #3188, which would probably make this much easier.

@jokasimr jokasimr added enhancement New feature or request question Ask a question on how to use scipp labels May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Ask a question on how to use scipp
Projects
None yet
Development

No branches or pull requests

5 participants