From 0acb143084f8b7fed554dde24ed99f5e24a3087d Mon Sep 17 00:00:00 2001 From: Simon Mathis Date: Thu, 12 Dec 2024 10:47:50 +0000 Subject: [PATCH] fix: allow matching `nan` values in annotations (e.g. when b_factors are set to `nan`) (#714) * fix: allow matching `nan` values in annotations (e.g. when b_factors are set to `nan`) * chore: ruff * fix: numpy type-casting issue for string arrays * chore: ruff & PR comments --- src/biotite/structure/atoms.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/biotite/structure/atoms.py b/src/biotite/structure/atoms.py index 2245e0cfa..0be02e172 100644 --- a/src/biotite/structure/atoms.py +++ b/src/biotite/structure/atoms.py @@ -233,7 +233,7 @@ def _del_element(self, index): else: raise TypeError(f"Index must be integer, not '{type(index).__name__}'") - def equal_annotations(self, item): + def equal_annotations(self, item, equal_nan=True): """ Check, if this object shares equal annotation arrays with the given :class:`AtomArray` or :class:`AtomArrayStack`. @@ -242,6 +242,8 @@ def equal_annotations(self, item): ---------- item : AtomArray or AtomArrayStack The object to compare the annotation arrays with. + equal_nan: bool + Whether to count `nan` values as equal. Default: True. Returns ------- @@ -253,7 +255,18 @@ def equal_annotations(self, item): if not self.equal_annotation_categories(item): return False for name in self._annot: - if not np.array_equal(self._annot[name], item._annot[name]): + # ... allowing `nan` values causes type-casting, which is + # only possible for floating-point arrays + allow_nan = ( + equal_nan + if np.issubdtype(self._annot[name].dtype, np.floating) + else False + ) + if not np.array_equal( + self._annot[name], + item._annot[name], + equal_nan=allow_nan, + ): return False return True