From f1a8ae214b0064b53952f8e6128900ead29cbaa5 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 14 Aug 2022 20:56:26 +0100 Subject: [PATCH 01/12] remove iloc --- src/alchemlyb/tests/test_convergence.py | 38 +++++++------------------ 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/alchemlyb/tests/test_convergence.py b/src/alchemlyb/tests/test_convergence.py index 7013d119..55ff832c 100644 --- a/src/alchemlyb/tests/test_convergence.py +++ b/src/alchemlyb/tests/test_convergence.py @@ -14,37 +14,21 @@ def test_convergence_ti(gmx_benzene): dHdl, u_nk = gmx_benzene convergence = forward_backward_convergence(dHdl, 'TI') assert convergence.shape == (10, 5) - assert convergence.iloc[0, 0] == pytest.approx(3.07, 0.01) - assert convergence.iloc[0, 2] == pytest.approx(3.11, 0.01) - assert convergence.iloc[-1, 0] == pytest.approx(3.09, 0.01) - assert convergence.iloc[-1, 2] == pytest.approx(3.09, 0.01) -def test_convergence_mbar(gmx_benzene): - dHdl, u_nk = gmx_benzene - convergence = forward_backward_convergence(u_nk, 'MBAR') - assert convergence.shape == (10, 5) - assert convergence.iloc[0, 0] == pytest.approx(3.02, 0.01) - assert convergence.iloc[0, 2] == pytest.approx(3.06, 0.01) - assert convergence.iloc[-1, 0] == pytest.approx(3.05, 0.01) - assert convergence.iloc[-1, 2] == pytest.approx(3.04, 0.01) - -def test_convergence_autombar(gmx_benzene): - dHdl, u_nk = gmx_benzene - convergence = forward_backward_convergence(u_nk, 'AutoMBAR') - assert convergence.shape == (10, 5) - assert convergence.iloc[0, 0] == pytest.approx(3.02, 0.01) - assert convergence.iloc[0, 2] == pytest.approx(3.06, 0.01) - assert convergence.iloc[-1, 0] == pytest.approx(3.05, 0.01) - assert convergence.iloc[-1, 2] == pytest.approx(3.04, 0.01) + assert convergence.loc[0, 'Forward'] == pytest.approx(3.07, 0.01) + assert convergence.loc[0, 'Backward'] == pytest.approx(3.11, 0.01) + assert convergence.loc[9, 'Forward'] == pytest.approx(3.09, 0.01) + assert convergence.loc[9, 'Backward'] == pytest.approx(3.09, 0.01) -def test_convergence_bar(gmx_benzene): +@pytest.mark.parametrize('estimator', ('MBAR', 'AutoMBAR', 'BAR')) +def test_convergence_fep(gmx_benzene, estimator): dHdl, u_nk = gmx_benzene - convergence = forward_backward_convergence(u_nk, 'BAR') + convergence = forward_backward_convergence(u_nk, estimator) assert convergence.shape == (10, 5) - assert convergence.iloc[0, 0] == pytest.approx(3.02, 0.01) - assert convergence.iloc[0, 2] == pytest.approx(3.06, 0.01) - assert convergence.iloc[-1, 0] == pytest.approx(3.05, 0.01) - assert convergence.iloc[-1, 2] == pytest.approx(3.04, 0.01) + 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 From afc54b879a84f5c862eb4cdaf6bcbf213ed7426b Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Wed, 17 Aug 2022 19:44:06 +0100 Subject: [PATCH 02/12] update loc --- src/alchemlyb/tests/test_visualisation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/alchemlyb/tests/test_visualisation.py b/src/alchemlyb/tests/test_visualisation.py index bcaf4a32..72f1a730 100644 --- a/src/alchemlyb/tests/test_visualisation.py +++ b/src/alchemlyb/tests/test_visualisation.py @@ -148,13 +148,13 @@ def test_plot_convergence(): 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) - forward.append(estimate.delta_f_.iloc[0,-1]) - forward_error.append(estimate.d_delta_f_.iloc[0,-1]) + forward.append(estimate.delta_f_.loc[0.0,1.0]) + forward_error.append(estimate.d_delta_f_.loc[0.0,1.0]) # Do the backward u_nk_coul = alchemlyb.concat([data[-slice:] for data in data_list]) estimate = MBAR().fit(u_nk_coul) - backward.append(estimate.delta_f_.iloc[0,-1]) - backward_error.append(estimate.d_delta_f_.iloc[0,-1]) + backward.append(estimate.delta_f_.loc[0.0,1.0]) + backward_error.append(estimate.d_delta_f_.loc[0.0,1.0]) ax = plot_convergence(forward, forward_error, backward, backward_error) assert isinstance(ax, matplotlib.axes.Axes) From 3fd100581aa2ac673e81d63242d67c9dfcb340df Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 16 Oct 2022 20:57:04 +0100 Subject: [PATCH 03/12] update --- src/alchemlyb/tests/parsing/test_gmx.py | 11 ++++++----- src/alchemlyb/tests/test_convergence.py | 2 +- src/alchemlyb/tests/test_ti_estimators.py | 4 ++-- src/alchemlyb/tests/test_units.py | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/alchemlyb/tests/parsing/test_gmx.py b/src/alchemlyb/tests/parsing/test_gmx.py index a5c6e0bc..d85ad1bf 100644 --- a/src/alchemlyb/tests/parsing/test_gmx.py +++ b/src/alchemlyb/tests/parsing/test_gmx.py @@ -124,7 +124,7 @@ def test_u_nk_with_total_energy(): # Check one specific value in the dataframe assert_almost_equal( - extract_u_nk(dataset['data']['AllStates'][0], T=300).iloc[0][0], + extract_u_nk(dataset['data']['AllStates'][0], T=300).loc[0][(0.0,0.0)].values[0], -11211.577658852531, decimal=6 ) @@ -142,7 +142,7 @@ def test_u_nk_with_potential_energy(): # Check one specific value in the dataframe assert_almost_equal( - extract_u_nk(dataset['data']['AllStates'][0], T=300).iloc[0][0], + extract_u_nk(dataset['data']['AllStates'][0], T=300).loc[0][(0.0,0.0)].values[0], -15656.557252200757, decimal=6 ) @@ -161,7 +161,7 @@ def test_u_nk_without_energy(): # Check one specific value in the dataframe assert_almost_equal( - extract_u_nk(dataset['data']['AllStates'][0], T=300).iloc[0][0], + extract_u_nk(dataset['data']['AllStates'][0], T=300).loc[0][(0.0,0.0)].values[0], 0.0, decimal=6 ) @@ -180,8 +180,9 @@ def _diag_sum(dataset): u_nk = extract_u_nk(filename, T=300) # Calculate the sum of diagonal elements: - for i in range(len(dataset['data'][leg])): - ds += u_nk.iloc[i][i] + for i, lambda_ in enumerate(u_nk.columns): + #18.6 is the time step + ds += u_nk.loc[i*186/10][lambda_].values[0] return ds diff --git a/src/alchemlyb/tests/test_convergence.py b/src/alchemlyb/tests/test_convergence.py index 7a363426..582cf9d5 100644 --- a/src/alchemlyb/tests/test_convergence.py +++ b/src/alchemlyb/tests/test_convergence.py @@ -22,7 +22,7 @@ def test_convergence_ti(gmx_benzene): def test_convergence_fep(gmx_benzene): dHdl, u_nk = gmx_benzene - convergence = forward_backward_convergence(u_nk, estimator) + convergence = forward_backward_convergence(u_nk, 'MBAR') 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) diff --git a/src/alchemlyb/tests/test_ti_estimators.py b/src/alchemlyb/tests/test_ti_estimators.py index 01b52de4..aed93d64 100644 --- a/src/alchemlyb/tests/test_ti_estimators.py +++ b/src/alchemlyb/tests/test_ti_estimators.py @@ -109,8 +109,8 @@ class TIestimatorMixin: def test_get_delta_f(self, X_delta_f): dHdl, E, dE = X_delta_f est = self.cls().fit(dHdl) - delta_f = est.delta_f_.iloc[0, -1] - d_delta_f = est.d_delta_f_.iloc[0, -1] + delta_f = est.delta_f_.loc[(0.0,1.0)] + d_delta_f = est.d_delta_f_.loc[(0.0,1.0)] assert E == pytest.approx(delta_f, rel=1e-3) assert dE == pytest.approx(d_delta_f, rel=1e-3) diff --git a/src/alchemlyb/tests/test_units.py b/src/alchemlyb/tests/test_units.py index db9c613e..2d2fd76b 100644 --- a/src/alchemlyb/tests/test_units.py +++ b/src/alchemlyb/tests/test_units.py @@ -56,7 +56,7 @@ def dhdl(): def test_kt2kt_number(self, dhdl): new_dhdl = to_kT(dhdl) - assert 12.9 == pytest.approx(new_dhdl.iloc[0, 0], 0.1) + assert 12.9 == pytest.approx(new_dhdl.loc[(0.0,0.0)], 0.1) def test_kt2kt_unit(self, dhdl): new_dhdl = to_kT(dhdl) @@ -70,7 +70,7 @@ def test_kj2kt_unit(self, dhdl): def test_kj2kt_number(self, dhdl): dhdl.attrs['energy_unit'] = 'kJ/mol' new_dhdl = to_kT(dhdl) - assert 5.0 == pytest.approx(new_dhdl.iloc[0, 0], 0.1) + assert 5.0 == pytest.approx(new_dhdl.loc[(0.0,0.0)], 0.1) def test_kcal2kt_unit(self, dhdl): dhdl.attrs['energy_unit'] = 'kcal/mol' @@ -80,7 +80,7 @@ def test_kcal2kt_unit(self, dhdl): def test_kcal2kt_number(self, dhdl): dhdl.attrs['energy_unit'] = 'kcal/mol' new_dhdl = to_kT(dhdl) - assert 21.0 == pytest.approx(new_dhdl.iloc[0, 0], 0.1) + assert 21.0 == pytest.approx(new_dhdl.loc[(0.0,0.0)], 0.1) def test_unknown2kt(self, dhdl): dhdl.attrs['energy_unit'] = 'ddd' From 7609dc26a8e6970319ab76f21ceaec28ab8b8ed4 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 16 Oct 2022 21:32:18 +0100 Subject: [PATCH 04/12] update --- src/alchemlyb/tests/test_fep_estimators.py | 2 +- src/alchemlyb/tests/test_preprocessing.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/alchemlyb/tests/test_fep_estimators.py b/src/alchemlyb/tests/test_fep_estimators.py index e6c340c7..7135c255 100644 --- a/src/alchemlyb/tests/test_fep_estimators.py +++ b/src/alchemlyb/tests/test_fep_estimators.py @@ -181,7 +181,7 @@ def test_failback_adaptive(self, n_uk_list): # The hybr will fail on this while adaptive will work mbar = AutoMBAR().fit(alchemlyb.concat([n_uk[:2] for n_uk in n_uk_list])) - assert np.isclose(mbar.d_delta_f_.iloc[0, -1], 1.76832, 0.1) + assert np.isclose(mbar.d_delta_f_[(0.0, 0.0, 0.0)][(1.0, 1.0, 1.0)], 1.76832, 0.1) def test_AutoMBAR_BGFS(): # A case where only BFGS would work diff --git a/src/alchemlyb/tests/test_preprocessing.py b/src/alchemlyb/tests/test_preprocessing.py index 595eda90..c189df28 100644 --- a/src/alchemlyb/tests/test_preprocessing.py +++ b/src/alchemlyb/tests/test_preprocessing.py @@ -135,10 +135,10 @@ def test_disordered_exception(self, data): """Test that a shuffled DataFrame yields a KeyError. """ - indices = np.arange(len(data)) + indices = data.index.values np.random.shuffle(indices) - df = data.iloc[indices] + df = data.loc[indices] with pytest.raises(KeyError): self.slicer(df, lower=200) From 50bf8aa790040d4a098d1d3eb63b67ee471e5d97 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 16 Oct 2022 22:00:19 +0100 Subject: [PATCH 05/12] ifx test --- src/alchemlyb/tests/test_ti_estimators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/alchemlyb/tests/test_ti_estimators.py b/src/alchemlyb/tests/test_ti_estimators.py index aed93d64..01b52de4 100644 --- a/src/alchemlyb/tests/test_ti_estimators.py +++ b/src/alchemlyb/tests/test_ti_estimators.py @@ -109,8 +109,8 @@ class TIestimatorMixin: def test_get_delta_f(self, X_delta_f): dHdl, E, dE = X_delta_f est = self.cls().fit(dHdl) - delta_f = est.delta_f_.loc[(0.0,1.0)] - d_delta_f = est.d_delta_f_.loc[(0.0,1.0)] + delta_f = est.delta_f_.iloc[0, -1] + d_delta_f = est.d_delta_f_.iloc[0, -1] assert E == pytest.approx(delta_f, rel=1e-3) assert dE == pytest.approx(d_delta_f, rel=1e-3) From e4c0a4d6532427730f9fac34a8b9b4a7623843c7 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 4 Dec 2022 10:48:49 +0000 Subject: [PATCH 06/12] update --- CHANGES | 1 + src/alchemlyb/tests/test_convergence.py | 5 +++-- src/alchemlyb/tests/test_preprocessing.py | 4 ++-- src/alchemlyb/tests/test_ti_estimators.py | 2 ++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 4bd7e8cc..45d1de88 100644 --- a/CHANGES +++ b/CHANGES @@ -18,6 +18,7 @@ The rules for this file: * 1.0.1 Fixes + - Remove most of the iloc in the tests (issue #202, PR #254). - AMBER parser now raises ValueError when the initial simulation time is not found (issue #272, PR #273). - The regex in the AMBER parser now reads also 'field=value' pairs where diff --git a/src/alchemlyb/tests/test_convergence.py b/src/alchemlyb/tests/test_convergence.py index f385a7b0..bae2b350 100644 --- a/src/alchemlyb/tests/test_convergence.py +++ b/src/alchemlyb/tests/test_convergence.py @@ -24,9 +24,10 @@ def test_convergence_ti(gmx_benzene): assert convergence.loc[9, 'Forward'] == pytest.approx(3.09, 0.01) assert convergence.loc[9, 'Backward'] == pytest.approx(3.09, 0.01) -def test_convergence_fep(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, 'MBAR') + convergence = forward_backward_convergence(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) diff --git a/src/alchemlyb/tests/test_preprocessing.py b/src/alchemlyb/tests/test_preprocessing.py index 05048ad8..a124c1aa 100644 --- a/src/alchemlyb/tests/test_preprocessing.py +++ b/src/alchemlyb/tests/test_preprocessing.py @@ -233,7 +233,7 @@ def test_subsampling(self, data, size): """Basic test for execution; resulting size of dataset sensitive to machine and depends on algorithm. """ - assert len(self.slicer(data, series=data.iloc[:, 0])) <= size + assert len(self.slicer(data, series=data.loc[:, data.columns[0]])) <= size @pytest.mark.parametrize('data', [gmx_benzene_dHdl(), gmx_benzene_u_nk()]) @@ -260,7 +260,7 @@ def slicer(self, *args, **kwargs): (False, gmx_benzene_u_nk(), 3571), ]) def test_conservative(self, data, size, conservative): - sliced = self.slicer(data, series=data.iloc[:, 0], conservative=conservative) + sliced = self.slicer(data, series=data.loc[:, data.columns[0]], conservative=conservative) # results can vary slightly with different machines # so possibly do # delta = 10 diff --git a/src/alchemlyb/tests/test_ti_estimators.py b/src/alchemlyb/tests/test_ti_estimators.py index 01b52de4..38b623a8 100644 --- a/src/alchemlyb/tests/test_ti_estimators.py +++ b/src/alchemlyb/tests/test_ti_estimators.py @@ -109,6 +109,8 @@ class TIestimatorMixin: def test_get_delta_f(self, X_delta_f): dHdl, E, dE = X_delta_f est = self.cls().fit(dHdl) + # Use .iloc[0, -1] as we want to cater for both + # delta_f_.loc[0.0, 1.0] and delta_f_.loc[(0.0, 0.0), (0.0, 1.0)] delta_f = est.delta_f_.iloc[0, -1] d_delta_f = est.d_delta_f_.iloc[0, -1] From 6839b8bd17f174342e6b396f7ce3d32957c2a280 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 4 Dec 2022 10:49:48 +0000 Subject: [PATCH 07/12] update --- src/alchemlyb/tests/test_fep_estimators.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/alchemlyb/tests/test_fep_estimators.py b/src/alchemlyb/tests/test_fep_estimators.py index 52c93e88..f61a1a43 100644 --- a/src/alchemlyb/tests/test_fep_estimators.py +++ b/src/alchemlyb/tests/test_fep_estimators.py @@ -139,6 +139,8 @@ def compare_delta_f(self, X_delta_f): assert X_delta_f[2] == pytest.approx(d_delta_f, rel=1e-3) def get_delta_f(self, est): + # Use .iloc[0, -1] as we want to cater for both + # delta_f_.loc[0.0, 1.0] and delta_f_.loc[(0.0, 0.0), (0.0, 1.0)] return est.delta_f_.iloc[0, -1], est.d_delta_f_.iloc[0, -1] @@ -227,6 +229,8 @@ def get_delta_f(self, est): for i in range(len(est.d_delta_f_) - 1): ee += est.d_delta_f_.values[i][i+1]**2 + # Use .iloc[0, -1] as we want to cater for both + # delta_f_.loc[0.0, 1.0] and delta_f_.loc[(0.0, 0.0), (0.0, 1.0)] return est.delta_f_.iloc[0, -1], ee**0.5 class Test_Units(): From 91a39289cc6e36ea5ac3155c8171d31d854eb4cb Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 4 Dec 2022 18:32:04 +0000 Subject: [PATCH 08/12] update --- environment.yml | 4 +++- readthedocs.yml | 9 ++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/environment.yml b/environment.yml index e8c92a4a..61a4b37d 100644 --- a/environment.yml +++ b/environment.yml @@ -2,10 +2,12 @@ name: alchemlyb channels: - conda-forge dependencies: -- python +- python=3.8 - numpy - pandas - pymbar >=3.0.5,<4 - scipy - scikit-learn - matplotlib +- pip + - -e . diff --git a/readthedocs.yml b/readthedocs.yml index 35ffa343..7b0ff11a 100644 --- a/readthedocs.yml +++ b/readthedocs.yml @@ -7,11 +7,10 @@ sphinx: formats: - pdf -python: - version: 3.8 - install: - - method: pip - path: . +build: + os: "ubuntu-20.04" + tools: + python: "mambaforge-4.10" conda: environment: environment.yml From 0d81a47be728f10cdb94a0c026c8143c6d706c44 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 4 Dec 2022 18:36:08 +0000 Subject: [PATCH 09/12] update --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 61a4b37d..6521b1f4 100644 --- a/environment.yml +++ b/environment.yml @@ -10,4 +10,4 @@ dependencies: - scikit-learn - matplotlib - pip - - -e . + - . From 1afd2b98817676cbd1e1d415b2c0960418bb5a4b Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 4 Dec 2022 18:37:13 +0000 Subject: [PATCH 10/12] update --- environment.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/environment.yml b/environment.yml index 6521b1f4..cdc9d1e9 100644 --- a/environment.yml +++ b/environment.yml @@ -9,5 +9,3 @@ dependencies: - scipy - scikit-learn - matplotlib -- pip - - . From 1ab2c67ae9ee1c21168a451971a87bcc227c7680 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Sun, 4 Dec 2022 20:03:58 +0000 Subject: [PATCH 11/12] update --- readthedocs.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/readthedocs.yml b/readthedocs.yml index 7b0ff11a..99e5cc16 100644 --- a/readthedocs.yml +++ b/readthedocs.yml @@ -14,3 +14,8 @@ build: conda: environment: environment.yml + +python: + install: + - method: pip + path: . \ No newline at end of file From 45c9e0ae0de346867533292c9a4564e196ee1154 Mon Sep 17 00:00:00 2001 From: "William (Zhiyi) Wu" Date: Mon, 5 Dec 2022 20:54:43 +0000 Subject: [PATCH 12/12] update the RTD part --- readthedocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readthedocs.yml b/readthedocs.yml index 99e5cc16..71851b19 100644 --- a/readthedocs.yml +++ b/readthedocs.yml @@ -18,4 +18,4 @@ conda: python: install: - method: pip - path: . \ No newline at end of file + path: .