From 4adc71f6a455fc5151089ad2c097b8644506f76b Mon Sep 17 00:00:00 2001 From: Kohei Noda Date: Mon, 15 Jan 2024 15:35:39 +0900 Subject: [PATCH] Fix IVO/CASPT2 test generator algorithm --- tools/dcaspt2_test_generator | 109 ++++++++++++++++------------------- 1 file changed, 50 insertions(+), 59 deletions(-) diff --git a/tools/dcaspt2_test_generator b/tools/dcaspt2_test_generator index 86ff2b4c..a6f7faee 100755 --- a/tools/dcaspt2_test_generator +++ b/tools/dcaspt2_test_generator @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -from pathlib import Path -import os import glob import logging +import os import shutil +from pathlib import Path import questionary @@ -15,12 +15,21 @@ def check_type(obj, type_: type): return obj -def check_test_type() -> str: +class TestType: + marker: str + dir: str + + def __init__(self, marker: str, dir: str): + self.marker = marker + self.dir = dir + + +def check_test_type() -> TestType: # Ask for the type of the test(slow, normal, fast) - test_speed_types: dict[str, str] = { - "slow, 20↑ sec": "slow", - "normal, 10-20 sec": "unmarked", - "fast(dev), 1-10 sec": "dev", + test_speed_types: dict[str, TestType] = { + "slow, 20↑ sec": TestType(marker="@pytest.mark.slowonly", dir="slow"), + "normal, 10-20 sec": TestType(marker="", dir="unmarker"), + "fast(dev), 1-10 sec": TestType(marker="@pytest.marker.dev", dir="dev"), } ref_test_speed_types = list(test_speed_types.keys()) ref_test_type = check_type( @@ -35,16 +44,13 @@ def check_test_type() -> str: return test_speed_types[ref_test_type] -def ask_point_group() -> str: - point_group_answer = check_type(questionary.text("Please enter the point group of the molecule.").ask(), str) - logging.info(f"Point group: {point_group_answer}") - return point_group_answer - - -def ask_molecule_name() -> str: - molecule_name_answer = check_type(questionary.text("Please enter the name or the formula of the molecule.").ask(), str) - logging.info(f"Molecule: {molecule_name_answer}") - return molecule_name_answer +def ask_test_directory_name() -> str: + test_directory_name_answer = check_type(questionary.text("Please enter the name of the test directory.").ask(), str) + if test_directory_name_answer == "": + logging.error("Test directory name cannot be empty. Exiting...") + raise ValueError + logging.info(f"Test directory name: {test_directory_name_answer}") + return test_directory_name_answer def ask_general_file_path(program_name: str, file_type: str, default_path: Path = Path()) -> Path: @@ -71,7 +77,9 @@ def ask_output_file_path(program_name: str, default_path: Path = Path()) -> Path def ask_dirac_integrals_directory(default_dir: Path = Path()) -> Path: - integral_dir_answer = check_type(questionary.path("Please enter the directory of the DIRAC integrals files (MRCONEE, MDCINT, MDCINXXXX1, ...) are stored.", default=str(default_dir)).ask(), str) + integral_dir_answer = check_type( + questionary.path("Please enter the directory of the DIRAC integrals files (MRCONEE, MDCINT, MDCINXXXX1, ...) are stored.", default=str(default_dir)).ask(), str + ) # Check if the directory exists integral_dir_path = Path(integral_dir_answer).expanduser().resolve() mrconee_path = Path.joinpath(integral_dir_path, "MRCONEE") @@ -122,21 +130,18 @@ def main(): # Ask for the type of the test(slow, normal, fast) selected_test_type = check_test_type() - # Ask for the path of the reference output - ref_output_path = ask_output_file_path(program_name="DIRAC-CASPT2", default_path=user_dir) - ref_parent_dir = ref_output_path.parent - # Get the point group and the molecule name - point_group = ask_point_group() - molecule_name = ask_molecule_name() - # Create the test directory name - type_str = "" if selected_test_type == "unmarked" else f"_{selected_test_type}" - dirname = point_group + "_" + molecule_name + type_str + # Get the test directory name + test_directory_name = ask_test_directory_name() # mkdir the test directory (the name of the directory is the name of the reference output file without the extension) - dir_path = Path.joinpath(test_dir, selected_test_type, dirname).resolve() + dir_path = Path.joinpath(test_dir, selected_test_type.dir, test_directory_name).resolve() + logging.info(f"Test directory path: {dir_path}") # if the directory already exists, exit - if dir_path.is_dir(): + if dir_path.exists(): logging.error(f"The test directory {dir_path} already exists. Exiting...") raise FileExistsError + # Ask for the path of the reference output + ref_output_path = ask_output_file_path(program_name="DIRAC-CASPT2", default_path=user_dir) + ref_parent_dir = ref_output_path.parent # Get the input file path input_file_path = ask_input_file_path(program_name="DIRAC-CASPT2", default_path=ref_parent_dir) @@ -151,7 +156,7 @@ def main(): # Copy the input and output file to the test directory logging.info("Copying input, and output files to the test directory...") shutil.copy(input_file_path, Path.joinpath(dir_path, "active.inp")) - shutil.copy(ref_output_path, Path.joinpath(dir_path, f"{dirname}.out")) + shutil.copy(ref_output_path, Path.joinpath(dir_path, f"reference.{test_directory_name}.out")) # Copy the integral files to the test directory logging.info("Copying integral files to the test directory...") shutil.copy(mrconee_path, dir_path) @@ -162,18 +167,13 @@ def main(): # Copy the template python file to the test directory logging.info("Copying template python file to the test directory...") template_python_file_path = Path.joinpath(test_template_dir, "caspt2_energy_test", "test_template_caspt2.py") - python_file_path = Path.joinpath(dir_path, f"test_{dirname}.py") + python_file_path = Path.joinpath(dir_path, f"test_{test_directory_name}.py") shutil.copy(template_python_file_path, python_file_path) # Replace the template python file with the correct values - # Replece ref_output_file.out with the correct value - new_marker = "" if selected_test_type == "unmarked" else "@pytest.mark." + selected_test_type replace_dict = { - "replace_ref_output_file.out": f"reference.{dirname}.out", - "replace_output_filename.out": f"{dirname}.caspt2.out", - "replace_latest_passed.output_filename.out": f"latest_passed.{dirname}.caspt2.out", - "def replace_test_template": f"def test_{dirname}", - "# @replace_marker": new_marker, + "def replace_test_template": f"def test_{test_directory_name}", + "# @replace_marker": selected_test_type.marker, } with open(python_file_path, "r") as f: lines = f.readlines() @@ -209,24 +209,18 @@ def main(): # Ask for the type of the test(slow, normal, fast) selected_test_type = check_test_type() - # Ask for the path of the reference output - ref_output_path = ask_output_file_path(program_name="DIRAC-IVO", default_path=user_dir) - ref_parent_dir = ref_output_path.parent - # Get the point group and the molecule name - point_group = ask_point_group() - molecule_name = ask_molecule_name() - # Create the test directory name - type_str = "" if selected_test_type == "unmarked" else f"_{selected_test_type}" - + # Get the test directory name + test_directory_name = ask_test_directory_name() # mkdir the test directory (the name of the directory is the name of the reference output file without the extension) - dirname = point_group + "_" + molecule_name + "_ivo" + type_str - dir_path = Path.joinpath(test_dir, selected_test_type, dirname).resolve() + dir_path = Path.joinpath(test_dir, selected_test_type.dir, test_directory_name).resolve() + logging.info(f"Test directory path: {dir_path}") # if the directory already exists, exit if dir_path.is_dir(): logging.error(f"The test directory {dir_path} already exists. Exiting...") raise FileExistsError - # Get the input file path - input_file_path = ask_input_file_path(program_name="DIRAC-IVO", default_path=ref_parent_dir) + # Ask for the path of the reference output + ref_output_path = ask_output_file_path(program_name="DIRAC-IVO", default_path=user_dir) + ref_parent_dir = ref_output_path.parent # Ask for the DFPCMO and DFPCMONEW files dfpcmo_file_path = ask_general_file_path(program_name="DIRAC-IVO", file_type="DFPCMO", default_path=ref_parent_dir) @@ -241,11 +235,11 @@ def main(): # Copy the input and output file to the test directory logging.info("Copying input, and output files to the test directory...") shutil.copy(input_file_path, Path.joinpath(dir_path, "active.ivo.inp")) - shutil.copy(ref_output_path, Path.joinpath(dir_path, f"{dirname}.out")) + shutil.copy(ref_output_path, Path.joinpath(dir_path, f"reference.{test_directory_name}.out")) # Copy the DFPCMO and DFPCMONEW files to the test directory logging.info("Copying DFPCMO and DFPCMONEW files to the test directory...") shutil.copy(dfpcmo_file_path, Path.joinpath(dir_path, "DFPCMO")) - shutil.copy(ref_dfpcmonew_file_path, Path.joinpath(dir_path, "ref_DFPCMONEW")) + shutil.copy(ref_dfpcmonew_file_path, Path.joinpath(dir_path, "reference.DFPCMONEW")) # Copy the integral files to the test directory logging.info("Copying integral files to the test directory...") shutil.copy(Path.joinpath(integrals_file_path, "MRCONEE"), dir_path) @@ -256,16 +250,13 @@ def main(): # Copy the template python file to the test directory logging.info("Copying template python file to the test directory...") template_python_file_path = Path.joinpath(test_template_dir, "ivo_test", "test_template_ivo.py") - python_file_path = Path.joinpath(dir_path, f"test_{dirname}.py") + python_file_path = Path.joinpath(dir_path, f"test_{test_directory_name}.py") shutil.copy(template_python_file_path, python_file_path) # Replace the template python file with the correct values - new_marker = "" if selected_test_type == "unmarked" else "@pytest.mark." + selected_test_type replace_dict = { - "replace_output_filename.out": f"latest_passed.{dirname}.ivo.out", - "replace_latest_passed_output_filename.out": f"latest_passed.{dirname}.ivo.out", - "def replace_test_template": f"def test_{dirname}", - "# @replace_marker": new_marker, + "def replace_test_template": f"def test_{test_directory_name}", + "# @replace_marker": selected_test_type.marker, } with open(python_file_path, "r") as f: