Skip to content

Commit 1e84ee5

Browse files
committed
Remove unused _mask_var_with_weight_threshold() function
1 parent bd872cf commit 1e84ee5

File tree

2 files changed

+1
-180
lines changed

2 files changed

+1
-180
lines changed

tests/test_utils.py

Lines changed: 1 addition & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import numpy as np
21
import pytest
32
import xarray as xr
43

5-
from tests.fixtures import generate_dataset
6-
from xcdat.utils import (
7-
_validate_min_weight,
8-
compare_datasets,
9-
mask_var_with_weight_threshold,
10-
str_to_bool,
11-
)
4+
from xcdat.utils import _validate_min_weight, compare_datasets, str_to_bool
125

136

147
class TestCompareDatasets:
@@ -112,132 +105,6 @@ def test_raises_error_if_str_is_not_a_python_bool(self):
112105
str_to_bool("1")
113106

114107

115-
class TestMaskVarWithWeightThreshold:
116-
@pytest.fixture(autouse=True)
117-
def setup(self):
118-
self.ds = generate_dataset(
119-
decode_times=True, cf_compliant=False, has_bounds=True
120-
)
121-
122-
def test_returns_mask_var_with_spatial_min_weight_of_100(self):
123-
ds = self.ds.copy()
124-
ds = ds.isel({"time": slice(0, 3), "lat": slice(0, 3), "lon": slice(0, 3)})
125-
ds["ts"][0, :, 2] = np.nan
126-
127-
# Function arguments.
128-
dv = ds["ts"].copy()
129-
weights = ds.spatial.get_weights(
130-
axis=["X", "Y"],
131-
lat_bounds=(-5.0, 5),
132-
lon_bounds=(-170, -120.1),
133-
data_var="ts",
134-
)
135-
136-
result = mask_var_with_weight_threshold(dv, weights, min_weight=1.0)
137-
expected = xr.DataArray(
138-
data=np.array(
139-
[
140-
[
141-
[np.nan, np.nan, np.nan],
142-
[np.nan, np.nan, np.nan],
143-
[np.nan, np.nan, np.nan],
144-
],
145-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
146-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
147-
]
148-
),
149-
coords={"time": ds.time, "lat": ds.lat, "lon": ds.lon},
150-
dims=["time", "lat", "lon"],
151-
)
152-
153-
xr.testing.assert_allclose(result, expected)
154-
155-
def test_returns_mask_var_with_spatial_min_weight_of_0(self):
156-
ds = self.ds.copy()
157-
ds = ds.isel({"time": slice(0, 3), "lat": slice(0, 3), "lon": slice(0, 3)})
158-
159-
# Function arguments.
160-
dv = ds["ts"].copy()
161-
weights = ds.spatial.get_weights(
162-
axis=["X", "Y"],
163-
lat_bounds=(-5.0, 5),
164-
lon_bounds=(-170, -120.1),
165-
data_var="ts",
166-
)
167-
168-
result = mask_var_with_weight_threshold(dv, weights, min_weight=0)
169-
expected = xr.DataArray(
170-
data=np.array(
171-
[
172-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
173-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
174-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
175-
]
176-
),
177-
coords={"time": ds.time, "lat": ds.lat, "lon": ds.lon},
178-
dims=["time", "lat", "lon"],
179-
)
180-
181-
xr.testing.assert_allclose(result, expected)
182-
183-
def test_returns_mask_var_with_temporal_min_weight_of_100(self):
184-
ds = self.ds.copy()
185-
ds = ds.isel({"time": slice(0, 3), "lat": slice(0, 3), "lon": slice(0, 3)})
186-
ds["ts"][0, :, 2] = np.nan
187-
188-
# Function arguments.
189-
dv = ds["ts"].copy()
190-
weights = xr.DataArray(
191-
name="time_wts",
192-
data=np.array([1.0, 1.0, 1.0]),
193-
dims="time",
194-
coords={"time": ds.time},
195-
)
196-
197-
result = mask_var_with_weight_threshold(dv, weights, min_weight=0)
198-
expected = xr.DataArray(
199-
data=np.array(
200-
[
201-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [np.nan, 1.0, 1.0]],
202-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [np.nan, 1.0, 1.0]],
203-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [np.nan, 1.0, 1.0]],
204-
]
205-
),
206-
coords={"lat": ds.lat, "lon": ds.lon, "time": ds.time},
207-
dims=["lat", "lon", "time"],
208-
)
209-
210-
xr.testing.assert_allclose(result, expected)
211-
212-
def test_returns_mask_var_with_temporal_min_weight_of_0(self):
213-
ds = self.ds.copy()
214-
ds = ds.isel({"time": slice(0, 3), "lat": slice(0, 3), "lon": slice(0, 3)})
215-
216-
# Function arguments.
217-
dv = ds["ts"].copy()
218-
weights = xr.DataArray(
219-
name="time_wts",
220-
data=np.array([1.0, 1.0, 1.0]),
221-
dims="time",
222-
coords={"time": ds.time},
223-
)
224-
225-
result = mask_var_with_weight_threshold(dv, weights, min_weight=0)
226-
expected = xr.DataArray(
227-
data=np.array(
228-
[
229-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
230-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
231-
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
232-
]
233-
),
234-
coords={"lat": ds.lat, "lon": ds.lon, "time": ds.time},
235-
dims=["lat", "lon", "time"],
236-
)
237-
238-
xr.testing.assert_allclose(result, expected)
239-
240-
241108
class TestValidateMinWeight:
242109
def test_pass_None_returns_0(self):
243110
result = _validate_min_weight(None)

xcdat/utils.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
from typing import Dict, List, Optional, Union
44

5-
import numpy as np
65
import xarray as xr
76
from dask.array.core import Array
87

@@ -135,51 +134,6 @@ def _if_multidim_dask_array_then_load(
135134
return None
136135

137136

138-
def mask_var_with_weight_threshold(
139-
dv: xr.DataArray, weights: xr.DataArray, min_weight: float
140-
) -> xr.DataArray:
141-
"""Mask values that do not meet the minimum weight threshold using np.nan.
142-
143-
This function is useful for cases where the weighting of data might be
144-
skewed based on the availability of data. For example, if one season in a
145-
time series has more significantly more missing data than other seasons, it
146-
can result in inaccurate calculations of climatologies. Masking values that
147-
do not meet the minimum weight threshold ensures more accurate calculations.
148-
149-
Parameters
150-
----------
151-
dv : xr.DataArray
152-
The weighted variable.
153-
weights : xr.DataArray
154-
A DataArray containing either the regional or temporal weights used for
155-
weighted averaging. ``weights`` must include the same axis dimensions
156-
and dimensional sizes as the data variable.
157-
min_weight : float
158-
Fraction of data coverage (i..e, weight) needed to return a
159-
spatial average value. Value must range from 0 to 1.
160-
161-
Returns
162-
-------
163-
xr.DataArray
164-
The variable with the minimum weight threshold applied.
165-
"""
166-
masked_weights = _get_masked_weights(dv, weights)
167-
168-
# Sum all weights, including zero for missing values.
169-
dim = weights.dims
170-
weight_sum_all = weights.sum(dim=dim)
171-
weight_sum_masked = masked_weights.sum(dim=dim)
172-
173-
# Get fraction of the available weight.
174-
frac = weight_sum_masked / weight_sum_all
175-
176-
# Nan out values that don't meet specified weight threshold.
177-
dv_new = xr.where(frac >= min_weight, dv, np.nan, keep_attrs=True)
178-
dv_new.name = dv.name
179-
180-
return dv_new
181-
182-
183137
def _get_masked_weights(dv: xr.DataArray, weights: xr.DataArray) -> xr.DataArray:
184138
"""Get weights with missing data (`np.nan`) receiving no weight (zero).
185139

0 commit comments

Comments
 (0)