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
Changes from 20 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
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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
37 changes: 22 additions & 15 deletions src/alchemlyb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import pandas as pd
from functools import wraps

import pandas as pd

from ._version import get_versions
__version__ = get_versions()['version']

__version__ = get_versions()["version"]
del get_versions


def pass_attrs(func):
'''Pass the attrs from the first positional argument to the output
"""Pass the attrs from the first positional argument to the output
dataframe.


.. versionadded:: 0.5.0
'''
"""

@wraps(func)
def wrapper(input_dataframe, *args,**kwargs):
dataframe = func(input_dataframe, *args,**kwargs)
def wrapper(input_dataframe, *args, **kwargs):
dataframe = func(input_dataframe, *args, **kwargs)
dataframe.attrs = input_dataframe.attrs
return dataframe

return wrapper


def concat(objs, *args, **kwargs):
'''Concatenate pandas objects while persevering the attrs.
"""Concatenate pandas objects while persevering the attrs.

Concatenate pandas objects along a particular axis with optional set
logic along the other axes. If all pandas objects have the same attrs
@@ -46,16 +51,18 @@ def concat(objs, *args, **kwargs):
See Also
--------
pandas.concat


.. versionadded:: 0.5.0'''


.. versionadded:: 0.5.0"""
if isinstance(objs, pd.DataFrame):
return objs
# Sanity check
try:
attrs = objs[0].attrs
except IndexError: # except empty list as input
raise ValueError('No objects to concatenate')
except IndexError: # except empty list as input
raise ValueError("No objects to concatenate")

for obj in objs:
if attrs != obj.attrs:
raise ValueError('All pandas objects should have the same attrs.')
raise ValueError("All pandas objects should have the same attrs.")
return pd.concat(objs, *args, **kwargs)
268 changes: 268 additions & 0 deletions src/alchemlyb/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
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
11 changes: 6 additions & 5 deletions src/alchemlyb/tests/parsing/test_gmx.py
Original file line number Diff line number Diff line change
@@ -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

Loading