From 1e20db64dc93768f4be17ab15c6ba5951fb47a07 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Fri, 13 Nov 2020 21:30:40 +0000 Subject: [PATCH 1/4] efficiency: loop through fewer keys --- lib/iris/cube.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 3d0854355c..cb8a378721 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2182,25 +2182,18 @@ def _summary_coord_extra(self, coord, indent): extra = "" similar_coords = self.coords(coord.name()) if len(similar_coords) > 1: - # Find all the attribute keys - keys = set() - for similar_coord in similar_coords: - keys.update(similar_coord.attributes.keys()) - # Look for any attributes that vary + # Look for any attributes that vary. vary = set() - attributes = {} - for key in keys: + for key, value in coord.attributes.items(): for similar_coord in similar_coords: if key not in similar_coord.attributes: vary.add(key) break - value = similar_coord.attributes[key] - if attributes.setdefault(key, value) != value: + if similar_coord.attributes[key] != value: vary.add(key) break - keys = sorted(vary & set(coord.attributes.keys())) bits = [ - "{}={!r}".format(key, coord.attributes[key]) for key in keys + "{}={!r}".format(key, coord.attributes[key]) for key in vary ] if bits: extra = indent + ", ".join(bits) From aae15b9149d00117bcc2b6f3c23cf3fc77a1d542 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Fri, 13 Nov 2020 22:24:16 +0000 Subject: [PATCH 2/4] fix array bug; add test --- lib/iris/cube.py | 7 +++++-- lib/iris/tests/unit/cube/test_Cube.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index cb8a378721..d8f9992af0 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2189,11 +2189,14 @@ def _summary_coord_extra(self, coord, indent): if key not in similar_coord.attributes: vary.add(key) break - if similar_coord.attributes[key] != value: + if not np.array_equal( + similar_coord.attributes[key], value + ): vary.add(key) break + keys = sorted(vary) bits = [ - "{}={!r}".format(key, coord.attributes[key]) for key in vary + "{}={!r}".format(key, coord.attributes[key]) for key in keys ] if bits: extra = indent + ", ".join(bits) diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index 01dfe365b4..72bb761cb4 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -484,6 +484,16 @@ def test_ancillary_variable(self): ) self.assertEqual(cube.summary(), expected_summary) + def test_similar_coords(self): + coord1 = AuxCoord( + 42, long_name="foo", attributes=dict(bar=np.array([2, 5])) + ) + coord2 = coord1.copy() + coord2.attributes = dict(bar="baz") + for coord in [coord1, coord2]: + self.cube.add_aux_coord(coord) + self.assertIn("baz", self.cube.summary()) + class Test_is_compatible(tests.IrisTest): def setUp(self): From 19738be1a7a10d20b78707ffaacd4052809005f5 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Fri, 13 Nov 2020 22:33:57 +0000 Subject: [PATCH 3/4] add whatsnew --- docs/iris/src/whatsnew/3.0.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/iris/src/whatsnew/3.0.rst b/docs/iris/src/whatsnew/3.0.rst index 59f7ec8735..72ccdf5066 100644 --- a/docs/iris/src/whatsnew/3.0.rst +++ b/docs/iris/src/whatsnew/3.0.rst @@ -168,7 +168,11 @@ This document explains the changes made to Iris for this release Previously, the first tick label would occasionally be duplicated. This also removes the use of Matplotlib's deprecated ``IndexFormatter``. (:pull:`3857`) -* `@znicholls`_ fixed :meth:`~iris.quickplot._title` to only check ``units.is_time_reference`` if the ``units`` symbol is not used. (:pull:`3902`) +* `@znicholls`_ fixed :meth:`~iris.quickplot._title` to only check + ``units.is_time_reference`` if the ``units`` symbol is not used. (:pull:`3902`) + +* `@rcomer`_ fixed a bug whereby numpy array type attributes on a cube's + coordinates could prevent printing it. See :issue:`3921`. (:pull:`3922`) .. _whatsnew 3.0 changes: From fe5c6b3c851b6dd2404d79f878c732ce59d96174 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Sat, 14 Nov 2020 10:34:11 +0000 Subject: [PATCH 4/4] don't compare coord to itself --- lib/iris/cube.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index d8f9992af0..daffe11835 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2182,6 +2182,7 @@ def _summary_coord_extra(self, coord, indent): extra = "" similar_coords = self.coords(coord.name()) if len(similar_coords) > 1: + similar_coords.remove(coord) # Look for any attributes that vary. vary = set() for key, value in coord.attributes.items():