From 3dae72ae5a77d0bbd9b549b0024c2c83e6a11aec Mon Sep 17 00:00:00 2001 From: Zhiyi Wu Date: Thu, 22 Jun 2023 13:28:45 +0100 Subject: [PATCH] ValueError will be raised if concatenated amber output file has been ingested (#326) * update * update * update --------- Co-authored-by: Zhiyi Wu --- CHANGES | 2 ++ src/alchemlyb/parsing/amber.py | 9 ++++++- src/alchemlyb/tests/parsing/test_amber.py | 33 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index c3ed0b3e..37d88468 100644 --- a/CHANGES +++ b/CHANGES @@ -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). diff --git a/src/alchemlyb/parsing/amber.py b/src/alchemlyb/parsing/amber.py index 543d4d54..6bb5441f 100644 --- a/src/alchemlyb/parsing/amber.py +++ b/src/alchemlyb/parsing/amber.py @@ -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 ) @@ -356,7 +364,6 @@ def extract(outfile, T): ) elif line == " 5. TIMINGS\n": finished = True - break if high_E_cnt: logger.warning( diff --git a/src/alchemlyb/tests/parsing/test_amber.py b/src/alchemlyb/tests/parsing/test_amber.py index 0d186cc7..21b6064e 100644 --- a/src/alchemlyb/tests/parsing/test_amber.py +++ b/src/alchemlyb/tests/parsing/test_amber.py @@ -1,6 +1,7 @@ """Amber parser tests. """ +import bz2 import logging import pandas as pd @@ -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)