-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from Lnaden/pytest
Convert to PyTest
- Loading branch information
Showing
12 changed files
with
371 additions
and
610 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.