forked from MDAnalysis/mdanalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rdf.py
More file actions
164 lines (128 loc) · 5.26 KB
/
test_rdf.py
File metadata and controls
164 lines (128 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
import pytest
import MDAnalysis as mda
from MDAnalysis.analysis.rdf import InterRDF
from MDAnalysisTests.util import distopia_conditional_backend
from MDAnalysisTests.datafiles import two_water_gro
from numpy.testing import assert_allclose
@pytest.fixture()
def u():
u = mda.Universe(two_water_gro, in_memory=True)
u.add_TopologyAttr("chainIDs", u.atoms.resids)
return u
@pytest.fixture()
def sels(u):
# modify the coordinates to produce specific test results
# (NOTE: requires in-memory coordinates to make them permanent)
for at, (x, y) in zip(u.atoms, zip([1] * 3 + [2] * 3, [2, 1, 3] * 2)):
at.position = x, y, 0.0
s1 = u.select_atoms("name OW")
s2 = u.select_atoms("name HW1 HW2")
return s1, s2
def test_nbins(u, client_InterRDF):
s1 = u.atoms[:3]
s2 = u.atoms[3:]
rdf = InterRDF(s1, s2, nbins=412).run(**client_InterRDF)
assert len(rdf.results.bins) == 412
def test_range(u, client_InterRDF):
s1 = u.atoms[:3]
s2 = u.atoms[3:]
rmin, rmax = 1.0, 13.0
rdf = InterRDF(s1, s2, range=(rmin, rmax)).run(**client_InterRDF)
assert rdf.results.edges[0] == rmin
assert rdf.results.edges[-1] == rmax
def test_count_sum(sels, client_InterRDF):
# OW vs HW
# should see 8 comparisons in count
s1, s2 = sels
rdf = InterRDF(s1, s2).run(**client_InterRDF)
assert rdf.results.count.sum() == 8
def test_count(sels, client_InterRDF):
# should see two distances with 4 counts each
s1, s2 = sels
rdf = InterRDF(s1, s2).run(**client_InterRDF)
assert len(rdf.results.count[rdf.results.count == 4]) == 2
def test_double_run(sels, client_InterRDF):
# running rdf twice should give the same result
s1, s2 = sels
rdf = InterRDF(s1, s2).run(**client_InterRDF)
rdf.run(**client_InterRDF)
assert len(rdf.results.count[rdf.results.count == 4]) == 2
def test_exclusion(sels, client_InterRDF):
# should see two distances with 4 counts each
s1, s2 = sels
rdf = InterRDF(s1, s2, exclusion_block=(1, 2)).run(**client_InterRDF)
assert rdf.results.count.sum() == 4
@pytest.mark.parametrize(
"attr, count", [("residue", 8), ("segment", 0), ("chain", 8)]
)
def test_ignore_same_residues(sels, attr, count, client_InterRDF):
# should see two distances with 4 counts each
s1, s2 = sels
rdf = InterRDF(s2, s2, exclude_same=attr).run(**client_InterRDF)
assert rdf.rdf[0] == 0
assert rdf.results.count.sum() == count
def test_ignore_same_residues_fails(sels, client_InterRDF):
s1, s2 = sels
with pytest.raises(
ValueError, match="The exclude_same argument to InterRDF must be"
):
InterRDF(s2, s2, exclude_same="unsupported").run(**client_InterRDF)
with pytest.raises(
ValueError,
match="The exclude_same argument to InterRDF cannot be used with",
):
InterRDF(s2, s2, exclude_same="residue", exclusion_block=tuple()).run(
**client_InterRDF
)
@pytest.mark.parametrize("attr", ("rdf", "bins", "edges", "count"))
def test_rdf_attr_warning(sels, attr, client_InterRDF):
s1, s2 = sels
rdf = InterRDF(s1, s2).run(**client_InterRDF)
wmsg = f"The `{attr}` attribute was deprecated in MDAnalysis 2.0.0"
with pytest.warns(DeprecationWarning, match=wmsg):
getattr(rdf, attr) is rdf.results[attr]
@pytest.mark.parametrize(
"norm, value", [("density", 1.956823), ("rdf", 244602.88385), ("none", 4)]
)
def test_norm(sels, norm, value, client_InterRDF):
s1, s2 = sels
rdf = InterRDF(s1, s2, norm=norm).run(**client_InterRDF)
assert_allclose(max(rdf.results.rdf), value)
@pytest.mark.parametrize(
"norm, norm_required", [("Density", "density"), (None, "none")]
)
def test_norm_values(sels, norm, norm_required, client_InterRDF):
s1, s2 = sels
rdf = InterRDF(s1, s2, norm=norm).run(**client_InterRDF)
assert rdf.norm == norm_required
def test_unknown_norm(sels):
s1, s2 = sels
with pytest.raises(ValueError, match="invalid norm"):
InterRDF(s1, s2, sels, norm="foo")
@pytest.mark.parametrize("backend", distopia_conditional_backend())
def test_norm(sels, backend):
s1, s2 = sels
rdf = InterRDF(s1, s2, norm="none", backend=backend).run()
assert_allclose(max(rdf.results.rdf), 4)