Skip to content

Commit

Permalink
ValueError will be raised if concatenated amber output file has been …
Browse files Browse the repository at this point in the history
…ingested (#326)

* update

* update

* update

---------

Co-authored-by: Zhiyi Wu <[email protected]>
  • Loading branch information
xiki-tempula and xiki-tempula authored Jun 22, 2023
1 parent 1ff15fe commit 3dae72a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The rules for this file:
* 2.1.0

Changes
- ValueError raised if concatenated amber output file is passed to amber
parser (issue #315, PR #326).
- Change the % based string formatting to {} based string formatting (issue #323, PR #324).
- Use loguru instead of logging for log (issue #301, PR #303).

Expand Down
9 changes: 8 additions & 1 deletion src/alchemlyb/parsing/amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,18 @@ def extract(outfile, T):
"^ NSTEP", "^ ---", ["NSTEP", "DV/DL"], extra=line
)
if nstep != old_nstep and dvdl is not None and nstep is not None:
if finished:
raise ValueError(
"TI Energy detected after the TIMINGS section. Did you concatenate the output file?"
)
file_datum.gradients.append(dvdl)
nensec += 1
old_nstep = nstep
elif line.startswith("MBAR Energy analysis") and file_datum.have_mbar:
if finished:
raise ValueError(
"MBAR Energy detected after the TIMINGS section. Did you concatenate the output file?"
)
mbar = secp.extract_section(
"^MBAR", "^ ---", file_datum.mbar_lambdas, extra=line
)
Expand All @@ -356,7 +364,6 @@ def extract(outfile, T):
)
elif line == " 5. TIMINGS\n":
finished = True
break

if high_E_cnt:
logger.warning(
Expand Down
33 changes: 33 additions & 0 deletions src/alchemlyb/tests/parsing/test_amber.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Amber parser tests.
"""
import bz2
import logging

import pandas as pd
Expand Down Expand Up @@ -250,3 +251,35 @@ def test_u_nk_improper(improper_filename, names=("time", "lambdas")):
assert u_nk.index.names == names
except Exception:
assert "0.5626" in improper_filename


def test_concatenated_amber_u_nk(tmp_path):
with bz2.open(load_bace_example()["data"]["complex"]["decharge"][0], "rt") as file:
content = file.read()

with open(tmp_path / "amber.out", "w") as f:
f.write(content)
f.write("\n")
f.write(content)

with pytest.raises(
ValueError,
match="MBAR Energy detected after the TIMINGS section.",
):
extract(tmp_path / "amber.out", 298)


def test_concatenated_amber_dhdl(tmp_path):
with bz2.open(load_bace_example()["data"]["complex"]["decharge"][0], "rt") as file:
content = file.read().replace("MBAR Energy analysis", "")

with open(tmp_path / "amber.out", "w") as f:
f.write(content)
f.write("\n")
f.write(content)

with pytest.raises(
ValueError,
match="TI Energy detected after the TIMINGS section.",
):
extract(tmp_path / "amber.out", 298)

0 comments on commit 3dae72a

Please sign in to comment.