Skip to content

Commit 2a858bd

Browse files
committed
fix lint
1 parent ef84f04 commit 2a858bd

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

neps/plot/plotting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ def _interpolate_time(
139139

140140
if x_range is not None:
141141
min_b, max_b = x_range
142-
new_entry = {c: np.nan for c in df.columns}
142+
new_entry = dict.fromkeys(df.columns, np.nan)
143143
_df = pd.DataFrame.from_dict(new_entry, orient="index").T
144144
_df.index = [min_b]
145145
df = pd.concat((df, _df)).sort_index()
146-
new_entry = {c: np.nan for c in df.columns}
146+
new_entry = dict.fromkeys(df.columns, np.nan)
147147
_df = pd.DataFrame.from_dict(new_entry, orient="index").T
148148
_df.index = [max_b]
149149
df = pd.concat((df, _df)).sort_index()
@@ -157,7 +157,7 @@ def _interpolate_time(
157157

158158
def _df_to_x_range(df: pd.DataFrame, x_range: tuple | None = None) -> pd.DataFrame:
159159
x_max = np.inf if x_range is None else int(x_range[-1])
160-
new_entry = {c: np.nan for c in df.columns}
160+
new_entry = dict.fromkeys(df.columns, np.nan)
161161
_df = pd.DataFrame.from_dict(new_entry, orient="index").T
162162
_df.index = [x_max]
163163
df = pd.concat((df, _df)).sort_index()

neps/plot/tensorboard_eval.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
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

33
from __future__ import annotations
44

55
import logging
66
import time
7-
from collections.abc import Mapping
87
from pathlib import Path
9-
from typing import TYPE_CHECKING, Any, ClassVar
8+
from typing import TYPE_CHECKING, ClassVar
109

1110
from torch.utils.tensorboard.writer import SummaryWriter
1211

1312
from 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
)
1918
from neps.status.status import status
2019
from 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
)

neps/space/domain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def __post_init__(self) -> None:
135135

136136
mid = self.from_unit(torch.tensor(0.5)).item()
137137
if is_int:
138-
mid = int(round(mid))
138+
mid = round(mid)
139139

140140
object.__setattr__(self, "bounds", (self.lower, self.upper))
141141

@@ -193,8 +193,8 @@ def integer(
193193
A domain for a range of integer values.
194194
"""
195195
return Domain(
196-
lower=int(round(lower)),
197-
upper=int(round(upper)),
196+
lower=round(lower),
197+
upper=round(upper),
198198
log_bounds=(math.log(lower), math.log(upper)) if log else None,
199199
round=True,
200200
bins=bins,

neps_examples/convenience/async_evaluation/run_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
batch_size=1024,
4343
)
4444

45-
writer = tblogger.config_writer(write_summary_incumbent=True,
45+
writer = tblogger.ConfigWriter(write_summary_incumbent=True,
4646
root_directory=args.root_directory,
4747
pipeline_directory=args.pipeline_directory,
4848
previous_pipeline_directory=args.previous_pipeline_directory)

neps_examples/convenience/neps_tblogger_tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def evaluate_pipeline(lr, optim, weight_decay):
253253

254254
# Substitute the Tensorboard SummaryWriter with ConfigWriter from NePS
255255
# writer = SummaryWriter()
256-
writer = tblogger.config_writer(write_summary_incumbent=True)
256+
writer = tblogger.ConfigWriter(write_summary_incumbent=True)
257257

258258
for i in range(max_epochs):
259259
objective_to_minimize = training(

0 commit comments

Comments
 (0)