Updated answer (for MDAnalysis ≥ 2.0)
Since MDAnalysis 2.0.0 (August 2021), Universes can be pickled.
import MDAnalysis as mda
import pickle
u = mda.Universe(topology, trajectory)
pickle.dump(u, open("universe.pkl", "wb"))
# load pickled universe
u_pickled = pickle.load(open("universe.pkl", "rb"))
# test that we get same positions
(u_pickled.atoms.positions == u.atoms.positions).all()
# -> True
# but that universes are different
u == u_pickled
# -> False
See also Parallelizing Analysis in the User Guide.
Old answer
MDAnalysis.Universe objects contain some objects that cannot be serialized and pickled by the standard mechanisms, such as open file descriptors. One would need to write specialized __getstate__()
and __setstate__()
methods as described in the Pickle protocol but none of this is implemented as of the current 0.8.1 (April 2014) release.
The specific error is explained by Manel in his comment on MDAnalysis Issue 173: Pickle searches for a __getstate__()
method. Although that is not implemented, Universe
, which manages manages its own attributes to generate "virtual attributes" on the fly, interprets this as an atom selection and eventually returns an empty AtomGroup instance. This, in turn, is called because pickle believes that it is the local implementation of __getstate__
. AtomGroup, however, is not callable and the error results.
As you probably noticed you got a quicker response by asking on the MDAnalysis user list or by filing an issue — Stackoverflow is typically lower on the list of developers for answering such specific questions.