Skip to content

Commit

Permalink
Update test_convergence and lambda tests in convergence.moving_average
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclark5 committed Aug 28, 2024
1 parent 04718d0 commit 75aa16e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
28 changes: 13 additions & 15 deletions src/alchemlyb/convergence/convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ def forward_backward_convergence(

# Check that each df in the list has only one value of lambda
for i, df in enumerate(df_list):
lambda_values = np.unique([x[1] for x in df.index.to_numpy()])
lambda_values = list(set([x[1:] for x in df.index.to_numpy()]))
if len(lambda_values) > 1:
if len(df_list[0].index[0]) > 2:
lambda_values = np.unique([x[2] for x in df.index.to_numpy()])
if len(lambda_values) > 1:
raise ValueError("Provided DataFrame, df_list[{}] has more than one lambda value".format(i))
else:
raise ValueError("Provided DataFrame, df_list[{}] has more than one lambda value".format(i))
for j in range(len(lambda_values[0])):
if len(list(set([x[j] for x in lambda_values]))) > 1:
raise ValueError(

Check warning on line 105 in src/alchemlyb/convergence/convergence.py

View check run for this annotation

Codecov / codecov/patch

src/alchemlyb/convergence/convergence.py#L105

Added line #L105 was not covered by tests
"Provided DataFrame, df_list[{}] has more than one lambda value in df.index[{}]".format(i, j)
)

logger.info("Begin forward analysis")
forward_list = []
Expand Down Expand Up @@ -459,18 +458,17 @@ def moving_average(df_list, estimator="MBAR", num=10, **kwargs):

# Check that each df in the list has only one value of lambda
for i, df in enumerate(df_list):
lambda_values = np.unique([x[1] for x in df.index.to_numpy()])
lambda_values = list(set([x[1:] for x in df.index.to_numpy()]))
if len(lambda_values) > 1:
if len(df_list[0].index[0]) > 2:
lambda_values = np.unique([x[2] for x in df.index.to_numpy()])
if len(lambda_values) > 1:
raise ValueError("Provided DataFrame, df_list[{}] has more than one lambda value in df.index[2]".format(i))
else:
raise ValueError("Provided DataFrame, df_list[{}] has more than one lambda value in df.index[1]".format(i))
for j in range(len(lambda_values[0])):
if len(list(set([x[j] for x in lambda_values]))) > 1:
raise ValueError(
"Provided DataFrame, df_list[{}] has more than one lambda value in df.index[{}]".format(i, j)
)

if estimator in ["BAR"] and len(df_list) > 2:
raise ValueError(
"Restrict to two DataFrames, one with a fep-lambda value and one its forward adjacent state for a"
"Restrict to two DataFrames, one with a fep-lambda value and one its forward adjacent state for a "
"meaningful result."
)

Expand Down
13 changes: 7 additions & 6 deletions src/alchemlyb/tests/test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
import pytest

from alchemlyb import concat
from alchemlyb.convergence import (
forward_backward_convergence,
fwdrev_cumavg_Rc,
Expand Down Expand Up @@ -52,31 +53,31 @@ def test_moving_average_error_2_mbar(gmx_ABFE_complex_u_nk, estimator):
df_list = gmx_ABFE_complex_u_nk[10:15]
with pytest.raises(
ValueError,
match=r"Restrict to a single fep-lambda value for a meaningful result. .*",
match=r"Provided DataFrame, df_list\[0\] has more than one lambda value in df.index\[0\]",
):
_ = moving_average(df_list, estimator)
_ = moving_average([concat(df_list)], estimator)

df_list = gmx_ABFE_complex_u_nk[14:17]
with pytest.raises(
ValueError,
match=r"Restrict to a single fep-lambda value for a meaningful result. .*",
match=r"Provided DataFrame, df_list\[0\] has more than one lambda value in df.index\[1\]",
):
_ = moving_average(df_list, estimator)
_ = moving_average([concat(df_list)], estimator)


@pytest.mark.parametrize("estimator", ["BAR"])
def test_moving_average_error_2_bar(gmx_ABFE_complex_u_nk, estimator):
df_list = gmx_ABFE_complex_u_nk[10:13]
with pytest.raises(
ValueError,
match=r"Restrict to a fep-lambda value and its forward adjacent state .*",
match=r"Restrict to two DataFrames, one with a fep-lambda value .*",
):
_ = moving_average(df_list, estimator)

df_list = gmx_ABFE_complex_u_nk[14:17]
with pytest.raises(
ValueError,
match=r"Restrict to a fep-lambda value and its forward adjacent state .*",
match=r"Restrict to two DataFrames, one with a fep-lambda value .*",
):
_ = moving_average(df_list, estimator)

Expand Down

0 comments on commit 75aa16e

Please sign in to comment.