Skip to content

Commit

Permalink
Refactor setting log and tracker defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar committed Dec 13, 2024
1 parent 452a64b commit 81f8b9d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
16 changes: 4 additions & 12 deletions janus_core/calculations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from janus_core.helpers.log import config_logger, config_tracker
from janus_core.helpers.struct_io import input_structs
from janus_core.helpers.utils import FileNameMixin, none_to_dict
from janus_core.helpers.utils import FileNameMixin, none_to_dict, set_log_tracker


class BaseCalculation(FileNameMixin):
Expand Down Expand Up @@ -155,17 +155,9 @@ def __init__(
if not self.model_path and "model_path" in self.calc_kwargs:
raise ValueError("`model_path` must be passed explicitly")

if "filename" in log_kwargs:
attach_logger = True
else:
attach_logger = attach_logger if attach_logger else False

if not attach_logger:
if track_carbon:
raise ValueError("Carbon tracking requires logging to be enabled")
self.track_carbon = False
else:
self.track_carbon = track_carbon if track_carbon is not None else True
attach_logger, self.track_carbon = set_log_tracker(
attach_logger, log_kwargs, track_carbon
)

# Read structures and/or attach calculators
# Note: logger not set up so yet so not passed here
Expand Down
35 changes: 35 additions & 0 deletions janus_core/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,38 @@ def selector_len(slc: SliceLike | list, selectable_length: int) -> int:
if stop is None:
stop = selectable_length
return len(range(start, stop, step))


def set_log_tracker(
attach_logger: bool, log_kwargs: dict, track_carbon: bool
) -> tuple[bool, bool]:
"""
Set attach_logger and track_carbon default values.
Parameters
----------
attach_logger : bool
Whether to attach a logger.
log_kwargs : dict[str, Any]
Keyword arguments to pass to `config_logger`.
track_carbon : bool
Whether to track carbon emissions of calculation.
Returns
-------
tuple[bool, bool]
Default values for attach_logger and track_carbon.
"""
if "filename" in log_kwargs:
attach_logger = True
else:
attach_logger = attach_logger if attach_logger else False

if not attach_logger:
if track_carbon:
raise ValueError("Carbon tracking requires logging to be enabled")
track_carbon = False
else:
track_carbon = track_carbon if track_carbon is not None else True

return attach_logger, track_carbon
16 changes: 4 additions & 12 deletions janus_core/training/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from janus_core.helpers.janus_types import PathLike
from janus_core.helpers.log import config_logger, config_tracker
from janus_core.helpers.utils import check_files_exist, none_to_dict
from janus_core.helpers.utils import check_files_exist, none_to_dict, set_log_tracker


def preprocess(
Expand Down Expand Up @@ -53,17 +53,9 @@ def preprocess(
options = yaml.safe_load(file)
check_files_exist(options, req_file_keys)

if "filename" in log_kwargs:
attach_logger = True
else:
attach_logger = attach_logger if attach_logger else False

if not attach_logger:
if track_carbon:
raise ValueError("Carbon tracking requires logging to be enabled")
track_carbon = False
else:
track_carbon = track_carbon if track_carbon is not None else True
attach_logger, track_carbon = set_log_tracker(
attach_logger, log_kwargs, track_carbon
)

# Configure logging
if attach_logger:
Expand Down
16 changes: 4 additions & 12 deletions janus_core/training/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from janus_core.helpers.janus_types import PathLike
from janus_core.helpers.log import config_logger, config_tracker
from janus_core.helpers.utils import check_files_exist, none_to_dict
from janus_core.helpers.utils import check_files_exist, none_to_dict, set_log_tracker


def train(
Expand Down Expand Up @@ -58,17 +58,9 @@ def train(
options = yaml.safe_load(file)
check_files_exist(options, req_file_keys)

if "filename" in log_kwargs:
attach_logger = True
else:
attach_logger = attach_logger if attach_logger else False

if not attach_logger:
if track_carbon:
raise ValueError("Carbon tracking requires logging to be enabled")
track_carbon = False
else:
track_carbon = track_carbon if track_carbon is not None else True
attach_logger, track_carbon = set_log_tracker(
attach_logger, log_kwargs, track_carbon
)

# Configure logging
if attach_logger:
Expand Down

0 comments on commit 81f8b9d

Please sign in to comment.