From e32fb61fded512c6e373ae0a3c88713a7bb28c23 Mon Sep 17 00:00:00 2001 From: madilynpaul <112991440+madilynpaul@users.noreply.github.com> Date: Tue, 21 May 2024 11:06:56 -0600 Subject: [PATCH 1/3] 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 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- cmeutils/sampling.py | 20 ++++++++++++++++++++ cmeutils/tests/test_gsd.py | 4 ++-- cmeutils/tests/test_sampling.py | 28 +++++++++++++++++++++++++++- environment-dev.yml | 2 +- environment.yml | 2 +- 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/cmeutils/sampling.py b/cmeutils/sampling.py index 4f1162b..c43455b 100644 --- a/cmeutils/sampling.py +++ b/cmeutils/sampling.py @@ -103,3 +103,23 @@ def is_equilibrated(data, threshold_fraction=0.50, threshold_neff=50, nskip=1): return [True, t0, g, Neff] else: return [False, None, None, None] + + +def autocorr1D(array): + """Takes in a linear np array, performs autocorrelation + function and returns normalized array with half the length + of the input. + + Parameters + ---------- + data : numpy.typing.Arraylike, required + 1-D series of data to perform autocorrelation on. + + Returns + ------- + 1D np.array + + """ + ft = np.fft.rfft(array - np.average(array)) + acorr = np.fft.irfft(ft * np.conjugate(ft)) / (len(array) * np.var(array)) + return acorr[0 : len(acorr) // 2] # noqa: E203 diff --git a/cmeutils/tests/test_gsd.py b/cmeutils/tests/test_gsd.py index 46cf35a..3be87d9 100644 --- a/cmeutils/tests/test_gsd.py +++ b/cmeutils/tests/test_gsd.py @@ -50,7 +50,7 @@ def test_ellipsoid_gsd(self, butane_gsd): def test_create_rigid_snapshot(self): benzene = mb.load("c1ccccc1", smiles=True) benzene.name = "Benzene" - box = mb.fill_box(benzene, 5, box=[1, 1, 1]) + box = mb.fill_box(benzene, 5, box=[3, 3, 3], seed=42) box.label_rigid_bodies(discrete_bodies="Benzene") rigid_init_snap = create_rigid_snapshot(box) @@ -60,7 +60,7 @@ def test_create_rigid_snapshot(self): def test_update_rigid_snapshot(self): benzene = mb.load("c1ccccc1", smiles=True) benzene.name = "Benzene" - box = mb.fill_box(benzene, 5, box=[1, 1, 1]) + box = mb.fill_box(benzene, 5, box=[3, 3, 3], seed=42) box.label_rigid_bodies(discrete_bodies="Benzene") rigid_init_snap = create_rigid_snapshot(box) diff --git a/cmeutils/tests/test_sampling.py b/cmeutils/tests/test_sampling.py index 8fbe6bc..f6a004b 100644 --- a/cmeutils/tests/test_sampling.py +++ b/cmeutils/tests/test_sampling.py @@ -1,7 +1,8 @@ import numpy as np import pytest +import scipy -from cmeutils.sampling import equil_sample, is_equilibrated +from cmeutils.sampling import autocorr1D, equil_sample, is_equilibrated from cmeutils.tests.base_test import BaseTest @@ -82,3 +83,28 @@ def test_trim_high_threshold(self, correlated_data_tau100_n10000): [equil_data, uncorr_indices, prod_start, Neff] = equil_sample( data, threshold_fraction=0.75, threshold_neff=10000 ) + + def test_autocorr1D(self): + randoms = np.random.randint(100, size=(100)) + integers = np.array(np.arange(100)) + + autocorrelation_randoms = autocorr1D(randoms) + autocorrelation_integers = autocorr1D(integers) + + def expfunc(x, a): + return np.exp(-x / a) + + x_values = np.array(range(len(autocorrelation_integers))) + + exp_coeff_randoms = scipy.optimize.curve_fit( + expfunc, x_values, autocorrelation_randoms + )[0][0] + exp_coeff_int = scipy.optimize.curve_fit( + expfunc, x_values, autocorrelation_integers + )[0][0] + + assert ( + round(autocorrelation_integers[0], 10) + and round(autocorrelation_randoms[0], 10) == 1 + ) + assert exp_coeff_int > 5 and exp_coeff_randoms < 1 diff --git a/environment-dev.yml b/environment-dev.yml index 650908f..65c1156 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -10,7 +10,7 @@ dependencies: - mbuild >=0.16.4 - numpy - pip - - python >= 3.10 + - python >= 3.10,<3.12 - matplotlib - pre-commit - pymbar >= 4.0 diff --git a/environment.yml b/environment.yml index 1be941e..eafb255 100644 --- a/environment.yml +++ b/environment.yml @@ -10,7 +10,7 @@ dependencies: - mbuild >=0.16.4 - numpy - pip - - python >= 3.10 + - python >= 3.10,<3.12 - matplotlib - pymbar >= 4.0 - rowan From 18d8797a0d42f8a43330bcaf705d9fabc1c554d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 13:25:15 -0600 Subject: [PATCH 2/3] [pre-commit.ci] pre-commit autoupdate (#95) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pycqa/flake8: 7.0.0 → 7.1.0](https://github.com/pycqa/flake8/compare/7.0.0...7.1.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0604cdb..b2a6291 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,7 +32,7 @@ repos: exclude: 'cmeuitls/tests/assets/.* ' - repo: https://github.com/pycqa/flake8 - rev: 7.0.0 + rev: 7.1.0 hooks: - id: flake8 args: From 7d00de9edffc88143aff3b6cda469635a8ac11cb Mon Sep 17 00:00:00 2001 From: Chris Jones <50423140+chrisjonesBSU@users.noreply.github.com> Date: Wed, 19 Jun 2024 07:49:02 -0600 Subject: [PATCH 3/3] Add support for freud 3.0 (#93) * fix parameter in cluster module * update version to match what we are using on conda-forge * use box module to get vectors * update version --- cmeutils/__version__.py | 2 +- cmeutils/gsd_utils.py | 16 +++++++++++++--- environment-dev.yml | 2 +- environment.yml | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmeutils/__version__.py b/cmeutils/__version__.py index a82b376..67bc602 100644 --- a/cmeutils/__version__.py +++ b/cmeutils/__version__.py @@ -1 +1 @@ -__version__ = "1.1.1" +__version__ = "1.3.0" diff --git a/cmeutils/gsd_utils.py b/cmeutils/gsd_utils.py index 88893d3..c3f414d 100644 --- a/cmeutils/gsd_utils.py +++ b/cmeutils/gsd_utils.py @@ -129,11 +129,21 @@ def get_molecule_cluster(gsd_file=None, snap=None, gsd_frame=-1): n_query_points = n_points = snap.particles.N query_point_indices = snap.bonds.group[:, 0] point_indices = snap.bonds.group[:, 1] - distances = system.box.compute_distances( - system.points[query_point_indices], system.points[point_indices] + box = freud.box.Box( + snap.configuration.box[0], + snap.configuration.box[1], + snap.configuration.box[2], + ) + vectors = box.wrap( + snap.particles.position[query_point_indices] + - snap.particles.position[point_indices] ) nlist = freud.NeighborList.from_arrays( - n_query_points, n_points, query_point_indices, point_indices, distances + num_query_points=n_query_points, + num_points=n_points, + query_point_indices=query_point_indices, + point_indices=point_indices, + vectors=vectors, ) cluster = freud.cluster.Cluster() cluster.compute(system=system, neighbors=nlist) diff --git a/environment-dev.yml b/environment-dev.yml index 65c1156..7638253 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -2,7 +2,7 @@ name: cmeutils-dev channels: - conda-forge dependencies: - - freud =2.13.2 + - freud >= 3.0 - gmso >=0.11.2 - fresnel >=0.13.5 - gsd >=3.0 diff --git a/environment.yml b/environment.yml index eafb255..a99670e 100644 --- a/environment.yml +++ b/environment.yml @@ -2,7 +2,7 @@ name: cmeutils channels: - conda-forge dependencies: - - freud =2.13.2 + - freud >= 3.0 - gmso >=0.11.2 - fresnel >=0.13.5 - gsd >=3.0