-
Notifications
You must be signed in to change notification settings - Fork 658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A Universe now holds onto its initialization kwargs. #813
Conversation
This is useful for external libraries, such as MDSynthesis (https://github.com/datreant/MDSynthesis), to re-initialize a Universe from its arguments.
The smallest PR you will ever merge. |
If you add a test I might consider reviewing this gigantic PR 😉 |
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.
Added some tests; see what you think. It's worth discussing if this should be an underscore attribute or whether it should be exposed (as a read-only property like |
u2 = MDAnalysis.Universe(u.filename, u.trajectory.filename, | ||
**u._kwargs) | ||
|
||
assert_(u2._kwargs['fake_kwarg'] is True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks good to me. Would it be to hard to compare the complete dictionaries or u
and u2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not at all. I'll add that in, too.
@@ -1724,7 +1724,19 @@ def test_set_dimensions(self): | |||
u.dimensions = np.array([10, 11, 12, 90, 90, 90]) | |||
assert_allclose(u.dimensions, box) | |||
|
|||
def test_universe_kwargs(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QC has a good point here making this a staticmethod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we don't use self
anywhere inside? Many of the other methods don't use self
anywhere inside either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like QC's rationale for making it @staticmethod
:
"This will improve performance and make it clear to users of your code that the function does not use any attributes of the class."
We should do it when we can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, done.
Looks good to me as soon as the tests go through. |
@@ -2121,11 +2133,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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does it matter if this kwarg is saved? The previous test should have covered this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous tests with a nonexistent kwarg. This is to test that we hold on to kwargs even after init is finished, which should be guaranteed since we do a deep copy of the kwarg dict. This was a convenient place to slot in a check on that.
I'm going to expose the stored kwargs with a read-only property |
Ready for merge if all is good. |
@@ -2136,6 +2150,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_bounds'] is True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bounds
Ah, sorry, really off my game today. Thanks @richardjgowers ! |
Will |
@orbeckst |
@@ -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(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you also need to remove the self variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Sorry!
Okay, my push isn't showing up. Not sure what's going on, here. |
github has some problems right now. |
Looks like it finally made it, and the tests pass: https://travis-ci.org/MDAnalysis/mdanalysis/builds/120977104 |
Fixes #292
Changes made in this Pull Request:
PR Checklist
This is useful for external libraries, such as MDSynthesis
(https://github.com/datreant/MDSynthesis), to re-initialize a Universe
from its arguments.