-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add an arithmetic_compat option to xr.set_options #10943
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
Open
mjwillson
wants to merge
2
commits into
pydata:main
Choose a base branch
from
mjwillson:non_index_coords_compat_override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−16
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,16 @@ | |
|
|
||
| import warnings | ||
| from collections.abc import Sequence | ||
| from typing import TYPE_CHECKING, Any, Literal, TypedDict | ||
| from typing import TYPE_CHECKING, Any, Literal, TypedDict, get_args | ||
|
|
||
| from xarray.core.types import CompatOptions | ||
| from xarray.core.utils import FrozenDict | ||
|
|
||
| if TYPE_CHECKING: | ||
| from matplotlib.colors import Colormap | ||
|
|
||
| Options = Literal[ | ||
| "arithmetic_compat", | ||
| "arithmetic_join", | ||
| "chunk_manager", | ||
| "cmap_divergent", | ||
|
|
@@ -40,6 +42,7 @@ | |
|
|
||
| class T_Options(TypedDict): | ||
| arithmetic_broadcast: bool | ||
| arithmetic_compat: CompatOptions | ||
| arithmetic_join: Literal["inner", "outer", "left", "right", "exact"] | ||
| chunk_manager: str | ||
| cmap_divergent: str | Colormap | ||
|
|
@@ -70,6 +73,7 @@ class T_Options(TypedDict): | |
|
|
||
| OPTIONS: T_Options = { | ||
| "arithmetic_broadcast": True, | ||
| "arithmetic_compat": "minimal", | ||
| "arithmetic_join": "inner", | ||
| "chunk_manager": "dask", | ||
| "cmap_divergent": "RdBu_r", | ||
|
|
@@ -109,6 +113,7 @@ def _positive_integer(value: Any) -> bool: | |
|
|
||
| _VALIDATORS = { | ||
| "arithmetic_broadcast": lambda value: isinstance(value, bool), | ||
| "arithmetic_compat": get_args(CompatOptions).__contains__, | ||
| "arithmetic_join": _JOIN_OPTIONS.__contains__, | ||
| "display_max_children": _positive_integer, | ||
| "display_max_rows": _positive_integer, | ||
|
|
@@ -178,18 +183,34 @@ class set_options: | |
|
|
||
| Parameters | ||
| ---------- | ||
| arithmetic_broadcast: bool, default: True | ||
|
Contributor
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. 👀 didn't know this was an option! |
||
| Whether to perform automatic broadcasting in binary operations. | ||
| arithmetic_compat: {"identical", "equals", "broadcast_equals", "no_conflicts", "override", "minimal"}, default: "minimal" | ||
| How to compare non-index coordinates of the same name for potential | ||
| conflicts when performing binary operations. (For the alignment of index | ||
| coordinates in binary operations, see `arithmetic_join`.) | ||
|
|
||
| - "identical": all values, dimensions and attributes of the coordinates | ||
| must be the same. | ||
| - "equals": all values and dimensions of the coordinates must be the | ||
| same. | ||
| - "broadcast_equals": all values of the coordinates must be equal after | ||
| broadcasting to ensure common dimensions. | ||
| - "no_conflicts": only values which are not null in both coordinates | ||
| must be equal. The returned coordinate then contains the combination | ||
| of all non-null values. | ||
| - "override": skip comparing and take the coordinates from the first | ||
| operand. | ||
| - "minimal": drop conflicting coordinates. | ||
| arithmetic_join : {"inner", "outer", "left", "right", "exact"}, default: "inner" | ||
| DataArray/Dataset alignment in binary operations: | ||
| DataArray/Dataset index alignment in binary operations: | ||
|
|
||
| - "outer": use the union of object indexes | ||
| - "inner": use the intersection of object indexes | ||
| - "left": use indexes from the first object with each dimension | ||
| - "right": use indexes from the last object with each dimension | ||
| - "exact": instead of aligning, raise `ValueError` when indexes to be | ||
| aligned are not equal | ||
| - "override": if indexes are of same size, rewrite indexes to be | ||
| those of the first object with that dimension. Indexes for the same | ||
| dimension must have the same size in all objects. | ||
| chunk_manager : str, default: "dask" | ||
| Chunk manager to use for chunked array computations when multiple | ||
| options are installed. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| DataArray, | ||
| Dataset, | ||
| IndexVariable, | ||
| MergeError, | ||
| Variable, | ||
| align, | ||
| broadcast, | ||
|
|
@@ -2516,6 +2517,43 @@ def test_math_with_coords(self) -> None: | |
| actual = alt + orig | ||
| assert_identical(expected, actual) | ||
|
|
||
| def test_math_with_arithmetic_compat_options(self) -> None: | ||
|
Contributor
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. can we add tests for datatree also please |
||
| # Setting up a clash of non-index coordinate 'foo': | ||
| a = xr.DataArray( | ||
| data=[0, 0, 0], | ||
| dims=["x"], | ||
| coords={ | ||
| "x": [1, 2, 3], | ||
| "foo": (["x"], [1.0, 2.0, np.nan]), | ||
| }, | ||
| ) | ||
| b = xr.DataArray( | ||
| data=[0, 0, 0], | ||
| dims=["x"], | ||
| coords={ | ||
| "x": [1, 2, 3], | ||
| "foo": (["x"], [np.nan, 2.0, 3.0]), | ||
| }, | ||
| ) | ||
|
|
||
| with xr.set_options(arithmetic_compat="minimal"): | ||
| assert_equal(a + b, a.drop_vars("foo")) | ||
|
|
||
| with xr.set_options(arithmetic_compat="override"): | ||
| assert_equal(a + b, a) | ||
| assert_equal(b + a, b) | ||
|
|
||
| with xr.set_options(arithmetic_compat="no_conflicts"): | ||
| expected = a.assign_coords(foo=(["x"], [1.0, 2.0, 3.0])) | ||
| assert_equal(a + b, expected) | ||
| assert_equal(b + a, expected) | ||
|
|
||
| with xr.set_options(arithmetic_compat="equals"): | ||
| with pytest.raises(MergeError): | ||
| a + b | ||
| with pytest.raises(MergeError): | ||
| b + a | ||
|
|
||
| def test_index_math(self) -> None: | ||
| orig = DataArray(range(3), dims="x", name="x") | ||
| actual = orig + 1 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.