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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster trivial equality checks for coordinates and arrays #5691

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ This document explains the changes made to Iris for this release
:func:`~iris.analysis.stats.pearsonr` so it preserves lazy data in all cases
and also runs a little faster. (:pull:`5638`)

#. `@bouweandela`_ made comparing coordinates and arrays to themselves faster. (:pull:`5691`)


馃敟 Deprecations
===============
Expand Down
3 changes: 3 additions & 0 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@ def __repr__(self):
return self.summary(shorten=True)

def __eq__(self, other):
if other is self:
return True

# Note: this method includes bounds handling code, but it only runs
# within Coord type instances, as only these allow bounds to be set.

Expand Down
7 changes: 5 additions & 2 deletions lib/iris/tests/unit/util/test_array_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ def test_string_arrays_0d_and_scalar(self):
self.assertFalse(array_equal(array_a, "foobar."))

def test_nan_equality_nan_ne_nan(self):
array = np.array([1.0, np.nan, 2.0, np.nan, 3.0])
self.assertFalse(array_equal(array, array))
array_a = np.array([1.0, np.nan, 2.0, np.nan, 3.0])
array_b = array_a.copy()
self.assertFalse(array_equal(array_a, array_a))
self.assertFalse(array_equal(array_a, array_b))

def test_nan_equality_nan_naneq_nan(self):
array_a = np.array([1.0, np.nan, 2.0, np.nan, 3.0])
array_b = np.array([1.0, np.nan, 2.0, np.nan, 3.0])
self.assertTrue(array_equal(array_a, array_a, withnans=True))
self.assertTrue(array_equal(array_a, array_b, withnans=True))

def test_nan_equality_nan_nanne_a(self):
Expand Down
2 changes: 2 additions & 0 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ def array_equal(array1, array2, withnans=False):
This function maintains laziness when called; it does not realise data.
See more at :doc:`/userguide/real_and_lazy_data`.
"""
if withnans and (array1 is array2):
return True

def normalise_array(array):
if not is_lazy_data(array):
Expand Down