Skip to content

Commit 2055b8e

Browse files
authored
Merge pull request #99 from VOD555/cdf
Add cdf function to InterRDF
2 parents 3605ee8 + 5d3716a commit 2055b8e

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Enhancements
2222
* store block information in `_block` attribute (Issue #89)
2323
* add parallel density class (Issue #8)
2424
* add parallel RMSF class (Issue #90)
25+
* add `cdf` attribute to `pmda.rdf.InterRDF` for cumulative
26+
distribution function
2527

2628
Fixes
2729
* default _conclude() in pmda.custom.AnalysisFromFunction fails with

pmda/leaflet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def _find_connected_components(self, data, cutoff=15.0):
9898
generated from `data`
9999
100100
"""
101+
# pylint: disable=unsubscriptable-object
101102
window, index = data[0]
102103
num = window[0].shape[0]
103104
i_index = index[0]
@@ -299,7 +300,6 @@ def run(self,
299300
scheduler_kwargs=scheduler_kwargs,
300301
n_jobs=n_jobs,
301302
cutoff=cutoff)
302-
303303
timings.append(b_compute.elapsed)
304304
leaflet1 = self._atomgroup[components[0]]
305305
leaflet2 = self._atomgroup[components[1]]

pmda/rdf.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ def _conclude(self, ):
161161
rdf = self.count / (density * vol * self.nf)
162162
self.rdf = rdf
163163

164+
@property
165+
def cdf(self):
166+
"""Calculate the cumulative distribution functions (CDF).
167+
Note that this is the actual count within a given radius, i.e.,
168+
:math:`N(r)`.
169+
170+
Returns
171+
-------
172+
cdf : numpy array
173+
a numpy array with the same structure as :attr:`rdf`
174+
175+
176+
.. versionadded:: 0.3.0
177+
"""
178+
cdf = np.cumsum(self.count) / self.nf
179+
180+
return cdf
181+
164182
@staticmethod
165183
def _reduce(res, result_single_frame):
166184
""" 'add' action for an accumulator"""
@@ -318,7 +336,8 @@ def _conclude(self):
318336

319337
self.rdf = rdf
320338

321-
def get_cdf(self):
339+
@property
340+
def cdf(self):
322341
"""Calculate the cumulative distribution functions (CDF) for all sites.
323342
Note that this is the actual count within a given radius, i.e.,
324343
:math:`N(r)`.
@@ -327,18 +346,20 @@ def get_cdf(self):
327346
-------
328347
cdf : list
329348
list of arrays with the same structure as :attr:`rdf`
349+
350+
351+
.. versionadded:: 0.3.0
352+
Method ``get_cdf()`` was removed and :attr:`cdf` is now a managed
353+
attribute that computes the CDF when accessed.
330354
"""
331355
# Calculate cumulative distribution function
332356
# Empty list to restore CDF
333357
cdf = []
334358

359+
# cdf is a list of cdf between pairs of AtomGroups in ags
335360
for count in self.count:
336361
cdf.append(np.cumsum(count, axis=2) / self.nf)
337362

338-
# Results stored in self.cdf
339-
# self.cdf is a list of cdf between pairs of AtomGroups in ags
340-
self.cdf = cdf
341-
342363
return cdf
343364

344365
@staticmethod

pmda/test/test_rdf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def test_same_result(sels, n_blocks):
8383
assert_almost_equal(nrdf.rdf, prdf.rdf)
8484

8585

86+
def test_cdf(sels):
87+
s1, s2 = sels
88+
rdf = InterRDF(s1, s2).run()
89+
cdf = np.cumsum(rdf.count) / rdf.nf
90+
assert_almost_equal(rdf.cdf[-1], rdf.count.sum()/rdf.nf)
91+
assert_almost_equal(rdf.cdf, cdf)
92+
93+
8694
def test_reduce(sels):
8795
# should see numpy.array addtion
8896
s1, s2 = sels

pmda/test/test_rdf_s.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def test_double_run(rdf_s):
8585

8686

8787
def test_cdf(rdf_s):
88-
rdf_s.get_cdf()
89-
assert rdf_s.cdf[0][0][0][-1] == rdf_s.count[0][0][0].sum()/rdf_s.nf
88+
assert_almost_equal(rdf_s.cdf[0][0][0][-1],
89+
rdf_s.count[0][0][0].sum()/rdf_s.nf)
9090

9191

9292
def test_reduce(rdf_s):
@@ -110,7 +110,7 @@ def test_same_result(u, sels, n_blocks):
110110

111111

112112
@pytest.mark.parametrize("density, value", [
113-
(True, 13275.775528444701),
113+
(True, 13275.775440503656),
114114
(False, 0.021915460340071267)])
115115
def test_density(u, sels, density, value):
116116
rdf = InterRDF_s(u, sels, density=density).run()

0 commit comments

Comments
 (0)