You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AnalysisBase.run() misinterpretation of NumPy booleans frame masks as integer frame indices #5472
The case with the Python boolean list correctly selects frames [0, 2] as we expect, but an equivalent NumPy boolean array misinterpret them as integer frame indices and processes [1, 0, 1, 0].
This happens without throwing any error and can produce plausible-looking analysis results calculated from the wrong frames.
Code to reproduce the behavior
importnumpyasnpimportMDAnalysisasmdafromMDAnalysis.analysis.baseimportAnalysisFromFunction# We create a trajectory with one atom and four frames.coordinates=np.zeros((4, 1, 3), dtype=np.float32)
# Encode the frame number in the atom's x coordinate:coordinates[:, 0, 0] = [0, 1, 2, 3]
u=mda.Universe.empty(n_atoms=1)
u.load_new(coordinates, format="MEMORY")
defread_x(atomgroup):
"""Return the x coordinate of the only atom."""returnatomgroup.positions[0, 0]
# Both masks should select frames 0 and 2.python_mask= [True, False, True, False]
numpy_mask=np.array([True, False, True, False], dtype=bool)
with_python_mask=AnalysisFromFunction(
read_x,
u.trajectory,
u.atoms,
).run(frames=python_mask)
with_numpy_mask=AnalysisFromFunction(
read_x,
u.trajectory,
u.atoms,
).run(frames=numpy_mask)
print("Python list values:", with_python_mask.results.timeseries)
print("NumPy array values:", with_numpy_mask.results.timeseries)
Technically speaking, frames only supports frame indices so the fact that the Python list boolean indexing works is an accident and the numpy array is technically correct but it's such unexpected behavior that we should call it a bug.
Extending frames to also work with masks is a good idea.
EDIT: I looked at the wrong part of the docs. The docs for run()
array of integers or booleans to slice trajectory;
Activity
orbeckst commentedon Sep 24, 2026
Technically speaking,framesonly supports frame indices so the fact that the Python list boolean indexing works is an accident and the numpy array is technically correct but it's such unexpected behavior that we should call it a bug.Extendingframesto also work with masks is a good idea.EDIT: I looked at the wrong part of the docs. The docs for run()
so this is clearly a bug.
Fix AnalysisBase.run misreading numpy boolean frame masks
Fix numpy boolean frame masks (MDAnalysis#5476)
Simplify PersistenceLength tests, document error