Skip to content
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

Remove start/stop/step from AnalysisBase kwargs #2505

Merged
merged 14 commits into from
Feb 15, 2020
3 changes: 3 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ Enhancements
* Improve the distance search in water bridge analysis with capped_distance (PR #2480)

Changes
* deprecated `start`, `stop`, and `step` keywords have been removed from `__init__`
in :class:`AnalysisBase`. These should now be called in :meth:`AnalysisBase.run`
(Issue #1463)
* Standardize `select` keyword by removing `selection`, `atomselection`
and `ref_select` (Issue #2461)
* Removes support for setting `start`/`stop`/`step` trajecotry slicing
Expand Down
21 changes: 9 additions & 12 deletions package/MDAnalysis/analysis/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,6 @@ def __init__(self, mobile, reference, select='all', filename=None,
exact methods
force : bool (optional)
Force overwrite of filename for rmsd-fitting
start : int (optional)
First frame of trajectory to analyse, Default: 0
stop : int (optional)
Last frame of trajectory to analyse, Default: -1
step : int (optional)
Step between frames to analyse, Default: 1
in_memory : bool (optional)
*Permanently* switch `mobile` to an in-memory trajectory
so that alignment can be done in-place, which can improve
Expand Down Expand Up @@ -632,6 +626,10 @@ def __init__(self, mobile, reference, select='all', filename=None,
.. versionchanged:: 0.17.0
removed deprecated `mass_weighted` keyword

.. versionchanged:: 1.0.0
Support for the ``start``, ``stop``, and ``step`` keywords has been
removed. These should instead be passed to :meth:`AlignTraj.run`.

"""
select = rms.process_selection(select)
self.ref_atoms = reference.select_atoms(*select['reference'])
Expand Down Expand Up @@ -761,12 +759,6 @@ def __init__(self, mobile, reference=None, select='all', filename=None,
exact methods
force : bool (optional)
Force overwrite of filename for rmsd-fitting
start : int (optional)
First frame of trajectory to analyse, Default: 0
stop : int (optional)
Last frame of trajectory to analyse, Default: -1
step : int (optional)
Step between frames to analyse, Default: 1
in_memory : bool (optional)
*Permanently* switch `mobile` to an in-memory trajectory
so that alignment can be done in-place, which can improve
Expand Down Expand Up @@ -810,6 +802,11 @@ def __init__(self, mobile, reference=None, select='all', filename=None,

.. versionadded:: 0.21.0

.. versionchanged:: 1.0.0
Support for the ``start``, ``stop``, and ``step`` keywords has been
removed. These should instead be passed
to :meth:`AverageStructure.run`.

"""
if in_memory or isinstance(mobile.trajectory, MemoryReader):
mobile.transfer_to_memory()
Expand Down
34 changes: 11 additions & 23 deletions package/MDAnalysis/analysis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,14 @@ def __init__(self, trajectory, verbose=False, **kwargs):
A trajectory Reader
verbose : bool, optional
Turn on more logging and debugging, default ``False``

.. versionchanged:: 1.0.0
Support for setting ``start``, ``stop``, and ``step`` has been
removed. These should now be directly passed to
:meth:`AnalysisBase.run`.
"""
self._trajectory = trajectory
self._verbose = verbose
# do deprecated kwargs
# remove in 1.0
deps = []
for arg in ['start', 'stop', 'step']:
if arg in kwargs and not kwargs[arg] is None:
deps.append(arg)
setattr(self, arg, kwargs[arg])
if deps:
warnings.warn('Setting the following kwargs should be '
'done in the run() method: {}'.format(
', '.join(deps)),
DeprecationWarning)

def _setup_frames(self, trajectory, start=None, stop=None, step=None):
"""
Expand All @@ -132,11 +125,6 @@ def _setup_frames(self, trajectory, start=None, stop=None, step=None):
number of frames to skip between each analysed frame
"""
self._trajectory = trajectory
# TODO: Remove once start/stop/step are deprecated from init
# See if these have been set as class attributes, and use that
start = getattr(self, 'start', start)
stop = getattr(self, 'stop', stop)
step = getattr(self, 'step', step)
start, stop, step = trajectory.check_slice_indices(start, stop, step)
self.start = start
self.stop = stop
Expand Down Expand Up @@ -236,6 +224,11 @@ def __init__(self, function, trajectory=None, *args, **kwargs):
**kwargs : dict
arguments for ``function`` and ``AnalysisBase``

.. versionchanged:: 1.0.0
Support for directly passing the ``start``, ``stop``, and ``step``
arguments has been removed. These should isntead be passed
to :meth:`AnalysisFromFunction.run`.

"""
if (trajectory is not None) and (not isinstance(
trajectory, coordinates.base.ProtoReader)):
Expand All @@ -258,14 +251,9 @@ def __init__(self, function, trajectory=None, *args, **kwargs):
self.function = function
self.args = args

# TODO: Remove in 1.0
my_kwargs = {}
for depped_arg in ['start', 'stop', 'step']:
if depped_arg in kwargs:
my_kwargs[depped_arg] = kwargs.pop(depped_arg)
self.kwargs = kwargs

super(AnalysisFromFunction, self).__init__(trajectory, **my_kwargs)
super(AnalysisFromFunction, self).__init__(trajectory)

def _prepare(self):
self.results = []
Expand Down
17 changes: 4 additions & 13 deletions package/MDAnalysis/analysis/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ def _new_selections(u_orig, selections, frame):
return [u.select_atoms(s) for s in selections]


def q1q2(u, select='all', radius=4.5,
start=None, stop=None, step=None):
def q1q2(u, select='all', radius=4.5):
"""Perform a q1-q2 analysis.

Compares native contacts between the starting structure and final structure
Expand All @@ -479,29 +478,21 @@ def q1q2(u, select='all', radius=4.5,
atoms to do analysis on
radius : float, optional
distance at which contact is formed
start : int, optional
First frame of trajectory to analyse, Default: 0
stop : int, optional
Last frame of trajectory to analyse, Default: -1
step : int, optional
Step between frames to analyse, Default: 1

Returns
-------
contacts : :class:`Contacts`
Contact Analysis that is set up for a q1-q2 analysis

Notes
-----

.. versionchanged:: 1.0.0
Changed `selection` keyword to `select`

Support for setting ``start``, ``stop``, and ``step`` has been removed.
These should now be directly passed to :meth:`Contacts.run`.
"""
selection = (select, select)
first_frame_refs = _new_selections(u, selection, 0)
last_frame_refs = _new_selections(u, selection, -1)
return Contacts(u, selection,
(first_frame_refs, last_frame_refs),
radius=radius, method='radius_cut',
start=start, stop=stop, step=step)
radius=radius, method='radius_cut')
2 changes: 1 addition & 1 deletion package/MDAnalysis/analysis/diffusionmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def __init__(self, u, select='all', metric=rmsd, cutoff=1E0-5,
computational cost of eigenvalue decomposition
scales at O(N^3) where N is the number of frames.
Cost can be reduced by increasing step interval or specifying a
start and stop.
start and stop value when calling :meth:`DistanceMatrix.run`.
select: str, optional
Any valid selection string for
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms`
Expand Down
11 changes: 3 additions & 8 deletions package/MDAnalysis/analysis/lineardensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ class LinearDensity(AnalysisBase):
Bin width in Angstrom used to build linear density
histograms. Defines the resolution of the resulting density
profile (smaller --> higher resolution) [0.25]
start : int
The frame to start at [0]
stop : int
The frame to end at [-1]
step : int
The step size through the trajectory in frames [0]
verbose : bool (optional)
Show detailed progress of the calculation if set to ``True``; the
default is ``False``.
Expand All @@ -80,10 +74,11 @@ class LinearDensity(AnalysisBase):
.. versionadded:: 0.14.0

.. versionchanged:: 1.0.0
``save()`` method was removed, you can use ``np.savetxt()`` or
Support for the ``start``, ``stop``, and ``step`` keywords has been
removed. These should instead be passed to :meth:`LinearDensity.run`.
The ``save()`` method was also removed, you can use ``np.savetxt()`` or
``np.save()`` on the :attr:`LinearDensity.results` dictionary contents
instead.

"""

def __init__(self, selection, grouping='atoms', binsize=0.25, **kwargs):
Expand Down
22 changes: 8 additions & 14 deletions package/MDAnalysis/analysis/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ class InterRDF(AnalysisBase):
exclusion_block : tuple (optional)
A tuple representing the tile to exclude from the distance
array. [None]
start : int (optional)
The frame to start at (default is first)
stop : int (optional)
The frame to end at (default is last)
step : int (optional)
The step size through the trajectory in frames (default is
every frame)
verbose : bool (optional)
Show detailed progress of the calculation if set to ``True``; the
default is ``False``.
Expand All @@ -164,6 +157,10 @@ class InterRDF(AnalysisBase):

.. versionadded:: 0.13.0

.. versionchanged:: 1.0.0
Support for the ``start``, ``stop``, and ``step`` keywords has been
removed. These should instead be passed to :meth:`InterRDF.run`.

"""
def __init__(self, g1, g2,
nbins=75, range=(0.0, 15.0), exclusion_block=None,
Expand Down Expand Up @@ -248,13 +245,6 @@ class InterRDF_s(AnalysisBase):
Number of bins in the histogram [75]
range : tuple or list (optional)
The size of the RDF [0.0, 15.0]
start : int (optional)
The frame to start at (default is first)
stop : int (optional)
The frame to end at (default is last)
step : int (optional)
The step size through the trajectory in frames (default is
every frame)

Example
-------
Expand Down Expand Up @@ -297,6 +287,10 @@ class InterRDF_s(AnalysisBase):

.. versionadded:: 0.19.0

.. versionchanged:: 1.0.0
Support for the ``start``, ``stop``, and ``step`` keywords has been
removed. These should instead be passed to :meth:`InterRDF_s.run`.

"""
def __init__(self, u, ags,
nbins=75, range=(0.0, 15.0), density=True, **kwargs):
Expand Down
11 changes: 3 additions & 8 deletions package/MDAnalysis/analysis/rms.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,6 @@ def __init__(self, atomgroup, **kwargs):
----------
atomgroup : AtomGroup
Atoms for which RMSF is calculated
start : int (optional)
starting frame, default None becomes 0.
stop : int (optional)
Frame index to stop analysis. Default: None becomes
n_frames. Iteration stops *before* this frame number,
which means that the trajectory would be read until the end.
step : int (optional)
step between frames, default None becomes 1.
verbose : bool (optional)
Show detailed progress of the calculation if set to ``True``; the
default is ``False``.
Expand Down Expand Up @@ -775,6 +767,9 @@ def __init__(self, atomgroup, **kwargs):
the keyword argument `quiet` is deprecated in favor of `verbose`.
.. versionchanged:: 0.17.0
removed unused keyword `weights`
.. versionchanged:: 1.0.0
Support for the ``start``, ``stop``, and ``step`` keywords has been
removed. These should instead be passed to :meth:`RMSF.run`.

"""
super(RMSF, self).__init__(atomgroup.universe.trajectory, **kwargs)
Expand Down
42 changes: 20 additions & 22 deletions testsuite/MDAnalysisTests/analysis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,31 @@ def simple_function(mobile):
return mobile.center_of_geometry()


def test_AnalysisFromFunction():
u = mda.Universe(PSF, DCD)
step = 2
ana1 = base.AnalysisFromFunction(
simple_function, mobile=u.atoms).run(step=step)
ana2 = base.AnalysisFromFunction(simple_function, u.atoms).run(step=step)
ana3 = base.AnalysisFromFunction(
simple_function, u.trajectory, u.atoms).run(step=step)
@pytest.mark.parametrize('start, stop, step, nframes', [
(None, None, 2, 49),
(None, 50, 2, 25),
(20, 50, 2, 15),
(20, 50, None, 30)
])
def test_AnalysisFromFunction(u, start, stop, step, nframes):
ana1 = base.AnalysisFromFunction(simple_function, mobile=u.atoms)
ana1.run(start=start, stop=stop, step=step)

ana2 = base.AnalysisFromFunction(simple_function, u.atoms)
ana2.run(start=start, stop=stop, step=step)

ana3 = base.AnalysisFromFunction(simple_function, u.trajectory, u.atoms)
ana3.run(start=start, stop=stop, step=step)

results = []
for ts in u.trajectory[::step]:

for ts in u.trajectory[start:stop:step]:
results.append(simple_function(u.atoms))

results = np.asarray(results)

assert np.size(results, 0) == nframes

for ana in (ana1, ana2, ana3):
assert_equal(results, ana.results)

Expand Down Expand Up @@ -183,16 +194,3 @@ def distance(a, b):

with no_deprecated_call():
d = Distances(u.atoms[:10], u.atoms[10:20]).run()

@pytest.mark.parametrize('param', ['start', 'stop', 'step'])
def test_runargs_deprecation(param):
u = mda.Universe(PSF, DCD)

class NothingAnalysis(base.AnalysisBase):
def _single_frame(self):
self.results = []

with pytest.warns(DeprecationWarning):
ana = NothingAnalysis(u.trajectory, **{param: 10})

ana.run()