-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (51 loc) · 2.02 KB
/
Copy pathutils.py
File metadata and controls
69 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from typing import Any
import numpy as np
import xarray as xr
from parcels._compat import add_note
_SUPPORTED_ATTR_TYPES = int | float | str | np.ndarray
def _print_mismatched_keys(d1: dict[Any, Any], d2: dict[Any, Any]) -> None:
k1 = set(d1.keys())
k2 = set(d2.keys())
if len(k1 ^ k2) == 0:
return
print("Mismatched keys:")
print(f"L: {k1 - k2!r}")
print(f"R: {k2 - k1!r}")
def assert_common_attrs_equal(
xr_attrs_1: dict[str, _SUPPORTED_ATTR_TYPES], xr_attrs_2: dict[str, _SUPPORTED_ATTR_TYPES], *, verbose: bool = True
) -> None:
d1, d2 = xr_attrs_1, xr_attrs_2
common_keys = set(d1.keys()) & set(d2.keys())
if verbose:
_print_mismatched_keys(d1, d2)
for key in common_keys:
try:
if isinstance(d1[key], np.ndarray):
np.testing.assert_array_equal(d1[key], d2[key])
else:
assert d1[key] == d2[key], f"{d1[key]} != {d2[key]}"
except AssertionError as e:
add_note(e, f"error on key {key!r}")
raise
def assert_common_variables_common_attrs_equal(ds1: xr.Dataset, ds2: xr.Dataset, *, verbose: bool = True) -> None:
if verbose:
print("Checking dataset attrs...")
assert_common_attrs_equal(ds1.attrs, ds2.attrs, verbose=verbose)
ds1_vars = set(ds1.variables)
ds2_vars = set(ds2.variables)
common_variables = ds1_vars & ds2_vars
if len(ds1_vars ^ ds2_vars) > 0 and verbose:
print("Mismatched variables:")
print(f"L: {ds1_vars - ds2_vars}")
print(f"R: {ds2_vars - ds1_vars}")
for var in common_variables:
if verbose:
print(f"Checking {var!r} attrs")
assert_common_attrs_equal(ds1[var].attrs, ds2[var].attrs, verbose=verbose)
def dataset_repr_diff(ds1: xr.Dataset, ds2: xr.Dataset) -> str:
"""Return a text diff of two datasets."""
repr1 = repr(ds1)
repr2 = repr(ds2)
import difflib
diff = difflib.ndiff(repr1.splitlines(keepends=True), repr2.splitlines(keepends=True))
return "".join(diff)