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

Preserve coord in reductions that select an element #3377

Open
jl-wynen opened this issue Jan 30, 2024 · 5 comments
Open

Preserve coord in reductions that select an element #3377

jl-wynen opened this issue Jan 30, 2024 · 5 comments

Comments

@jl-wynen
Copy link
Member

jl-wynen commented Jan 30, 2024

Some reductions (max, min, median, etc.) select one element along a dimension. It is possible to retain coordinates along that dimension by also selecting the corresponding element. Currently, we simply remove all coords along a reduced dim. But if we changed this, we would get sort of combined max and argmax, etc. operations. I don't know of a cocnrete use case, it's just something that came to my mind.

E.g.

da = sc.DataArray(
    sc.array(dims=['x'], values=[3, 5, 1, 2]),
    coords={'x': sc.array(dims=['x'], values=[1, 2, 3, 4])}
)
da.max()

returns

da = sc.DataArray(
    sc.scalar(5),
    coords={'x': sc.scalar(2)}
)
@SimonHeybrock
Copy link
Member

Would you convert to an unaligned coord, such that you can still to da - da.max()?

@SimonHeybrock
Copy link
Member

Have you thought about the cost? This would probably be significantly more expensive to implement?

@jl-wynen
Copy link
Member Author

Would you convert to an unaligned coord, such that you can still to da - da.max()?

Yes, I'd think so. This is akin to indexing into an element where we also preserve the coord.

Have you thought about the cost? This would probably be significantly more expensive to implement?

Not in detail. But if we implement it as argmax, etc., it shouldn't be much more expensive. It would be something like

i = argmax(da.data, dim)
res = da[dim, i]

@SimonHeybrock
Copy link
Member

One could make it on opt-in keyword arg?

@jl-wynen
Copy link
Member Author

Possibly.

I need to correct my initial list of functions where this can be used: median may return the average of two elements if there is an even number. So it actually cannot return a coord.

@SimonHeybrock SimonHeybrock changed the title Preservecoord in reductions that select an element Preserve coord in reductions that select an element Jan 31, 2024
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

No branches or pull requests

2 participants