Skip to content

Commit

Permalink
Merge pull request #333 from Lnaden/pytest
Browse files Browse the repository at this point in the history
Convert to PyTest
  • Loading branch information
mrshirts committed Feb 14, 2020
2 parents aa21dab + 9179de8 commit 90666f6
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 610 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,9 @@ jobs:
python -m pip install . --no-deps
conda list --show-channel-urls
# - name: Run tests (pytest)
# shell: bash
# run: |
# . devtools/github-actions/initialize_conda.sh
# conda activate test
# pytest -v --cov=$PACKAGE --cov-report=xml --color=yes $PACKAGE/tests/

- name: Run tests (nose)
- name: Run tests (pytest)
shell: bash
run: |
. devtools/github-actions/initialize_conda.sh
conda activate test
cd devtools
nosetests $PACKAGE --nocapture --verbosity=2 --with-doctest --with-timer
cd ..
pytest -v --cov=$PACKAGE --cov-report=xml --color=yes $PACKAGE/tests/
6 changes: 2 additions & 4 deletions devtools/conda-envs/test_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ dependencies:
- six

# Testing
# - pytest
# - pytest-cov
- pytest
- pytest-cov
- codecov
- nose
- nose-timer
- statsmodels

2 changes: 1 addition & 1 deletion pymbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
__maintainer__ = "Levi N. Naden, Michael R. Shirts and John D. Chodera"
__email__ = "levi.naden@choderalab.org,michael.shirts@colorado.edu,john.chodera@choderalab.org"

from pymbar import timeseries, testsystems, confidenceintervals, version
from pymbar import timeseries, testsystems, confidenceintervals
from pymbar.mbar import MBAR
from pymbar.bar import BAR, BARzero
from pymbar.exp import EXP, EXPGauss
Expand Down
76 changes: 23 additions & 53 deletions pymbar/tests/test_covariance.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,41 @@
import numpy as np
import pytest
import pymbar
from pymbar.utils_for_testing import eq, suppress_derivative_warnings_for_tests

def load_oscillators(n_states, n_samples):
name = "%dx%d oscillators" % (n_states, n_samples)
O_k = np.linspace(1, 5, n_states)
k_k = np.linspace(1, 3, n_states)
N_k = (np.ones(n_states) * n_samples).astype('int')
test = pymbar.testsystems.harmonic_oscillators.HarmonicOscillatorsTestCase(O_k, k_k)
x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn')
return name, u_kn, N_k_output, s_n


def load_exponentials(n_states, n_samples):
name = "%dx%d exponentials" % (n_states, n_samples)
rates = np.linspace(1, 3, n_states)
N_k = (np.ones(n_states) * n_samples).astype('int')
test = pymbar.testsystems.exponential_distributions.ExponentialTestCase(rates)
x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn')
return name, u_kn, N_k_output, s_n


def _test(data_generator):
name, U, N_k, s_n = data_generator()
from pymbar.utils_for_testing import (suppress_derivative_warnings_for_tests, assert_almost_equal,
oscillators, exponentials)


@pytest.mark.parametrize(
'statesa, statesb, test_system',
[(100, 100, oscillators),
(200, 50, oscillators),
(200, 50, exponentials)]
)
def _test(statesa, statesb, test_system):
name, U, N_k, s_n = test_system(statesa, statesb)
print(name)
mbar = pymbar.MBAR(U, N_k)
results1 = mbar.getFreeEnergyDifferences(uncertainty_method="svd")
fij1_t, dfij1_t = mbar.getFreeEnergyDifferences(uncertainty_method="svd")
results2 = mbar.getFreeEnergyDifferences(uncertainty_method="svd-ew")
fij1 = results1['Delta_f']
dfij1 = results1['dDelta_f']
fij2 = results2['Delta_f']
dfij2 = results2['dDelta_f']

# Check to make sure the returns from with and w/o dict are the same
eq(fij1, fij1_t)
eq(dfij1, dfij1_t)

eq(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8)
eq(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10)
eq(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10)
eq(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10)
assert_almost_equal(pymbar.mbar_solvers.mbar_gradient(U, N_k, mbar.f_k), np.zeros(N_k.shape), decimal=8)
assert_almost_equal(np.exp(mbar.Log_W_nk).sum(0), np.ones(len(N_k)), decimal=10)
assert_almost_equal(np.exp(mbar.Log_W_nk).dot(N_k), np.ones(U.shape[1]), decimal=10)
assert_almost_equal(pymbar.mbar_solvers.self_consistent_update(U, N_k, mbar.f_k), mbar.f_k, decimal=10)

# Test against old MBAR code.
with suppress_derivative_warnings_for_tests():
mbar0 = pymbar.old_mbar.MBAR(U, N_k)
fij0, dfij0 = mbar0.getFreeEnergyDifferences(uncertainty_method="svd")
eq(mbar.f_k, mbar0.f_k, decimal=8)
eq(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5)

eq(fij0, fij1, decimal=8)
eq(dfij0, dfij1, decimal=8)

eq(fij0, fij2, decimal=8)
eq(dfij0, dfij2, decimal=8)


def test_100x100_oscillators():
data_generator = lambda: load_oscillators(100, 100)
_test(data_generator)


def test_200x50_oscillators():
data_generator = lambda: load_oscillators(200, 50)
_test(data_generator)
assert_almost_equal(mbar.f_k, mbar0.f_k, decimal=8)
assert_almost_equal(np.exp(mbar.Log_W_nk), np.exp(mbar0.Log_W_nk), decimal=5)

assert_almost_equal(fij0, fij1, decimal=8)
assert_almost_equal(dfij0, dfij1, decimal=8)

def test_200x50_exponentials():
data_generator = lambda: load_exponentials(200, 50)
_test(data_generator)
assert_almost_equal(fij0, fij2, decimal=8)
assert_almost_equal(dfij0, dfij2, decimal=8)

0 comments on commit 90666f6

Please sign in to comment.