Skip to content

Commit e32fb61

Browse files
madilynpaulchrisjonesBSUpre-commit-ci[bot]
authored
Adding autocorrelation function to sampling.py. (#92)
* Adding autocorrelation function to sampling.py. * pin python < 3.12 * Adding test_autocorr1D to test_sampling.py. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix packmol seed in failing gsd tests * ignore pre-commit error at line level * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add parameters and return to doc strings --------- Co-authored-by: chrisjonesBSU <chrisjones4@u.boisestate.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a642a34 commit e32fb61

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

cmeutils/sampling.py

+20
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,23 @@ def is_equilibrated(data, threshold_fraction=0.50, threshold_neff=50, nskip=1):
103103
return [True, t0, g, Neff]
104104
else:
105105
return [False, None, None, None]
106+
107+
108+
def autocorr1D(array):
109+
"""Takes in a linear np array, performs autocorrelation
110+
function and returns normalized array with half the length
111+
of the input.
112+
113+
Parameters
114+
----------
115+
data : numpy.typing.Arraylike, required
116+
1-D series of data to perform autocorrelation on.
117+
118+
Returns
119+
-------
120+
1D np.array
121+
122+
"""
123+
ft = np.fft.rfft(array - np.average(array))
124+
acorr = np.fft.irfft(ft * np.conjugate(ft)) / (len(array) * np.var(array))
125+
return acorr[0 : len(acorr) // 2] # noqa: E203

cmeutils/tests/test_gsd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_ellipsoid_gsd(self, butane_gsd):
5050
def test_create_rigid_snapshot(self):
5151
benzene = mb.load("c1ccccc1", smiles=True)
5252
benzene.name = "Benzene"
53-
box = mb.fill_box(benzene, 5, box=[1, 1, 1])
53+
box = mb.fill_box(benzene, 5, box=[3, 3, 3], seed=42)
5454
box.label_rigid_bodies(discrete_bodies="Benzene")
5555

5656
rigid_init_snap = create_rigid_snapshot(box)
@@ -60,7 +60,7 @@ def test_create_rigid_snapshot(self):
6060
def test_update_rigid_snapshot(self):
6161
benzene = mb.load("c1ccccc1", smiles=True)
6262
benzene.name = "Benzene"
63-
box = mb.fill_box(benzene, 5, box=[1, 1, 1])
63+
box = mb.fill_box(benzene, 5, box=[3, 3, 3], seed=42)
6464
box.label_rigid_bodies(discrete_bodies="Benzene")
6565

6666
rigid_init_snap = create_rigid_snapshot(box)

cmeutils/tests/test_sampling.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import numpy as np
22
import pytest
3+
import scipy
34

4-
from cmeutils.sampling import equil_sample, is_equilibrated
5+
from cmeutils.sampling import autocorr1D, equil_sample, is_equilibrated
56
from cmeutils.tests.base_test import BaseTest
67

78

@@ -82,3 +83,28 @@ def test_trim_high_threshold(self, correlated_data_tau100_n10000):
8283
[equil_data, uncorr_indices, prod_start, Neff] = equil_sample(
8384
data, threshold_fraction=0.75, threshold_neff=10000
8485
)
86+
87+
def test_autocorr1D(self):
88+
randoms = np.random.randint(100, size=(100))
89+
integers = np.array(np.arange(100))
90+
91+
autocorrelation_randoms = autocorr1D(randoms)
92+
autocorrelation_integers = autocorr1D(integers)
93+
94+
def expfunc(x, a):
95+
return np.exp(-x / a)
96+
97+
x_values = np.array(range(len(autocorrelation_integers)))
98+
99+
exp_coeff_randoms = scipy.optimize.curve_fit(
100+
expfunc, x_values, autocorrelation_randoms
101+
)[0][0]
102+
exp_coeff_int = scipy.optimize.curve_fit(
103+
expfunc, x_values, autocorrelation_integers
104+
)[0][0]
105+
106+
assert (
107+
round(autocorrelation_integers[0], 10)
108+
and round(autocorrelation_randoms[0], 10) == 1
109+
)
110+
assert exp_coeff_int > 5 and exp_coeff_randoms < 1

environment-dev.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
- mbuild >=0.16.4
1111
- numpy
1212
- pip
13-
- python >= 3.10
13+
- python >= 3.10,<3.12
1414
- matplotlib
1515
- pre-commit
1616
- pymbar >= 4.0

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
- mbuild >=0.16.4
1111
- numpy
1212
- pip
13-
- python >= 3.10
13+
- python >= 3.10,<3.12
1414
- matplotlib
1515
- pymbar >= 4.0
1616
- rowan

0 commit comments

Comments
 (0)