From 5bebf47700d6c5c8388f16614b9e6554e2654483 Mon Sep 17 00:00:00 2001 From: Guillaume PIERRE Date: Thu, 4 Apr 2024 13:20:38 +0200 Subject: [PATCH 1/2] Refactoring : move executable finding from command building to main program Note that executable finding is now out from the loop over studies. Inside the loop, this task was unnecessarily repeated. Now it's done once. --- scripts/antares_test_utils.py | 3 --- scripts/generate_reference.py | 10 ++++++---- scripts/run_command_building.py | 19 +++++++------------ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/scripts/antares_test_utils.py b/scripts/antares_test_utils.py index 3c3e455..159ba4e 100644 --- a/scripts/antares_test_utils.py +++ b/scripts/antares_test_utils.py @@ -26,9 +26,6 @@ def find_studies_in_batch_dir(batch_name): return studies -def get_headers(df) -> set : - return set(df.columns) - def remove_possibly_remaining_outputs(study_path): output_path = study_path / 'output' shutil.rmtree(output_path, ignore_errors=True) diff --git a/scripts/generate_reference.py b/scripts/generate_reference.py index 029142c..2db5e5c 100644 --- a/scripts/generate_reference.py +++ b/scripts/generate_reference.py @@ -4,20 +4,22 @@ from run_command_building import * import sys -# Parsing args +# Parsing command line args parser = argparse.ArgumentParser() parser.add_argument("batch_name", help="Batch directory (containing studies)", type=str) parser.add_argument("path_where_to_find_exe", help="Path to executable", type=str) -# Storing args +# Storing command line args args = parser.parse_args() batch_name = args.batch_name path_where_to_find_exe = Path(args.path_where_to_find_exe) +# Find exe path if not path_where_to_find_exe.is_dir() and not path_where_to_find_exe.is_file(): raise RuntimeError("Path where to find an executable does not exist") - +exe_identifier = exe_id_from_batch_name(batch_name) +exe_path = find_exe_path(path_where_to_find_exe, exe_identifier) # Looking for studies in batch directory study_path_collection = antares_utils.find_studies_in_batch_dir(batch_name) @@ -29,7 +31,7 @@ antares_utils.remove_possibly_remaining_outputs(study_path) antares_utils.enable_study_output(study_path, True) - command_to_run = make_command_to_run(path_where_to_find_exe, batch_name, study_path) + command_to_run = make_command_to_run(exe_path, batch_name, study_path) result = run_command(command_to_run) ret.append(result) print('OK' if result else 'KO') diff --git a/scripts/run_command_building.py b/scripts/run_command_building.py index 1d6019a..103dbcd 100644 --- a/scripts/run_command_building.py +++ b/scripts/run_command_building.py @@ -2,6 +2,12 @@ import subprocess import sys +def exe_id_from_batch_name(batch_name): + exe_identifier = "solver" # Default value + if batch_name == "ts-generator": + exe_identifier = "ts-generator" + return exe_identifier + def find_exe_path(path_to_search_exe_in, exe_identifier): searched_exe = f"antares-{exe_identifier}" if sys.platform.startswith("win"): @@ -14,33 +20,22 @@ def find_exe_path(path_to_search_exe_in, exe_identifier): return path_item.resolve() raise RuntimeError("Missing {searched_exe}") -def make_command_to_run(path_where_to_find_exe, batch_name, study_path): +def make_command_to_run(exe_path, batch_name, study_path): command_to_run = [] - exe_path = Path() - exe_identifier = "solver" # Default value - if batch_name == "ts-generator": - exe_identifier = "ts-generator" - exe_path = find_exe_path(path_where_to_find_exe, exe_identifier) print(f"Found executabled : {exe_path}") - cluster_to_gen_file = open(study_path / "clustersToGen.txt", 'r') cluster_to_gen = cluster_to_gen_file.readline().rstrip() cluster_to_gen_file.close() command_to_run = [exe_path, cluster_to_gen, str(study_path)] - else: - exe_path = find_exe_path(path_where_to_find_exe, exe_identifier) print(f"Found executabled : {exe_path}") - command_to_run = [exe_path, "-i", str(study_path)] if batch_name == "valid-milp": command_to_run.append('--use-ortools') command_to_run.append('--ortools-solver=coin') - if batch_name == "valid-named-mps": command_to_run.append('--named-mps-problems') - return command_to_run def run_command(command): From ef71961a0993978238dd2f50c22cda7031ab9ec4 Mon Sep 17 00:00:00 2001 From: Guillaume PIERRE Date: Thu, 4 Apr 2024 17:31:56 +0200 Subject: [PATCH 2/2] Refactoring : small clarification by moving a log where it belongs --- scripts/generate_reference.py | 1 + scripts/run_command_building.py | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/generate_reference.py b/scripts/generate_reference.py index 2db5e5c..846ac8e 100644 --- a/scripts/generate_reference.py +++ b/scripts/generate_reference.py @@ -20,6 +20,7 @@ raise RuntimeError("Path where to find an executable does not exist") exe_identifier = exe_id_from_batch_name(batch_name) exe_path = find_exe_path(path_where_to_find_exe, exe_identifier) +print(f"Found executabled : {exe_path}") # Looking for studies in batch directory study_path_collection = antares_utils.find_studies_in_batch_dir(batch_name) diff --git a/scripts/run_command_building.py b/scripts/run_command_building.py index 103dbcd..2c10b75 100644 --- a/scripts/run_command_building.py +++ b/scripts/run_command_building.py @@ -23,13 +23,11 @@ def find_exe_path(path_to_search_exe_in, exe_identifier): def make_command_to_run(exe_path, batch_name, study_path): command_to_run = [] if batch_name == "ts-generator": - print(f"Found executabled : {exe_path}") cluster_to_gen_file = open(study_path / "clustersToGen.txt", 'r') cluster_to_gen = cluster_to_gen_file.readline().rstrip() cluster_to_gen_file.close() command_to_run = [exe_path, cluster_to_gen, str(study_path)] else: - print(f"Found executabled : {exe_path}") command_to_run = [exe_path, "-i", str(study_path)] if batch_name == "valid-milp": command_to_run.append('--use-ortools')