Skip to content
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

Fix attribute array comparison #6181

Merged
merged 11 commits into from
Oct 25, 2024
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ This document explains the changes made to Iris for this release
attribute. Also made cell_method string parsing more lenient w.r.t.
whitespace. (:pull:`6083`)

#. `@ukmo-ccbunney`_ fixed comparison of cubes with array type attributes;
fixes :issue:`6027` (:pull:`6181`)

💣 Incompatible Changes
=======================

Expand Down
4 changes: 3 additions & 1 deletion lib/iris/_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ def _defn_msgs(self, other_defn):
diff_attrs = [
repr(key[1])
for key in attrs_1
if np.all(attrs_1[key] != attrs_2[key])
if not np.array_equal(
np.array(attrs_1[key], ndmin=1), np.array(attrs_2[key], ndmin=1)
)
]
diff_attrs = ", ".join(sorted(diff_attrs))
msgs.append(
Expand Down
9 changes: 4 additions & 5 deletions lib/iris/common/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Any

import cf_units
import numpy as np

import iris.std_names

Expand Down Expand Up @@ -104,11 +105,9 @@ def __eq__(self, other):
match = set(self.keys()) == set(other.keys())
if match:
for key, value in self.items():
match = value == other[key]
try:
match = bool(match)
except ValueError:
match = match.all()
match = np.array_equal(
np.array(value, ndmin=1), np.array(other[key], ndmin=1)
)
if not match:
break
return match
Expand Down
10 changes: 10 additions & 0 deletions lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ def test___eq___numpy(self):
right = LimitedAttributeDict(**values)
self.assertEqual(left, right)
self.assertEqual(left, values)

values = dict(one=np.arange(1), two=np.arange(1), three=np.arange(1))
left = LimitedAttributeDict(dict(one=0, two=0, three=0))
right = LimitedAttributeDict(**values)
self.assertEqual(left, right)
self.assertEqual(left, values)

# Test inequality:
values = dict(one=np.arange(1), two=np.arange(2), three=np.arange(3))
left = LimitedAttributeDict(**values)
right = LimitedAttributeDict(
one=np.arange(3), two=np.arange(2), three=np.arange(1)
)
self.assertNotEqual(left, right)
self.assertNotEqual(values, right)

def test___setitem__(self):
for key in self.forbidden_keys:
item = LimitedAttributeDict()
Expand Down
Loading