Skip to content

check_in_box(): returns atoms inside instead of outside the box #1049

Backlog

Description

@padix-key

check_in_box() is documented to return the indices of atoms outside the box, but it returns the indices of the atoms inside the box (the mask is not inverted).

import numpy as np
import biotite.structure as struc

atoms = struc.AtomArray(3)
# Atom 0 is inside the box, atoms 1 and 2 are outside
atoms.coord = np.array([[1, 1, 1], [20, 1, 1], [-1, 1, 1]])
atoms.box = np.eye(3) * 10
print(struc.check_in_box(atoms))

Actual output: [0]

Expected: [1 2]

Proposed fix

Invert the mask.
The condition fractions < 1 is kept, as it is consistent with move_inside_box(), which moves an atom at a fraction of exactly 1 to 0.
Instead the docstring is corrected from 0 <= a_i <= 1 to 0 <= a_i < 1.

--- a/src/biotite/structure/integrity.py
+++ b/src/biotite/structure/integrity.py
@@ def check_in_box(array: AtomArray[N]) -> NDArray1[K, np.integer]:
     Coordinates are outside the box, when they cannot be represented by
     a linear combination of the box vectors with scalar factors
-    :math:`0 \le a_i \le 1`.
+    :math:`0 \le a_i \lt 1`.
@@
     box = array.box
     fractions = coord_to_fraction(array.coord, box)
-    return np.where(((fractions >= 0) & (fractions < 1)).all(axis=-1))[0]
+    return np.where(~((fractions >= 0) & (fractions < 1)).all(axis=-1))[0]

Authored by Claude

Activity

  1. padix-key commented on Nov 26, 2026

    @padix-key
    MemberAuthor

    Triage: high priority.

    The function returns the opposite set of atoms from its documented validation contract. A caller using it to identify, reject, or repair out-of-box atoms will act on the wrong atoms, even for a simple orthogonal box. This is a systematic correctness failure in a validation utility, not a rare boundary-only discrepancy.

    Authored by Codex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions