-
Notifications
You must be signed in to change notification settings - Fork 709
LinearDensity now working with residues/segments #3572
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
Conversation
LinearDensity did not work with grouping='residues' and grouping='segments'. This is now fixed, and more tests have been added. Changes made: MDAnalysis/analysis/lineardensity.py: - in _prepare(self): - try/except statement replaced by if statements checking the grouping specified for LinearDensity. - masses and charges are obtained in a way appropriate for each case. - in _single_frame(self): - 'positions' variable assignment changed from elem.centroid() to elem.atoms.centroid() so it works for residues/segments - in _conclude(self): - Floating point arithmetic issues could lead to negative radicands. A check was implemented to set numbers which are close to zero to precisely zero before the square root (for standard deviation) is calculated. MDAnalysisTests/analysis/test_lineardensity.py: - renamed previous test function and added expected masses and charges - added test functions for the other groupings with their respective expected masses and charges
Hello @BFedder! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2022-04-08 21:38:30 UTC |
Codecov Report
@@ Coverage Diff @@
## develop #3572 +/- ##
===========================================
+ Coverage 94.17% 94.22% +0.05%
===========================================
Files 190 190
Lines 24686 24729 +43
Branches 3330 3320 -10
===========================================
+ Hits 23247 23301 +54
+ Misses 1391 1380 -11
Partials 48 48
Continue to review full report at Codecov.
|
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.
Couple of quick comments on the first half of the code (didn't get pass _conclude for now)
Hi, and thanks for the feedback! I could change the code you mention above to the following to have if/elif/else, raise an exception in the case of unknown groupings, and use the same logic for residues, segments and fragments? # Get masses and charges for the selection
if self.grouping == "atoms":
self.masses = self._ags[0].masses
self.charges = self._ags[0].charges
elif self.grouping in ["residues", "segments", "fragments"]:
self.masses = np.array([elem.atoms.total_mass() for elem in group])
self.charges = np.array(
[elem.atoms.total_charge() for elem in group])
else:
raise RuntimeError(
f"{self.grouping} is not a valid value for grouping.") |
Flake8 can be really useful here, especially if combined with something like VS code. That being said, even of the coredevs are terrible at keeping up with pep8 🙄
Sure, at a quick glance that seems reasonable, although you might want to use an AttributeError instead of a RuntimeError here. |
I'm going to cycle the PR (opening and closing it), I'm not sure why gh actions is being silly today... |
well at least the webhooks got triggered, that should be enough for now until actions comes back (another day another gh service issue... https://www.githubstatus.com/) |
Changed the if/if/if for reading masses and charges to if/elif/else. Will now raise an AttributeError if invalid grouping is chosen. 'residues', 'segments' and 'fragments' now use the same logic (elem.atoms.total_mass()).
Exchanged assert_almost_equal with assert_allclose
Updated check of radicands in _conclude(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.
Good work to fix this!
I have some code simplifications and one physical question/addition. For me the center of geometry does not make sense to use as a reference point. It has no physical meaning.
* Changed test of radicand to only convert negative numbers to 0 * Now uses CoM rather than CoG for density calculations * Changed Class docstring to reflect change to CoM * Now uses parameter for compound instead of list comprehension when calculating masses, charges, and CoM * refactored test_lineardensity.py, make use of pytest functionality better * Added test for case of invalid grouping selection
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.
Very well. LGTM
Added second example to class docstring
@BFedder can you please resolve the conflicts? |
@IAlibay can you please check if your comments were addressed? |
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.
Just two small suggested changes - I'm directly suggesting them here since they are more "my style" than anything. Feel free to say no, I'll approve after.
Co-authored-by: Irfan Alibay <[email protected]>
Co-authored-by: Irfan Alibay <[email protected]>
Thanks, I have taken the suggestions on board |
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.
Thanks @BFedder
@MDAnalysis/gsoc-mentors - I can't merge this as a GSoC PR due to conflicts of interest. Can someone please take over the responsibility of merging this?
@PicoCentauri can you please have a final look and then squash-merge? Thanks. |
Thanks @BFedder for your fix! |
Fixes #3571
LinearDensity did not work with grouping='residues' and grouping='segments'.
This is now fixed, and more tests have been added.
Changes made:
MDAnalysis/analysis/lineardensity.py:
in _prepare(self):
specified for LinearDensity.
in _single_frame(self):
elem.atoms.centroid() so it works for residues/segments
in _conclude(self):
was implemented to set numbers which are close to zero to precisely zero
before the square root (for standard deviation) is calculated.
MDAnalysisTests/analysis/test_lineardensity.py:
masses and charges
Regarding my suggested change to _conclude(self):
I stuck with binsize=5 for the new tests for the other groupings for internal
consistency in the tests. With this bin size, the positions of the centres of
geometry are invariant, yielding standard deviations equal to zero. Floating point
imprecision made it possible for negative values on the order of e-13 to e-14 to
appear and cause a warning from np.sqrt() though, and that is why I added the
extra step of calculating the radicand and editing it where appropriate beforehand.
Other solutions are to just ignore this and accept nan values in the result arrays,
or change the bin size for the tests. I was hesitant to do the later to stay consistent
with the established test, but I'm happy to change this of course if that's the consensus.
PR Checklist