4.1.1. Analysis building blocks — MDAnalysis.analysis.base

A collection of useful building blocks for creating Analysis classes.

class MDAnalysis.analysis.base.AnalysisBase(trajectory, verbose=False, **kwargs)[source]

Base class for defining multi frame analysis

The class it is designed as a template for creating multiframe analyses. This class will automatically take care of setting up the trajectory reader for iterating, and it offers to show a progress meter.

To define a new Analysis, AnalysisBase needs to be subclassed _single_frame must be defined. It is also possible to define _prepare and _conclude for pre and post processing. See the example below.

class NewAnalysis(AnalysisBase):
    def __init__(self, atomgroup, parameter, **kwargs):
        super(NewAnalysis, self).__init__(atomgroup.universe.trajectory,
        self._parameter = parameter
        self._ag = atomgroup

    def _prepare(self):
        # OPTIONAL
        # Called before iteration on the trajectory has begun.
        # Data structures can be set up at this time
        self.result = []

    def _single_frame(self):
        # REQUIRED
        # Called after the trajectory is moved onto each new frame.
        # store result of `some_function` for a single frame
        self.result.append(some_function(self._ag, self._parameter))

    def _conclude(self):
        # OPTIONAL
        # Called once iteration on the trajectory is finished.
        # Apply normalisation and averaging to results here.
        self.result = np.asarray(self.result) / np.sum(self.result)

Afterwards the new analysis can be run like this.

na = NewAnalysis(u.select_atoms('name CA'), 35).run(start=10, stop=20)
  • trajectory (mda.Reader) – A trajectory Reader
  • verbose (bool, optional) – Turn on more logging and debugging, default False
run(start=None, stop=None, step=None, verbose=None)[source]

Perform the calculation

  • start (int, optional) – start frame of analysis
  • stop (int, optional) – stop frame of analysis
  • step (int, optional) – number of frames to skip between each analysed frame
  • verbose (bool, optional) – Turn on verbosity
class MDAnalysis.analysis.base.AnalysisFromFunction(function, trajectory=None, *args, **kwargs)[source]

Create an analysis from a function working on AtomGroups


results of calculation are stored after call to run



>>> def rotation_matrix(mobile, ref):
>>>     return mda.analysis.align.rotation_matrix(mobile, ref)[0]
>>> rot = AnalysisFromFunction(rotation_matrix, trajectory, mobile, ref).run()
>>> print(rot.results)

ValueError : if function has the same kwargs as BaseAnalysis

  • function (callable) – function to evaluate at each frame
  • trajectory (mda.coordinates.Reader (optional)) – trajectory to iterate over. If None the first AtomGroup found in args and kwargs is used as a source for the trajectory.
  • *args (list) – arguments for function
  • **kwargs (dict) – arugments for function and AnalysisBase

Transform a function operating on a single frame to an analysis class

For an usage in a library we recommend the following style:

>>> def rotation_matrix(mobile, ref):
>>>     return mda.analysis.align.rotation_matrix(mobile, ref)[0]
>>> RotationMatrix = analysis_class(rotation_matrix)

It can also be used as a decorator:

>>> @analysis_class
>>> def RotationMatrix(mobile, ref):
>>>     return mda.analysis.align.rotation_matrix(mobile, ref)[0]
>>> rot = RotationMatrix(u.trajectory, mobile, ref).run(step=2)
>>> print(rot.results)