Skip to content

Commit

Permalink
fix: allow matching nan values in annotations (e.g. when b_factors …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Croydon-Brixton authored Dec 12, 2024
1 parent 05e40cb commit 0acb143
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/biotite/structure/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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
-------
Expand All @@ -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

Expand Down

0 comments on commit 0acb143

Please sign in to comment.