Skip to content

Index aliasing for xarray dataarrays #108

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

Closed
deltamarnix opened this issue Apr 4, 2025 · 5 comments · Fixed by #114
Closed

Index aliasing for xarray dataarrays #108

deltamarnix opened this issue Apr 4, 2025 · 5 comments · Fixed by #114
Assignees

Comments

@deltamarnix
Copy link
Contributor

Instead of getting data with ncol=5, nrow=10, we want to use j=5, i=10.
We should be able to set aliases on the xarray labels.

@wpbonelli
Copy link
Member

wpbonelli commented Apr 14, 2025

Eventually I think we can use custom xarray indices not just for i/j/k indexing but more generally. There is some work in progress to bundle e.g. RangeIndex and RasterIndex with xarray. We can keep an eye on it. The current advice for range indices with integer step is to use an xarray.PandasIndex wrapping a pandas.RangeIndex. That we can begin to prototype immediately, I think.

@wpbonelli
Copy link
Member

from the xarray person doing the majority of index-relating development

I think that using a combination of indexes (custom data structures) and accessors (custom API possibly consuming index data) is a great way to extend Xarray.

UXARRAY/uxarray#185 (reply in thread)

@wpbonelli
Copy link
Member

wpbonelli commented Apr 15, 2025

After a bit more reading, it seems like the recommended pattern here is to store grid/time in a custom xarray index. (The index is the place for extra state that should be preserved with dataset operations). The index then adds some extra "discretization-awareness" on top of what the data tree already knows about its shape. Specific functionality (e.g. plotting) can be built on the index and exposed with accessors if needed.

@wpbonelli
Copy link
Member

I sketched out how we might attach custom indices to xattree components in modflowpy/xattree@fdfa082. Rough and dead simple but I think it might work? Will soon try it here..

@wpbonelli
Copy link
Member

think it's working with modflowpy/xattree@14e2531.

grid.data
<xarray.DataTree 'grid'>
Group: /
    Dimensions:  (rows: 3, cols: 3, nodes: 9)
    Coordinates:
      * nodes    (nodes) int64 72B 0 1 2 3 4 5 6 7 8
      * i        (rows) int64 24B 0 1 2
      * j        (cols) int64 24B 0 1 2
    Dimensions without coordinates: rows, cols
    Data variables:
        a        (rows, cols) float64 72B 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
        aa       (nodes) float64 72B 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    Indexes:
      ┌ i        GridIndex
      └ j
    Attributes:
        rows:     3
        cols:     3

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 a pull request may close this issue.

2 participants