Skip to content

Commit

Permalink
Automatically delete temporary CodeCarbon files (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasFehring authored Feb 20, 2024
1 parent 8eca12b commit de1b3f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
===================
Expand Down
34 changes: 26 additions & 8 deletions py_experimenter/experimenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -278,13 +270,17 @@ 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))
else:
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down

0 comments on commit de1b3f9

Please sign in to comment.