From f431cfc7d3fb9f125e005cf3f75bbe076c656d8c Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Fri, 27 Nov 2026 14:01:53 -0500 Subject: [PATCH] Return false for unequal Record signal arrays --- changelog.d/record-equality.rst | 3 +++ src/wfdb/io/record.py | 29 +++++++++++++++++++------- tests/test_record_equality.py | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 changelog.d/record-equality.rst create mode 100644 tests/test_record_equality.py diff --git a/changelog.d/record-equality.rst b/changelog.d/record-equality.rst new file mode 100644 index 00000000..3c698dc6 --- /dev/null +++ b/changelog.d/record-equality.rst @@ -0,0 +1,3 @@ +**Return false for records with different signal values** + Comparing ``Record`` objects with unequal uniform or expanded signal arrays + now returns ``False`` instead of raising an exception. diff --git a/src/wfdb/io/record.py b/src/wfdb/io/record.py index 533a4b67..032e75f6 100644 --- a/src/wfdb/io/record.py +++ b/src/wfdb/io/record.py @@ -892,6 +892,9 @@ def __eq__(self, other, verbose=False): Determines if the objects are equal (True) or not equal (False). """ + if not isinstance(other, Record): + return False + # Read provenance does not affect whether record contents are equal. read_metadata = {"_applied_skew", "source_segments"} att1 = {k: v for k, v in self.__dict__.items() if k not in read_metadata} @@ -913,14 +916,24 @@ def __eq__(self, other, verbose=False): if isinstance(v1, np.ndarray): # Necessary for nans - np.testing.assert_array_equal(v1, v2) - elif ( - isinstance(v1, list) - and len(v1) == len(v2) - and all(isinstance(e, np.ndarray) for e in v1) - ): - for e1, e2 in zip(v1, v2): - np.testing.assert_array_equal(e1, e2) + try: + np.testing.assert_array_equal(v1, v2) + except AssertionError: + if verbose: + print("Mismatch in attribute: %s" % k, v1, v2) + return False + elif isinstance(v1, list) and any(isinstance(e, np.ndarray) for e in v1): + if len(v1) != len(v2) or not all(isinstance(e, np.ndarray) for e in v2): + if verbose: + print("Mismatch in attribute: %s" % k, v1, v2) + return False + try: + for e1, e2 in zip(v1, v2): + np.testing.assert_array_equal(e1, e2) + except AssertionError: + if verbose: + print("Mismatch in attribute: %s" % k, v1, v2) + return False else: if v1 != v2: if verbose: diff --git a/tests/test_record_equality.py b/tests/test_record_equality.py new file mode 100644 index 00000000..923cd007 --- /dev/null +++ b/tests/test_record_equality.py @@ -0,0 +1,37 @@ +"""Equality behavior for records with loaded signal arrays.""" + +import numpy as np + +import wfdb + + +def test_record_equality_returns_false_for_different_uniform_signals(): + """A difference in loaded values is an unequal record, not an error.""" + first = wfdb.Record(d_signal=np.array([[1], [2]])) + second = wfdb.Record(d_signal=np.array([[1], [3]])) + + assert first != second + assert not first.__eq__(second, verbose=True) + + +def test_record_equality_preserves_nan_comparison(): + """Corresponding missing physical samples still compare equal.""" + first = wfdb.Record(p_signal=np.array([[1.0], [np.nan]])) + second = wfdb.Record(p_signal=np.array([[1.0], [np.nan]])) + + assert first == second + + +def test_record_equality_handles_expanded_signal_mismatch(): + """Expanded arrays with different values or counts compare unequal.""" + first = wfdb.Record(e_d_signal=[np.array([1, 2]), np.array([3])]) + changed = wfdb.Record(e_d_signal=[np.array([1, 2]), np.array([4])]) + missing = wfdb.Record(e_d_signal=[np.array([1, 2])]) + + assert first != changed + assert first != missing + + +def test_record_equality_with_other_type_is_false(): + """An unrelated object is not a record with equal contents.""" + assert wfdb.Record() != object()