-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor pmda after universe can be serialized #132
base: master
Are you sure you want to change the base?
Changes from 33 commits
1e3d27b
4435f29
1981673
0a70857
cafe65f
185d19a
ef86b9d
c0b0bd6
bfa629c
495033f
8700223
c8e9973
356cfb9
58fea5d
dfd3588
0921882
49288e8
9a0e0c5
187463b
8a42040
053225b
83becd7
f9c89e6
61bce8f
cb99fc8
d95add1
a505282
608a803
18988c5
61a34c7
7bf68f5
5ceaebf
d424f6d
b649c04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,6 @@ | |
same universe and return a value. | ||
|
||
""" | ||
from __future__ import absolute_import | ||
|
||
from MDAnalysis.core.groups import AtomGroup | ||
from MDAnalysis.core.universe import Universe | ||
from MDAnalysis.coordinates.base import ProtoReader | ||
import numpy as np | ||
|
@@ -44,7 +41,7 @@ class AnalysisFromFunction(ParallelAnalysisBase): | |
>>> 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() | ||
universe, mobile, ref).run() | ||
>>> print(rot.results) | ||
|
||
Raises | ||
|
@@ -64,40 +61,24 @@ def __init__(self, function, universe, *args, **kwargs): | |
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 : :class:`~MDAnalysis.core.groups.Universe` | ||
universe : :class:`~MDAnalysis.core.groups.Universe` | ||
a :class:`MDAnalysis.core.groups.Universe` (the | ||
`atomgroups` must belong to this Universe) | ||
`atomgroups` in other args 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. | ||
|
||
keyword arguments for ``function``. | ||
""" | ||
|
||
self.function = function | ||
|
||
# collect all atomgroups with the same trajectory object as universe | ||
trajectory = universe.trajectory | ||
arg_ags = [] | ||
self.other_args = [] | ||
for arg in args: | ||
if isinstance(arg, | ||
AtomGroup) and arg.universe.trajectory == trajectory: | ||
arg_ags.append(arg) | ||
else: | ||
self.other_args.append(arg) | ||
|
||
super(AnalysisFromFunction, self).__init__(universe, arg_ags) | ||
super().__init__(universe) | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def _prepare(self): | ||
self.results = [] | ||
|
||
def _single_frame(self, ts, atomgroups): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, cool that this works. |
||
args = atomgroups + self.other_args | ||
return self.function(*args, **self.kwargs) | ||
def _single_frame(self): | ||
return self.function(*self.args, **self.kwargs) | ||
|
||
def _conclude(self): | ||
self.results = np.concatenate(self._results) | ||
|
@@ -132,7 +113,7 @@ def analysis_class(function): | |
>>> def RotationMatrix(mobile, ref): | ||
>>> return mda.analysis.align.rotation_matrix(mobile, ref)[0] | ||
|
||
>>> rot = RotationMatrix(u.trajectory, mobile, ref, step=2).run() | ||
>>> rot = RotationMatrix(u, mobile, ref, step=2).run() | ||
>>> print(rot.results) | ||
|
||
See Also | ||
|
@@ -144,13 +125,14 @@ def analysis_class(function): | |
class WrapperClass(AnalysisFromFunction): | ||
"""Custom Analysis Function""" | ||
|
||
def __init__(self, trajectory=None, *args, **kwargs): | ||
if not (isinstance(trajectory, ProtoReader) or isinstance( | ||
trajectory, Universe)): | ||
print(type(trajectory)) | ||
def __init__(self, universe=None, *args, **kwargs): | ||
if not isinstance(universe, Universe): | ||
print(type(universe)) | ||
raise ValueError( | ||
"First argument needs to be an MDAnalysis reader object.") | ||
super(WrapperClass, self).__init__(function, trajectory, *args, | ||
**kwargs) | ||
"First argument needs to be an MDAnalysis Universe.") | ||
super().__init__(function, | ||
universe, | ||
*args, | ||
**kwargs) | ||
|
||
return WrapperClass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,6 @@ | |
MDAnalysis.analysis.density.density_from_Universe | ||
|
||
""" | ||
|
||
from __future__ import absolute_import | ||
|
||
import numpy as np | ||
|
||
from MDAnalysis.lib.util import fixedwidth_bins | ||
|
@@ -241,7 +238,7 @@ def __init__(self, atomgroup, delta=1.0, atomselection=None, | |
parameters=None, gridcenter=None, xdim=None, ydim=None, | ||
zdim=None): | ||
u = atomgroup.universe | ||
super(DensityAnalysis, self).__init__(u, (atomgroup, )) | ||
super().__init__(u) | ||
self._atomgroup = atomgroup | ||
self._delta = delta | ||
self._atomselection = atomselection | ||
|
@@ -259,10 +256,14 @@ def __init__(self, atomgroup, delta=1.0, atomselection=None, | |
elif not updating and atomselection is not None: | ||
raise ValueError("""With updating=False, the atomselection='{}' is | ||
not used and should be None""".format(atomselection)) | ||
elif updating and atomselection is not None: | ||
self._select_atomgroup = atomgroup.select_atoms(atomselection, | ||
updating=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do updating AtomGroups work with the serialization? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! Thanks to what has already been implemented by Richard:) |
||
else: | ||
self._select_atomgroup = atomgroup | ||
|
||
def _prepare(self): | ||
coord = self.current_coordinates(self._atomgroup, self._atomselection, | ||
self._updating) | ||
coord = self._select_atomgroup.positions | ||
if self._gridcenter is not None: | ||
# Generate a copy of smin/smax from coords to later check if the | ||
# defined box might be too small for the selection | ||
|
@@ -294,10 +295,10 @@ def _prepare(self): | |
self._arange = arange | ||
self._bins = bins | ||
|
||
def _single_frame(self, ts, atomgroups): | ||
coord = self.current_coordinates(atomgroups[0], self._atomselection, | ||
self._updating) | ||
result = np.histogramdd(coord, bins=self._bins, range=self._arange, | ||
def _single_frame(self): | ||
result = np.histogramdd(self._select_atomgroup.positions, | ||
bins=self._bins, | ||
range=self._arange, | ||
normed=False) | ||
return result[0] | ||
|
||
|
@@ -330,12 +331,3 @@ def _reduce(res, result_single_frame): | |
else: | ||
res += result_single_frame | ||
return res | ||
|
||
@staticmethod | ||
def current_coordinates(atomgroup, atomselection, updating): | ||
"""Retrieves the current coordinates of all atoms in the chosen atom | ||
selection. | ||
Note: currently required to allow for updating selections""" | ||
ag = atomgroup if not updating else atomgroup.select_atoms( | ||
atomselection) | ||
return ag.positions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment is outdated