Skip to content

Feat: xarray extra dims coordinates - #662

Merged
tmichela merged 4 commits into
masterfrom
feat/xarray-extra-dims-coordinates
Oct 14, 2025
Merged

Feat: xarray extra dims coordinates#662
tmichela merged 4 commits into
masterfrom
feat/xarray-extra-dims-coordinates

Conversation

@tmichela

Copy link
Copy Markdown
Member

Adds coordinates for extra dimensions, particularly useful when roi is defined.

@tmichela tmichela added the enhancement New feature or request label Sep 19, 2025
@tmichela tmichela added this to the 1.23 milestone Sep 19, 2025
@tmichela

Copy link
Copy Markdown
Member Author

A potential bug I noticed while testing this feature: if a roi contains an integer (e.g. np.s_[0, :10]) the assigned dimensions will skip that axis, i.e.:
The resulting xarray has shape=(<n-trains>, 10) and dims=['trainId', 'dim_0'].
But I think it should rather be dims=['trainId', 'dim_1'] (dim_0 is dropped by the slicing).

?

@takluyver

Copy link
Copy Markdown
Member

if a roi contains an integer (e.g. np.s_[0, :10]) the assigned dimensions will skip that axis, i.e.:
The resulting xarray has shape=(<n-trains>, 10) and dims=['trainId', 'dim_0'].

I think you're right. I didn't really give any attention to the logical consistency of dimension names, they just have to be called something. 🤷

Comment thread extra_data/keydata.py Outdated

@philsmt philsmt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think I stumbled on this problem yet, but this seems to be the better solution.

If arbitrary coordinates are a concern here, we should rather make using those more convenient than have the returned dimensions be more useless.

The question around dim_0 or dim_1 if a dimension is omitted is tricky. Given that extra_dims must match the final shape, this is more in line with starting with dim_0. In other words, if we skip dimensions sliced out of the result, extra_dims should also look at the full shape rather than the final one.

roi4 = ()
darr4 = am0.xarray(roi=roi4, extra_dims=['a', 'b', 'c'])
assert darr4.shape == (64, 2, 512, 128)
np.testing.assert_array_equal(darr4.coords['a'], np.arange(0, 2))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was surprised this works, but it does:

In [11]: 'dim_0' in da.coords
Out[11]: False

In [12]: da.coords['dim_0'].values
Out[12]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

TIL

@takluyver

Copy link
Copy Markdown
Member

LGTM

@tmichela

Copy link
Copy Markdown
Member Author

thanks

@tmichela
tmichela merged commit ac466ec into master Oct 14, 2025
10 checks passed
@takluyver
takluyver deleted the feat/xarray-extra-dims-coordinates branch October 14, 2025 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants