From 2efd8984f8bbe08738e02f0dde70b3b9227b2275 Mon Sep 17 00:00:00 2001 From: Will Benfold Date: Fri, 22 Oct 2021 12:07:47 +0100 Subject: [PATCH 1/4] Move where the type of mismatch is declared out of _coordinate_differences (which doesn't know) to _match (which does) --- lib/iris/_concatenate.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/iris/_concatenate.py b/lib/iris/_concatenate.py index ffa290d1a1..29076a549d 100644 --- a/lib/iris/_concatenate.py +++ b/lib/iris/_concatenate.py @@ -452,11 +452,11 @@ def _coordinate_differences(self, other, attr): between `self` and `other`. Returns: - Tuple of a descriptive error message and the names of coordinates - that differ between `self` and `other`. + Tuple of the names of attributes that differ between `self` and + `other`. """ - # Set up {name: coord_metadata} dictionaries. + # Set up {name: attribute} dictionaries. self_dict = {x.name(): x for x in getattr(self, attr)} other_dict = {x.name(): x for x in getattr(other, attr)} if len(self_dict) == 0: @@ -466,7 +466,7 @@ def _coordinate_differences(self, other, attr): self_names = sorted(self_dict.keys()) other_names = sorted(other_dict.keys()) - # Compare coord metadata. + # Compare coord attributes. if len(self_names) != len(other_names) or self_names != other_names: result = ("", ", ".join(self_names), ", ".join(other_names)) else: @@ -476,7 +476,6 @@ def _coordinate_differences(self, other, attr): if self_value != other_value: diff_names.append(self_key) result = ( - " metadata", ", ".join(diff_names), ", ".join(diff_names), ) @@ -522,29 +521,39 @@ def match(self, other, error_on_mismatch): if self.dim_metadata != other.dim_metadata: differences = self._coordinate_differences(other, "dim_metadata") msgs.append( - msg_template.format("Dimension coordinates", *differences) + msg_template.format( + "Dimension coordinates", " metadata", *differences + ) ) # Check aux coordinates. if self.aux_metadata != other.aux_metadata: differences = self._coordinate_differences(other, "aux_metadata") msgs.append( - msg_template.format("Auxiliary coordinates", *differences) + msg_template.format( + "Auxiliary coordinates", " metadata", *differences + ) ) # Check cell measures. if self.cm_metadata != other.cm_metadata: differences = self._coordinate_differences(other, "cm_metadata") - msgs.append(msg_template.format("Cell measures", *differences)) + msgs.append( + msg_template.format("Cell measures", " metadata", *differences) + ) # Check ancillary variables. if self.av_metadata != other.av_metadata: differences = self._coordinate_differences(other, "av_metadata") msgs.append( - msg_template.format("Ancillary variables", *differences) + msg_template.format( + "Ancillary variables", " metadata", *differences + ) ) # Check scalar coordinates. if self.scalar_coords != other.scalar_coords: differences = self._coordinate_differences(other, "scalar_coords") msgs.append( - msg_template.format("Scalar coordinates", *differences) + msg_template.format( + "Scalar coordinates", " values or metadata", *differences + ) ) # Check ndim. if self.ndim != other.ndim: From dcded9870d361dff57db7e0021d23d0f7103e4a2 Mon Sep 17 00:00:00 2001 From: Will Benfold Date: Fri, 22 Oct 2021 12:24:06 +0100 Subject: [PATCH 2/4] Change method so that a reason is passed in when necessary --- lib/iris/_concatenate.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/iris/_concatenate.py b/lib/iris/_concatenate.py index 29076a549d..f079dc0448 100644 --- a/lib/iris/_concatenate.py +++ b/lib/iris/_concatenate.py @@ -437,7 +437,7 @@ def meta_key_func(dm): av_and_dims = _CoordAndDims(av, tuple(dims)) self.ancillary_variables_and_dims.append(av_and_dims) - def _coordinate_differences(self, other, attr): + def _coordinate_differences(self, other, attr, reason="metadata"): """ Determine the names of the coordinates that differ between `self` and `other` for a coordinate attribute on a _CubeSignature. @@ -451,6 +451,10 @@ def _coordinate_differences(self, other, attr): The _CubeSignature attribute within which differences exist between `self` and `other`. + * reason (string): + The reason to give for mismatch (function is normally, but not + always, testing metadata) + Returns: Tuple of the names of attributes that differ between `self` and `other`. @@ -476,6 +480,7 @@ def _coordinate_differences(self, other, attr): if self_value != other_value: diff_names.append(self_key) result = ( + " " + reason, ", ".join(diff_names), ", ".join(diff_names), ) @@ -521,39 +526,31 @@ def match(self, other, error_on_mismatch): if self.dim_metadata != other.dim_metadata: differences = self._coordinate_differences(other, "dim_metadata") msgs.append( - msg_template.format( - "Dimension coordinates", " metadata", *differences - ) + msg_template.format("Dimension coordinates", *differences) ) # Check aux coordinates. if self.aux_metadata != other.aux_metadata: differences = self._coordinate_differences(other, "aux_metadata") msgs.append( - msg_template.format( - "Auxiliary coordinates", " metadata", *differences - ) + msg_template.format("Auxiliary coordinates", *differences) ) # Check cell measures. if self.cm_metadata != other.cm_metadata: differences = self._coordinate_differences(other, "cm_metadata") - msgs.append( - msg_template.format("Cell measures", " metadata", *differences) - ) + msgs.append(msg_template.format("Cell measures", *differences)) # Check ancillary variables. if self.av_metadata != other.av_metadata: differences = self._coordinate_differences(other, "av_metadata") msgs.append( - msg_template.format( - "Ancillary variables", " metadata", *differences - ) + msg_template.format("Ancillary variables", *differences) ) # Check scalar coordinates. if self.scalar_coords != other.scalar_coords: - differences = self._coordinate_differences(other, "scalar_coords") + differences = self._coordinate_differences( + other, "scalar_coords", reason="values or metadata" + ) msgs.append( - msg_template.format( - "Scalar coordinates", " values or metadata", *differences - ) + msg_template.format("Scalar coordinates", *differences) ) # Check ndim. if self.ndim != other.ndim: From 04b9a98f74318f5dc9fa979d15513532f2a2f943 Mon Sep 17 00:00:00 2001 From: Will Benfold Date: Fri, 22 Oct 2021 12:27:49 +0100 Subject: [PATCH 3/4] Improve docstring --- lib/iris/_concatenate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/_concatenate.py b/lib/iris/_concatenate.py index f079dc0448..5debc452ee 100644 --- a/lib/iris/_concatenate.py +++ b/lib/iris/_concatenate.py @@ -456,8 +456,8 @@ def _coordinate_differences(self, other, attr, reason="metadata"): always, testing metadata) Returns: - Tuple of the names of attributes that differ between `self` and - `other`. + Tuple of a descriptive error message and the names of attributes + that differ between `self` and `other`. """ # Set up {name: attribute} dictionaries. From 2d53161af562b513bb36438f47ed315b292bac65 Mon Sep 17 00:00:00 2001 From: Will Benfold Date: Fri, 22 Oct 2021 12:30:17 +0100 Subject: [PATCH 4/4] Update corresponding test --- lib/iris/tests/unit/concatenate/test_concatenate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/concatenate/test_concatenate.py b/lib/iris/tests/unit/concatenate/test_concatenate.py index f71f9c4596..2af568f077 100644 --- a/lib/iris/tests/unit/concatenate/test_concatenate.py +++ b/lib/iris/tests/unit/concatenate/test_concatenate.py @@ -154,7 +154,7 @@ def test_scalar_coords_metadata_difference_message(self): cube_1 = self.cube cube_2 = cube_1.copy() cube_2.coord("height").long_name = "alice" - exc_regexp = "Scalar coordinates metadata differ: .* != .*" + exc_regexp = "Scalar coordinates values or metadata differ: .* != .*" with self.assertRaisesRegex(ConcatenateError, exc_regexp): _ = concatenate([cube_1, cube_2], True)