Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclark5 committed Jul 24, 2024
1 parent f8aff24 commit c739c0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/alchemlyb/tests/test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pandas as pd
import pytest

from alchemlyb.convergence import forward_backward_convergence, fwdrev_cumavg_Rc, A_c
from alchemlyb.convergence import forward_backward_convergence, fwdrev_cumavg_Rc, A_c, moving_average
from alchemlyb.convergence.convergence import _cummean


Expand All @@ -25,6 +25,14 @@ def test_convergence_fep(gmx_benzene_Coulomb_u_nk, estimator):
assert convergence.loc[9, "Forward"] == pytest.approx(3.05, 0.01)
assert convergence.loc[9, "Backward"] == pytest.approx(3.04, 0.01)

@pytest.mark.parametrize("estimator", ["MBAR"])
def test_moving_average_fep(gmx_benzene_Coulomb_u_nk, estimator):
df_avg = moving_average(gmx_benzene_Coulomb_u_nk, estimator)
assert df_avg.shape == (9, 2)
assert df_avg.loc[0, "FE"] == pytest.approx(3.01, 0.01)
assert df_avg.loc[0, "FE_Error"] == pytest.approx(0.067, 0.01)
assert df_avg.loc[8, "FE"] == pytest.approx(3.10, 0.01)
assert df_avg.loc[8, "FE_Error"] == pytest.approx(0.066, 0.01)

def test_convergence_wrong_estimator(gmx_benzene_Coulomb_dHdl):
with pytest.raises(ValueError, match="is not available in"):
Expand Down
24 changes: 23 additions & 1 deletion src/alchemlyb/tests/test_visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import alchemlyb
from alchemlyb.convergence import forward_backward_convergence
from alchemlyb.estimators import MBAR, TI, BAR
from alchemlyb.visualisation import plot_convergence
from alchemlyb.visualisation import plot_convergence, plot_moving_average
from alchemlyb.visualisation.dF_state import plot_dF_state
from alchemlyb.visualisation.mbar_matrix import plot_mbar_overlap_matrix
from alchemlyb.visualisation.ti_dhdl import plot_ti_dhdl
Expand Down Expand Up @@ -217,6 +217,28 @@ def test_plot_convergence_final_nan():
assert isinstance(ax, matplotlib.axes.Axes)
plt.close(ax.figure)

def test_plot_moving_average(gmx_benzene_Coulomb_u_nk):
data_list = gmx_benzene_Coulomb_u_nk
fe = []
fe_error = []
num_points = 10
for i in range(1, num_points + 1):
slice = int(len(data_list[0]) / num_points * i)
u_nk_coul = alchemlyb.concat([data[:slice] for data in data_list])
estimate = MBAR().fit(u_nk_coul)
fe.append(estimate.delta_f_.loc[0, 1])
fe_error.append(estimate.d_delta_f_.loc[0, 1])

df = pd.DataFrame(
data={
"FE": fe,
"FE_Error": fe_error,
}
)
df.attrs = estimate.delta_f_.attrs
ax = plot_moving_average(df)
assert isinstance(ax, matplotlib.axes.Axes)
plt.close(ax.figure)

class Test_Units:
@staticmethod
Expand Down

0 comments on commit c739c0e

Please sign in to comment.