From 41ab4d17144ae63dee44478955666807f5492d29 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Wed, 26 Aug 2020 17:54:27 -0700 Subject: [PATCH] deprecate core.universe.as_Universe - see #2920 - add deprecation note - add test - update CHANGELOG for 1.0.1 --- package/CHANGELOG | 1 + package/MDAnalysis/core/universe.py | 4 ++- .../MDAnalysisTests/core/test_universe.py | 34 +++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index fe2dc564b0b..ce3d3d9429f 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -50,6 +50,7 @@ Deprecations * waterdynamics.HydrogenBondLifetimes will be removed in 2.0.0 and replaced with hydrogenbonds.HydrogenBondAnalysis.lifetime() (#2547) * lib.util.echo() will be removed in 2.0.0 + * core.universe.as_Universe() will be removed in 2.0.0 06/09/20 richardjgowers, kain88-de, lilyminium, p-j-smith, bdice, joaomcteixeira, diff --git a/package/MDAnalysis/core/universe.py b/package/MDAnalysis/core/universe.py index 25f4d1c3761..2ae4343247b 100644 --- a/package/MDAnalysis/core/universe.py +++ b/package/MDAnalysis/core/universe.py @@ -102,6 +102,8 @@ from .topologyattrs import AtomAttr, ResidueAttr, SegmentAttr from .topologyobjects import TopologyObject +from ..lib.util import deprecate + logger = logging.getLogger("MDAnalysis.core.universe") @@ -1293,7 +1295,7 @@ def _fragdict(self): return fragdict -# TODO: what is the point of this function??? +@deprecate(release="1.0.1", remove="2.0.0") def as_Universe(*args, **kwargs): """Return a universe from the input arguments. diff --git a/testsuite/MDAnalysisTests/core/test_universe.py b/testsuite/MDAnalysisTests/core/test_universe.py index 5f8d73c0264..29a5d5e80f8 100644 --- a/testsuite/MDAnalysisTests/core/test_universe.py +++ b/testsuite/MDAnalysisTests/core/test_universe.py @@ -601,7 +601,7 @@ def test_add_charges(self, universe, toadd, attrname, default): assert hasattr(universe.atoms, attrname) assert getattr(universe.atoms, attrname)[0] == default - + @pytest.mark.parametrize( 'attr,values', ( ('bonds', [(1, 0), (1, 2)]), @@ -636,7 +636,7 @@ def test_add_connection(self, universe, attr, values): def add_connection_error(self, universe, attr, values): with pytest.raises(ValueError): universe.add_TopologyAttr(attr, values) - + def _a_or_reversed_in_b(a, b): """ @@ -694,7 +694,7 @@ def _check_invalid_addition(self, u, attr, to_add, err_msg): with pytest.raises(ValueError) as excinfo: _add_func(to_add) assert err_msg in str(excinfo.value) - + @pytest.mark.parametrize( 'attr,values', small_atom_indices ) @@ -752,7 +752,7 @@ def test_add_topologyobjects_wrong_universe_error(self, universe, empty, attr, v 'attr,values', large_atom_indices ) def test_add_topologygroups_to_populated(self, universe, attr, values): - topologygroup = mda.core.topologyobjects.TopologyGroup(np.array(values), + topologygroup = mda.core.topologyobjects.TopologyGroup(np.array(values), universe) self._check_valid_added_to_populated(universe, attr, values, topologygroup) @@ -760,15 +760,15 @@ def test_add_topologygroups_to_populated(self, universe, attr, values): 'attr,values', small_atom_indices ) def test_add_topologygroup_wrong_universe_error(self, universe, empty, attr, values): - tg = mda.core.topologyobjects.TopologyGroup(np.array(values), + tg = mda.core.topologyobjects.TopologyGroup(np.array(values), universe) self._check_invalid_addition(empty, attr, tg, 'different Universes') - + @pytest.mark.parametrize( 'attr,values', small_atom_indices ) def test_add_topologygroup_different_universe(self, universe, empty, attr, values): - tg = mda.core.topologyobjects.TopologyGroup(np.array(values), + tg = mda.core.topologyobjects.TopologyGroup(np.array(values), universe) self._check_valid_added_to_empty(empty, attr, values, tg.to_indices()) @@ -809,7 +809,7 @@ def test_add_wrong_number_of_atoms_error(self, universe, attr, n): 'tuples with {} atom indices').format(attr, n) idx = [(0, 1), (0, 1, 2), (8, 22, 1, 3), (5, 3, 4, 2)] self._check_invalid_addition(universe, attr, idx, errmsg) - + def test_add_bonds_refresh_fragments(self, empty): with pytest.raises(NoDataError): getattr(empty.atoms, 'fragments') @@ -833,7 +833,7 @@ def test_roundtrip(self, empty, attr, values): _delete_func(values) u_attr = getattr(empty, attr) assert len(u_attr) == 0 - + class TestDeleteTopologyObjects(object): TOP = {'bonds': [(0, 1), (2, 3), (3, 4), (4, 5), (7, 8)], @@ -879,7 +879,7 @@ def _check_valid_deleted(self, u, attr, values, to_delete): assert len(u_attr) == original_length-len(values) not_deleted = [x for x in self.TOP[attr] if list(x) not in values] - assert all([x in u_attr.indices or x[::-1] in u_attr.indices + assert all([x in u_attr.indices or x[::-1] in u_attr.indices for x in not_deleted]) def _check_invalid_deleted(self, u, attr, to_delete, err_msg): @@ -923,7 +923,7 @@ def test_delete_atomgroup_wrong_universe_error(self, universe, universe2, attr, def test_delete_missing_atomgroup(self, universe, attr, values): ag = [universe.atoms[x] for x in values] self._check_invalid_deleted(universe, attr, ag, 'Cannot delete nonexistent') - + @pytest.mark.parametrize( 'attr,values', existing_atom_indices ) @@ -969,7 +969,7 @@ def test_delete_topologygroup_different_universe(self, universe, universe2, attr arr = np.array(values) tg = mda.core.topologyobjects.TopologyGroup(arr, universe2) self._check_valid_deleted(universe, attr, values, tg.to_indices()) - + @pytest.mark.parametrize( 'attr,n', ( ('bonds', 2), @@ -982,8 +982,8 @@ def test_delete_wrong_number_of_atoms_error(self, universe, attr, n): idx = [(0, 1), (0, 1, 2), (8, 22, 1, 3), (5, 3, 4, 2)] errmsg = ('{} must be an iterable of ' 'tuples with {} atom indices').format(attr, n) - self._check_invalid_deleted(universe, attr, idx, errmsg) - + self._check_invalid_deleted(universe, attr, idx, errmsg) + @pytest.mark.parametrize( 'attr,values', existing_atom_indices ) @@ -1020,7 +1020,7 @@ def test_roundtrip(self, universe, attr, values): assert_array_equal(u_attr.indices, nu_attr.indices) - + class TestAllCoordinatesKwarg(object): @pytest.fixture(scope='class') def u_GRO_TRR(self): @@ -1131,3 +1131,7 @@ def test_empty_creation_raises_error(self): with pytest.raises(TypeError) as exc: u = mda.Universe() assert 'Universe.empty' in str(exc.value) + +def test_as_Universe_deprecation(): + with pytest.deprecated_call(): + _ = mda.core.universe.as_Universe(PSF, DCD)