Skip to content
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

Fix robustness fractions and categories attributes #1535

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tests/test_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,9 @@ def test_robustness_fractions_empty():


def test_robustness_categories():
changed = xr.DataArray([0.5, 0.8, 1, 1])
agree = xr.DataArray([1, 0.5, 0.5, 1])
lat = xr.DataArray([1, 2, 3, 4], dims=("lat",), attrs={"axis": "Y"}, name="lat")
changed = xr.DataArray([0.5, 0.8, 1, 1], dims=("lat",), coords={"lat": lat})
agree = xr.DataArray([1, 0.5, 0.5, 1], dims=("lat",), coords={"lat": lat})

categories = ensembles.robustness_categories(changed, agree)
np.testing.assert_array_equal(categories, [2, 3, 3, 1])
Expand All @@ -753,6 +754,7 @@ def test_robustness_categories():
categories.flag_meanings
== "robust_signal no_change_or_no_signal conflicting_signal"
)
assert categories.lat.attrs["axis"] == "Y"


def test_robustness_coefficient():
Expand Down
21 changes: 13 additions & 8 deletions xclim/ensembles/_robustness.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ def robustness_fractions( # noqa: C901
)
out = out.assign(pvals=pvals)

# Keep attrs on non-modified coordinates
for ncrd, crd in fut.coords.items():
if ncrd in out.coords:
out[ncrd].attrs.update(crd.attrs)

return out


Expand Down Expand Up @@ -370,16 +375,16 @@ def robustness_categories(
Categorical (int) array following the flag variables CF conventions.
99 is used as a fill value for points that do not fall in any category.
"""
if isinstance(changed_or_fractions, xr.Dataset):
src = changed_or_fractions.copy() # Ensure no inplace changing of coords...
if isinstance(src, xr.Dataset):
# Output of robustness fractions
changed = changed_or_fractions.changed
agree = changed_or_fractions.agree
changed = src.changed
agree = src.agree
else:
changed = changed_or_fractions
changed = src

# Initial map is all 99, same shape as change_frac
robustness = changed * 0 + 99

robustness = (changed.copy() * 0).astype(int) + 99
# We go in reverse gear so that the first categories have precedence in the case of multiple matches.
for i, ((chg_op, agr_op), (chg_thresh, agr_thresh)) in reversed(
list(enumerate(zip(ops, thresholds), 1))
Expand All @@ -392,9 +397,9 @@ def robustness_categories(
cond = compare(changed, chg_op, chg_thresh) & compare(
agree, agr_op, agr_thresh
)
robustness = xr.where(cond, i, robustness)
robustness = xr.where(~cond, robustness, i, keep_attrs=True)

robustness = robustness.astype(np.uint8).assign_attrs(
robustness = robustness.assign_attrs(
flag_values=list(range(1, len(categories) + 1)),
_FillValue=99,
flag_descriptions=categories,
Expand Down