diff --git a/climanet/utils.py b/climanet/utils.py index c60bc5b..3501f48 100644 --- a/climanet/utils.py +++ b/climanet/utils.py @@ -4,6 +4,7 @@ import numpy as np import xarray as xr import torch +import time from torch.utils.tensorboard import SummaryWriter @@ -222,7 +223,8 @@ def set_seed(seed: int = 42): def setup_logging(log_dir: str) -> SummaryWriter: """Set up TensorBoard logging directory and writer.""" Path(log_dir).mkdir(parents=True, exist_ok=True) - return SummaryWriter(log_dir) + timestamp_utc = time.strftime("%Y%m%dT%H%M%S", time.gmtime()) + return SummaryWriter(log_dir, filename_suffix=f'_UTC{timestamp_utc}') def compute_masked_loss( diff --git a/pyproject.toml b/pyproject.toml index 9261094..1c4aae6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ dependencies = [ "matplotlib>=3.10.8", "netcdf4>=1.7.4", "scipy>=1.17.1", + "tbparse>=0.0.9", "tensorboard", "torch>=2.10.0", "xarray>=2025.12.0", diff --git a/tests/test_utils.py b/tests/test_utils.py index f4f5361..66bcb93 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,2 +1,19 @@ -def test_dummy(): - assert True +from climanet.utils import setup_logging +from tbparse import SummaryReader + + +def test_setup_logging(tmp_path): + writer = setup_logging(tmp_path) + log_text = "This is a test log entry." + writer.add_scalar("Test Scalar", 42) + writer.add_text("Test Text", log_text) + writer.close() + + # Test that there is one event file + # The file should have "UTC" keyword in timestamp suffix + assert len(list(tmp_path.glob("events*UTC*"))) == 1 + + # Load the events file with SummaryReader + reader = SummaryReader(tmp_path) + assert reader.text["value"].iloc[0] == log_text # check text + assert reader.scalars["value"].iloc[0] == 42 # check scalar