Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fixture for the test #278

Merged
merged 25 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/alchemlyb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def concat(objs, *args, **kwargs):


.. versionadded:: 0.5.0"""
if isinstance(objs, pd.DataFrame):
return objs
# Sanity check
try:
attrs = objs[0].attrs
Expand Down
267 changes: 267 additions & 0 deletions src/alchemlyb/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
import pytest
from alchemtest.amber import load_bace_example, load_simplesolvated
from alchemtest.gmx import (
load_benzene,
load_expanded_ensemble_case_1,
load_expanded_ensemble_case_2,
load_expanded_ensemble_case_3,
load_water_particle_with_total_energy,
load_water_particle_with_potential_energy,
load_water_particle_without_energy,
load_ABFE,
)
from alchemtest.gomc import load_benzene as gomc_load_benzene
from alchemtest.namd import (
load_tyr2ala,
load_idws,
load_restarted,
load_restarted_reversed,
)

from alchemlyb.parsing import gmx, amber, gomc, namd


@pytest.fixture
orbeckst marked this conversation as resolved.
Show resolved Hide resolved
def gmx_benzene():
dataset = load_benzene()
return dataset["data"]


@pytest.fixture
def gmx_benzene_Coulomb_dHdl(gmx_benzene):
return [gmx.extract_dHdl(file, T=300) for file in gmx_benzene["Coulomb"]]


@pytest.fixture
def gmx_benzene_VDW_dHdl(gmx_benzene):
return [gmx.extract_dHdl(file, T=300) for file in gmx_benzene["VDW"]]


@pytest.fixture
def gmx_benzene_Coulomb_u_nk(gmx_benzene):
return [gmx.extract_u_nk(file, T=300) for file in gmx_benzene["Coulomb"]]


@pytest.fixture
def gmx_benzene_VDW_u_nk(gmx_benzene):
return [gmx.extract_u_nk(file, T=300) for file in gmx_benzene["VDW"]]


@pytest.fixture
def gmx_benzene_VDW_dHdl(gmx_benzene):
return [gmx.extract_dHdl(file, T=300) for file in gmx_benzene["VDW"]]


@pytest.fixture
def gmx_ABFE():
dataset = load_ABFE()
return dataset["data"]


@pytest.fixture
def gmx_ABFE_complex_n_uk(gmx_ABFE):
return [gmx.extract_u_nk(file, T=300) for file in gmx_ABFE["complex"]]


@pytest.fixture
def gmx_ABFE_complex_dHdl(gmx_ABFE):
return [gmx.extract_dHdl(file, T=300) for file in gmx_ABFE["complex"]]


@pytest.fixture
def gmx_expanded_ensemble_case_1():
dataset = load_expanded_ensemble_case_1()

return [
gmx.extract_u_nk(filename, T=300, filter=False)
for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_expanded_ensemble_case_1_dHdl():
dataset = load_expanded_ensemble_case_1()

return [
gmx.extract_dHdl(filename, T=300, filter=False)
for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_expanded_ensemble_case_2():
dataset = load_expanded_ensemble_case_2()

return [
gmx.extract_u_nk(filename, T=300, filter=False)
for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_expanded_ensemble_case_2_dHdl():
dataset = load_expanded_ensemble_case_2()

return [
gmx.extract_dHdl(filename, T=300, filter=False)
for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_expanded_ensemble_case_3():
dataset = load_expanded_ensemble_case_3()

return [
gmx.extract_u_nk(filename, T=300, filter=False)
for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_expanded_ensemble_case_3_dHdl():
dataset = load_expanded_ensemble_case_3()

return [
gmx.extract_dHdl(filename, T=300, filter=False)
for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_water_particle_with_total_energy():
dataset = load_water_particle_with_total_energy()

return [
gmx.extract_u_nk(filename, T=300) for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_water_particle_with_total_energy_dHdl():
dataset = load_water_particle_with_total_energy()

return [
gmx.extract_dHdl(filename, T=300) for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_water_particle_with_potential_energy():
dataset = load_water_particle_with_potential_energy()

return [
gmx.extract_u_nk(filename, T=300) for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_water_particle_with_potential_energy_dHdl():
dataset = load_water_particle_with_potential_energy()

return [
gmx.extract_dHdl(filename, T=300) for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_water_particle_without_energy():
dataset = load_water_particle_without_energy()

return [
gmx.extract_u_nk(filename, T=300) for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def gmx_water_particle_without_energy_dHdl():
dataset = load_water_particle_without_energy()

return [
gmx.extract_dHdl(filename, T=300) for filename in dataset["data"]["AllStates"]
]


@pytest.fixture
def amber_simplesolvated():
dataset = load_simplesolvated()
return dataset["data"]


@pytest.fixture
def amber_simplesolvated_charge_dHdl(amber_simplesolvated):
return [
amber.extract_dHdl(filename, T=298.0)
for filename in amber_simplesolvated["charge"]
]


@pytest.fixture
def amber_simplesolvated_vdw_dHdl(amber_simplesolvated):
return [
amber.extract_dHdl(filename, T=298.0)
for filename in amber_simplesolvated["vdw"]
]


@pytest.fixture
def amber_bace_example_complex_vdw():
dataset = load_bace_example()

return [
amber.extract_u_nk(filename, T=298.0)
for filename in dataset["data"]["complex"]["vdw"]
]


@pytest.fixture
def gomc_benzene():
dataset = gomc_load_benzene()
return dataset["data"]


@pytest.fixture
def gomc_benzene_u_nk(gomc_benzene):
return [gomc.extract_u_nk(filename, T=298) for filename in gomc_benzene]


@pytest.fixture
def gomc_benzene_dHdl(gomc_benzene):
return [gomc.extract_dHdl(filename, T=298) for filename in gomc_benzene]


@pytest.fixture
def namd_tyr2ala():
dataset = load_tyr2ala()
u_nk1 = namd.extract_u_nk(dataset["data"]["forward"][0], T=300)
u_nk2 = namd.extract_u_nk(dataset["data"]["backward"][0], T=300)

# combine dataframes of fwd and rev directions
u_nk1[u_nk1.isna()] = u_nk2
u_nk = u_nk1.sort_index(level=u_nk1.index.names[1:])

return u_nk


@pytest.fixture
def namd_idws():
dataset = load_idws()
u_nk = namd.extract_u_nk(dataset["data"]["forward"], T=300)

return u_nk


@pytest.fixture
def namd_idws_restarted():
dataset = load_restarted()
u_nk = namd.extract_u_nk(dataset["data"]["both"], T=300)

return u_nk


@pytest.fixture
def namd_idws_restarted_reversed():
dataset = load_restarted_reversed()
u_nk = namd.extract_u_nk(dataset["data"]["both"], T=300)

return u_nk
74 changes: 16 additions & 58 deletions src/alchemlyb/tests/test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@
from alchemlyb.parsing import gmx


@pytest.fixture()
def gmx_benzene():
dataset = load_benzene()
return [gmx.extract_dHdl(dhdl, T=300) for dhdl in dataset["data"]["Coulomb"]], [
gmx.extract_u_nk(dhdl, T=300) for dhdl in dataset["data"]["Coulomb"]
]


def test_convergence_ti(gmx_benzene):
dHdl, u_nk = gmx_benzene
convergence = forward_backward_convergence(dHdl, "TI")
def test_convergence_ti(gmx_benzene_Coulomb_dHdl):
convergence = forward_backward_convergence(gmx_benzene_Coulomb_dHdl, "TI")
assert convergence.shape == (10, 5)

assert convergence.loc[0, "Forward"] == pytest.approx(3.07, 0.01)
Expand All @@ -28,31 +19,29 @@ def test_convergence_ti(gmx_benzene):


@pytest.mark.parametrize("estimator", ["MBAR", "BAR"])
def test_convergence_fep(gmx_benzene, estimator):
dHdl, u_nk = gmx_benzene
convergence = forward_backward_convergence(u_nk, estimator)
def test_convergence_fep(gmx_benzene_Coulomb_u_nk, estimator):
convergence = forward_backward_convergence(gmx_benzene_Coulomb_u_nk, estimator)
assert convergence.shape == (10, 5)
assert convergence.loc[0, "Forward"] == pytest.approx(3.02, 0.01)
assert convergence.loc[0, "Backward"] == pytest.approx(3.06, 0.01)
assert convergence.loc[9, "Forward"] == pytest.approx(3.05, 0.01)
assert convergence.loc[9, "Backward"] == pytest.approx(3.04, 0.01)


def test_convergence_wrong_estimator(gmx_benzene):
dHdl, u_nk = gmx_benzene
def test_convergence_wrong_estimator(gmx_benzene_Coulomb_dHdl):
with pytest.raises(ValueError, match="is not available in"):
forward_backward_convergence(u_nk, "WWW")
forward_backward_convergence(gmx_benzene_Coulomb_dHdl, "WWW")


def test_convergence_wrong_cases(gmx_benzene):
dHdl, u_nk = gmx_benzene
def test_convergence_wrong_cases(gmx_benzene_Coulomb_u_nk):
with pytest.warns(DeprecationWarning, match="Using lower-case strings for"):
forward_backward_convergence(u_nk, "mbar")
forward_backward_convergence(gmx_benzene_Coulomb_u_nk, "mbar")


def test_convergence_method(gmx_benzene):
dHdl, u_nk = gmx_benzene
convergence = forward_backward_convergence(u_nk, "MBAR", num=2, method="adaptive")
def test_convergence_method(gmx_benzene_Coulomb_u_nk):
convergence = forward_backward_convergence(
gmx_benzene_Coulomb_u_nk, "MBAR", num=2, method="adaptive"
)
assert len(convergence) == 2


Expand All @@ -75,12 +64,7 @@ def test_cummean_long_none_integter():


def test_R_c_converged():
data = pd.Series(
data=[
0,
]
* 100
)
data = pd.Series(data=[0] * 100)
data.attrs["temperature"] = 310
data.attrs["energy_unit"] = "kcal/mol"
value, running_average = fwdrev_cumavg_Rc(data)
Expand All @@ -96,42 +80,16 @@ def test_R_c_notconverged():


def test_R_c_real():
data = pd.Series(
data=np.hstack(
(
range(10),
[
4.5,
]
* 10,
)
)
)
data = pd.Series(data=np.hstack((range(10), [4.5] * 10)))
data.attrs["temperature"] = 310
data.attrs["energy_unit"] = "kcal/mol"
value, running_average = fwdrev_cumavg_Rc(data, tol=2.0)
np.testing.assert_allclose(value, 0.35)


def test_A_c_real():
data = pd.Series(
data=np.hstack(
(
range(10),
[
4.5,
]
* 10,
)
)
)
data = pd.Series(data=np.hstack((range(10), [4.5] * 10)))
data.attrs["temperature"] = 310
data.attrs["energy_unit"] = "kcal/mol"
value = A_c(
[
data,
]
* 2,
tol=2.0,
)
value = A_c([data] * 2, tol=2.0)
np.testing.assert_allclose(value, 0.65)
Loading