Skip to content

Commit

Permalink
moved NAMD BAR test to test_fep
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
orbeckst committed Jul 17, 2019
1 parent 38b33bc commit f0f6a9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
48 changes: 13 additions & 35 deletions src/alchemlyb/tests/parsing/test_namd.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion src/alchemlyb/tests/test_fep_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f0f6a9e

Please sign in to comment.