-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why iris.cube.Cube.collapsed requires weights with full shape? #3707
Comments
I can't answer the specific question, but if you use iris.util.broadcast_to_shape, it claims to make the new array out of multiple views of the input array. Does this help with the memory footprint issue? |
We already use Still, using 1D weights seems like a common case and it would be nice to allow it. Would the following change be acceptable? diff --git a/lib/iris/cube.py b/lib/iris/cube.py
index fec68c57..f1fe313b 100644
--- a/lib/iris/cube.py
+++ b/lib/iris/cube.py
@@ -3957,8 +3957,9 @@ bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00)
unrolled_data = np.transpose(self.data, dims).reshape(new_shape)
# Perform the same operation on the weights if applicable
- if kwargs.get("weights") is not None:
- weights = kwargs["weights"].view()
+ weights = kwargs.get("weights")
+ if weights is not None and weights.ndim > 1:
+ weights = weights.view()
kwargs["weights"] = np.transpose(weights, dims).reshape(
new_shape
) |
Bump. This is a simple change and it would be useful to have it. Any thoughts on this? |
If we're going to get this into Iris 3.0 we need to move quick ! |
The scope of this proposed change is still rather limited, and doesn't do all that we might. It allows us to pass 1D weights matching a single "collapse dimension". It still can't broadcast any multi-dimensional weights, even if the dims match by normal array rules So, ideally, I feel we should be able to pass weights matching the shape of the collapsed-dims (not just the whole cube), or the flattened equivalent.
I assume this is more or less why you are using |
It does make sense (with question mark about the flattened array case), but these cases are also less common and not directly supported at the numpy layer (would require
Might as well in the interest of getting this in quickly. |
How does #3943 look ? |
Looks all good to me 👍 |
This is linked to #3943, which was merged. So I believe it should be closed. |
We are looking at ways to reduce memory footprint of our iris code in https://github.com/metoppv/improver and are somewhat stuck on calculating weighted averages with
iris.cube.Cube.collapsed
. It looks like Iris insists on weights having the same shape as the collapsed cube even though the underlyingnumpy.ma.average
iniris.analysis.MEAN
can happily work with 1D array of weights. Why this restriction?The text was updated successfully, but these errors were encountered: