From f0f6a9ef30c9944d1636123c5adee28bd97f92f4 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Sun, 14 Apr 2019 11:30:15 -0700 Subject: [PATCH] moved NAMD BAR test to test_fep - moved test - added test for the error (regression test) - use fixture for NAMD dataset and parametrized test for forward/backward legs in order to simplify code --- src/alchemlyb/tests/parsing/test_namd.py | 48 ++++++---------------- src/alchemlyb/tests/test_fep_estimators.py | 18 +++++++- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/alchemlyb/tests/parsing/test_namd.py b/src/alchemlyb/tests/parsing/test_namd.py index 693f67cd..f2fb504d 100644 --- a/src/alchemlyb/tests/parsing/test_namd.py +++ b/src/alchemlyb/tests/parsing/test_namd.py @@ -1,46 +1,24 @@ """NAMD parser tests. """ +import pytest from alchemlyb.parsing.namd import extract_u_nk from alchemtest.namd import load_tyr2ala -from numpy.testing import assert_almost_equal +@pytest.fixture(scope="module") +def dataset(): + return load_tyr2ala() -def test_u_nk(): +@pytest.mark.parametrize("direction,shape", + [('forward', (21021, 21)), + ('backward', (21021, 21)), + ]) +def test_u_nk(dataset, direction, shape): """Test that u_nk has the correct form when extracted from files. """ - dataset = load_tyr2ala() + for filename in dataset['data'][direction]: + u_nk = extract_u_nk(filename) - for direction in dataset['data']: - for filename in dataset['data'][direction]: - u_nk = extract_u_nk(filename) - - assert u_nk.index.names == ['timestep', 'fep-lambda'] - if direction == 'forward': - assert u_nk.shape == (21021, 21) - elif direction == 'backward': - assert u_nk.shape == (21021, 21) - -def test_bar_namd(): - """Test BAR calculation on NAMD data. - """ - from alchemlyb.estimators import BAR - import numpy as np - - # load data - dataset = load_tyr2ala() - u_nk1 = extract_u_nk(dataset['data']['forward'][0]) - u_nk2 = extract_u_nk(dataset['data']['backward'][0]) - - # combine dataframes of fwd and rev directions - u_nk1.replace(0, np.nan, inplace=True) - u_nk1[u_nk1.isnull()] = u_nk2 - u_nk1.replace(np.nan, 0, inplace=True) - u_nk = u_nk1.sort_index(level=u_nk1.index.names[1:]) - - # after loading BOTH fwd and rev data, do BAR calculation - bar = BAR() - bar.fit(u_nk) - dg = (bar.delta_f_.iloc[0].iloc[-1]) - assert_almost_equal(dg, 6.03126982925, decimal=4) + assert u_nk.index.names == ['timestep', 'fep-lambda'] + assert u_nk.shape == shape diff --git a/src/alchemlyb/tests/test_fep_estimators.py b/src/alchemlyb/tests/test_fep_estimators.py index c00d50f9..49ed718b 100644 --- a/src/alchemlyb/tests/test_fep_estimators.py +++ b/src/alchemlyb/tests/test_fep_estimators.py @@ -3,13 +3,15 @@ """ import pytest +import numpy as np import pandas as pd -from alchemlyb.parsing import gmx, amber +from alchemlyb.parsing import gmx, amber, namd from alchemlyb.estimators import MBAR from alchemlyb.estimators import BAR import alchemtest.gmx import alchemtest.amber +import alchemtest.namd def gmx_benzene_coul_u_nk(): dataset = alchemtest.gmx.load_benzene() @@ -82,6 +84,19 @@ def amber_bace_example_complex_vdw(): for filename in dataset['data']['complex']['vdw']]) return u_nk +def namd_tyr2ala(): + dataset = alchemtest.namd.load_tyr2ala() + u_nk1 = namd.extract_u_nk(dataset['data']['forward'][0]) + u_nk2 = namd.extract_u_nk(dataset['data']['backward'][0]) + + # combine dataframes of fwd and rev directions + u_nk1.replace(0, np.nan, inplace=True) + u_nk1[u_nk1.isnull()] = u_nk2 + u_nk1.replace(np.nan, 0, inplace=True) + u_nk = u_nk1.sort_index(level=u_nk1.index.names[1:]) + + return u_nk + class FEPestimatorMixin: """Mixin for all FEP Estimator test classes. @@ -134,6 +149,7 @@ class TestBAR(FEPestimatorMixin): (gmx_water_particle_with_potential_energy(), -11.724, 0.064964), (gmx_water_particle_without_energy(), -11.660, 0.064914), (amber_bace_example_complex_vdw(), 2.37846, 0.050899), + (namd_tyr2ala(), 6.031269829, 0.069813058), ]) def test_bar(self, X_delta_f): self.compare_delta_f(X_delta_f)