From e1f11970482f6f9ddc02c564a12dd8eea07066e1 Mon Sep 17 00:00:00 2001 From: Irfan Alibay Date: Thu, 6 Feb 2020 17:39:05 +0000 Subject: [PATCH] Remove save part2 (#2494) * Partly addresses #1745 (completes save() removal with PR #2487 and PR #2486 ) * removes save methods - removes save method from psa.py - also minor change to test_hbonds.py * deprecates hbond_analysis - close #2492 * updates changelog --- package/CHANGELOG | 7 +- .../analysis/hbonds/hbond_analysis.py | 49 +++-------- .../analysis/hbonds/hbond_autocorrel.py | 30 ++----- package/MDAnalysis/analysis/psa.py | 83 +++---------------- .../MDAnalysisTests/analysis/test_hbonds.py | 3 +- .../analysis/test_hydrogenbondautocorrel.py | 29 ------- 6 files changed, 36 insertions(+), 165 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 91a9ccea712..2e37adca064 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -79,6 +79,11 @@ Enhancements convert between a parmed.Structure and MDAnalysis Universe (PR #2404) Changes + * Removes; `save_table()` from :class:`HydrogenBondAnalysis`, + `save_results()` from :class:`HydrogenBondAutoCorrel`, and + `save_results()`/`filename`/`store` from :class:`PSAnalysis`. Also sets + the deprecation of `hbonds/hbond_analysis.py` to v1.0 (Issues #1745, + #2486, #2487, #2491, #2492). * Removes the `format` and `skip` keywords from :meth:`DCDReader.timeseries` (Issue #2443) * `fullgroup` selection has now been removed (#268) @@ -89,7 +94,7 @@ Changes is "-" (Issue #2448, PR #2457) Deprecations - * analysis.hbonds.HydrogenBondAnalysis will be deprecated in 1.0 + * analysis.hbonds.HydrogenBondAnalysis is deprecated in 1.0 (remove in 2.0) 09/05/19 IAlibay, richardjgowers diff --git a/package/MDAnalysis/analysis/hbonds/hbond_analysis.py b/package/MDAnalysis/analysis/hbonds/hbond_analysis.py index 99fb6287b2c..9810151de89 100644 --- a/package/MDAnalysis/analysis/hbonds/hbond_analysis.py +++ b/package/MDAnalysis/analysis/hbonds/hbond_analysis.py @@ -30,7 +30,7 @@ :Copyright: GNU Public License v3 ..Warning: - This module will be deprecated in version 1.0. + This module is deprecated and will be removed in version 2.0. Please use :mod:`MDAnalysis.analysis.hydrogenbonds.hbond_analysis` instead. Given a :class:`~MDAnalysis.core.universe.Universe` (simulation @@ -321,7 +321,7 @@ class HydrogenBondAnalysis_OtherFF(HydrogenBondAnalysis): """ from __future__ import division, absolute_import import six -from six.moves import range, zip, map, cPickle +from six.moves import range, zip import warnings import logging @@ -334,17 +334,18 @@ class HydrogenBondAnalysis_OtherFF(HydrogenBondAnalysis): from MDAnalysis.lib.log import ProgressMeter from MDAnalysis.lib.NeighborSearch import AtomNeighborSearch from MDAnalysis.lib import distances -from MDAnalysis.lib.util import deprecate logger = logging.getLogger('MDAnalysis.analysis.hbonds') warnings.warn( - "This module will be deprecated in version 1.0." + "This module is deprecated as of MDAnalysis version 1.0." + "It will be removed in MDAnalysis version 2.0" "Please use MDAnalysis.analysis.hydrogenbonds.hbond_analysis instead.", category=DeprecationWarning ) + class HydrogenBondAnalysis(base.AnalysisBase): """Perform a hydrogen bond analysis @@ -380,6 +381,10 @@ class HydrogenBondAnalysis(base.AnalysisBase): .. versionchanged:: 0.7.6 DEFAULT_DONORS/ACCEPTORS is now embedded in a dict to switch between default values for different force fields. + + .. versionchanged:: 1.0.0 + ``save_table()`` method has been removed. You can use ``np.save()`` or + ``cPickle.dump()`` on :attr:`HydrogenBondAnalysis.table` instead. """ # use tuple(set()) here so that one can just copy&paste names from the @@ -567,7 +572,8 @@ def __init__(self, universe, selection1='protein', selection2='all', selection1_ super(HydrogenBondAnalysis, self).__init__(universe.trajectory, **kwargs) warnings.warn( - "This class will be deprecated in version 1.0." + "This class is deprecated as of MDAnalysis version 1.0 and will " + "be removed in version 2.0." "Please use MDAnalysis.analysis.hydrogenbonds.hbond_analysis.HydrogenBondAnalysis instead.", category=DeprecationWarning ) @@ -1120,12 +1126,6 @@ def _reformat_hb(hb, atomformat="{0[0]!s}{0[1]!s}:{0[2]!s}"): In 0.16.1, donor and acceptor are stored as a tuple(resname, resid, atomid). In 0.16.0 and earlier they were stored as a string. - .. deprecated:: 1.0 - This is a compatibility layer so that we can provide the same output - in timeseries as before. However, for 1.0 we should make timeseries - just return _timeseries, i.e., change the format of timeseries to - the un-ambiguous representation provided in _timeseries. - """ return (hb[:2] + [atomformat.format(hb[2]), atomformat.format(hb[3])] @@ -1171,33 +1171,6 @@ def generate_table(self): self.table = out.view(np.recarray) logger.debug("HBond: Stored results as table with %(num_records)d entries.", vars()) - @deprecate(release="0.19.0", remove="1.0.0", - message="You can instead use ``np.save(filename, " - "HydrogendBondAnalysis.table)``.") - def save_table(self, filename="hbond_table.pickle"): - """Saves :attr:`~HydrogenBondAnalysis.table` to a pickled file. - - If :attr:`~HydrogenBondAnalysis.table` does not exist yet, - :meth:`generate_table` is called first. - - Parameters - ---------- - filename : str (optional) - path to the filename - - Example - ------- - Load with :: - - import cPickle - table = cPickle.load(open(filename)) - - """ - if self.table is None: - self.generate_table() - with open(filename, 'w') as f: - cPickle.dump(self.table, f, protocol=cPickle.HIGHEST_PROTOCOL) - def _has_timeseries(self): has_timeseries = self._timeseries is not None if not has_timeseries: diff --git a/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py b/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py index 06dad62e6f9..f2c00a51aca 100644 --- a/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py +++ b/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py @@ -215,7 +215,6 @@ from MDAnalysis.lib.log import ProgressMeter from MDAnalysis.lib.distances import capped_distance, calc_angles, calc_bonds -from MDAnalysis.lib.util import deprecate from MDAnalysis.core.groups import requires from MDAnalysis.due import due, Doi @@ -285,6 +284,11 @@ class HydrogenBondAutoCorrel(object): Within each run, the number of frames to analyse [50] pbc : bool, optional Whether to consider periodic boundaries in calculations [``True``] + + ..versionchanged: 1.0.0 + ``save_results()`` method was removed. You can instead use ``np.savez()`` + on :attr:`HydrogenBondAutoCorrel.solution['time']` and + :attr:`HydrogenBondAutoCorrel.solution['results']` instead. """ def __init__(self, universe, @@ -426,7 +430,7 @@ def _single_run(self, start, stop): # set to above dist crit to exclude exclude = np.column_stack((self.exclusions[0], self.exclusions[1])) pair = np.delete(pair, np.where(pair==exclude), 0) - + hidx, aidx = np.transpose(pair) @@ -481,28 +485,6 @@ def _single_run(self, start, stop): return results - @deprecate(release="0.19.0", remove="1.0.0", - message="You can instead use " - "``np.savez(filename, time=HydrogenBondAutoCorrel.solution['time'], " - "results=HydrogenBondAutoCorrel.solution['results'])``.") - def save_results(self, filename='hbond_autocorrel'): - """Saves the results to a numpy zipped array (.npz, see np.savez) - - This can be loaded using np.load(filename) - - Parameters - ---------- - filename : str, optional - The desired filename [hbond_autocorrel] - - """ - if self.solution['results'] is not None: - np.savez(filename, time=self.solution['time'], - results=self.solution['results']) - else: - raise ValueError( - "Results have not been generated, use the run method first") - def solve(self, p_guess=None): """Fit results to an multi exponential decay and integrate to find characteristic time diff --git a/package/MDAnalysis/analysis/psa.py b/package/MDAnalysis/analysis/psa.py index f7835d0c595..3866db8f9c4 100644 --- a/package/MDAnalysis/analysis/psa.py +++ b/package/MDAnalysis/analysis/psa.py @@ -183,11 +183,6 @@ int, frame index to select frame from :attr:`Path.u_reference` - .. attribute:: filename - - string, name of file to store calculated distance matrix - (:attr:`PSAnalysis.D`) - .. attribute:: paths list of :class:`numpy.ndarray` objects representing the set/ensemble of @@ -195,8 +190,7 @@ .. attribute:: D - string, name of file to store calculated distance matrix - (:attr:`PSAnalysis.D`) + :class:`numpy.ndarray` whichs store calculated distance matrix .. Markup definitions @@ -1287,6 +1281,10 @@ class PSAnalysis(object): alignment of the original trajectories to a reference structure. .. versionadded:: 0.8 + + .. versionchanged:: 1.0.0 + ``save_result()`` method has been removed. You can use ``np.save()`` on + :attr:`PSAnalysis.D` instead. """ def __init__(self, universes, reference=None, ref_select='name CA', ref_frame=0, path_select=None, labels=None, @@ -1509,31 +1507,15 @@ def run(self, **kwargs): ``trajectory[start:stop:step]`` [``None``] stop : int step : int - store : bool - if ``True`` then writes :attr:`PSAnalysis.D` to text and - compressed npz (numpy) files [``True``] - - .. deprecated:: 0.19.0 - `store` will be removed together with :meth:`save_results` in 1.0.0. - - filename : str - string, filename to save :attr:`PSAnalysis.D` - - .. deprecated:: 0.19.0 - `filename` will be removed together with :meth:`save_results` in 1.0.0. + .. versionchanged:: 1.0.0 + `store` and `filename` have been removed. """ metric = kwargs.pop('metric', 'hausdorff') start = kwargs.pop('start', None) stop = kwargs.pop('stop', None) step = kwargs.pop('step', None) - # DEPRECATED 0.19.0: remove in 1.0 - if 'store' in kwargs: - warnings.warn("PSAnalysis.run(): 'store' was deprecated in 0.19.0 " - "and will be removed in 1.0", - category=DeprecationWarning) - store = kwargs.pop('store', True) if isinstance(metric, string_types): metric_func = get_path_metric_func(str(metric)) @@ -1549,16 +1531,6 @@ def run(self, **kwargs): D[i,j] = metric_func(P, Q) D[j,i] = D[i,j] self.D = D - if store: - # DEPRECATED 0.19.0: remove in 1.0 - if 'filename' in kwargs: - warnings.warn("PSAnalysis.run(): 'filename' was deprecated in " - "0.19.0 and will be removed in 1.0", - category=DeprecationWarning) - filename = kwargs.pop('filename', metric) - if not isinstance(metric, string_types): - filename = 'custom_metric' - self.save_result(filename=filename) def run_pairs_analysis(self, **kwargs): """Perform PSA Hausdorff (nearest neighbor) pairs analysis on all unique @@ -1618,38 +1590,6 @@ def run_pairs_analysis(self, **kwargs): self._HP.append(pp.get_hausdorff_pair()) self.D = D - @deprecate(release="0.19.0", remove="1.0.0", - message="You can save the distance matrix :attr:`D` to a numpy " - "file with ``np.save(filename, PSAnalysis.D)``.") - def save_result(self, filename=None): - """Save distance matrix :attr:`PSAnalysis.D` to a numpy compressed npz - file and text file. - - The data are saved with :func:`numpy.savez_compressed` and - :func:`numpy.savetxt` in the directory specified by - :attr:`PSAnalysis.targetdir`. - - Parameters - ---------- - filename : str - specifies filename [``None``] - - Returns - ------- - filename : str - - """ - filename = filename or 'psa_distances' - head = os.path.join(self.targetdir, self.datadirs['distance_matrices']) - outfile = os.path.join(head, filename) - if self.D is None: - raise NoDataError("Distance matrix has not been calculated yet") - np.save(outfile + '.npy', self.D) - np.savetxt(outfile + '.dat', self.D) - logger.info("Wrote distance matrix to file %r.npz", outfile) - logger.info("Wrote distance matrix to file %r.dat", outfile) - return filename - def save_paths(self, filename=None): """Save fitted :attr:`PSAnalysis.paths` to numpy compressed npz files. @@ -1756,7 +1696,7 @@ def plot(self, filename=None, linkage='ward', count_sort=False, if self.D is None: raise ValueError( - "No distance data; do 'PSAnalysis.run(store=True)' first.") + "No distance data; do 'PSAnalysis.run()' first.") npaths = len(self.D) dist_matrix = self.D @@ -1907,7 +1847,7 @@ def plot_annotated_heatmap(self, filename=None, linkage='ward', \ if self.D is None: raise ValueError( - "No distance data; do 'PSAnalysis.run(store=True)' first.") + "No distance data; do 'PSAnalysis.run()' first.") dist_matrix = self.D Z, dgram = self.cluster(method=linkage, \ @@ -2215,8 +2155,7 @@ def get_pairwise_distances(self, vectorform=False, checks=False): Note ---- - Must run :meth:`PSAnalysis.run` with ``store=True`` prior to - calling this method. + Must run :meth:`PSAnalysis.run` prior to calling this method. Parameters ---------- @@ -2234,7 +2173,7 @@ def get_pairwise_distances(self, vectorform=False, checks=False): """ if self.D is None: raise ValueError( - "No distance data; do 'PSAnalysis.run(store=True)' first.") + "No distance data; do 'PSAnalysis.run()' first.") if vectorform: return spatial.distance.squareform(self.D, force='tovector', checks=checks) diff --git a/testsuite/MDAnalysisTests/analysis/test_hbonds.py b/testsuite/MDAnalysisTests/analysis/test_hbonds.py index b10cce2624e..388200b5b02 100644 --- a/testsuite/MDAnalysisTests/analysis/test_hbonds.py +++ b/testsuite/MDAnalysisTests/analysis/test_hbonds.py @@ -20,6 +20,8 @@ # MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations. # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # +# Note: to be removed with MDAnalysis.analysis.hbonds.hbond_analysis in 2.0 + from __future__ import print_function, absolute_import @@ -78,7 +80,6 @@ def h(self, universe): kw = self.kwargs.copy() # kw.update(kwargs) h = MDAnalysis.analysis.hbonds.HydrogenBondAnalysis(universe, **kw) - # remove in 1.0 if kw['detect_hydrogens'] == 'heuristic': with pytest.warns(DeprecationWarning): h.run(verbose=False) diff --git a/testsuite/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py b/testsuite/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py index 0dab955832e..acb0331c244 100644 --- a/testsuite/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py +++ b/testsuite/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py @@ -205,24 +205,6 @@ def actual_function_int(t): np.array([0.33, 0.33, 5, 1, 0.1]), ) - def test_save(self, u, hydrogens, oxygens, nitrogens, tmpdir): - hbond = HBAC(u, - hydrogens=hydrogens, - acceptors=oxygens, - donors=nitrogens, - bond_type='continuous', - sample_time=0.06, - ) - hbond.run() - - tmpfile = os.path.join(str(tmpdir), 'hbondout.npz') - - hbond.save_results(tmpfile) - - loaded = np.load(tmpfile) - assert 'time' in loaded - assert 'results' in loaded - # setup errors def test_wronglength_DA(self, u, hydrogens, oxygens, nitrogens): with pytest.raises(ValueError): @@ -287,17 +269,6 @@ def test_unslicable_traj_VE(self, mock_read, u, hydrogens, oxygens, nitrogens): sample_time=0.06 ) - def test_save_without_run_VE(self, u, hydrogens, oxygens, nitrogens): - hbond = HBAC(u, - hydrogens=hydrogens, - acceptors=oxygens, - donors=nitrogens, - bond_type='continuous', - sample_time=0.06, - ) - with pytest.raises(ValueError): - hbond.save_results() - def test_repr(self, u, hydrogens, oxygens, nitrogens): hbond = HBAC(u, hydrogens=hydrogens,