From fdd5dca1216e2a8f43952144fd8a7ecc9e47d5d9 Mon Sep 17 00:00:00 2001 From: David Dotson Date: Tue, 5 Apr 2016 21:40:32 -0700 Subject: [PATCH] A Universe now holds onto its initialization kwargs. * A Universe now holds onto its initialization kwargs. (Fixes #292) This is useful for external libraries, such as MDSynthesis (https://github.com/datreant/MDSynthesis), to re-initialize a Universe from its arguments. * Added tests for Universe keeping a copy of its init kwargs. Basic test that it holds on to these as we'd expect in test_atomgroup.TestUniverse. Another test that these work as expected in guess_bonds tests. * Test equality of full kwargs dict * Exposed Universe kwargs with read-only property * Updated changelog with Universe kwargs exposure * guess_bounds -> guess_bonds * Made test for universe kwargs staticmethod since we don't use self * Removed `self` from staticmethod test. --- package/CHANGELOG | 4 +++- package/MDAnalysis/core/AtomGroup.py | 11 +++++++++++ testsuite/MDAnalysisTests/test_atomgroup.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index a6db1d2d279..6bde458705b 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -13,7 +13,8 @@ The rules for this file: * release numbers follow "Semantic Versioning" http://semver.org ------------------------------------------------------------------------------ -??/??/16 jandom, abhinavgupta94, orbeckst, kain88-de, hainm, jdetle, jbarnoud +??/??/16 jandom, abhinavgupta94, orbeckst, kain88-de, hainm, jdetle, jbarnoud, + dotsdl * 0.15.0 @@ -32,6 +33,7 @@ API Changes Enhancements * Add conda build scripts (Issue #608) + * Added read-only property giving Universe init kwargs (Issue #292) Fixes diff --git a/package/MDAnalysis/core/AtomGroup.py b/package/MDAnalysis/core/AtomGroup.py index 18b053efd4d..f8991d81dda 100644 --- a/package/MDAnalysis/core/AtomGroup.py +++ b/package/MDAnalysis/core/AtomGroup.py @@ -3966,6 +3966,10 @@ def __init__(self, *args, **kwargs): from ..topology.base import TopologyReader from ..coordinates.base import ProtoReader + # hold on to copy of kwargs; used by external libraries that + # reinitialize universes + self._kwargs = copy.deepcopy(kwargs) + # managed attribute holding Reader self._trajectory = None @@ -4262,6 +4266,13 @@ def universe(self): # which might be undesirable if it has a __del__ method. It is also cleaner than a weakref. return self + @property + def kwargs(self): + """Keyword arguments used to initialize this universe (read-only). + + """ + return copy.deepcopy(self._kwargs) + @property @cached('fragments') def fragments(self): diff --git a/testsuite/MDAnalysisTests/test_atomgroup.py b/testsuite/MDAnalysisTests/test_atomgroup.py index 38fd06759a2..c242f26bda8 100644 --- a/testsuite/MDAnalysisTests/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/test_atomgroup.py @@ -1724,7 +1724,20 @@ def test_set_dimensions(self): u.dimensions = np.array([10, 11, 12, 90, 90, 90]) assert_allclose(u.dimensions, box) + @staticmethod + def test_universe_kwargs(): + u = MDAnalysis.Universe(PSF, PDB_small, fake_kwarg=True) + assert_equal(len(u.atoms), 3341, "Loading universe failed somehow") + + assert_(u.kwargs['fake_kwarg'] is True) + # initialize new universe from pieces of existing one + u2 = MDAnalysis.Universe(u.filename, u.trajectory.filename, + **u.kwargs) + + assert_(u2.kwargs['fake_kwarg'] is True) + assert_equal(u.kwargs, u2.kwargs) + class TestPBCFlag(TestCase): @dec.skipif(parser_not_found('TRZ'), 'TRZ parser not available. Are you using python 3?') @@ -2121,11 +2134,13 @@ def _check_universe(self, u): assert_equal(len(u.atoms[3].bonds), 2) assert_equal(len(u.atoms[4].bonds), 1) assert_equal(len(u.atoms[5].bonds), 1) + assert_('guess_bonds' in u.kwargs) def test_universe_guess_bonds(self): """Test that making a Universe with guess_bonds works""" u = MDAnalysis.Universe(two_water_gro, guess_bonds=True) self._check_universe(u) + assert_(u.kwargs['guess_bonds'] is True) def test_universe_guess_bonds_no_vdwradii(self): """Make a Universe that has atoms with unknown vdwradii.""" @@ -2136,6 +2151,8 @@ def test_universe_guess_bonds_with_vdwradii(self): u = MDAnalysis.Universe(two_water_gro_nonames, guess_bonds=True, vdwradii=self.vdw) self._check_universe(u) + assert_(u.kwargs['guess_bonds'] is True) + assert_equal(self.vdw, u.kwargs['vdwradii']) def test_universe_guess_bonds_off(self): u = MDAnalysis.Universe(two_water_gro_nonames, guess_bonds=False) @@ -2143,6 +2160,7 @@ def test_universe_guess_bonds_off(self): assert_equal(len(u.bonds), 0) assert_equal(len(u.angles), 0) assert_equal(len(u.dihedrals), 0) + assert_(u.kwargs['guess_bonds'] is False) def _check_atomgroup(self, ag, u): """Verify that the AtomGroup made bonds correctly,