Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/record-equality.rst
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 21 additions & 8 deletions src/wfdb/io/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_record_equality.py
Original file line number Diff line number Diff line change
@@ -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()
Loading