iris icon indicating copy to clipboard operation
iris copied to clipboard

`WPERCENTILE` doesn't work with `rolling_window`

Open malininae opened this issue 1 year ago • 1 comments

🐛 Bug Report

How To Reproduce

Steps to reproduce the behaviour:

  1. I have a 1D temperature cube (cb) with only 'time' coordinate, the cb.shape is 10958 (30 years from 1991-01-01 to 2020-12-31).
  2. I tried running cb.rolling_window('time', iris.analysis.WPERCENTILE , window=5, percent=90, weights = np.arange(5)/5) I also tried with different windows, but it doesn't work either.

Error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/space/hall5/sitestore/eccc/crd/ccrn/users/emr001/miniconda3/envs/rapid_event_climex/lib/python3.10/site-packages/iris/cube.py", line 4504, in rolling_window
    data_result = aggregator.aggregate(
  File "/space/hall5/sitestore/eccc/crd/ccrn/users/emr001/miniconda3/envs/rapid_event_climex/lib/python3.10/site-packages/iris/analysis/__init__.py", line 803, in aggregate
    return self._base_aggregate(data, axis, lazy=False, **kwargs)
  File "/space/hall5/sitestore/eccc/crd/ccrn/users/emr001/miniconda3/envs/rapid_event_climex/lib/python3.10/site-packages/iris/analysis/__init__.py", line 766, in _base_aggregate
    return _Aggregator.aggregate(self, data, axis, **kwargs)
  File "/space/hall5/sitestore/eccc/crd/ccrn/users/emr001/miniconda3/envs/rapid_event_climex/lib/python3.10/site-packages/iris/analysis/__init__.py", line 600, in aggregate
    result = self.call_func(data, axis=axis, **kwargs)
  File "/space/hall5/sitestore/eccc/crd/ccrn/users/emr001/miniconda3/envs/rapid_event_climex/lib/python3.10/site-packages/iris/analysis/__init__.py", line 1571, in _weighted_percentile
    raise ValueError("_weighted_percentile: weights wrong shape.")
ValueError: _weighted_percentile: weights wrong shape.

Expected behaviour

According to documentation 'Weights matching the shape of the cube or the length of the window for rolling window operations. ', so it should have worked, cb.rolling_window('time', iris.analysis.MEAN, window=len(wghts), weights=wghts) works, so I suspect it is WPERCENTILE specific.

malininae avatar Feb 23 '24 01:02 malininae

Thanks for the report @malininae! This can be reproduced with:

import iris.cube
import numpy as np

cube = iris.cube.Cube(np.ones(42))
coord = iris.coords.DimCoord(range(42), long_name='foo')
cube.add_dim_coord(coord, 0)

cube.rolling_window('foo', iris.analysis.WPERCENTILE, window=5, percent=90,
                    weights=np.arange(5)/5)

The problem is that _weighted_percentile expects the data and weight arrays it receives to have the same shape, but in this case it is getting (38, 5) for the data and (5,) for the weights.

rcomer avatar Feb 23 '24 15:02 rcomer