Skip to content

Commit

Permalink
Merge branch 'master' into fix-1524
Browse files Browse the repository at this point in the history
  • Loading branch information
huard authored Nov 30, 2023
2 parents d6a3744 + ba8e431 commit 39ad747
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.46.5-beta
current_version = 0.46.6-beta
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+).(?P<patch>\d+)(\-(?P<release>[a-z]+))?
Expand Down
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
2 changes: 1 addition & 1 deletion xclim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

__author__ = """Travis Logan"""
__email__ = "[email protected]"
__version__ = "0.46.5-beta"
__version__ = "0.46.6-beta"


_module_data = _files("xclim.data")
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

0 comments on commit 39ad747

Please sign in to comment.