Skip to content

Commit fb466ba

Browse files
committed
Make weights handling simpler by explicitly calling _Weights constructor
1 parent 0e5b625 commit fb466ba

File tree

3 files changed

+22
-64
lines changed

3 files changed

+22
-64
lines changed

lib/iris/analysis/__init__.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,35 +1288,6 @@ def __init__(self, weights, cube):
12881288
self.array = derived_array
12891289
self.units = derived_units
12901290

1291-
@classmethod
1292-
def get_updated_kwargs(cls, kwargs, cube):
1293-
"""Get kwargs and weights units updated with `weights` information.
1294-
1295-
Args:
1296-
1297-
* kwargs (dict):
1298-
Keyword arguments that will be updated if a `weights` keyword is
1299-
present which is not ``None``.
1300-
* cube (Cube):
1301-
Input cube for aggregation. If weights is given as :obj:`str`, try
1302-
to extract a cell measure with the corresponding name from this
1303-
cube. Otherwise, this argument is ignored.
1304-
1305-
Returns:
1306-
tuple. A tuple containing the updated keyword arguments and the
1307-
weights units.
1308-
1309-
"""
1310-
kwargs = dict(kwargs)
1311-
weights_units = None
1312-
1313-
if kwargs.get("weights") is not None:
1314-
weights = cls(kwargs["weights"], cube)
1315-
kwargs["weights"] = weights.array
1316-
weights_units = weights.units
1317-
1318-
return (kwargs, weights_units)
1319-
13201291

13211292
def create_weighted_aggregator_fn(aggregator_fn, axis, **kwargs):
13221293
"""Return an aggregator function that can explicitly handle weights.

lib/iris/cube.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3836,7 +3836,10 @@ def collapsed(self, coords, aggregator, **kwargs):
38363836
"""
38373837
# Update weights kwargs (if necessary) to handle different types of
38383838
# weights
3839-
(kwargs, weights_units) = _Weights.get_updated_kwargs(kwargs, self)
3839+
weights_info = None
3840+
if kwargs.get("weights") is not None:
3841+
weights_info = _Weights(kwargs["weights"], self)
3842+
kwargs["weights"] = weights_info.array
38403843

38413844
# Convert any coordinate names to coordinates
38423845
coords = self._as_list_of_coords(coords)
@@ -3984,7 +3987,7 @@ def collapsed(self, coords, aggregator, **kwargs):
39843987
collapsed_cube,
39853988
coords,
39863989
axis=collapse_axis,
3987-
_weights_units=weights_units,
3990+
_weights_units=getattr(weights_info, "units", None),
39883991
**kwargs,
39893992
)
39903993
result = aggregator.post_process(
@@ -4078,7 +4081,10 @@ def aggregated_by(
40784081
"""
40794082
# Update weights kwargs (if necessary) to handle different types of
40804083
# weights
4081-
(kwargs, weights_units) = _Weights.get_updated_kwargs(kwargs, self)
4084+
weights_info = None
4085+
if kwargs.get("weights") is not None:
4086+
weights_info = _Weights(kwargs["weights"], self)
4087+
kwargs["weights"] = weights_info.array
40824088

40834089
groupby_coords = []
40844090
dimension_to_groupby = None
@@ -4275,7 +4281,7 @@ def aggregated_by(
42754281
aggregateby_cube,
42764282
groupby_coords,
42774283
aggregate=True,
4278-
_weights_units=weights_units,
4284+
_weights_units=getattr(weights_info, "units", None),
42794285
**kwargs,
42804286
)
42814287
# Replace the appropriate coordinates within the aggregate-by cube.
@@ -4415,7 +4421,10 @@ def rolling_window(self, coord, aggregator, window, **kwargs):
44154421
"""
44164422
# Update weights kwargs (if necessary) to handle different types of
44174423
# weights
4418-
(kwargs, weights_units) = _Weights.get_updated_kwargs(kwargs, self)
4424+
weights_info = None
4425+
if kwargs.get("weights") is not None:
4426+
weights_info = _Weights(kwargs["weights"], self)
4427+
kwargs["weights"] = weights_info.array
44194428

44204429
coord = self._as_list_of_coords(coord)[0]
44214430

@@ -4502,7 +4511,7 @@ def rolling_window(self, coord, aggregator, window, **kwargs):
45024511
new_cube,
45034512
[coord],
45044513
action="with a rolling window of length %s over" % window,
4505-
_weights_units=weights_units,
4514+
_weights_units=getattr(weights_info, "units", None),
45064515
**kwargs,
45074516
)
45084517
# and perform the data transformation, generating weights first if

lib/iris/tests/test_analysis.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,35 +1814,13 @@ def test_init_with_cell_measure(self):
18141814
np.testing.assert_array_equal(weights.array, self.data)
18151815
assert weights.units == "m2"
18161816

1817-
def test_get_updated_kwargs_no_weights(self):
1818-
kwargs = {"test": [1, 2, 3]}
1819-
(new_kwargs, weights_units) = _Weights.get_updated_kwargs(
1820-
kwargs, self.cube
1821-
)
1822-
assert new_kwargs is not kwargs
1823-
assert new_kwargs == {"test": [1, 2, 3]}
1824-
assert weights_units is None
1825-
1826-
def test_get_updated_kwargs_weights_none(self):
1827-
kwargs = {"test": [1, 2, 3], "weights": None}
1828-
(new_kwargs, weights_units) = _Weights.get_updated_kwargs(
1829-
kwargs, self.cube
1830-
)
1831-
assert new_kwargs is not kwargs
1832-
assert new_kwargs == {"test": [1, 2, 3], "weights": None}
1833-
assert weights_units is None
1834-
1835-
def test_get_updated_kwargs_weights(self):
1836-
kwargs = {"test": [1, 2, 3], "weights": self.data}
1837-
(new_kwargs, weights_units) = _Weights.get_updated_kwargs(
1838-
kwargs, self.cube
1839-
)
1840-
assert new_kwargs is not kwargs
1841-
assert len(new_kwargs) == 2
1842-
assert new_kwargs["test"] == [1, 2, 3]
1843-
assert isinstance(new_kwargs["weights"], self.target_type)
1844-
assert new_kwargs["weights"] is self.data
1845-
assert weights_units == "1"
1817+
def test_init_with_list(self):
1818+
list_in = [0, 1, 2]
1819+
weights = _Weights(list_in, self.cube)
1820+
assert isinstance(weights.array, list)
1821+
assert isinstance(weights.units, cf_units.Unit)
1822+
assert weights.array is list_in
1823+
assert weights.units == "1"
18461824

18471825

18481826
class TestWeightsLazy(TestWeights):

0 commit comments

Comments
 (0)