Skip to content

Commit

Permalink
Fix dhdl2series and u_nk2series would not reattach the unit (#248)
Browse files Browse the repository at this point in the history
* ensure that `@pass_attrs` is applied to all functions that handle our DataFrames
  fix: dhdl2series() and u_nk2series()
* update tests
* update CHANGELOG

Co-authored-by: Zhiyi Wu <zwu@exscientia.co.uk>
  • Loading branch information
xiki-tempula and xiki-tempula authored Oct 18, 2022
1 parent b949785 commit be3e73a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Fixes
- All parsers now have a 'extract(file, T)' method that returns a dict with both
"dHdl" and "u_nk" data (or None). THe AMBER parser when using this function will read the
file just once, extracting all data at once. (issue #222, PR #240)
- Fix dhdl2series and u_nk2series would not reattach the unit. (PR #248)

07/22/2022 xiki-tempula, IAlibay, dotsdl, orbeckst, ptmerz

Expand Down
3 changes: 3 additions & 0 deletions src/alchemlyb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pandas as pd
from functools import wraps

from ._version import get_versions
__version__ = get_versions()['version']
Expand All @@ -11,6 +12,8 @@ def pass_attrs(func):
.. versionadded:: 0.5.0
'''

@wraps(func)
def wrapper(input_dataframe, *args,**kwargs):
dataframe = func(input_dataframe, *args,**kwargs)
dataframe.attrs = input_dataframe.attrs
Expand Down
3 changes: 3 additions & 0 deletions src/alchemlyb/preprocessing/subsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pymbar.timeseries import (statisticalInefficiency,
detectEquilibration,
subsampleCorrelatedData, )
from .. import pass_attrs

def decorrelate_u_nk(df, method='dhdl', drop_duplicates=True,
sort=True, remove_burnin=False, **kwargs):
Expand Down Expand Up @@ -106,6 +107,7 @@ def decorrelate_dhdl(df, drop_duplicates=True, sort=True,
else:
return statistical_inefficiency(df, series, **kwargs)

@pass_attrs
def u_nk2series(df, method='dhdl'):
"""Convert an u_nk DataFrame into a series based on the selected method
for subsampling.
Expand Down Expand Up @@ -175,6 +177,7 @@ def u_nk2series(df, method='dhdl'):
return series


@pass_attrs
def dhdl2series(df, method=''):
"""Convert a dhdl DataFrame to a series for subsampling.
Expand Down
35 changes: 34 additions & 1 deletion src/alchemlyb/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import alchemlyb
from alchemlyb import pass_attrs
from alchemtest.gmx import load_benzene
from alchemlyb.parsing.gmx import extract_dHdl
from alchemlyb.parsing.gmx import extract_dHdl, extract_u_nk
from alchemlyb.postprocessors.units import to_kT
from alchemlyb.preprocessing import (dhdl2series, u_nk2series,
decorrelate_u_nk, decorrelate_dhdl,
slicing, statistical_inefficiency,
equilibrium_detection)

def test_noT():
'''Test no temperature error'''
Expand Down Expand Up @@ -120,3 +124,32 @@ def test_pd_slice():
df = pd.DataFrame(data=d)
df.attrs = {1: 1}
assert df[::2].attrs == {1: 1}

class TestRetainUnit():
'''This test tests if the functions that should retain the unit would actually
retain the units.'''
@staticmethod
@pytest.fixture(scope='class')
def dhdl():
dataset = load_benzene()
dhdl = extract_dHdl(dataset['data']['Coulomb'][0], 310)
return dhdl

@staticmethod
@pytest.fixture(scope='class')
def u_nk():
dataset = load_benzene()
u_nk = extract_u_nk(dataset['data']['Coulomb'][0], 310)
return u_nk

@pytest.mark.parametrize('func,fixture_in',
[(dhdl2series, 'dhdl'),
(u_nk2series, 'u_nk'),
(decorrelate_u_nk, 'u_nk'),
(decorrelate_dhdl, 'dhdl'),
(slicing, 'dhdl'),
(statistical_inefficiency, 'dhdl'),
(equilibrium_detection, 'dhdl')])
def test_function(self, func, fixture_in, request):
result = func(request.getfixturevalue(fixture_in))
assert result.attrs['energy_unit'] is not None

0 comments on commit be3e73a

Please sign in to comment.