-
Notifications
You must be signed in to change notification settings - Fork 189
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
system.part does not behave like an array #4278
Comments
what is "failing"? |
|
so, your MWE can be made more "minimal": import espressomd
import numpy as np
sys = espressomd.System(box_l=[10,10,10])
sys.part.add(pos=[4,4,4], id=1)
print(np.random.choice(sys.part)) |
the issue is that the particle list in espresso is not "array-like" because it can have gaps and the index is interpreted as the id (which is not the best idea), right? |
yes, and np.random.choice thinks if the length of a list is one the index must be 0 |
okay, but is it a bug or expected behavior? looking at https://espressomd.github.io/doc/espressomd.html#espressomd.particle_data.ParticleList I'd say it's documented behavior |
I am using numpy v1.20.3 and I can't reproduce this problem. However running the same snippet with numpy v1.17.4 returns the mentioned error. Seems like this has been resolved somewhere inbetween. |
This is duck-typing at its worst. If it has [], it must be a list. Unfortunately, dictionaries use [] too. If we are prepared to break the interface, we can remove the [] operator and instead have |
it might be that the underlying implementation in numpy changed, but as I said it's not an issue of numpy but an issue of our ParticleList implementation that does not behave like an "array-like" which is the requirement for the numpy function. This is in principle also connected to #3992 and #3369 |
btw: our list is also officially non-pythonic:
... and
|
btw: our list is also officially non-pythonic:
When comparing to Py built-in types System.part should be thought of a dict, I guess.
|
That's why we call it |
Python dictionaries also do not support slicing. |
Fixes / Avoids #4278 causing a RuntimeError if a box runs empty
This is an issue in the OpenGL visualizer, which relies on particle ids to index arrays. Thanks @mebrito for finding this out! Here is how the list of bonds is serialized: espresso/src/python/espressomd/visualization_opengl.py Lines 861 to 874 in 9f8072f
The visualizer breaks as soon as a particle id larger or equal to the number of particles is hit (access out of bounds): espresso/src/python/espressomd/visualization_opengl.py Lines 1117 to 1118 in 9f8072f
|
This is done, to my understanding |
choosing a random particle via
np.random.choice
will fail, if there is only one particle in the system and if this particle has not the id of 0The text was updated successfully, but these errors were encountered: