MCPcopy
hub / github.com/mne-tools/mne-python / object_diff

Function object_diff

mne/utils/numerics.py:760–850  ·  view source on GitHub ↗

Compute all differences between two python variables. Parameters ---------- a : object Currently supported: class, dict, list, tuple, ndarray, int, str, bytes, float, StringIO, BytesIO. b : object Must be same type as ``a``. pre : str String to pr

(a, b, pre="", *, allclose=False)

Source from the content-addressed store, hash-verified

758
759
760def object_diff(a, b, pre="", *, allclose=False):
761 """Compute all differences between two python variables.
762
763 Parameters
764 ----------
765 a : object
766 Currently supported: class, dict, list, tuple, ndarray,
767 int, str, bytes, float, StringIO, BytesIO.
768 b : object
769 Must be same type as ``a``.
770 pre : str
771 String to prepend to each line.
772 allclose : bool
773 If True (default False), use assert_allclose.
774
775 Returns
776 -------
777 diffs : str
778 A string representation of the differences.
779 """
780 pd = _check_pandas_installed(strict=False)
781 out = ""
782 if type(a) is not type(b):
783 # Deal with NamedInt and NamedFloat
784 for sub in (int, float):
785 if isinstance(a, sub) and isinstance(b, sub):
786 break
787 else:
788 return f"{pre} type mismatch ({type(a)}, {type(b)})\n"
789 if inspect.isclass(a):
790 if inspect.isclass(b) and a != b:
791 return f"{pre} class mismatch ({a}, {b})\n"
792 elif isinstance(a, dict):
793 k1s = _sort_keys(a)
794 k2s = _sort_keys(b)
795 m1 = set(k2s) - set(k1s)
796 if len(m1):
797 out += pre + f" left missing keys {m1}\n"
798 for key in k1s:
799 if key not in k2s:
800 out += pre + f" right missing key {key}\n"
801 else:
802 out += object_diff(
803 a[key], b[key], pre=(pre + f"[{repr(key)}]"), allclose=allclose
804 )
805 elif isinstance(a, list | tuple):
806 if len(a) != len(b):
807 out += pre + f" length mismatch ({len(a)}, {len(b)})\n"
808 else:
809 for ii, (xx1, xx2) in enumerate(zip(a, b)):
810 out += object_diff(xx1, xx2, pre + f"[{ii}]", allclose=allclose)
811 elif isinstance(a, float):
812 if not _array_equal_nan(a, b, allclose):
813 out += pre + f" value mismatch ({a}, {b})\n"
814 elif isinstance(a, str | int | bytes | np.generic):
815 if a != b:
816 out += pre + f" value mismatch ({a}, {b})\n"
817 elif a is None:

Callers 15

test_hashFunction · 0.90
test_fine_cal_ioFunction · 0.90
test_csd_fifFunction · 0.90
test_cross_talkFunction · 0.90
test_anonymize_with_ioFunction · 0.90
test_io_egi_evokeds_mffFunction · 0.90
check_info_fieldsFunction · 0.90
_test_raw_readerFunction · 0.90
test_parse_impedanceFunction · 0.90
test_io_surfaceFunction · 0.90
test_epochs_io_projFunction · 0.90

Calls 6

_check_pandas_installedFunction · 0.85
setFunction · 0.85
_array_equal_nanFunction · 0.85
getvalueMethod · 0.80
_sort_keysFunction · 0.70
__getstate__Method · 0.45

Tested by 15

test_hashFunction · 0.72
test_fine_cal_ioFunction · 0.72
test_csd_fifFunction · 0.72
test_cross_talkFunction · 0.72
test_anonymize_with_ioFunction · 0.72
test_io_egi_evokeds_mffFunction · 0.72
_test_raw_readerFunction · 0.72
test_parse_impedanceFunction · 0.72
test_io_surfaceFunction · 0.72
test_epochs_io_projFunction · 0.72