Skip to content
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

Hbond excl fix #3242

Merged
merged 12 commits into from
Apr 28, 2021
Merged

Hbond excl fix #3242

merged 12 commits into from
Apr 28, 2021

Conversation

richardjgowers
Copy link
Member

Fixes #2987

Changes made in this Pull Request:

PR Checklist

  • Tests?
  • Docs?
  • CHANGELOG updated?
  • Issue raised/referenced?

@pep8speaks
Copy link

pep8speaks commented Apr 25, 2021

Hello @richardjgowers! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 328:80: E501 line too long (93 > 79 characters)
Line 433:80: E501 line too long (99 > 79 characters)

Line 87:63: W605 invalid escape sequence '('
Line 87:69: W605 invalid escape sequence ')'
Line 87:80: E501 line too long (80 > 79 characters)

Comment last updated at 2021-04-28 07:32:55 UTC

@codecov
Copy link

codecov bot commented Apr 25, 2021

Codecov Report

Merging #3242 (50531d9) into master (361ca7e) will increase coverage by 0.84%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3242      +/-   ##
==========================================
+ Coverage   90.99%   91.84%   +0.84%     
==========================================
  Files         162      167       +5     
  Lines       22217    22737     +520     
  Branches     3203     3203              
==========================================
+ Hits        20216    20882     +666     
- Misses       1378     1771     +393     
+ Partials      623       84     -539     
Impacted Files Coverage Δ
package/MDAnalysis/lib/distances.py 98.42% <ø> (+0.94%) ⬆️
...age/MDAnalysis/analysis/hbonds/hbond_autocorrel.py 95.00% <100.00%> (+7.10%) ⬆️
package/MDAnalysis/lib/_cutil.pyx 100.00% <100.00%> (ø)
package/MDAnalysis/topology/tpr/obj.py 96.82% <0.00%> (-3.18%) ⬇️
...onality_reduction/DimensionalityReductionMethod.py 97.14% <0.00%> (-2.86%) ⬇️
package/MDAnalysis/due.py 56.09% <0.00%> (ø)
package/MDAnalysis/analysis/legacy/x3dna.py 0.00% <0.00%> (ø)
package/MDAnalysis/tests/datafiles.py 31.25% <0.00%> (ø)
package/MDAnalysis/tests/__init__.py 100.00% <0.00%> (ø)
package/MDAnalysis/analysis/legacy/__init__.py 0.00% <0.00%> (ø)
... and 88 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 361ca7e...50531d9. Read the comment docs.

Copy link
Member

@IAlibay IAlibay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of tests & logic comments.

Also my cython is probably too green to probably review stuff, if someone with more experience can have a look that'd be great - pinging @MDAnalysis/coredevs

package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py Outdated Show resolved Hide resolved
package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py Outdated Show resolved Hide resolved
pair = capped_distance(self.h.positions, self.a.positions, max_cutoff=self.d_crit, box=box,
return_distances=False)
if not self.exclusions is None:
pair = pair[~ _in2d(pair, self.exclusions)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised there was no suitable numpy operation for this :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The below passes the analysis suite locally without using a custom Cython function. Maybe double check with Richard if his mixture of Cython/C++ is more efficient -- probably any advantage is related to the integer only nature of the data structure?

--- a/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py
+++ b/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py
@@ -210,6 +210,7 @@ from six import raise_from
 
 import numpy as np
 import scipy.optimize
+from scipy.spatial.distance import cdist
 
 import warnings
 
@@ -433,7 +434,7 @@ class HydrogenBondAutoCorrel(object):
         pair = capped_distance(self.h.positions, self.a.positions, max_cutoff=self.d_crit, box=box,
                                return_distances=False)
         if not self.exclusions is None:
-            pair = pair[~ _in2d(pair, self.exclusions)]
+            pair = pair[~ (cdist(pair, self.exclusions)==0).any(axis=1)]
 
         hidx, aidx = np.transpose(pair)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use cdist but it's odd to use an O(n2) solution when you can just use a set

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong view on it--if this is genuinely a performance-critical part of the code maybe the custom maintenance burden is worth it.

package/MDAnalysis/lib/_cutil.pyx Show resolved Hide resolved
package/MDAnalysis/lib/_cutil.pyx Show resolved Hide resolved
@IAlibay
Copy link
Member

IAlibay commented Apr 25, 2021

Forgot to mention, as far as I understand the old tests weren't picking up that this code path wasn't working. If that's the case, are we sure they are now working properly?

Copy link
Member

@IAlibay IAlibay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tests. For some reason codecov isn't recognising the ValueError test, just proposing a match to make sure it's actually picking up the right thing.

testsuite/MDAnalysisTests/lib/test_cutil.py Outdated Show resolved Hide resolved
package/MDAnalysis/lib/_cutil.pyx Show resolved Hide resolved
pair = capped_distance(self.h.positions, self.a.positions, max_cutoff=self.d_crit, box=box,
return_distances=False)
if not self.exclusions is None:
pair = pair[~ _in2d(pair, self.exclusions)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The below passes the analysis suite locally without using a custom Cython function. Maybe double check with Richard if his mixture of Cython/C++ is more efficient -- probably any advantage is related to the integer only nature of the data structure?

--- a/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py
+++ b/package/MDAnalysis/analysis/hbonds/hbond_autocorrel.py
@@ -210,6 +210,7 @@ from six import raise_from
 
 import numpy as np
 import scipy.optimize
+from scipy.spatial.distance import cdist
 
 import warnings
 
@@ -433,7 +434,7 @@ class HydrogenBondAutoCorrel(object):
         pair = capped_distance(self.h.positions, self.a.positions, max_cutoff=self.d_crit, box=box,
                                return_distances=False)
         if not self.exclusions is None:
-            pair = pair[~ _in2d(pair, self.exclusions)]
+            pair = pair[~ (cdist(pair, self.exclusions)==0).any(axis=1)]
 
         hidx, aidx = np.transpose(pair)


for i in range(arr2.shape[0]):
p = pair[np.intp_t, np.intp_t](arr2[i, 0], arr2[i, 1])
hits.insert(p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need a comment or two about the C++ magic happening here; I suppose we are slowly growing expertise with devs familiar with C++ (I'm not one of them).

richardjgowers and others added 3 commits April 26, 2021 15:45
Co-authored-by: Irfan Alibay <IAlibay@users.noreply.github.com>
Co-authored-by: Irfan Alibay <IAlibay@users.noreply.github.com>
@richardjgowers
Copy link
Member Author

WRT codecov, I'm guessing Cython linenumbers and linetracing isn't a match made in heaven.

richardjgowers and others added 3 commits April 28, 2021 08:19
Co-authored-by: Oliver Beckstein <orbeckst@gmail.com>
Co-authored-by: Irfan Alibay <IAlibay@users.noreply.github.com>
Copy link
Member

@IAlibay IAlibay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@IAlibay IAlibay merged commit 76c9dc0 into master Apr 28, 2021
@IAlibay IAlibay deleted the hbond_excl_fix branch May 29, 2022 12:55
@IAlibay IAlibay added the defect label Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants