1- """The tblogger module provides a simplified interface for logging NePS trials to TensorBoard."""
1+ """The tblogger module provides a simplified interface for logging to TensorBoard."""
22
33from __future__ import annotations
44
55import logging
66import time
7- from collections .abc import Mapping
87from pathlib import Path
9- from typing import TYPE_CHECKING , Any , ClassVar
8+ from typing import TYPE_CHECKING , ClassVar
109
1110from torch .utils .tensorboard .writer import SummaryWriter
1211
1312from neps .runtime import (
1413 get_in_progress_trial ,
1514 get_workers_neps_state ,
16- register_notify_trial_end ,
1715 is_in_progress_trial_set ,
16+ register_notify_trial_end ,
1817)
1918from neps .status .status import status
2019from neps .utils .common import get_initial_directory
@@ -40,9 +39,9 @@ class tblogger: # noqa: N801
4039 @classmethod
4140 def initiate_internal_configurations (
4241 cls ,
43- root_directory : Path | str | None = None ,
44- pipeline_directory : Path | str | None = None ,
45- previous_pipeline_directory : Path | str | None = None ,
42+ root_directory : Path | None = None ,
43+ pipeline_directory : Path | None = None ,
44+ previous_pipeline_directory : Path | None = None ,
4645 ) -> None :
4746 """Initialize internal directories and configuration for TensorBoard logging.
4847
@@ -54,13 +53,12 @@ def initiate_internal_configurations(
5453 pipeline_directory (Path | str | None): Current trial directory.
5554 previous_pipeline_directory (Path | str | None): Previous trial directory.
5655 """
57- if not is_in_progress_trial_set ():
58- if not (root_directory and pipeline_directory ):
59- raise RuntimeError (
60- "Cannot determine directories for TensorBoard logging. "
61- "Provide `root_directory`, `pipeline_directory`, and optionally "
62- "`previous_pipeline_directory`."
63- )
56+ if not is_in_progress_trial_set () and not (root_directory and pipeline_directory ):
57+ raise RuntimeError (
58+ "Cannot determine directories for TensorBoard logging. "
59+ "Provide `root_directory`, `pipeline_directory`, and optionally "
60+ "`previous_pipeline_directory`."
61+ )
6462
6563 if is_in_progress_trial_set ():
6664 trial = get_in_progress_trial ()
@@ -73,18 +71,6 @@ def initiate_internal_configurations(
7371 if trial .metadata .previous_trial_location
7472 else None
7573 )
76- else :
77- # Convert str paths to Path objects
78- root_directory = Path (root_directory ) if isinstance (root_directory , str ) else root_directory
79- pipeline_directory = Path (pipeline_directory ) if isinstance (pipeline_directory , str ) else pipeline_directory
80- previous_pipeline_directory = (
81- Path (previous_pipeline_directory )
82- if isinstance (previous_pipeline_directory , str )
83- else previous_pipeline_directory
84- )
85-
86- if previous_pipeline_directory and not previous_pipeline_directory .exists ():
87- previous_pipeline_directory = None
8874
8975 register_notify_trial_end ("NEPS_TBLOGGER" , cls .end_of_config )
9076
@@ -93,13 +79,13 @@ def initiate_internal_configurations(
9379 cls .optimizer_dir = root_directory
9480
9581 @classmethod
96- def write_incumbent (cls ) -> None :
82+ def WriteIncumbent (cls ) -> None : # noqa: N802
9783 """Enable logging of the incumbent (best) configuration for the current search."""
9884 cls .initiate_internal_configurations ()
9985 cls .write_incumbent = True
10086
10187 @classmethod
102- def config_writer (
88+ def ConfigWriter ( # noqa: N802
10389 cls ,
10490 * ,
10591 write_summary_incumbent : bool = True ,
@@ -120,14 +106,23 @@ def config_writer(
120106 or None if a writer cannot be initialized.
121107 """
122108 cls .write_incumbent = write_summary_incumbent
123- cls .initiate_internal_configurations (root_directory , pipeline_directory , previous_pipeline_directory )
124-
125- if cls .config_previous_directory is None and cls .config_working_directory is not None :
109+ cls .initiate_internal_configurations (
110+ root_directory ,
111+ pipeline_directory ,
112+ previous_pipeline_directory ,
113+ )
114+
115+ if (
116+ cls .config_previous_directory is None
117+ and cls .config_working_directory is not None
118+ ):
126119 cls .config_writer = SummaryWriter (cls .config_working_directory / "tbevents" )
127120 return cls .config_writer
128121
129122 if cls .config_working_directory is not None :
130- init_dir = get_initial_directory (pipeline_directory = cls .config_working_directory )
123+ init_dir = get_initial_directory (
124+ pipeline_directory = cls .config_working_directory ,
125+ )
131126 if (init_dir / "tbevents" ).exists ():
132127 cls .config_writer = SummaryWriter (init_dir / "tbevents" )
133128 return cls .config_writer
@@ -139,7 +134,7 @@ def config_writer(
139134 return None
140135
141136 @classmethod
142- def end_of_config (cls , trial : Trial ) -> None : # noqa: ARG004
137+ def end_of_config (cls , _ : Trial ) -> None :
143138 """Close the TensorBoard writer at the end of a configuration."""
144139 if cls .config_writer :
145140 cls .config_writer .close ()
@@ -176,7 +171,7 @@ def _tracking_incumbent_api(cls) -> None:
176171 cls .summary_writer .close ()
177172 time .sleep (0.5 )
178173
179- except Exception as e :
174+ except Exception as e : # noqa: BLE001
180175 logger .warning (
181176 "Incumbent tracking for TensorBoard failed and is now disabled: %s" , e
182177 )
0 commit comments