Coverage for sparkle/CLI/add_instances.py: 86%
69 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-05 14:48 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-05 14:48 +0000
1#!/usr/bin/env python3
2"""Sparkle command to add an instance set to the Sparkle platform."""
4import sys
5import argparse
6from pathlib import Path
7import shutil
9from sparkle.CLI.help import global_variables as gv
10from sparkle.platform import file_help as sfh
11from sparkle.platform.settings_objects import SettingState
12from sparkle.instance import Instance_Set
13from sparkle.structures import FeatureDataFrame, PerformanceDataFrame
14from sparkle.CLI.compute_features import compute_features
15from sparkle.CLI.run_solvers import running_solvers_performance_data
16from sparkle.CLI.help import logging as sl
17from sparkle.platform import CommandName, COMMAND_DEPENDENCIES
18from sparkle.CLI.initialise import check_for_initialise
19from sparkle.CLI.help import argparse_custom as ac
22def parser_function() -> argparse.ArgumentParser:
23 """Define the command line arguments."""
24 parser = argparse.ArgumentParser(description="Add instances to the platform.")
25 parser.add_argument(*ac.InstancesPathArgument.names,
26 **ac.InstancesPathArgument.kwargs)
27 parser.add_argument(*ac.RunExtractorNowArgument.names,
28 **ac.RunExtractorNowArgument.kwargs)
29 parser.add_argument(*ac.RunSolverNowArgument.names,
30 **ac.RunSolverNowArgument.kwargs)
31 parser.add_argument(*ac.NicknameInstanceSetArgument.names,
32 **ac.NicknameInstanceSetArgument.kwargs)
33 parser.add_argument(*ac.RunOnArgument.names,
34 **ac.RunOnArgument.kwargs)
36 return parser
39def main(argv: list[str]) -> None:
40 """Main function of the add instances command."""
41 # Log command call
42 sl.log_command(sys.argv)
44 # Define command line arguments
45 parser = parser_function()
47 # Process command line arguments
48 args = parser.parse_args(argv)
49 instances_source = Path(args.instances_path)
50 instances_target = gv.settings().DEFAULT_instance_dir / instances_source.name
52 if args.run_on is not None:
53 gv.settings().set_run_on(
54 args.run_on.value, SettingState.CMD_LINE)
55 run_on = gv.settings().get_run_on()
57 check_for_initialise(COMMAND_DEPENDENCIES[CommandName.ADD_INSTANCES])
59 if not instances_source.exists():
60 print(f'Instance set path "{instances_source}" does not exist!')
61 sys.exit(-1)
62 if instances_target.exists():
63 print(f'Instance set "{instances_source.name}" already exists in Sparkle! '
64 "Exiting...")
65 sys.exit(-1)
66 if args.nickname is not None:
67 sfh.add_remove_platform_item(instances_target,
68 gv.instances_nickname_path, key=args.nickname)
70 print(f"Start adding all instances in directory {instances_source} ...")
71 new_instance_set = Instance_Set(instances_source)
73 instances_target.mkdir(parents=True)
74 print("Copying files...")
75 for instance_path_source in new_instance_set.all_paths:
76 print(f"Copying {instance_path_source} to {instances_target}...", end="\r")
77 shutil.copy(instance_path_source, instances_target)
78 print("\nCopying done!")
79 # Refresh the instance set as the target instance set
80 new_instance_set = Instance_Set(instances_target)
82 # Add the instances to the Feature Data / Performance Data
83 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path)
84 # When adding instances, an empty performance DF has no objectives yet
85 performance_data = PerformanceDataFrame(
86 gv.settings().DEFAULT_performance_data_path,
87 objectives=gv.settings().get_general_sparkle_objectives())
88 for instance_path in new_instance_set.instance_paths:
89 # Construct a name path due to multi-file instances
90 feature_data.add_instances(str(instance_path))
91 performance_data.add_instance(str(instance_path))
92 feature_data.save_csv()
93 performance_data.save_csv()
95 print(f"\nAdding instance set {new_instance_set.name} done!")
97 if args.run_extractor_now:
98 print("Start computing features ...")
99 compute_features(gv.settings().DEFAULT_feature_data_path, False)
101 if args.run_solver_now:
102 num_job_in_parallel = gv.settings().get_number_of_jobs_in_parallel()
103 running_solvers_performance_data(gv.settings().DEFAULT_performance_data_path,
104 num_job_in_parallel,
105 rerun=False, run_on=run_on)
106 print("Running solvers...")
108 # Write used settings to file
109 gv.settings().write_used_settings()
110 sys.exit(0)
113if __name__ == "__main__":
114 main(sys.argv[1:])