Skip to content
Open
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
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Chronological list of authors
- Francesco Siciliani
- Akshit Boora
- Sumit Saraswat
- Dhrub Chatterjee


External code
Expand Down
3 changes: 3 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ Fixes
DSSP by porting upstream PyDSSP 0.9.1 fix (Issue #4913)

Enhancements
* Optimized boolean array indexing for trajectory readers by using
`np.flatnonzero` instead of `np.arange` allocation, significantly improving
performance and reducing memory overhead (Issue #1982)
* Add reading single-frame files from mmCIF, also known as PDBx format, using
crystallographic library `gemmi` as a backend (Issue #2367 and extension of
#4303, also solves #5089, PR #4712).
Expand Down
14 changes: 12 additions & 2 deletions package/MDAnalysis/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,21 @@ def __getitem__(self, frame):
frame = self._apply_limits(frame)
return self._read_frame_with_aux(frame)
elif isinstance(frame, (list, np.ndarray)):
if len(frame) != 0 and isinstance(frame[0], (bool, np.bool_)):
if (
isinstance(frame, np.ndarray) and np.issubdtype(frame.dtype, np.bool_)
) or (
len(frame) != 0 and isinstance(frame[0], (bool, np.bool_))
):
# Avoid having list of bools
frame = np.asarray(frame, dtype=bool)
if frame.ndim != 1 or len(frame) != len(self):
raise IndexError(
f"boolean index did not match indexed array along "
f"dimension 0; dimension is {len(self)} but "
f"corresponding boolean dimension is {len(frame)}"
)
# Convert bool array to int array
frame = np.arange(len(self))[frame]
frame = np.flatnonzero(frame)
return FrameIteratorIndices(self, frame)
elif isinstance(frame, slice):
start, stop, step = self.check_slice_indices(
Expand Down
31 changes: 31 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_reader_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,37 @@ def test_getitem_IE(self, sl1, idx2, reader):
with pytest.raises(IndexError):
partial_reader[idx2]

@pytest.mark.parametrize(
"mask",
[
[True, False],
[True] * 12,
np.array([True, False]),
np.ones(12, dtype=bool),
np.array([], dtype=bool),
np.ones((10, 1), dtype=bool),
],
)
def test_getitem_bool_length_mismatch(self, mask, reader):
with pytest.raises(IndexError, match="boolean index did not match"):
reader[mask]

@pytest.mark.parametrize(
"mask",
[
np.array([True, False] * 5),
np.array([False] * 9 + [True]),
np.array([True] + [False] * 9),
np.zeros(10, dtype=bool),
np.ones(10, dtype=bool),
],
)
def test_getitem_bool_array(self, mask, reader):
res = [ts.frame for ts in reader[mask]]
ref = self.reference[mask]
assert_equal(res, ref)
assert len(reader[mask]) == len(ref)


class _Single(_TestReader):
n_frames = 1
Expand Down
Loading