Skip to content

Commit

Permalink
A Universe now holds onto its initialization kwargs.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dotsdl authored and kain88-de committed Apr 6, 2016
1 parent ee9210f commit fdd5dca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package/CHANGELOG
Expand Up @@ -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

Expand All @@ -32,6 +33,7 @@ API Changes
Enhancements

* Add conda build scripts (Issue #608)
* Added read-only property giving Universe init kwargs (Issue #292)

Fixes

Expand Down
11 changes: 11 additions & 0 deletions package/MDAnalysis/core/AtomGroup.py
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions testsuite/MDAnalysisTests/test_atomgroup.py
Expand Up @@ -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?')
Expand Down Expand Up @@ -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."""
Expand All @@ -2136,13 +2151,16 @@ 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)

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,
Expand Down

0 comments on commit fdd5dca

Please sign in to comment.