From 200849589450ba8681d472ea4d79a2969be99cf0 Mon Sep 17 00:00:00 2001 From: Akshat-Raj Date: Wed, 11 Feb 2026 02:44:41 +0530 Subject: [PATCH 1/6] adding guards to index assignment in Universe creation --- package/MDAnalysis/core/universe.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/package/MDAnalysis/core/universe.py b/package/MDAnalysis/core/universe.py index 4b9ec247bd..1bab7fb881 100644 --- a/package/MDAnalysis/core/universe.py +++ b/package/MDAnalysis/core/universe.py @@ -745,6 +745,22 @@ 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. " From cbaefcb4c03559b23d0c746fa43b861004987a54 Mon Sep 17 00:00:00 2001 From: Akshat-Raj Date: Wed, 11 Feb 2026 02:49:56 +0530 Subject: [PATCH 2/6] adding my name to package/authors --- package/AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/package/AUTHORS b/package/AUTHORS index 973f1dd794..89e5b96bb6 100644 --- a/package/AUTHORS +++ b/package/AUTHORS @@ -270,6 +270,7 @@ Chronological list of authors 2026 - Mohammad Ayaan - Khushi Phougat + - Akshat Raj External code ------------- From 75c084de1667d074fb8f81a3a360589793cbd52c Mon Sep 17 00:00:00 2001 From: Akshat-Raj Date: Wed, 11 Feb 2026 02:58:42 +0530 Subject: [PATCH 3/6] adding changes to changelog --- package/CHANGELOG | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 7ac8f41b18..4d085bd073 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -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 @@ -3610,4 +3617,4 @@ Testsuite licenses 11/12/07 naveen - * prepared for release outside lab + * prepared for release outside lab \ No newline at end of file From 3be73b74f54efb03d043ce12d140b70e0d168395 Mon Sep 17 00:00:00 2001 From: Akshat-Raj Date: Wed, 11 Feb 2026 03:12:42 +0530 Subject: [PATCH 4/6] adding tests --- testsuite/MDAnalysisTests/core/test_universe.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/testsuite/MDAnalysisTests/core/test_universe.py b/testsuite/MDAnalysisTests/core/test_universe.py index 15ff1aeb86..b8385c7cd2 100644 --- a/testsuite/MDAnalysisTests/core/test_universe.py +++ b/testsuite/MDAnalysisTests/core/test_universe.py @@ -1867,3 +1867,10 @@ 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]) \ No newline at end of file From adb8029cf700940ed4b03d70f07cc71c24c0f606 Mon Sep 17 00:00:00 2001 From: Akshat-Raj Date: Wed, 11 Feb 2026 03:29:32 +0530 Subject: [PATCH 5/6] Applying black formatting --- package/MDAnalysis/core/universe.py | 16 ++++++++------ .../MDAnalysisTests/core/test_universe.py | 21 +++++++++++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/package/MDAnalysis/core/universe.py b/package/MDAnalysis/core/universe.py index 1bab7fb881..68ce3b4f05 100644 --- a/package/MDAnalysis/core/universe.py +++ b/package/MDAnalysis/core/universe.py @@ -747,19 +747,23 @@ def empty( 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): + if np.any(atom_resindex >= n_residues) or np.any( + atom_resindex < 0 + ): raise ValueError( - "atom_resindex contains invalid residue indices. " + "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): + 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( diff --git a/testsuite/MDAnalysisTests/core/test_universe.py b/testsuite/MDAnalysisTests/core/test_universe.py index b8385c7cd2..58a7623dd3 100644 --- a/testsuite/MDAnalysisTests/core/test_universe.py +++ b/testsuite/MDAnalysisTests/core/test_universe.py @@ -1869,8 +1869,21 @@ def test_no_affect_custom_attrs(self, good): ) 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="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]) \ No newline at end of file + 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], + ) From 9c3df62c31836b3debb8a222fa9ab2acc0acc836 Mon Sep 17 00:00:00 2001 From: Akshat-Raj Date: Wed, 11 Feb 2026 18:04:21 +0530 Subject: [PATCH 6/6] fixing ci errors --- testsuite/MDAnalysisTests/analysis/test_helix_analysis.py | 5 ++++- .../MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_helix_analysis.py b/testsuite/MDAnalysisTests/analysis/test_helix_analysis.py index e8e50717a2..c298dcb976 100644 --- a/testsuite/MDAnalysisTests/analysis/test_helix_analysis.py +++ b/testsuite/MDAnalysisTests/analysis/test_helix_analysis.py @@ -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( diff --git a/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py b/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py index 59b090c549..0e056802c0 100644 --- a/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py +++ b/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py @@ -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 )