Skip to content

Commit c199b5f

Browse files
committed
Improve logging in Colab
1 parent c201a0e commit c199b5f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

napari_cellseg3d/dev_scripts/colab_training.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Script to run WNet training in Google Colab."""
2+
23
import time
34
from pathlib import Path
45

@@ -55,7 +56,28 @@
5556
)
5657
WANDB_INSTALLED = False
5758

58-
# TODO subclass to reduce code duplication
59+
60+
class LogFixture:
61+
"""Fixture for napari-less logging, replaces napari_cellseg3d.interface.Log in model_workers.
62+
63+
This allows to redirect the output of the workers to stdout instead of a specialized widget.
64+
"""
65+
66+
def __init__(self):
67+
"""Creates a LogFixture object."""
68+
super(LogFixture, self).__init__()
69+
70+
def print_and_log(self, text, printing=None):
71+
"""Prints and logs text."""
72+
print(text)
73+
74+
def warn(self, warning):
75+
"""Logs warning."""
76+
logger.warning(warning)
77+
78+
def error(self, e):
79+
"""Logs error."""
80+
raise (e)
5981

6082

6183
class WNetTrainingWorkerColab(TrainingWorkerBase):
@@ -728,8 +750,14 @@ def get_colab_worker(
728750
worker_config (config.WNetTrainingWorkerConfig): config for the training worker
729751
wandb_config (config.WandBConfig): config for wandb
730752
"""
753+
log = LogFixture()
731754
worker = WNetTrainingWorkerColab(worker_config)
732755
worker.wandb_config = wandb_config
756+
757+
worker.log_signal.connect(log.print_and_log)
758+
worker.warn_signal.connect(log.warn)
759+
worker.error_signal.connect(log.error)
760+
733761
return worker
734762

735763

notebooks/Colab_WNet3D_training.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"id": "Ax-vJAWRwIKi"
9292
},
9393
"source": [
94-
"## (optional) **1.3 Initialize Weights & Biases integration **\n",
94+
"## Optional - *1.3 Initialize Weights & Biases integration*\n",
9595
"---\n",
9696
"If you wish to utilize Weights & Biases (WandB) for monitoring and logging your training session, uncomment and execute the cell below.\n",
9797
"To enable it, just input your API key in the space provided."

0 commit comments

Comments
 (0)