Skip to content

Commit 11c8eea

Browse files
authored
Raise error if weights are used with unweighted aggregator (#2640)
1 parent c2f1029 commit 11c8eea

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

esmvalcore/preprocessor/_area.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,12 @@ def area_statistics(
354354
# Get aggregator and correct kwargs (incl. weights)
355355
(agg, agg_kwargs) = get_iris_aggregator(operator, **operator_kwargs)
356356
agg_kwargs = update_weights_kwargs(
357-
agg, agg_kwargs, "cell_area", cube, try_adding_calculated_cell_area
357+
operator,
358+
agg,
359+
agg_kwargs,
360+
"cell_area",
361+
cube,
362+
try_adding_calculated_cell_area,
358363
)
359364

360365
with ignore_iris_vague_metadata_warnings():

esmvalcore/preprocessor/_shared.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_iris_aggregator(
9292
x_coord = DimCoord([1.0], bounds=[0.0, 2.0], var_name="x")
9393
cube = Cube([0.0], dim_coords_and_dims=[(x_coord, 0)])
9494
test_kwargs = update_weights_kwargs(
95-
aggregator, aggregator_kwargs, np.array([1.0])
95+
operator, aggregator, aggregator_kwargs, np.array([1.0])
9696
)
9797
try:
9898
with ignore_iris_vague_metadata_warnings():
@@ -127,6 +127,7 @@ def aggregator_accept_weights(aggregator: iris.analysis.Aggregator) -> bool:
127127

128128

129129
def update_weights_kwargs(
130+
operator: str,
130131
aggregator: iris.analysis.Aggregator,
131132
kwargs: dict,
132133
weights: Any,
@@ -138,6 +139,8 @@ def update_weights_kwargs(
138139
139140
Parameters
140141
----------
142+
operator:
143+
Named operator.
141144
aggregator:
142145
Iris aggregator.
143146
kwargs:
@@ -161,6 +164,10 @@ def update_weights_kwargs(
161164
162165
"""
163166
kwargs = dict(kwargs)
167+
if not aggregator_accept_weights(aggregator) and "weights" in kwargs:
168+
raise ValueError(
169+
f"Aggregator '{operator}' does not support 'weights' option"
170+
)
164171
if aggregator_accept_weights(aggregator) and kwargs.get("weights", True):
165172
kwargs["weights"] = weights
166173
if cube is not None and callback is not None:

esmvalcore/preprocessor/_time.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,12 @@ def climate_statistics(
836836
if period in ("full",):
837837
(agg, agg_kwargs) = get_iris_aggregator(operator, **operator_kwargs)
838838
agg_kwargs = update_weights_kwargs(
839-
agg, agg_kwargs, "_time_weights_", cube, _add_time_weights_coord
839+
operator,
840+
agg,
841+
agg_kwargs,
842+
"_time_weights_",
843+
cube,
844+
_add_time_weights_coord,
840845
)
841846
with warnings.catch_warnings():
842847
warnings.filterwarnings(

esmvalcore/preprocessor/_volume.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ def volume_statistics(
293293

294294
(agg, agg_kwargs) = get_iris_aggregator(operator, **operator_kwargs)
295295
agg_kwargs = update_weights_kwargs(
296+
operator,
296297
agg,
297298
agg_kwargs,
298299
"ocean_volume",
@@ -377,6 +378,7 @@ def axis_statistics(
377378
# bounds of the original coordinate (this handles units properly, e.g., for
378379
# sums)
379380
agg_kwargs = update_weights_kwargs(
381+
operator,
380382
agg,
381383
agg_kwargs,
382384
"_axis_statistics_weights_",

tests/unit/preprocessor/test_shared.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ def test_get_iris_aggregator_mean(operator, kwargs):
6666
assert agg_kwargs == kwargs
6767

6868

69-
@pytest.mark.parametrize("kwargs", [{}, {"weights": True}])
7069
@pytest.mark.parametrize("operator", ["median", "mEdIaN", "MEDIAN"])
71-
def test_get_iris_aggregator_median(operator, kwargs):
70+
def test_get_iris_aggregator_median(operator):
7271
"""Test ``get_iris_aggregator``."""
73-
(agg, agg_kwargs) = get_iris_aggregator(operator, **kwargs)
72+
(agg, agg_kwargs) = get_iris_aggregator(operator)
7473
assert agg == iris.analysis.MEDIAN
75-
assert agg_kwargs == kwargs
74+
assert agg_kwargs == {}
7675

7776

7877
@pytest.mark.parametrize("operator", ["min", "MiN", "MIN"])
@@ -172,6 +171,14 @@ def test_get_iris_aggregator_missing_kwarg():
172171
get_iris_aggregator("percentile")
173172

174173

174+
def test_get_iris_aggregator_no_weights_allowed():
175+
"""Test ``get_iris_aggregator``."""
176+
operator = "median"
177+
kwargs = {"weights": True}
178+
with pytest.raises(ValueError):
179+
get_iris_aggregator(operator, **kwargs)
180+
181+
175182
@pytest.mark.parametrize(
176183
"aggregator,result",
177184
[

0 commit comments

Comments
 (0)