diff --git a/glotaran/project/result.py b/glotaran/project/result.py index fda9033e8..20f5564a4 100644 --- a/glotaran/project/result.py +++ b/glotaran/project/result.py @@ -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" @@ -54,13 +63,13 @@ 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) @@ -68,11 +77,15 @@ def save(self, path: Path, options: SavingOptions = SAVING_OPTIONS_DEFAULT): 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)