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 @@ -270,6 +270,7 @@ Chronological list of authors
2026
- Mohammad Ayaan
- Khushi Phougat
- Akshat Raj

External code
-------------
Expand Down
9 changes: 8 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ The rules for this file:
??/??/?? IAlibay, orbeckst, marinegor, tylerjereddy, ljwoods2, marinegor,
spyke7, talagayev, tanii1125, BradyAJohnston

Fixes
* Universe.empty now raises a ValueError if atom_resindex or residue_segindex
contains out-of-bounds indices (Issue #5223)

02/11/2026 IAlibay, orbeckst, marinegor, tylerjereddy, ljwoods2, marinegor,
spyke7, talagayev, tanii1125, BradyAJohnston, Akshat Raj

* 2.11.0

Fixes
Expand Down Expand Up @@ -3610,4 +3617,4 @@ Testsuite
licenses

11/12/07 naveen
* prepared for release outside lab
* prepared for release outside lab
20 changes: 20 additions & 0 deletions package/MDAnalysis/core/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,26 @@ def empty(
UserWarning,
)

if atom_resindex is not None:
atom_resindex = np.asarray(atom_resindex)
if np.any(atom_resindex >= n_residues) or np.any(
atom_resindex < 0
):
raise ValueError(
"atom_resindex contains invalid residue indices."
"All values must be between 0 and n_residues-1."
)

if residue_segindex is not None:
residue_segindex = np.asarray(residue_segindex)
if np.any(residue_segindex >= n_segments) or np.any(
residue_segindex < 0
):
raise ValueError(
"residue_segindex contains invalid segment indices. "
"All values must be between 0 and n_segments-1."
)

if residue_segindex is None and n_segments > 1:
warnings.warn(
"Segments specified but no segment_resindex given. "
Expand Down
5 changes: 4 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_helix_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ def zigzag():
"""
n_atoms = 100
u = mda.Universe.empty(
100, atom_resindex=np.arange(n_atoms), trajectory=True
100,
atom_resindex=np.arange(n_atoms),
n_residues=n_atoms,
trajectory=True,
)
xyz = np.array(
list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def universe():
n_residues=n_residues,
atom_resindex=[0, 0, 0, 1, 1, 1, 2, 2, 3, 3],
residue_segindex=[0, 0, 1, 1],
n_segments=2,
trajectory=True, # necessary for adding coordinates
)

Expand Down
20 changes: 20 additions & 0 deletions testsuite/MDAnalysisTests/core/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1867,3 +1867,23 @@ def test_no_affect_custom_attrs(self, good):
sorted([each.attrname for each in original_attrs]),
sorted([each.attrname for each in good._topology.attrs]),
)

def test_empty_universe_bounds(self):
with pytest.raises(
ValueError, match="atom_resindex contains invalid residue indices."
):
mda.Universe.empty(
n_atoms=2, n_residues=2, atom_resindex=[0, 2], trajectory=True
)

with pytest.raises(
ValueError,
match="residue_segindex contains invalid segment indices.",
):
mda.Universe.empty(
n_atoms=2,
n_residues=2,
n_segments=2,
atom_resindex=[0, 1],
residue_segindex=[0, 2],
)
Loading