Skip to content

Commit

Permalink
Add docstrings, explain example more, fix circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
helegraf committed Jul 30, 2024
1 parent f273b4f commit 7c5946c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
11 changes: 10 additions & 1 deletion examples/6_advanced_features/1_wandb_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
Use Weights and Biases for logging
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This example shows how to use Weights and Biases for logging.
This example shows how to use Weights and Biases (WandB) for logging.
To use WandB, you need to install the package via pip:
.. code-block:: bash
pip install wandb
Then you can use the WandBCallback to log the results of the optimization as well as intermediate information to WandB.
This is done by creating a WandBCallback object and passing it to the used Facade.
"""
from __future__ import annotations
Expand Down
43 changes: 39 additions & 4 deletions smac/callback/wandb_logging.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
from typing import Any

from dataclasses import asdict

import smac
from smac import Callback
from smac.callback import Callback
from smac.runhistory import TrialInfo, TrialValue


class WandBCallback(Callback):
"""
Callback to log the results of the optimization as well as intermediate information to WandB.
Logs TrialInfo, TrialValue and the number of successfully executed trials (as step) to WandB `on_tell_end`.
Upon the end of the run, logs the trajectory of the intensifier to WandB.
Parameters
----------
project : str
The project name of the WandB project.
entity : str
The entity name of the WandB project.
id : str, optional
The id of the run.
outdir : str, optional
The output directory of the WandB run.
mode : str, optional
The mode of the WandB run.
resume : str, optional
The resume mode of the WandB run.
job_type : str, optional
The job type of the WandB run.
group : str, optional
The group of the WandB run.
config : dict or str, optional
The configuration of the WandB run.
save_code : bool, optional
Whether to save the code of the WandB run.
**kwargs : dict
Additional arguments to pass to the WandB run.
"""

def __init__(
self,
project: str,
Expand All @@ -18,7 +53,7 @@ def __init__(
group: str | None = None,
config: dict | str | None = None,
save_code: bool = True,
**kwargs,
**kwargs: dict[str, Any],
) -> None:
import wandb

Expand All @@ -37,7 +72,7 @@ def __init__(
)
super().__init__()

def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialValue) -> bool | None:
def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialValue) -> bool | None: # noqa: D102
info_dict = asdict(info)
info_dict["config"] = info_dict["config"].get_dictionary()
value_dict = asdict(value)
Expand All @@ -46,7 +81,7 @@ def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialVa
self.run.log(data=log_dict)
return super().on_tell_end(smbo, info, value)

def on_end(self, smbo: smac.main.smbo.SMBO) -> None:
def on_end(self, smbo: smac.main.smbo.SMBO) -> None: # noqa: D102
intensifier_data = smbo.intensifier.get_data()
trajectory = intensifier_data["trajectory"]
import pandas as pd
Expand Down

0 comments on commit 7c5946c

Please sign in to comment.