Skip to content

Pickling AtomGroups #293

@mnmelo

Description

@mnmelo

Universe pickling might be somewhat tricky (#173), and unpickling might be a relatively heavy operation if it involves reloading trajectories.
I've run into the need to pickle Universes mostly as a side effect of actually wanting to pickle AtomGroups, in my case as a return from a multiprocessing.pool.map.

My idea was to provide lightweight pickling and unpickling functions that'd do the following:

  • Pickling: only atom indices are stored, together with some sort of heuristic for Universe identification (total number of atoms and trajectory filename, for instance).
  • Unpickling: run through globals() looking for a Universe that matches the heuristic. Recreate AtomGroup from the indices. (Raise an error if no suitable Universe is found).

Search could be sped up by saving with the pickle the Universe names that have the same id as the AtomGroup's, and then looking at those names first when unpickling:

universe_names = []
key, val = None, None #Extremely unpythonic, but these can't be created on the for loop otherwise the globals() dict gets changed.  
for key,val in globals().iteritems():
    if val is atom_group.universe:
        universe_names.append(key)

The whole thing feels quite unpythonic, and granted can be easily worked around by just passing back and forth atom index lists instead of AtomGroups. But it'd make MDAnalysis work better and more natively with other code that requires pickling.

Opinions?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions