From ff50be9e9e12307dd77cbb97fc487687039307e3 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Wed, 5 May 2021 21:12:19 +0100 Subject: [PATCH 01/11] Update DensityAnalysis to use new Results class --- package/MDAnalysis/analysis/density.py | 21 ++++++++++++------- .../MDAnalysisTests/analysis/test_density.py | 13 +++++++++--- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index 2895fde798c..994ee696b30 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -166,14 +166,15 @@ import MDAnalysis from MDAnalysis.core import groups -from MDAnalysis.lib.util import fixedwidth_bins, iterable, asiterable +from MDAnalysis.lib.util import (fixedwidth_bins, iterable, asiterable, + deprecate,) from MDAnalysis.lib import NeighborSearch as NS from MDAnalysis import NoDataError, MissingDataWarning from .. import units from ..lib import distances from MDAnalysis.lib.log import ProgressBar -from .base import AnalysisBase +from .base import AnalysisBase, Results import logging @@ -208,9 +209,9 @@ class DensityAnalysis(AnalysisBase): zdim : float (optional) User defined z dimension box edge in ångström. - Returns - ------- - :class:`Density` + Attributes + ---------- + results.density : :class:`Density` A :class:`Density` instance containing a physical density of units :math:`Angstrom^{-3}`. @@ -457,7 +458,8 @@ def _prepare(self): self._edges = edges self._arange = arange self._bins = bins - self.density = None + self.results = Results() + self.results.density = None def _single_frame(self): h, _ = np.histogramdd(self._atomgroup.positions, @@ -476,7 +478,12 @@ def _conclude(self): units={'length': "Angstrom"}, parameters={'isDensity': False}) density.make_density() - self.density = density + self.results.density = density + + @deprecate(release="2.0.0", remove="3.0.0") + @property + def density(self): + return self.results.density @staticmethod def _set_user_grid(gridcenter, xdim, ydim, zdim, smin, smax): diff --git a/testsuite/MDAnalysisTests/analysis/test_density.py b/testsuite/MDAnalysisTests/analysis/test_density.py index 81242bb5f59..6bdb787751b 100644 --- a/testsuite/MDAnalysisTests/analysis/test_density.py +++ b/testsuite/MDAnalysisTests/analysis/test_density.py @@ -158,10 +158,10 @@ def check_DensityAnalysis(self, ag, ref_meandensity, D = density.DensityAnalysis(ag, delta=self.delta, **kwargs).run(**runargs) assert_almost_equal(D.density.grid.mean(), ref_meandensity, err_msg="mean density does not match") - D.density.export(self.outfile) + D.results.density.export(self.outfile) D2 = density.Density(self.outfile) - assert_almost_equal(D.density.grid, D2.grid, decimal=self.precision, + assert_almost_equal(D.results.density.grid, D2.grid, decimal=self.precision, err_msg="DX export failed: different grid sizes") @pytest.mark.parametrize("mode", ("static", "dynamic")) @@ -211,7 +211,7 @@ def test_userdefn_boxshape(self, universe): universe.select_atoms(self.selections['static']), delta=1.0, xdim=8.0, ydim=12.0, zdim=17.0, gridcenter=self.gridcenters['static_defined']).run() - assert D.density.grid.shape == (8, 12, 17) + assert D.results.density.grid.shape == (8, 12, 17) def test_warn_userdefn_padding(self, universe): regex = (r"Box padding \(currently set at 1\.0\) is not used " @@ -294,6 +294,13 @@ def test_ValueError_noatomgroup(self, universe): D = density.DensityAnalysis( universe.select_atoms(self.selections['none'])).run(step=5) + def test_warn_results_deprecated(self, universe): + D = density.DensityAnalysis( + universe.select_atoms(self.selections['static'])) + with pytest.warns(UserWarning): + D.density + + class TestGridImport(object): @block_import('gridData') From c414ad190a734b21dd16607db2b848a8c730d553 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Wed, 5 May 2021 21:42:46 +0100 Subject: [PATCH 02/11] Update changelog and docstring --- package/CHANGELOG | 5 ++++- package/MDAnalysis/analysis/density.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 153319ddc7f..9bd0028284d 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -18,7 +18,7 @@ The rules for this file: calcraven,xiki-tempula, mieczyslaw, manuel.nuno.melo, PicoCentauri, hanatok, rmeli, aditya-kamath, tirkarthi, LeonardoBarneschi, hejamu, biogen98, orioncohen, z3y50n, hp115, ojeda-e, thadanipaarth, HenryKobin, - 1ut, sulays, PicoCentauri, ahy3nz + 1ut, sulays, ahy3nz * 2.0.0 @@ -173,6 +173,9 @@ Enhancements checking if it can be used in parallel analysis. (Issue #2996, PR #2950) Changes + * `DensityAnalysis` now uses the `results.density` attribute for storing + data. The `DensityAnalysis.density` attribute is now deprecated + (Issue #3233) * Fixed inaccurate docstring inside the RMSD class (Issue #2796, PR #3134) * TPRParser now loads TPR files with `tpr_resid_from_one=True` by default, which starts TPR resid indexing from 1 (instead of 0 as in 1.x) (Issue #2364, PR #3152) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index 994ee696b30..170ed6e50a4 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -385,6 +385,9 @@ class DensityAnalysis(AnalysisBase): .. versionadded:: 1.0.0 .. versionchanged:: 2.0.0 :func:`_set_user_grid` is now a method of :class:`DensityAnalysis`. + .. deprecated:: 2.0.0 + The :attr:`density` attribute is deprecated in favour of + :attr:`results.density` """ def __init__(self, atomgroup, delta=1.0, From e700621dce6d9d55fcc6c809be261c4cbd47186e Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 12:29:10 +0100 Subject: [PATCH 03/11] fix tests and decorator --- package/MDAnalysis/analysis/density.py | 8 +++++++- testsuite/MDAnalysisTests/analysis/test_density.py | 8 +++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index 170ed6e50a4..35c6ce79cbb 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -483,9 +483,15 @@ def _conclude(self): density.make_density() self.results.density = density - @deprecate(release="2.0.0", remove="3.0.0") @property + @deprecate(release="2.0.0", remove="3.0.0") def density(self): + """:class:`Density` results attribute + + + .. deprecated:: 2.0.0 + Please use :attr:`DensityAnalysis.results.density` instead + """ return self.results.density @staticmethod diff --git a/testsuite/MDAnalysisTests/analysis/test_density.py b/testsuite/MDAnalysisTests/analysis/test_density.py index 6bdb787751b..a572a4e8135 100644 --- a/testsuite/MDAnalysisTests/analysis/test_density.py +++ b/testsuite/MDAnalysisTests/analysis/test_density.py @@ -156,7 +156,7 @@ def check_DensityAnalysis(self, ag, ref_meandensity, runargs = runargs if runargs else {} with tmpdir.as_cwd(): D = density.DensityAnalysis(ag, delta=self.delta, **kwargs).run(**runargs) - assert_almost_equal(D.density.grid.mean(), ref_meandensity, + assert_almost_equal(D.results.density.grid.mean(), ref_meandensity, err_msg="mean density does not match") D.results.density.export(self.outfile) @@ -297,8 +297,10 @@ def test_ValueError_noatomgroup(self, universe): def test_warn_results_deprecated(self, universe): D = density.DensityAnalysis( universe.select_atoms(self.selections['static'])) - with pytest.warns(UserWarning): - D.density + D.run(stop=1) + wmsg = "`density` will be removed in release 3.0.0" + with pytest.warns(DeprecationWarning, match=wmsg): + assert_equal(D.density.grid, D.results.density.grid) class TestGridImport(object): From 4f092688441070b27333b01b3d94d17295bc9363 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 12:29:45 +0100 Subject: [PATCH 04/11] Update issue number --- package/CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index fb6a85a84b8..7cbdf2c750d 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -176,7 +176,7 @@ Enhancements Changes * `DensityAnalysis` now uses the `results.density` attribute for storing data. The `DensityAnalysis.density` attribute is now deprecated - (Issue #3233) + (Issue #3261) * `GNMAnalysis`, `LinearDensity`, `PersistenceLength` and `AnalysisFromFunction` use the `results` attribute. * Fixed inaccurate docstring inside the RMSD class (Issue #2796, PR #3134) From 594e38b22627605536667d2031bfb456a4112638 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 12:33:34 +0100 Subject: [PATCH 05/11] fix pep8 --- testsuite/MDAnalysisTests/analysis/test_density.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_density.py b/testsuite/MDAnalysisTests/analysis/test_density.py index a572a4e8135..09840e52560 100644 --- a/testsuite/MDAnalysisTests/analysis/test_density.py +++ b/testsuite/MDAnalysisTests/analysis/test_density.py @@ -161,8 +161,10 @@ def check_DensityAnalysis(self, ag, ref_meandensity, D.results.density.export(self.outfile) D2 = density.Density(self.outfile) - assert_almost_equal(D.results.density.grid, D2.grid, decimal=self.precision, - err_msg="DX export failed: different grid sizes") + assert_almost_equal( + D.results.density.grid, D2.grid, decimal=self.precision, + err_msg="DX export failed: different grid sizes" + ) @pytest.mark.parametrize("mode", ("static", "dynamic")) def test_run(self, mode, universe, tmpdir): From 70ceb073abd0d0e4110320743fad8ef149a9c498 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 13:34:24 +0100 Subject: [PATCH 06/11] fix docs --- package/MDAnalysis/analysis/density.py | 43 +++++++++---------- .../MDAnalysisTests/analysis/test_density.py | 2 +- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index 35c6ce79cbb..d52374ad2d1 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -74,8 +74,8 @@ ow = u.select_atoms("name OW") D = DensityAnalysis(ow, delta=1.0) D.run() - D.density.convert_density('TIP4P') - D.density.export("water.dx", type="double") + D.results.density.convert_density('TIP4P') + D.results.density.export("water.dx", type="double") The positions of all water oxygens (the :class:`AtomGroup` `ow`) are histogrammed on a grid with spacing *delta* = 1 Å. Initially the density is @@ -87,9 +87,9 @@ can be read in VMD_, Chimera_, or PyMOL_. The :class:`Density` object is accessible as the -:attr:`DensityAnalysis.density` attribute. In particular, the data for the -density is stored as a NumPy array in :attr:`Density.grid`, which can be -processed in any manner. +:attr:`DensityAnalysis.results.density` attribute. In particular, the data +for the density is stored as a NumPy array in :attr:`Density.grid`, which can +be processed in any manner. Creating densities @@ -102,10 +102,12 @@ :members: :inherited-members: run - .. attribute:: density + .. attribute:: results.density - After the analysis (see the :meth:`~DensityAnalysis.run` method), the resulting density is - stored in the :attr:`density` attribute as a :class:`Density` instance. + After the analysis (see the :meth:`~DensityAnalysis.run` method), the + resulting density is stored in the :attr:`results.density` attribute as + a :class:`Density` instance. Note: this replaces the now deprecated + :attr:`density` attribute. .. automethod:: _set_user_grid @@ -283,11 +285,11 @@ class DensityAnalysis(AnalysisBase): ow = u.select_atoms("name OW") D = density.DensityAnalysis(ow, delta=1.0) D.run() - D.density.convert_density('TIP4P') + D.results.density.convert_density('TIP4P') The positions of all water oxygens are histogrammed on a grid with spacing *delta* = 1 Å and stored as a :class:`Density` object in the attribute - :attr:`DensityAnalysis.density`. + :attr:`DensityAnalysis.results.density`. .. rubric:: Working with a density @@ -305,7 +307,7 @@ class DensityAnalysis(AnalysisBase): density in units of Å\ :sup:`-3`. If you are interested in recovering the underlying **probability density**, simply divide by the sum:: - probability_density = D.density.grid / D.density.grid.sum() + probability_density = D.results.density.grid / D.results.density.grid.sum() Similarly, if you would like to recover a grid containing a **histogram of atom counts**, simply multiply by the volume `dV` of each bin (or voxel); @@ -315,10 +317,10 @@ class DensityAnalysis(AnalysisBase): import numpy as np # ensure that the density is A^{-3} - D.density.convert_density("A^{-3}") + D.results.density.convert_density("A^{-3}") - dV = np.prod(D.density.delta) - atom_count_histogram = D.density.grid * dV + dV = np.prod(D.results.density.delta) + atom_count_histogram = D.results.density.grid * dV .. rubric:: Writing the density to a file @@ -331,7 +333,7 @@ class DensityAnalysis(AnalysisBase): `_ ``water.dx`` that can be read with VMD, PyMOL, or Chimera:: - D.density.export("water.dx", type="double") + D.results.density.export("water.dx", type="double") .. rubric:: Example: Water density in the whole simulation @@ -484,14 +486,11 @@ def _conclude(self): self.results.density = density @property - @deprecate(release="2.0.0", remove="3.0.0") def density(self): - """:class:`Density` results attribute - - - .. deprecated:: 2.0.0 - Please use :attr:`DensityAnalysis.results.density` instead - """ + wmsg = ("The `density` attribute was deprecated in MDAnalysis 2.0.0 " + "and will be removed in MDAnalysis 3.0.0. Please use " + "`results.density` instead") + warnings.warn(wmsg, DeprecationWarning) return self.results.density @staticmethod diff --git a/testsuite/MDAnalysisTests/analysis/test_density.py b/testsuite/MDAnalysisTests/analysis/test_density.py index 09840e52560..bd76449a921 100644 --- a/testsuite/MDAnalysisTests/analysis/test_density.py +++ b/testsuite/MDAnalysisTests/analysis/test_density.py @@ -300,7 +300,7 @@ def test_warn_results_deprecated(self, universe): D = density.DensityAnalysis( universe.select_atoms(self.selections['static'])) D.run(stop=1) - wmsg = "`density` will be removed in release 3.0.0" + wmsg = "The `density` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): assert_equal(D.density.grid, D.results.density.grid) From 59b1e3d3e7f22dd4556a31f96aa56b2b3e8fc620 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 13:52:27 +0100 Subject: [PATCH 07/11] Add density back in the docstring --- package/MDAnalysis/analysis/density.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index d52374ad2d1..42c8f3d56f3 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -217,6 +217,10 @@ class DensityAnalysis(AnalysisBase): A :class:`Density` instance containing a physical density of units :math:`Angstrom^{-3}`. + density : :class:`Density` + Deprecated alias to the :attr:`results.density`. Will be removed + in MDAnalysis 3.0.0. + Raises ------ ValueError From 37fdc8eec30d77561523007b80856be4c038b1cc Mon Sep 17 00:00:00 2001 From: Irfan Alibay Date: Thu, 6 May 2021 14:15:48 +0100 Subject: [PATCH 08/11] Update density.py --- package/MDAnalysis/analysis/density.py | 1 - 1 file changed, 1 deletion(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index 42c8f3d56f3..e573484b343 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -467,7 +467,6 @@ def _prepare(self): self._edges = edges self._arange = arange self._bins = bins - self.results = Results() self.results.density = None def _single_frame(self): From 138ca976b84ab61f02fd7a8122a4526a8724186f Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 14:21:10 +0100 Subject: [PATCH 09/11] unused import --- package/MDAnalysis/analysis/density.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index e573484b343..075ac35baad 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -176,7 +176,7 @@ from ..lib import distances from MDAnalysis.lib.log import ProgressBar -from .base import AnalysisBase, Results +from .base import AnalysisBase import logging From 0989ee6ff5b66bbe58c97b156e84297f82bf8dbe Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 14:42:01 +0100 Subject: [PATCH 10/11] Remove unecessary assignment --- package/MDAnalysis/analysis/density.py | 1 - 1 file changed, 1 deletion(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index 075ac35baad..c3617745cad 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -467,7 +467,6 @@ def _prepare(self): self._edges = edges self._arange = arange self._bins = bins - self.results.density = None def _single_frame(self): h, _ = np.histogramdd(self._atomgroup.positions, From ccaa89c1371c83691ffda1a7ec503367ac15db35 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Thu, 6 May 2021 17:21:23 +0100 Subject: [PATCH 11/11] fix docstring and add changelog deprecation entry --- package/CHANGELOG | 3 +++ package/MDAnalysis/analysis/density.py | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 7cbdf2c750d..e49f3f5b326 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -223,6 +223,9 @@ Changes * Added OpenMM coordinate and topology converters (Issue #2863, PR #2917) Deprecations + * The `density` attribute of `analysis.density.DensityAnalysis` is now + deprecated in favour of `results.density`. It will be removed in 3.0.0 + (Issue #3261) * In 2.1.0 the TRZReader will default to a dt of 1.0 ps when failing to read it from the input TRZ trajectory. diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index c3617745cad..dda724726d9 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -218,8 +218,11 @@ class DensityAnalysis(AnalysisBase): :math:`Angstrom^{-3}`. density : :class:`Density` - Deprecated alias to the :attr:`results.density`. Will be removed - in MDAnalysis 3.0.0. + Alias to the :attr:`results.density`. + + .. deprecated:: 2.0.0 + Will be removed in MDAnalysis 3.0.0. Please use + :attr:`results.density` instead. Raises ------ @@ -391,9 +394,8 @@ class DensityAnalysis(AnalysisBase): .. versionadded:: 1.0.0 .. versionchanged:: 2.0.0 :func:`_set_user_grid` is now a method of :class:`DensityAnalysis`. - .. deprecated:: 2.0.0 - The :attr:`density` attribute is deprecated in favour of - :attr:`results.density` + :class:`Density` results are now stored in a + :class:`MDAnalysis.analysis.base.Results` instance. """ def __init__(self, atomgroup, delta=1.0,