Skip to content

Commit

Permalink
Merge pull request #4656 from MattTDavies/atomgroup_indexing
Browse files Browse the repository at this point in the history
Fixed high dimensional GroupBase indexing.
  • Loading branch information
richardjgowers authored Aug 22, 2024
2 parents 481e36a + 9b83ae6 commit 326698d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Chronological list of authors
- Kurt McKee
- Fabian Zills
- Laksh Krishna Sharma

- Matthew Davies


External code
Expand Down
1 change: 1 addition & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The rules for this file:
* 2.8.0

Fixes
* Catch higher dimensional indexing in GroupBase (Issue #4647)
* Do not raise an Error reading H5MD files with datasets like
`observables/<particle>/<property>` (part of Issue #4598, PR #4615)
* Fix failure in double-serialization of TextIOPicklable file reader.
Expand Down
6 changes: 6 additions & 0 deletions package/MDAnalysis/core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,12 @@ def __getitem__(self, item):
# hack to make lists into numpy arrays
# important for boolean slicing
item = np.array(item)

if isinstance(item, np.ndarray) and item.ndim > 1:
# disallow high dimensional indexing.
# this doesnt stop the underlying issue
raise IndexError('Group index must be 1d')

# We specify _derived_class instead of self.__class__ to allow
# subclasses, such as UpdatingAtomGroup, to control the class
# resulting from slicing.
Expand Down
7 changes: 7 additions & 0 deletions testsuite/MDAnalysisTests/core/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,13 @@ def test_index_advancedslice(self, universe):
"an AtomGroup")
assert_equal(ag[1], ag[-1], "advanced slicing does not preserve order")

def test_2d_indexing_caught(self, universe):
u = universe
index_2d = [[1, 2, 3],
[4, 5, 6]]
with pytest.raises(IndexError):
u.atoms[index_2d]

@pytest.mark.parametrize('sel', (np.array([True, False, True]),
[True, False, True]))
def test_boolean_indexing_2(self, universe, sel):
Expand Down

0 comments on commit 326698d

Please sign in to comment.