Coverage for src / sparkle / CLI / help / global_variables.py: 55%
67 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 15:31 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 15:31 +0000
1#!/usr/bin/env python3
2"""Definitions of constants broadly used in Sparkle."""
4from __future__ import annotations
5from typing import TYPE_CHECKING
6import ast
7from argparse import Namespace
8import random
9import numpy as np
11from sparkle.platform.settings_objects import Settings
13if TYPE_CHECKING:
14 from sparkle.configurator.configurator import ConfigurationScenario
15 from sparkle.selector import SelectionScenario
16 from sparkle.structures import PerformanceDataFrame
18__settings: Settings = None
21def settings(argsv: Namespace = None) -> Settings:
22 """Function to get the global settings object."""
23 global __settings
24 if __settings is None:
25 __settings = Settings(Settings.DEFAULT_settings_path, argsv=argsv)
26 # Set global random state
27 max_seed = 2**32 - 1
28 latest_ini = Settings(Settings.DEFAULT_previous_settings_path)
29 # Determine seed priority: latest.ini > __settings > random
30 seed = latest_ini.seed or __settings.seed or random.randint(0, max_seed)
31 # Set global RNG states
32 np.random.seed(seed)
33 random.seed(seed)
34 __settings.random_state = seed
35 # Next seed will be saved in latest.ini when the cli script calls 'write_used_settings()'
36 next_seed = random.randint(0, max_seed)
37 __settings.seed = next_seed
39 elif argsv is not None:
40 __settings.apply_arguments(argsv)
42 return __settings
45__configuration_scenarios: list[ConfigurationScenario] = None
46__selection_scenarios: list[SelectionScenario] = None
49def configuration_scenarios(refresh: bool = False) -> list[ConfigurationScenario]:
50 """Fetch all known configuration scenarios."""
51 global __configuration_scenarios
52 config_path = Settings.DEFAULT_configuration_output
53 if __configuration_scenarios is None or refresh:
54 # NOTE: Import here for platform speedup
55 from sparkle.configurator.implementations import (
56 SMAC2Scenario,
57 SMAC3Scenario,
58 ParamILSScenario,
59 IRACEScenario,
60 )
62 __configuration_scenarios = []
63 for f in config_path.glob("*/*/*.*"): # We look for files at depth three
64 if "scenario" not in f.name:
65 continue
66 if "SMAC2" in str(f):
67 __configuration_scenarios.append(SMAC2Scenario.from_file(f))
68 elif "SMAC3" in str(f):
69 __configuration_scenarios.append(SMAC3Scenario.from_file(f))
70 elif "ParamILS" in str(f):
71 __configuration_scenarios.append(ParamILSScenario.from_file(f))
72 elif "IRACE" in str(f):
73 __configuration_scenarios.append(IRACEScenario.from_file(f))
74 return __configuration_scenarios
77def selection_scenarios(refresh: bool = False) -> list[SelectionScenario]:
78 """Fetch all known selection scenarios."""
79 global __selection_scenarios
80 selection_path = Settings.DEFAULT_selection_output
81 if __selection_scenarios is None or refresh:
82 # NOTE: Import here for speedup
83 from sparkle.selector import SelectionScenario
85 __selection_scenarios = []
86 for f in selection_path.glob("*/*/*.txt"): # We look for files at depth three
87 if "scenario" not in f.name:
88 continue
89 __selection_scenarios.append(SelectionScenario.from_file(f))
90 return __selection_scenarios
93def parallel_portfolio_scenarios() -> list[PerformanceDataFrame]:
94 """Fetch all known parallel portfolio scenarios."""
95 parallel_portfolio_path = Settings.DEFAULT_parallel_portfolio_output
96 # NOTE: Import here to speedup
97 from sparkle.structures import PerformanceDataFrame
99 return [PerformanceDataFrame(f) for f in parallel_portfolio_path.glob("*/*.csv")]
102reference_list_dir = Settings.DEFAULT_reference_dir
103extractor_nickname_list_path = reference_list_dir / "sparkle_extractor_nickname_list.txt"
104solver_nickname_list_path = reference_list_dir / "sparkle_solver_nickname_list.txt"
105instances_nickname_path = reference_list_dir / "sparkle_instance_nickname_list.txt"
107file_storage_data_mapping = {
108 solver_nickname_list_path: {},
109 instances_nickname_path: {},
110 extractor_nickname_list_path: {},
111}
113for data_path in file_storage_data_mapping.keys():
114 if data_path.exists():
115 with data_path.open("r+") as fo:
116 file_storage_data_mapping[data_path] = ast.literal_eval(fo.read())
118solver_nickname_mapping = file_storage_data_mapping[solver_nickname_list_path]
119instance_set_nickname_mapping = file_storage_data_mapping[instances_nickname_path]
120extractor_nickname_mapping = file_storage_data_mapping[extractor_nickname_list_path]