Skip to content

Commit

Permalink
Add allow_overwrite to Result.save.
Browse files Browse the repository at this point in the history
  • Loading branch information
joernweissenborn authored and jsnel committed Oct 14, 2023
1 parent c58222d commit 6b1b751
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions glotaran/project/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ class Config:
parameters_intitial: Parameters
parameters_optimized: Parameters

def save(self, path: Path, options: SavingOptions = SAVING_OPTIONS_DEFAULT):
def save(
self,
path: Path,
options: SavingOptions = SAVING_OPTIONS_DEFAULT,
allow_overwrite: bool = False,
):
if path.exists() and path.is_file():
raise GlotaranUserError("Save folder must be a path.")
path.mkdir()
raise GlotaranUserError("Save path must be a folder.")
if path.exists() and not allow_overwrite:
raise GlotaranUserError(
"Save path already exists. Use allow_overwrite=True to overwrite."
)
result_dict: dict[str, Any] = {"data": {}, "experiments": {}}
path.mkdir(exist_ok=True, parents=True)

# TODO: Save scheme or experiments
# experiment_folder = path / "experiments"
Expand All @@ -54,25 +63,29 @@ def save(self, path: Path, options: SavingOptions = SAVING_OPTIONS_DEFAULT):
# write_dict(experiment.dict(), experiment_path)

data_path = path / "data"
data_path.mkdir()
data_path.mkdir(exist_ok=True)
for label, data in self.data.items():
dataset_path = data_path / f"{label}.{options.data_format}"
result_dict["data"][label] = str(dataset_path)
if options.data_filter is not None:
data = data[options.data_filter]
save_dataset(data, dataset_path)
save_dataset(data, dataset_path, allow_overwrite=allow_overwrite)

optimization_history_path = path / "optimization_history.csv"
result_dict["optimization_history"] = str(optimization_history_path)
self.optimization.optimization_history.to_csv(optimization_history_path)

parameters_initial_path = path / f"parameters_initial.{options.parameter_format}"
result_dict["parameters_initial"] = str(parameters_initial_path)
save_parameters(self.parameters_intitial, parameters_initial_path)
save_parameters(
self.parameters_intitial, parameters_initial_path, allow_overwrite=allow_overwrite
)

parameters_optimized_path = path / f"parameters_optimized.{options.parameter_format}"
result_dict["parameters_optimized"] = str(parameters_optimized_path)
save_parameters(self.parameters_optimized, parameters_optimized_path)
save_parameters(
self.parameters_optimized, parameters_optimized_path, allow_overwrite=allow_overwrite
)

result_path = path / "glotaran_result.yml"
write_dict(result_dict, result_path)

0 comments on commit 6b1b751

Please sign in to comment.