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

add ignore_same_residue kwarg to InterRDF #4161

Merged
merged 15 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
??/??/?? IAlibay
??/??/?? IAlibay, orionarcher

* 2.6.0

Fixes

Enhancements
* Added a ignore_same_residue kwarg to InterRDF (PR #4161)
orionarcher marked this conversation as resolved.
Show resolved Hide resolved

Changes

Expand Down
11 changes: 11 additions & 0 deletions package/MDAnalysis/analysis/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class InterRDF(AnalysisBase):

exclusion_block : tuple
A tuple representing the tile to exclude from the distance array.
ignore_same_residue : bool
If `True`, ignore distances between atoms in the same residue.
verbose : bool
Show detailed progress of the calculation if set to `True`

Expand Down Expand Up @@ -220,6 +222,7 @@ def __init__(self,
range=(0.0, 15.0),
norm="rdf",
exclusion_block=None,
ignore_same_residue=False,
**kwargs):
super(InterRDF, self).__init__(g1.universe.trajectory, **kwargs)
self.g1 = g1
Expand All @@ -229,6 +232,7 @@ def __init__(self,
self.rdf_settings = {'bins': nbins,
'range': range}
self._exclusion_block = exclusion_block
self.ignore_same_residue = ignore_same_residue

if self.norm not in ['rdf', 'density', 'none']:
raise ValueError(f"'{self.norm}' is an invalid norm. "
Expand Down Expand Up @@ -261,6 +265,13 @@ def _single_frame(self):
mask = np.where(idxA != idxB)[0]
dist = dist[mask]

if self.ignore_same_residue:
orionarcher marked this conversation as resolved.
Show resolved Hide resolved
# Ignore distances between atoms in the same residue
orionarcher marked this conversation as resolved.
Show resolved Hide resolved
resixA = self.g1.resindices[pairs[:, 0]]
orionarcher marked this conversation as resolved.
Show resolved Hide resolved
resixB = self.g2.resindices[pairs[:, 1]]
mask = np.where(resixA != resixB)[0]
dist = dist[mask]

count, _ = np.histogram(dist, **self.rdf_settings)
self.results.count += count

Expand Down
6 changes: 6 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def test_exclusion(sels):
rdf = InterRDF(s1, s2, exclusion_block=(1, 2)).run()
assert rdf.results.count.sum() == 4

def test_ignore_same_residues(sels):
orionarcher marked this conversation as resolved.
Show resolved Hide resolved
# should see two distances with 4 counts each
s1, s2 = sels
rdf = InterRDF(s2, s2, ignore_same_residue=True).run()
assert rdf.rdf[0] == 0
assert rdf.results.count.sum() == 8

orionarcher marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize("attr", ("rdf", "bins", "edges", "count"))
def test_rdf_attr_warning(sels, attr):
Expand Down