Simulation name now also being saved as metadata
This commit is contained in:
@@ -7,7 +7,7 @@ import typing
|
||||
from tqdm import tqdm
|
||||
import signal
|
||||
import pickle
|
||||
import os.path
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from utility import noise, misc
|
||||
@@ -249,14 +249,14 @@ class SimulationDeSerializer:
|
||||
Simulator objects."""
|
||||
|
||||
def __init__(self, save_dir: str, results_dir: str):
|
||||
self._save_dir = save_dir
|
||||
self._saves_dir = save_dir
|
||||
self._results_dir = results_dir
|
||||
|
||||
Path(self._save_dir).mkdir(parents=True, exist_ok=True)
|
||||
Path(self._saves_dir).mkdir(parents=True, exist_ok=True)
|
||||
Path(self._results_dir).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _get_savefile_path(self, sim_name):
|
||||
return f"{self._save_dir}/{misc.slugify(sim_name)}_sim_state.pickle"
|
||||
return f"{self._saves_dir}/{misc.slugify(sim_name)}_state.pickle"
|
||||
|
||||
def _get_metadata_path(self, sim_name):
|
||||
return f"{self._results_dir}/{misc.slugify(sim_name)}_metadata.json"
|
||||
@@ -264,16 +264,41 @@ class SimulationDeSerializer:
|
||||
def _get_results_path(self, sim_name):
|
||||
return f"{self._results_dir}/{misc.slugify(sim_name)}.csv"
|
||||
|
||||
# TODO: Should this function also check for the metadata file?
|
||||
# Currently, if the state-savefile is present, but not the metadata,
|
||||
# this function will return true but loading the saved state will fail
|
||||
def unfinished_sim_present(self, sim_name: str):
|
||||
"""Check if the savefile of a previously paused simulation is present.
|
||||
def _read_metadata(self, sim_name) -> typing.Dict:
|
||||
with open(self._get_metadata_path(sim_name), 'r',
|
||||
encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
:param sim_name:
|
||||
def _save_metadata(self, sim_name, metadata) -> None:
|
||||
with open(self._get_metadata_path(sim_name), 'w+',
|
||||
encoding='utf-8') as f:
|
||||
json.dump(metadata, f, ensure_ascii=False, indent=4)
|
||||
|
||||
def unfinished_sim_present(self, sim_name: str):
|
||||
"""Check if the savefile of a previously paused simulation is
|
||||
present.
|
||||
|
||||
:param sim_name: Name
|
||||
:return: True if a paused simulation with the given name is found
|
||||
"""
|
||||
return os.path.isfile(self._get_savefile_path(sim_name))
|
||||
return os.path.isfile(
|
||||
self._get_savefile_path(sim_name)) and os.path.isfile(
|
||||
self._get_metadata_path(sim_name))
|
||||
|
||||
# TODO: Make the directories configurable in the init function
|
||||
def get_unfinished_sims(self) -> typing.List[str]:
|
||||
"""Get a list unfinished simulations."""
|
||||
result = []
|
||||
|
||||
save_files = [f for f in os.listdir(self._saves_dir) if
|
||||
os.path.isfile(os.path.join(self._saves_dir, f))]
|
||||
|
||||
state_files = [f for f in save_files if f.endswith("_state.pickle")]
|
||||
sim_slugs = [f.removesuffix("_state.pickle") for f in state_files]
|
||||
|
||||
sim_names = [self._read_metadata(slug)["name"] for slug in sim_slugs]
|
||||
|
||||
return sim_names
|
||||
|
||||
def remove_unfinished_sim(self, sim_name):
|
||||
"""Remove the savefile of a previously paused simulation.
|
||||
@@ -292,9 +317,7 @@ class SimulationDeSerializer:
|
||||
:param metadata: Metadata to be saved besides the actual state
|
||||
"""
|
||||
# Save metadata
|
||||
with open(self._get_metadata_path(sim_name), 'w+',
|
||||
encoding='utf-8') as f:
|
||||
json.dump(metadata, f, ensure_ascii=False, indent=4)
|
||||
self._save_metadata(sim_name, metadata)
|
||||
|
||||
# Save simulation state
|
||||
with open(self._get_savefile_path(sim_name), "wb") as file:
|
||||
@@ -311,9 +334,7 @@ class SimulationDeSerializer:
|
||||
simulator = None
|
||||
|
||||
# Read metadata
|
||||
with open(self._get_metadata_path(sim_name), 'r',
|
||||
encoding='utf-8') as f:
|
||||
metadata = json.load(f)
|
||||
metadata = self._read_metadata(sim_name)
|
||||
|
||||
# Read simulation state
|
||||
with open(self._get_savefile_path(sim_name), "rb") as file:
|
||||
@@ -332,9 +353,7 @@ class SimulationDeSerializer:
|
||||
results
|
||||
"""
|
||||
# Save metadata
|
||||
with open(self._get_metadata_path(sim_name), 'w+',
|
||||
encoding='utf-8') as f:
|
||||
json.dump(metadata, f, ensure_ascii=False, indent=4)
|
||||
self._save_metadata(sim_name, metadata)
|
||||
|
||||
# Save results
|
||||
df = simulator.get_current_results()
|
||||
@@ -349,9 +368,7 @@ class SimulationDeSerializer:
|
||||
dataframe and metadata is a dict
|
||||
"""
|
||||
# Read metadata
|
||||
with open(self._get_metadata_path(sim_name), 'r',
|
||||
encoding='utf-8') as f:
|
||||
metadata = json.load(f)
|
||||
metadata = self._read_metadata(sim_name)
|
||||
|
||||
# Read results
|
||||
results = pd.read_csv(self._get_results_path(sim_name))
|
||||
@@ -369,15 +386,15 @@ class SimulationManager:
|
||||
All actual work is outsourced to a provided simulator class.
|
||||
"""
|
||||
|
||||
def __init__(self, save_dir: str, results_dir: str):
|
||||
def __init__(self, saves_dir: str, results_dir: str):
|
||||
"""Construct a SimulationManager object.
|
||||
|
||||
:param save_dir: Directory in which the simulation state of a paused
|
||||
:param saves_dir: Directory in which the simulation state of a paused
|
||||
simulation should be stored
|
||||
:param results_dir: Directory in which the results of the simulation
|
||||
should be stored
|
||||
"""
|
||||
self._de_serializer = SimulationDeSerializer(save_dir, results_dir)
|
||||
self._de_serializer = SimulationDeSerializer(saves_dir, results_dir)
|
||||
|
||||
self._simulator = None
|
||||
self._sim_name = None
|
||||
@@ -398,19 +415,19 @@ class SimulationManager:
|
||||
"""Configure a new simulation."""
|
||||
self._simulator = simulator
|
||||
self._sim_name = name
|
||||
self._metadata["name"] = name
|
||||
self._metadata["labels"] = column_labels
|
||||
|
||||
def unfinished_simulation_present(self, sim_name: str) -> bool:
|
||||
"""Check whether the savefile of a previously unfinished simulation
|
||||
is present."""
|
||||
return self._de_serializer.unfinished_sim_present(sim_name)
|
||||
def get_unfinished(self) -> typing.List[str]:
|
||||
"""Get a list of names of all present unfinished simulations."""
|
||||
return self._de_serializer.get_unfinished_sims()
|
||||
|
||||
def load_unfinished(self, sim_name: str) -> None:
|
||||
"""Load the state of an unfinished simulation its savefile.
|
||||
|
||||
Warning: This function deletes the savefile after loading.
|
||||
"""
|
||||
assert self.unfinished_simulation_present(sim_name)
|
||||
assert self._de_serializer.unfinished_sim_present(sim_name)
|
||||
|
||||
self._sim_name = sim_name
|
||||
self._simulator, self._metadata = self._de_serializer.read_state(
|
||||
|
||||
Reference in New Issue
Block a user