From de1b3f90632c7081c4ccb2511b1b62ff3acf1a66 Mon Sep 17 00:00:00 2001 From: LukasFehring <72503857+LukasFehring@users.noreply.github.com> Date: Tue, 20 Feb 2024 17:02:52 +0100 Subject: [PATCH] Automatically delete temporary CodeCarbon files (#170) --- CHANGELOG.rst | 4 ++++ py_experimenter/experimenter.py | 34 +++++++++++++++++++++++++-------- pyproject.toml | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b7c2964d..b9c278b1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,10 @@ Feature - Change the supported credentials file type to YAML. - Add support for ssh jump hosts in the database connection. +Fix +--- +- Temporary CodeCarbon files are automatically removed at termination. + v1.3.2 (23.01.2024) =================== diff --git a/py_experimenter/experimenter.py b/py_experimenter/experimenter.py index 9fa7209d..cc7834b7 100644 --- a/py_experimenter/experimenter.py +++ b/py_experimenter/experimenter.py @@ -103,14 +103,6 @@ def __init__( if not self.config.valid(): raise InvalidConfigError("Invalid configuration") - if self.use_codecarbon: - if "offline_mode" in self.config.codecarbon_configuration.config: - self.codecarbon_offline_mode = self.config.codecarbon_configuration.config["offline_mode"] - else: - self.codecarbon_offline_mode = False - self.config.codecarbon_configuration.config["offline_mode"] = False - utils.write_codecarbon_config(self.config.codecarbon_configuration.config) - self.database_credential_file_path = database_credential_file_path self.use_ssh_tunnel = use_ssh_tunnel @@ -278,6 +270,8 @@ def execute( if n_jobs is None: n_jobs = self.config.n_jobs + self._write_codecarbon_config() + with Parallel(n_jobs=n_jobs) as parallel: if max_experiments == -1: parallel(delayed(self._worker)(experiment_function, random_order) for _ in range(n_jobs)) @@ -285,6 +279,8 @@ def execute( parallel(delayed(self._execution_wrapper)(experiment_function, random_order) for _ in range(max_experiments)) self.logger.info("All configured executions finished.") + self._delete_codecarbon_config() + def unpause_experiment(self, experiment_id: int, experiment_function: Callable) -> None: """ Pulls the experiment with the given `experiment_id` from the database (if it is `paused`) table and executes it. In @@ -391,6 +387,28 @@ def _execute_experiment(self, experiment_id, keyfield_values, experiment_functio emission_data = tracker._prepare_emissions_data().values result_processor._write_emissions(emission_data, self.codecarbon_offline_mode) + def _write_codecarbon_config(self) -> None: + """ " + Writes the CodeCarbon config file if CodeCarbon is used in this experiment. + """ + if self.use_codecarbon: + if "offline_mode" in self.config.codecarbon_configuration.config: + self.codecarbon_offline_mode = self.config.codecarbon_configuration.config["offline_mode"] + else: + self.codecarbon_offline_mode = False + self.config.codecarbon_configuration.config["offline_mode"] = False + utils.write_codecarbon_config(self.config.codecarbon_configuration.config) + + def _delete_codecarbon_config(self) -> None: + """ + Deletes the CodeCarbon config file if CodeCarbon is used in this experiment. + """ + if self.use_codecarbon: + try: + os.remove(".codecarbon.config") + except FileNotFoundError as e: + self.logger.error(f"Could not delete CodeCarbon config file. Error: {e}") + def reset_experiments(self, *states: Tuple["str"]) -> None: """ Deletes the experiments from the database table that have the given `states`. Afterward, all deleted rows are added to the diff --git a/pyproject.toml b/pyproject.toml index dade9ad2..63590118 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "py-experimenter" -version = "1.4.0a1" +version = "1.4.0a2" description = "The PyExperimenter is a tool for the automatic execution of experiments, e.g. for machine learning (ML), capturing corresponding results in a unified manner in a database." authors = [ "Tanja Tornede ",