-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
also apply combine_attrs to the attrs of the variables #4902
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 16 commits
28b857e
4ab0013
c53c71a
df8354b
a0a2265
9af2c3f
78f9ef9
77d0208
b234ddf
0cbf1a4
71d605a
fb1fcb9
dad9c38
3ee536d
f03c5e8
78a3efd
abacdc9
b4b64c1
cd3fd0e
8ca5e44
34d6615
8ca3aeb
6e06dc4
2c5c1be
cc08b53
94c7896
dffda43
db0fc56
a1f1dda
59f0732
065e757
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 |
---|---|---|
|
@@ -5,7 +5,14 @@ | |
import numpy as np | ||
import pytest | ||
|
||
from xarray import DataArray, Dataset, combine_by_coords, combine_nested, concat | ||
from xarray import ( | ||
DataArray, | ||
Dataset, | ||
MergeError, | ||
combine_by_coords, | ||
combine_nested, | ||
concat, | ||
) | ||
from xarray.core import dtypes | ||
from xarray.core.combine import ( | ||
_check_shape_tile_ids, | ||
|
@@ -20,6 +27,13 @@ | |
from .test_dataset import create_test_data | ||
|
||
|
||
def clear_attrs(ds): | ||
for var in ds.variables.values(): | ||
var.attrs.clear() | ||
ds.attrs.clear() | ||
return ds | ||
|
||
|
||
def assert_combined_tile_ids_equal(dict1, dict2): | ||
assert len(dict1) == len(dict2) | ||
for k, v in dict1.items(): | ||
|
@@ -472,7 +486,8 @@ def test_concat_name_symmetry(self): | |
assert_identical(x_first, y_first) | ||
|
||
def test_concat_one_dim_merge_another(self): | ||
data = create_test_data() | ||
data = clear_attrs(create_test_data()) | ||
|
||
data1 = data.copy(deep=True) | ||
data2 = data.copy(deep=True) | ||
|
||
|
@@ -498,7 +513,7 @@ def test_auto_combine_2d(self): | |
assert_equal(result, expected) | ||
|
||
def test_auto_combine_2d_combine_attrs_kwarg(self): | ||
ds = create_test_data | ||
ds = lambda x: clear_attrs(create_test_data(x)) | ||
|
||
partway1 = concat([ds(0), ds(3)], dim="dim1") | ||
partway2 = concat([ds(1), ds(4)], dim="dim1") | ||
|
@@ -744,6 +759,142 @@ def test_combine_nested_combine_attrs_drop_conflicts(self): | |
) | ||
assert_identical(expected, actual) | ||
|
||
@pytest.mark.parametrize( | ||
"combine_attrs, attrs1, attrs2, expected_attrs, expect_exception", | ||
[ | ||
( | ||
"no_conflicts", | ||
{"a": 1, "b": 2}, | ||
{"a": 1, "c": 3}, | ||
{"a": 1, "b": 2, "c": 3}, | ||
False, | ||
), | ||
("no_conflicts", {"a": 1, "b": 2}, {}, {"a": 1, "b": 2}, False), | ||
("no_conflicts", {}, {"a": 1, "c": 3}, {"a": 1, "c": 3}, False), | ||
( | ||
"no_conflicts", | ||
{"a": 1, "b": 2}, | ||
{"a": 4, "c": 3}, | ||
{"a": 1, "b": 2, "c": 3}, | ||
True, | ||
), | ||
("drop", {"a": 1, "b": 2}, {"a": 1, "c": 3}, {}, False), | ||
("identical", {"a": 1, "b": 2}, {"a": 1, "b": 2}, {"a": 1, "b": 2}, False), | ||
("identical", {"a": 1, "b": 2}, {"a": 1, "c": 3}, {"a": 1, "b": 2}, True), | ||
( | ||
"override", | ||
{"a": 1, "b": 2}, | ||
{"a": 4, "b": 5, "c": 3}, | ||
{"a": 1, "b": 2}, | ||
False, | ||
), | ||
( | ||
"drop_conflicts", | ||
{"a": 1, "b": 2, "c": 3}, | ||
{"b": 1, "c": 3, "d": 4}, | ||
{"a": 1, "c": 3, "d": 4}, | ||
False, | ||
), | ||
], | ||
) | ||
def test_combine_nested_combine_attrs_variables( | ||
self, combine_attrs, attrs1, attrs2, expected_attrs, expect_exception | ||
): | ||
"""check that combine_attrs is used on data variables and coords""" | ||
data1 = Dataset( | ||
{ | ||
"a": ("x", [1, 2], attrs1), | ||
"b": ("x", [3, -1], attrs1), | ||
"x": ("x", [0, 1], attrs1), | ||
} | ||
) | ||
data2 = Dataset( | ||
{ | ||
"a": ("x", [2, 3], attrs2), | ||
"b": ("x", [-2, 1], attrs2), | ||
"x": ("x", [2, 3], attrs2), | ||
} | ||
) | ||
|
||
if expect_exception: | ||
with raises_regex(MergeError, "combine_attrs"): | ||
combine_by_coords([data1, data2], combine_attrs=combine_attrs) | ||
else: | ||
actual = combine_by_coords([data1, data2], combine_attrs=combine_attrs) | ||
expected = Dataset( | ||
{ | ||
"a": ("x", [1, 2, 2, 3], expected_attrs), | ||
"b": ("x", [3, -1, -2, 1], expected_attrs), | ||
}, | ||
{"x": ("x", [0, 1, 2, 3], expected_attrs)}, | ||
) | ||
|
||
assert_identical(actual, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"combine_attrs, attrs1, attrs2, expected_attrs, expect_exception", | ||
[ | ||
( | ||
"no_conflicts", | ||
{"a": 1, "b": 2}, | ||
{"a": 1, "c": 3}, | ||
{"a": 1, "b": 2, "c": 3}, | ||
False, | ||
), | ||
("no_conflicts", {"a": 1, "b": 2}, {}, {"a": 1, "b": 2}, False), | ||
("no_conflicts", {}, {"a": 1, "c": 3}, {"a": 1, "c": 3}, False), | ||
( | ||
"no_conflicts", | ||
{"a": 1, "b": 2}, | ||
{"a": 4, "c": 3}, | ||
{"a": 1, "b": 2, "c": 3}, | ||
True, | ||
), | ||
("drop", {"a": 1, "b": 2}, {"a": 1, "c": 3}, {}, False), | ||
("identical", {"a": 1, "b": 2}, {"a": 1, "b": 2}, {"a": 1, "b": 2}, False), | ||
("identical", {"a": 1, "b": 2}, {"a": 1, "c": 3}, {"a": 1, "b": 2}, True), | ||
( | ||
"override", | ||
{"a": 1, "b": 2}, | ||
{"a": 4, "b": 5, "c": 3}, | ||
{"a": 1, "b": 2}, | ||
False, | ||
), | ||
( | ||
"drop_conflicts", | ||
{"a": 1, "b": 2, "c": 3}, | ||
{"b": 1, "c": 3, "d": 4}, | ||
{"a": 1, "c": 3, "d": 4}, | ||
False, | ||
), | ||
], | ||
) | ||
def test_combine_by_coords_combine_attrs_variables( | ||
self, combine_attrs, attrs1, attrs2, expected_attrs, expect_exception | ||
): | ||
"""check that combine_attrs is used on data variables and coords""" | ||
data1 = Dataset( | ||
{"x": ("a", [0], attrs1), "y": ("a", [0], attrs1), "a": ("a", [0], attrs1)} | ||
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. should we also check for 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. I had thought we already did that. Turns out we don't, which I didn't realize because that file is in urgent need of a refactor: some test names still reference the removed |
||
) | ||
data2 = Dataset( | ||
{"x": ("a", [1], attrs2), "y": ("a", [1], attrs2), "a": ("a", [1], attrs2)} | ||
) | ||
|
||
if expect_exception: | ||
with raises_regex(MergeError, "combine_attrs"): | ||
combine_by_coords([data1, data2], combine_attrs=combine_attrs) | ||
else: | ||
actual = combine_by_coords([data1, data2], combine_attrs=combine_attrs) | ||
expected = Dataset( | ||
{ | ||
"x": ("a", [0, 1], expected_attrs), | ||
"y": ("a", [0, 1], expected_attrs), | ||
"a": ("a", [0, 1], expected_attrs), | ||
} | ||
) | ||
|
||
assert_identical(actual, expected) | ||
|
||
def test_infer_order_from_coords(self): | ||
data = create_test_data() | ||
objs = [data.isel(dim2=slice(4, 9)), data.isel(dim2=slice(4))] | ||
|
Uh oh!
There was an error while loading. Please reload this page.