Skip to content

Commit

Permalink
Additional options for sign_agreement (#118)
Browse files Browse the repository at this point in the history
* More file handling functions.

* Added option to not count nans in sign_agreement

* Fixed default value

* Update whats-new.rst
  • Loading branch information
jbusecke committed Nov 2, 2021
1 parent d575823 commit e2225a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

What's New
===========
.. _whats-new.1.1.0:

v1.1.0 (unreleased)

New Features
~~~~~~~~~~~~
- `sing_agreement` now supports the option to not count nans along the given dimension (:pull:`118`). By `Julius Busecke <https://github.com/jbusecke>`_

.. _whats-new.1.0.0:

Expand Down
16 changes: 16 additions & 0 deletions xarrayutils/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ def test_linear_trend():
assert np.allclose(fit, test)


def test_sign_agreement_count_nans():
a = xr.DataArray(
[
[1, -1, np.nan, 4],
[1, 1, -1, 4],
[1, -1, -1, 4],
[1, 1, -1, 4],
[1, -1, -1, 4],
],
dims=["i", "j"],
)
sa = sign_agreement(a, a.mean("i"), "i", count_nans=False)
expected = xr.DataArray([1.0, 0.0, 1.0, 1.0], dims=["j"])
xr.testing.assert_allclose(expected, sa)


def test_sign_agreement():
# test dataset
target_dim = "member"
Expand Down
12 changes: 9 additions & 3 deletions xarrayutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ def lag_and_combine(ds, lags, dim="time"):
return xr.concat(datasets, dim=concat_dim_da(lags, "lag"))


def sign_agreement(da, ds_ref, dim, threshold=0.75, mask=True):
def sign_agreement(da, ds_ref, dim, threshold=0.75, mask=True, count_nans=True):
"""[summary]
Parameters
Expand All @@ -743,12 +743,18 @@ def sign_agreement(da, ds_ref, dim, threshold=0.75, mask=True):
threshold : float, optional
The minimum fraction of elements that have to agree along `dim`, by default 0.75 (75%)
mask : bool, optional
If True nan values get masked out in the output, by default True
If True, datapoints with all nan values along `dim` get masked out in the output, by default True
count_nans : bool, optional
If True, nans along `dim` are counted towards the threshold. If False sign agreement is
calculated according to non-nan values only, by default True
"""
if mask:
mask_data = np.isnan(da).all(dim)
ndim = len(da[dim].data)
if count_nans:
ndim = len(da[dim].data)
else:
ndim = (~np.isnan(da)).sum(dim)
sign_agreement = (np.sign(da) == np.sign(ds_ref)).sum(dim) >= (threshold * ndim)
if mask:
sign_agreement = sign_agreement.where(~mask_data)
Expand Down

0 comments on commit e2225a7

Please sign in to comment.