Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frankenatom fix #4692

Merged
merged 4 commits into from
Sep 7, 2024
Merged
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
4 changes: 2 additions & 2 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ The rules for this file:
??/??/?? IAlibay, HeetVekariya, marinegor, lilyminium, RMeli,
ljwoods2, aditya292002, pstaerk, PicoCentauri, BFedder,
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, orionarcher,
yuxuanzhuang, PythonFZ, laksh-krishna-sharma, orbeckst
yuxuanzhuang, PythonFZ, laksh-krishna-sharma, orbeckst, MattTDavies

* 2.8.0

Fixes
* Catch higher dimensional indexing in GroupBase (Issue #4647)
* Catch higher dimensional indexing in GroupBase & ComponentBase (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
13 changes: 7 additions & 6 deletions package/MDAnalysis/core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,10 @@ def __init__(self, *args):
raise TypeError(errmsg) from None

# indices for the objects I hold
self._ix = np.asarray(ix, dtype=np.intp)
ix = np.asarray(ix, dtype=np.intp)
if ix.ndim > 1:
raise IndexError('Group index must be 1d')
self._ix = ix
self._u = u
self._cache = dict()

Expand Down Expand Up @@ -598,11 +601,6 @@ def __getitem__(self, item):
# 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 Expand Up @@ -4252,6 +4250,9 @@ class ComponentBase(_MutableBase):

def __init__(self, ix, u):
# index of component
if not isinstance(ix, numbers.Integral):
raise IndexError('Component can only be indexed by a single integer')

self._ix = ix
self._u = u

Expand Down
5 changes: 5 additions & 0 deletions testsuite/MDAnalysisTests/core/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def test_atom_pickle(self, universe, ix):
atm_in = pickle.loads(pickle.dumps(atm_out))
assert atm_in == atm_out

def test_improper_initialisation(self, universe):
with pytest.raises(IndexError):
indices = [0, 1]
mda.core.groups.Atom(indices, universe)


class TestAtomNoForceNoVel(object):
@staticmethod
Expand Down
6 changes: 6 additions & 0 deletions testsuite/MDAnalysisTests/core/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,12 @@ def test_bad_make(self):
with pytest.raises(TypeError):
mda.core.groups.AtomGroup(['these', 'are', 'not', 'atoms'])

def test_invalid_index_initialisation(self, universe):
indices = [[1, 2, 3],
[4, 5, 6]]
with pytest.raises(IndexError):
mda.core.groups.AtomGroup(indices, universe)

def test_n_atoms(self, ag):
assert ag.n_atoms == 3341

Expand Down
Loading