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

atoms.positions returns a copy of the underlying array #2069

Open
kain88-de opened this issue Sep 11, 2018 · 0 comments
Open

atoms.positions returns a copy of the underlying array #2069

kain88-de opened this issue Sep 11, 2018 · 0 comments

Comments

@kain88-de
Copy link
Member

Expected behavior

atoms.positions returns a view to the underlying array. This means code like u.atoms[:n].positions[:] = np.ones((n,3)) does an inplace update of the coordinates.

Actual behavior

atoms.positions returns a copy. This is especially true if only a selection of atoms in the universe is used.

Code to reproduce the behavior

import MDAnalysis as mda
import numpy as np

u = mda.fetch_mmtf('1UBQ')
u.atoms[:5].positions[:] = np.ones((5, 3))
print(np.allclose(np.ones((5,3)), u.atoms[:5].positions))

Comments

I know this a limitation of numpy when indexing. I'm also aware that my code example is not how one would ideally write the code. However, this is still potentially confusing behavior and I think we get questions about this on the mailing list once in a while.

I also do not know a good solution to this problem. Or even if there is one using numpy. It would still be nice to have this in a Common Pitfalls section of the documentation. (Maybe this is already there and I just don't know?)

It is definitely possible to write our own array structure in C that supports to create views to individual atoms. I don't think the effort this would involve is worth it though.

Currently version of MDAnalysis

  • Which version are you using? (run python -c "import MDAnalysis as mda; print(mda.__version__)")
    0.18.0
  • Which version of Python (python -V)?
    3.6
  • Which operating system?
    Arch Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants