Source code for PlotConfig
1"""Configuration reader for the live plotting system."""
2
3from configparser import ConfigParser
4from pathlib import Path
5
6
[docs]
7class PlotConfig:
8 """Reads experiment config from an INI file for use by the plotting scripts."""
9
[docs]
10 def __init__(self, filename: str):
11 """Load config from the given INI file path."""
12 self.__config_parser = ConfigParser()
13 self.__config_parser.read(filename)
14
[docs]
15 def get_population_size(self) -> int:
16 """Return the population size."""
17 return self.__config_parser.getint('BASE', 'population_size')
18
[docs]
19 def is_pulse_count(self) -> bool:
20 """Return whether pulse-count fitness is used."""
21 return self.__config_parser.getboolean('BASE', 'is_pulse_count')
22
[docs]
23 def is_tone_discriminator(self) -> bool:
24 """Return whether tone-discriminator mode is enabled."""
25 return self.__config_parser.getboolean('BASE', 'is_tone_discriminator')
26
[docs]
27 def get_desired_frequency(self) -> int:
28 """Return the target frequency in Hz."""
29 return self.__config_parser.getint('BASE', 'desired_frequency')
30
[docs]
31 def using_transfer_interval(self) -> bool:
32 """Return whether a positive transfer interval is configured."""
33 return self.get_transfer_interval() > 0
34
[docs]
35 def get_transfer_interval(self) -> int:
36 """Return the migration transfer interval in generations."""
37 return self.__config_parser.getint('BASE', 'transfer_interval')
38
[docs]
39 def get_diversity_measure(self) -> str:
40 """Return the name of the diversity measure."""
41 return self.__config_parser.get('BASE', 'diversity_measure')
42
[docs]
43 def get_save_plots(self) -> bool:
44 """Return whether plots should be saved to disk."""
45 return self.__config_parser.getboolean('BASE', 'save_plots')
46
[docs]
47 def get_plots_directory(self) -> Path:
48 """Return the directory path for saved plots."""
49 return Path(self.__config_parser.get('BASE', 'plots_dir'))
50
[docs]
51 def uses_init_existing_population(self) -> bool:
52 """Return whether the run starts from an existing population."""
53 return self.__config_parser.getboolean('BASE', 'existing_population')
54
[docs]
55def save_plot_config(file_path: Path, *, pop_sz: int, is_pulse_count: bool, is_td: bool, desired_freq: int,
56 transfer_interval: int, diversity_measure: str, save_plots: bool,
57 plots_dir: str, uses_init_existing_population: bool) -> None:
58 """Write experiment configuration to an INI file at *file_path*."""
59 with open(file_path, 'w') as f:
60 f.write('[BASE]\n')
61 f.write(f'population_size={pop_sz}\n')
62 f.write(f'is_pulse_count={is_pulse_count}\n')
63 f.write(f'is_tone_discriminator={is_td}\n')
64 f.write(f'desired_frequency={desired_freq}\n')
65 f.write(f'transfer_interval={transfer_interval}\n')
66 f.write(f'diversity_measure={diversity_measure}\n')
67 f.write(f'save_plots={save_plots}\n')
68 f.write(f'plots_dir={plots_dir}\n')
69 f.write(f'existing_population={uses_init_existing_population}\n')