-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
DataArray: propagate index coordinates with non-array dimensions #10116
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
Changes from 9 commits
f93b45a
01fc5fd
75b9ae5
b647df7
90ce0f9
ebcfa22
f27f2d0
528f356
27a1e38
695fb86
eea90cf
bc578d8
5e8093b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -130,18 +130,30 @@ | |||||
| T_XarrayOther = TypeVar("T_XarrayOther", bound="DataArray" | Dataset) | ||||||
|
|
||||||
|
|
||||||
| def _check_coords_dims(shape, coords, dim): | ||||||
| def _check_coords_dims( | ||||||
| shape: tuple[int, ...], coords: Coordinates, dim: tuple[Hashable, ...] | ||||||
| ): | ||||||
benbovy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| sizes = dict(zip(dim, shape, strict=True)) | ||||||
| extra_index_dims: set[str] = set() | ||||||
|
|
||||||
| for k, v in coords.items(): | ||||||
| if any(d not in dim for d in v.dims): | ||||||
| # allow any coordinate associated with an index that shares at least | ||||||
| # one of dataarray's dimensions | ||||||
| indexes = coords.xindexes | ||||||
benbovy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if k in indexes: | ||||||
| index_dims = indexes.get_all_dims(k) | ||||||
| if any(d in dim for d in index_dims): | ||||||
| extra_index_dims.update(d for d in v.dims if d not in dim) | ||||||
| continue | ||||||
| raise ValueError( | ||||||
| f"coordinate {k} has dimensions {v.dims}, but these " | ||||||
| "are not a subset of the DataArray " | ||||||
| f"dimensions {dim}" | ||||||
| ) | ||||||
|
|
||||||
| for d, s in v.sizes.items(): | ||||||
| if s != sizes[d]: | ||||||
| if d not in extra_index_dims and s != sizes[d]: | ||||||
|
||||||
| if d not in extra_index_dims and s != sizes[d]: | |
| if d not in extra_index_dims or s != sizes[d]: |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be and.
extra_index_dims corresponds to all non-array dimensions of index coordinates to include in the dataarray (size[d] would return a KeyError).
I renamed it and added comment in 695fb86.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1210,10 +1210,20 @@ def _construct_dataarray(self, name: Hashable) -> DataArray: | |
| needed_dims = set(variable.dims) | ||
|
|
||
| coords: dict[Hashable, Variable] = {} | ||
| temp_indexes = self.xindexes | ||
| # preserve ordering | ||
| for k in self._variables: | ||
| if k in self._coord_names and set(self._variables[k].dims) <= needed_dims: | ||
| coords[k] = self._variables[k] | ||
| if k in self._coord_names: | ||
benbovy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ( | ||
| k not in coords | ||
| and k in temp_indexes | ||
| and set(temp_indexes.get_all_dims(k)) & needed_dims | ||
| ): | ||
| # add all coordinates of each index that shares at least one dimension | ||
| # with the dimensions of the extracted variable | ||
| coords.update(temp_indexes.get_all_coords(k)) | ||
|
Comment on lines
+1217
to
+1224
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coud this use a separate loop after the existing loop instead? e.g., for k in self._indexes:
if k in coords:
coords.update(self.xindexes.get_all_coords(k))Or if we allow indexes without a coordinate of the same name: for k in self._indexes:
if set(self.xindexes.get_all_dims(k)) & needed_dims:
coords.update(self.xindexes.get_all_coords(k))Ideally, I would like the logic here to be just as simple as the words describing how it works, so a comment is not necessary! |
||
| elif set(self._variables[k].dims) <= needed_dims: | ||
| coords[k] = self._variables[k] | ||
|
|
||
| indexes = filter_indexes_from_coords(self._indexes, set(coords)) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.