From d58081291254cd69183dc09fff315b478fffc217 Mon Sep 17 00:00:00 2001 From: Julius Busecke Date: Fri, 8 Jan 2021 09:19:02 -0500 Subject: [PATCH] Deprecate `weighted_operations.py` (#82) * Deprecate `weighted_operations.py` The module `weighted_operations.py` is quite old and as far as I can see it is entirely superseded by xarray's `weighted` functionality. I will just remove this unless somebody has objections. * Delete weighted_operations.py --- xarrayutils/test/test_weighted_operations.py | 133 ------------------- xarrayutils/weighted_operations.py | 117 ---------------- 2 files changed, 250 deletions(-) delete mode 100644 xarrayutils/test/test_weighted_operations.py delete mode 100644 xarrayutils/weighted_operations.py diff --git a/xarrayutils/test/test_weighted_operations.py b/xarrayutils/test/test_weighted_operations.py deleted file mode 100644 index 394c85c..0000000 --- a/xarrayutils/test/test_weighted_operations.py +++ /dev/null @@ -1,133 +0,0 @@ -import xarray as xr -import pytest -import numpy as np -import dask.array as dsa -from xarray.testing import assert_allclose -from xarrayutils.weighted_operations import ( - _broadcast_weights, - weighted_mean, - weighted_sum, - weighted_sum_raw, -) - - -def test_broadcast_weights(): - chunks = 1 - data_test = np.array([[np.nan, 1, 2], [1, 2, 4]]) - weight_test = np.array([1, 3, 5]) - attrs = {"test": "content"} - - weight_expanded_test = xr.DataArray( - np.array([[np.nan, 3, 5], [1, 3, 5]]), dims=["x", "y"], name="data", attrs=attrs - ) - weight_dsa_expanded_test = xr.DataArray( - dsa.from_array(weight_expanded_test, chunks=chunks), - dims=["x", "y"], - name="data", - attrs=attrs, - ) - - a = xr.DataArray(data_test, dims=["x", "y"], name="data", attrs=attrs) - w = xr.DataArray(weight_test, dims=["y"], name="data", attrs=attrs) - - a_dsa = xr.DataArray( - dsa.from_array(data_test, chunks=chunks), - dims=["x", "y"], - name="data", - attrs=attrs, - ) - w_dsa = xr.DataArray( - dsa.from_array(weight_test, chunks=chunks), dims=["y"], name="data", attrs=attrs - ) - - w_dsa_expanded = _broadcast_weights(a_dsa, w_dsa) - w_dsa_expanded_attrs = _broadcast_weights(a_dsa, w_dsa, keep_attrs=True) - w_expanded = _broadcast_weights(a, w) - xr.testing.assert_allclose(w_dsa_expanded, weight_dsa_expanded_test) - xr.testing.assert_identical(w_dsa_expanded_attrs, weight_dsa_expanded_test) - xr.testing.assert_allclose(w_expanded, weight_expanded_test) - - -def test_weighted_sum_raw(): - chunks = 1 - data_test = np.array([[np.nan, 2, 1], [2, 2, 4]]) - weight_test = np.array([1, 1, 2]) - - attrs = {"test": "content"} - a_dsa = xr.DataArray( - dsa.from_array(data_test, chunks=chunks), dims=["x", "y"], attrs=attrs - ) - w_dsa = xr.DataArray( - dsa.from_array(weight_test, chunks=chunks), dims=["y"], attrs=attrs - ) - - data_sum, weight_sum = weighted_sum_raw( - a_dsa, w_dsa, dim=["x", "y"], dimcheck=False - ) - data_sum_attrs, weight_sum_attrs = weighted_sum_raw( - a_dsa, w_dsa, dim=["x", "y"], dimcheck=False, keep_attrs=True - ) - - assert weight_sum_attrs.attrs["test"] == attrs["test"] - assert data_sum_attrs.attrs["test"] == attrs["test"] - - -def test_weighted_mean(): - chunks = 1 - data_test = np.array([[np.nan, 2, np.nan], [2, 2, 4]]) - weight_test = np.array([1, 1, 2]) - - attrs = {"test": "content"} - a_dsa = xr.DataArray( - dsa.from_array(data_test, chunks=chunks), dims=["x", "y"], attrs=attrs - ) - w_dsa = xr.DataArray(dsa.from_array(weight_test, chunks=chunks), dims=["y"]) - - expected_mean = np.array(2.8) - expected_ymean = np.array([2.0, 3.0]) - expected_xmean = np.array([2, 2, 4]) - - mean = weighted_mean(a_dsa, w_dsa, dim=["x", "y"], dimcheck=False) - ymean = weighted_mean(a_dsa, w_dsa, dim=["y"], dimcheck=False) - xmean = weighted_mean(a_dsa, w_dsa, dim=["x"], dimcheck=False) - xmean_alt = weighted_mean(a_dsa, w_dsa, dim="x", dimcheck=False, keep_attrs=True) - - with pytest.raises(RuntimeError): - weighted_mean(a_dsa, w_dsa, dim=["x", "y"], dimcheck=True) - - assert np.isclose(mean, expected_mean) - assert np.all(np.isclose(ymean, expected_ymean)) - assert np.all(np.isclose(xmean, expected_xmean)) - assert np.all(np.isclose(xmean, xmean_alt)) - assert xmean_alt.attrs["test"] == attrs["test"] - - -def test_weighted_sum(): - chunks = 1 - data_test = np.array([[np.nan, 2, 1], [2, 2, 4]]) - weight_test = np.array([1, 1, 2]) - - attrs = {"test": "content"} - a_dsa = xr.DataArray( - dsa.from_array(data_test, chunks=chunks), dims=["x", "y"], attrs=attrs - ) - w_dsa = xr.DataArray(dsa.from_array(weight_test, chunks=chunks), dims=["y"]) - - expected_mean = np.array(16.0) - expected_ymean = np.array([4.0, 12.0]) - expected_xmean = np.array([2.0, 4.0, 10.0]) - - mean = weighted_sum(a_dsa, w_dsa, dim=["x", "y"], dimcheck=False) - - ymean = weighted_sum(a_dsa, w_dsa, dim=["y"], dimcheck=False) - xmean = weighted_sum(a_dsa, w_dsa, dim=["x"], dimcheck=False) - xmean_alt = weighted_sum(a_dsa, w_dsa, dim="x", dimcheck=False, keep_attrs=True) - - with pytest.raises(RuntimeError): - weighted_sum(a_dsa, w_dsa, dim=["x", "y"], dimcheck=True) - - assert np.isclose(mean, expected_mean) - assert np.all(np.isclose(ymean, expected_ymean)) - assert np.all(np.isclose(xmean, expected_xmean)) - assert np.all(np.isclose(xmean, xmean_alt)) - assert xmean_alt.attrs["test"] == attrs["test"] diff --git a/xarrayutils/weighted_operations.py b/xarrayutils/weighted_operations.py deleted file mode 100644 index 147e0ea..0000000 --- a/xarrayutils/weighted_operations.py +++ /dev/null @@ -1,117 +0,0 @@ -import dask.array as dsa -import xarray as xr -import numpy as np - - -def weighted_mean(da_data, da_weight, **kwargs): - """calculate average of da_data weighted by da_weight - - Parameters - ---------- - - da_data : xarray.DataArray - Data to be averaged - da_weight : xarray.DataArray - weights to be used during averaging. Dimensions have to be - matching with 'da_data' - dim : {None, str, list}, optional - Dimensions to average over - preweighted: Bool, optional - Specifies whether weights will be applied (False, default) or - have already been - applied to da_data (True). - dim_check: Bool, optional - Activates check for dimension consistency. If dimensions of 'da_weight' - do not include all elements of 'dim' error is raised - """ - data, weight_expanded = weighted_sum_raw(da_data, da_weight, **kwargs) - out = data / weight_expanded - out.attrs = data.attrs - return out - - -def weighted_sum(da_data, da_weight, **kwargs): - """calculate sum of da_data weighted by da_weight - - Parameters - ---------- - - da_data : xarray.DataArray - Data to be averaged - da_weight : xarray.DataArray - weights to be used during averaging. Dimensions have to be matching - with 'da_data' - dim : {None, str, list}, optional - Dimensions to average over - preweighted: Bool, optional - Specifies whether weights will be applied (False, default) or have - already been - applied to da_data (True). - dim_check: Bool, optional - Activates check for dimension consistency. If dimensions of - 'da_weight' do not include all elements of 'dim' error is raised - """ - data, _ = weighted_sum_raw(da_data, da_weight, **kwargs) - return data - - -def weighted_sum_raw( - da_data, da_weight, dim=None, preweighted=False, dimcheck=True, **kwargs -): - """calculate sum of da_data weighted by da_weight and the weights themselves - - Parameters - ---------- - - da_data : xarray.DataArray - Data to be averaged - da_weight : xarray.DataArray - weights to be used during averaging. Dimensions have to be matching - with 'da_data' - dim : {None, str, list}, optional - Dimensions to average over - preweighted: Bool, optional - Specifies whether weights will be applied (False, default) or have - already been - applied to da_data (True). - dim_check: Bool, optional - Activates check for dimension consistency. If dimensions of - 'da_weight' do not include all elements of 'dim' error is raised - """ - if isinstance(dim, str): - dim = [dim] - - # Check dimension consistency - if dim: - if dimcheck: - if not set(dim).issubset(da_weight.dims): - raise RuntimeError( - "Dimensions of 'da_weight' do not include all averaging dimensions.\ - Broadcast da_weight or deactivate 'dim_check'." - ) - if "keep_attrs" in kwargs.keys(): - keep_attrs = kwargs["keep_attrs"] - else: - keep_attrs = False - - weight_expanded = _broadcast_weights(da_data, da_weight, keep_attrs=keep_attrs) - - if preweighted: - data = da_data - else: - data = da_data * weight_expanded - data.attrs = da_data.attrs - - return data.sum(dim, **kwargs), weight_expanded.sum(dim, **kwargs) - - -def _broadcast_weights(da_data, da_weight, keep_attrs=False): - """broadcasts da_weights to the same shape as da_data and \ - masks the same missing values""" - da_data = da_data.copy() - ones = (da_data * 0) + 1 - weights_expanded = ones * da_weight - # add attrs back in - if keep_attrs: - weights_expanded.attrs = da_data.attrs - return weights_expanded