Parallel Analysis helper — pmda.custom

This module contains the class AnalysisFromFunction and the decorator analysis_class. Both can be used to generate custom analysis classes that can be run in parallel from functions that take one or more atom groups from the same universe and return a value.

class pmda.custom.AnalysisFromFunction(function, universe, *args, **kwargs)[source]

Create an analysis from a function working on AtomGroups

The function that is used

results

results of calculation are stored after call to run

Type

ndarray

Example

>>> # Create a new function to analyze a single frame
>>> def rotation_matrix(mobile, ref):
>>>     return mda.analysis.align.rotation_matrix(mobile, ref)[0]
>>> # now run an analysis using the standalone function
>>> rot = AnalysisFromFunction(rotation_matrix,
                               trajectory, mobile, ref).run()
>>> print(rot.results)

:raises ValueError : if function has the same kwargs as BaseAnalysis:

See also

analysis_class

Parameters
  • function (callable) – function to evaluate at each frame. The first arguments are assumed to be ‘mobile’ Atomgroups if they belong to the same universe. All other Atomgroups are assumed to be reference. Here ‘mobile’ means they will be iterated over.

  • Universe (Universe) – a MDAnalysis.core.groups.Universe (the atomgroups must belong to this Universe)

  • *args (list) – arguments for function

  • **kwargs (dict) – keyword arguments for function. keyword arguments with name ‘universe’ or ‘atomgroups’ will be ignored! Mobile atomgroups to analyze can not be passed as keyword arguments currently.

pmda.custom.analysis_class(function)[source]

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

Parameters

function (callable) – The function that can analyze a single or more atomgroups. It is always assumed that the mobile atomgroups (which will be iterated over) come first. All atomgroups that come directly after the first that are part of the same universe will iterated over

Returns

Return type

A new AnalysisClass with function as analysis

Example

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, step=2).run()
>>> print(rot.results)