aegis_gui.utilities.manipulate

 1import yaml
 2import logging
 3
 4from aegis_sim.parameterization.default_parameters import DEFAULT_PARAMETERS
 5from aegis_gui.utilities.utilities import get_config_path, get_container, safe_sim_name, safe_sim_path
 6from aegis_sim.utilities.container import Container
 7import subprocess
 8
 9# NOTE: this dict is per-process. On a multi-worker server (gunicorn etc.) only
10# the worker that started a sim will see it here. Termination uses Container.terminate()
11# via ticker files instead, which works across workers.
12running_processes = {}
13
14
15def make_config_file(filename, configs):
16    safe_sim_name(filename)
17    configs["PHENOMAP_SPECS"] = []
18    configs["NOTES"] = []
19    for k, v in configs.items():
20        configs[k] = DEFAULT_PARAMETERS[k].convert(v)
21    logging.info("Making a config file.")
22    config_path = get_config_path(filename)
23    with open(config_path, "w") as file_:
24        yaml.dump(configs, file_)
25
26
27def run_simulation(filename, prerun_sim_path):
28    global running_processes
29    safe_sim_name(filename)
30    config_path = get_config_path(filename)
31    logging.info(f"Running a simulation at path {config_path}.")
32    if prerun_sim_path is None:
33        pickle_command = []
34    else:
35        # Validate the prerun sim is one of ours — never trust a raw path string.
36        prerun_path = safe_sim_path(_extract_sim_name(prerun_sim_path))
37        container = Container(prerun_path)
38        latest_pickle_path = container.get_path(["pickles"])[-1]
39        logging.info(f"Using pickled population from {latest_pickle_path}.")
40        pickle_command = ["-p", str(latest_pickle_path)]
41    process = subprocess.Popen(
42        ["aegis", "sim", "--config_path", str(config_path)] + pickle_command
43    )  # used to use sys.executable for cross-platform 'python3' command
44    running_processes[filename] = process
45
46
47def _extract_sim_name(value):
48    """Pull the bare sim name out of whatever the caller handed us — could be a
49    name, a relative path, or an absolute path. We only ever use the stem."""
50    import pathlib
51
52    return pathlib.Path(str(value)).name
53
54
55def terminate_simulation(simname):
56    safe_sim_name(simname)
57    container = get_container(filename=simname)
58    container.terminate()
running_processes = {}
def make_config_file(filename, configs):
16def make_config_file(filename, configs):
17    safe_sim_name(filename)
18    configs["PHENOMAP_SPECS"] = []
19    configs["NOTES"] = []
20    for k, v in configs.items():
21        configs[k] = DEFAULT_PARAMETERS[k].convert(v)
22    logging.info("Making a config file.")
23    config_path = get_config_path(filename)
24    with open(config_path, "w") as file_:
25        yaml.dump(configs, file_)
def run_simulation(filename, prerun_sim_path):
28def run_simulation(filename, prerun_sim_path):
29    global running_processes
30    safe_sim_name(filename)
31    config_path = get_config_path(filename)
32    logging.info(f"Running a simulation at path {config_path}.")
33    if prerun_sim_path is None:
34        pickle_command = []
35    else:
36        # Validate the prerun sim is one of ours — never trust a raw path string.
37        prerun_path = safe_sim_path(_extract_sim_name(prerun_sim_path))
38        container = Container(prerun_path)
39        latest_pickle_path = container.get_path(["pickles"])[-1]
40        logging.info(f"Using pickled population from {latest_pickle_path}.")
41        pickle_command = ["-p", str(latest_pickle_path)]
42    process = subprocess.Popen(
43        ["aegis", "sim", "--config_path", str(config_path)] + pickle_command
44    )  # used to use sys.executable for cross-platform 'python3' command
45    running_processes[filename] = process
def terminate_simulation(simname):
56def terminate_simulation(simname):
57    safe_sim_name(simname)
58    container = get_container(filename=simname)
59    container.terminate()