Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tensorboard/plugins/hparams/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ py_test(
],
)

py_test(
name = "metrics_test",
size = "small",
srcs = [
"metrics_test.py",
],
deps = [
":hparams_plugin",
"//tensorboard:test",
],
)

py_test(
name = "list_metric_evals_test",
size = "small",
Expand Down
9 changes: 5 additions & 4 deletions tensorboard/plugins/hparams/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def run_tag_from_session_and_metric(session_name, metric_name):
"""
assert isinstance(session_name, str)
assert isinstance(metric_name, api_pb2.MetricName)
# os.path.join() will append a final slash if the group is empty; it seems
# like multiplexer.Tensors won't recognize paths that end with a '/' so
# we remove the final '/' in that case.
# os.path.join() will append a final separator if the group is empty; it
# seems like multiplexer.Tensors won't recognize paths that end with one,
# so we remove it in that case. The separator is '\' on Windows, and a
# session name read from a cloud path can still end with a '/' there.
run = os.path.join(session_name, metric_name.group)
if run.endswith("/"):
if run.endswith(("/", os.sep)):
run = run[:-1]
tag = metric_name.tag
return run, tag
80 changes: 80 additions & 0 deletions tensorboard/plugins/hparams/metrics_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2026 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for the hparams plugin's `metrics` module."""

import ntpath
import posixpath
from unittest import mock

from tensorboard import test as tb_test
from tensorboard.plugins.hparams import api_pb2
from tensorboard.plugins.hparams import metrics


def _metric(group, tag):
return api_pb2.MetricName(group=group, tag=tag)


class RunTagFromSessionAndMetricTest(tb_test.TestCase):
def _run_tag(self, path_module, session_name, group, tag):
# Run names are built with the separator of the platform the logdir is
# read on, so a Windows run is "Model001\train" and a POSIX run is
# "Model001/train". Patching `os` lets both be checked on one machine.
with mock.patch.object(
metrics.os, "path", path_module
), mock.patch.object(metrics.os, "sep", path_module.sep):
return metrics.run_tag_from_session_and_metric(
session_name, _metric(group, tag)
)

def test_empty_group_posix(self):
self.assertEqual(
("Model001", "loss"),
self._run_tag(posixpath, "Model001", "", "loss"),
)

def test_empty_group_windows(self):
self.assertEqual(
("Model001", "loss"),
self._run_tag(ntpath, "Model001", "", "loss"),
)

def test_empty_group_windows_nested_session(self):
self.assertEqual(
("logs\\Model001", "loss"),
self._run_tag(ntpath, "logs\\Model001", "", "loss"),
)

def test_empty_group_session_with_trailing_forward_slash(self):
self.assertEqual(
("gs://bucket/Model001", "loss"),
self._run_tag(ntpath, "gs://bucket/Model001/", "", "loss"),
)

def test_group_is_joined_posix(self):
self.assertEqual(
("Model001/train", "loss"),
self._run_tag(posixpath, "Model001", "train", "loss"),
)

def test_group_is_joined_windows(self):
self.assertEqual(
("Model001\\train", "loss"),
self._run_tag(ntpath, "Model001", "train", "loss"),
)


if __name__ == "__main__":
tb_test.main()
Loading