From e3a05dff10388b0a7c25f447efacea2531d725c8 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Fri, 20 Dec 2019 16:19:25 +0000 Subject: [PATCH 1/2] Make '_ones_like' internal to 'pearsonr', and have it discard additional dim-metas. --- lib/iris/analysis/stats.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/iris/analysis/stats.py b/lib/iris/analysis/stats.py index ab0521be60..0634eea423 100644 --- a/lib/iris/analysis/stats.py +++ b/lib/iris/analysis/stats.py @@ -14,19 +14,6 @@ from iris.util import broadcast_to_shape -def _ones_like(cube): - """ - Return a copy of cube with the same mask, but all data values set to 1. - - The operation is non-lazy. - """ - ones_cube = cube.copy() - ones_cube.data = np.ones_like(cube.data) - ones_cube.rename("unknown") - ones_cube.units = 1 - return ones_cube - - def pearsonr( cube_a, cube_b, @@ -98,7 +85,21 @@ def pearsonr( if corr_coords is None: corr_coords = common_dim_coords - smaller_shape = cube_2.shape + def _ones_like(cube): + # Return a copy of cube with the same mask, but all data values set to 1. + # The operation is non-lazy. + # For safety we also discard any cell-measures and ancillary-variables, to + # avoid cube arithmetic possibly objecting to them, or inadvertently retaining + # them in the result where they might be inappropriate. + ones_cube = cube.copy() + ones_cube.data = np.ones_like(cube.data) + ones_cube.rename("unknown") + ones_cube.units = 1 + for cm in ones_cube.cell_measures(): + ones_cube.remove_cell_measure(cm) + for av in ones_cube.ancillary_variables(): + ones_cube.remove_ancillary_variable(av) + return ones_cube # Match up data masks if required. if common_mask: @@ -126,6 +127,7 @@ def pearsonr( dim_coords_2 = [coord.name() for coord in cube_2.dim_coords] # Broadcast weights to shape of cubes if necessary. + smaller_shape = cube_2.shape if weights is None or cube_1.shape == smaller_shape: weights_1 = weights weights_2 = weights From aee217b9c1e53342b3309eb0ff632d685dedaca4 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Fri, 20 Dec 2019 16:48:42 +0000 Subject: [PATCH 2/2] Set 'smaller_shape' before cube_2 may change. --- lib/iris/analysis/stats.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/iris/analysis/stats.py b/lib/iris/analysis/stats.py index 0634eea423..ba3ed2504c 100644 --- a/lib/iris/analysis/stats.py +++ b/lib/iris/analysis/stats.py @@ -78,6 +78,8 @@ def pearsonr( cube_1 = cube_a cube_2 = cube_b + smaller_shape = cube_2.shape + dim_coords_1 = [coord.name() for coord in cube_1.dim_coords] dim_coords_2 = [coord.name() for coord in cube_2.dim_coords] common_dim_coords = list(set(dim_coords_1) & set(dim_coords_2)) @@ -127,7 +129,6 @@ def _ones_like(cube): dim_coords_2 = [coord.name() for coord in cube_2.dim_coords] # Broadcast weights to shape of cubes if necessary. - smaller_shape = cube_2.shape if weights is None or cube_1.shape == smaller_shape: weights_1 = weights weights_2 = weights