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

Atomgroup index with None give back the full Atomgroup #3092

Closed
xiki-tempula opened this issue Jan 10, 2021 · 7 comments · Fixed by #3517
Closed

Atomgroup index with None give back the full Atomgroup #3092

xiki-tempula opened this issue Jan 10, 2021 · 7 comments · Fixed by #3517

Comments

@xiki-tempula
Copy link
Contributor

xiki-tempula commented Jan 10, 2021

Expected behavior

If I use None as atomgroup index, I expect to obtain an empty atom group.

>>> u = mda.Universe(crd)
>>> u.atoms[None]
<AtomGroup with 0 atoms>

Actual behavior

The original atomgroup is returned

>>> u = mda.Universe(crd)
>>> u.atoms[None]
<AtomGroup with 1 atom>
>>> u.atoms[None].names
array([['N', 'H1', 'H2', ..., 'CL', 'CL', 'CL']], dtype=object)

Code to reproduce the behavior

import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD,  GRO, PDB, TPR, XTC, TRR,  PRMncdf, NCDF

u = mda.Universe(PSF, DCD)
u.atoms[None].names
#array([['N', 'HT1', 'HT2', ..., 'C', 'OT1', 'OT2']], dtype=object)

Current version of MDAnalysis

  • Which version are you using? (run python -c "import MDAnalysis as mda; print(mda.__version__)") 2.0.0-dev0
  • Which version of Python (python -V)? 3.8
  • Which operating system? OSX
@IAlibay
Copy link
Member

IAlibay commented Jan 10, 2021

Mm this is indeed annoying behaviour, although as @richardjgowers points out, this is numpy default behaviour, i.e. passing None returns the entire array 😖

edit: to clarify, None returns what essentially amounts to newaxis so it's an array for the array, which is what's happening here.

@xiki-tempula
Copy link
Contributor Author

@IAlibay well this doesn't explain

>>> u.atoms[None]
<AtomGroup with 1 atom>
>>> len(u.atoms[None])
1

If the entire array is returned.

@IAlibay
Copy link
Member

IAlibay commented Jan 10, 2021

Discussing this off-github with @richardjgowers, looks like the issue might be here?

return self._derived_class(self.ix[item], self.universe)

where the default Numpy behaviour is coming back to bite us (i.e. it returns an array of the array of ix, instead of the array of ix). There's probably a clean pythonic way to deal with this, but I can't think of one off the top of my head.

@jbarnoud
Copy link
Contributor

jbarnoud commented Feb 2, 2022

In numpy, indexing with None is a shortcut for indexing with numpy.newaxis. This does not make sense for our groups. I think we should do the same as regular lists and raise a TypeError, even if it comes with an explicit test for None.

@zemanj
Copy link
Member

zemanj commented Feb 2, 2022

In numpy, indexing with None is a shortcut for indexing with numpy.newaxis.

In fact, numpy.newaxis is None. So testing for None and raising an exception would definitely defeat using numpy.newaxis. Don't get me wrong, I don't see why using numpy.newaxis for indexing AtomGroups would be useful.
I'm all in favor of changing the behavior in one or the other way.

@IAlibay
Copy link
Member

IAlibay commented Feb 2, 2022

Sure TypeError sounds good to me :)

@jbarnoud
Copy link
Contributor

jbarnoud commented Feb 2, 2022

In fact, numpy.newaxis is None.

Indeed. My point was that None creates a new axis which makes no sense for a group in mda. So I think we agree.

jbarnoud added a commit that referenced this issue Feb 3, 2022
Indexing a group with `None` should raise a TypeError. This commit adds
a test about that. Because the code has not been fixed yet,the test is
failing at the moment.
@jbarnoud jbarnoud mentioned this issue Feb 3, 2022
4 tasks
IAlibay pushed a commit that referenced this issue Feb 3, 2022
Fixes #3092
* Raise TypeError when a group is indexed with None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants