Coverage for sparkle/CLI/add_instances.py: 93%
56 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-29 10:17 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-29 10:17 +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.instance import Instance_Set
12from sparkle.structures import FeatureDataFrame, PerformanceDataFrame
13from sparkle.CLI.help import logging as sl
15from sparkle.CLI.initialise import check_for_initialise
16from sparkle.CLI.help import argparse_custom as ac
19def parser_function() -> argparse.ArgumentParser:
20 """Define the command line arguments."""
21 parser = argparse.ArgumentParser(description="Add instances to the platform.")
22 parser.add_argument(
23 *ac.InstancesPathArgument.names, **ac.InstancesPathArgument.kwargs
24 )
25 parser.add_argument(
26 *ac.NicknameInstanceSetArgument.names, **ac.NicknameInstanceSetArgument.kwargs
27 )
28 parser.add_argument(*ac.NoCopyArgument.names, **ac.NoCopyArgument.kwargs)
29 return parser
32def main(argv: list[str]) -> None:
33 """Main function of the add instances command."""
34 # Log command call
35 sl.log_command(sys.argv, gv.settings().random_state)
37 # Define command line arguments
38 parser = parser_function()
39 check_for_initialise()
41 # Process command line arguments
42 args = parser.parse_args(argv)
43 instances_source = Path(args.instances_path)
44 instances_target = gv.settings().DEFAULT_instance_dir / instances_source.name
46 if not instances_source.exists():
47 print(f'Instance set path "{instances_source}" does not exist!')
48 sys.exit(-1)
49 if instances_target.exists():
50 print(
51 f'Instance set "{instances_source.name}" already exists in Sparkle! '
52 "Exiting..."
53 )
54 sys.exit(-1)
55 if args.nickname is not None:
56 sfh.add_remove_platform_item(
57 instances_target,
58 gv.instances_nickname_path,
59 gv.file_storage_data_mapping[gv.instances_nickname_path],
60 key=args.nickname,
61 )
63 print(f"Start adding all instances in directory {instances_source} ...")
64 new_instance_set = Instance_Set(instances_source)
66 if args.no_copy:
67 print(f"Creating symbolic link from {instances_source} to {instances_target}...")
68 instances_target.symlink_to(instances_source.absolute())
69 else:
70 instances_target.mkdir(parents=True)
71 print("Copying files...")
72 for instance_path_source in new_instance_set.all_paths:
73 print(f"Copying {instance_path_source} to {instances_target}...", end="\r")
74 shutil.copy(instance_path_source, instances_target)
75 print("\nCopying done!")
76 # Refresh the instance set as the target instance set
77 new_instance_set = Instance_Set(instances_target)
79 # Add the instances to the Feature Data / Performance Data
80 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path)
81 # When adding instances, an empty performance DF has no objectives yet
82 performance_data = PerformanceDataFrame(
83 gv.settings().DEFAULT_performance_data_path,
84 objectives=gv.settings().objectives,
85 )
86 for instance_name in new_instance_set.instance_names:
87 # Construct a name path due to multi-file instances
88 feature_data.add_instances(str(instance_name))
89 performance_data.add_instance(str(instance_name))
90 feature_data.save_csv()
91 performance_data.save_csv()
93 print(f"Adding instance set {new_instance_set.name} done!")
95 # Write used settings to file
96 gv.settings().write_used_settings()
97 sys.exit(0)
100if __name__ == "__main__":
101 main(sys.argv[1:])