From 87f883f2a6ad2b3895eb3adee59ccfcbd0f9e95c Mon Sep 17 00:00:00 2001 From: mspils <111068086+mspils@users.noreply.github.com> Date: Tue, 30 Aug 2022 13:15:28 +0200 Subject: [PATCH 1/6] Added support for pathlib paths as a logdir using the recommended os.PathLike class. --- tensorboard/plugins/hparams/_keras.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorboard/plugins/hparams/_keras.py b/tensorboard/plugins/hparams/_keras.py index dc57c09301..e51dae3108 100644 --- a/tensorboard/plugins/hparams/_keras.py +++ b/tensorboard/plugins/hparams/_keras.py @@ -17,7 +17,7 @@ Use `tensorboard.plugins.hparams.api` to access this module's contents. """ - +import os import tensorflow as tf from tensorboard.plugins.hparams import api_pb2 @@ -39,7 +39,7 @@ def __init__(self, writer, hparams, trial_id=None): Args: writer: The `SummaryWriter` object to which hparams should be - written, or a logdir (as a `str`) to be passed to + written, or a logdir (as a `str` or `PathLike`) to be passed to `tf.summary.create_file_writer` to create such a writer. hparams: A `dict` mapping hyperparameters to the values used in this session. Keys should be the names of `HParam` objects used @@ -62,10 +62,12 @@ def __init__(self, writer, hparams, trial_id=None): summary_v2.hparams_pb(self._hparams, trial_id=self._trial_id) if writer is None: raise TypeError( - "writer must be a `SummaryWriter` or `str`, not None" + "writer must be a `SummaryWriter`, `str` or `PathLike`, not None" ) elif isinstance(writer, str): self._writer = tf.compat.v2.summary.create_file_writer(writer) + elif isinstance(writer, os.PathLike): + self._writer = tf.compat.v2.summary.create_file_writer(os.fsdecode(writer)) else: self._writer = writer From 814037e4df319f25f150e2a04ba2ae39fc5f8da7 Mon Sep 17 00:00:00 2001 From: mspils <111068086+mspils@users.noreply.github.com> Date: Thu, 1 Sep 2022 13:52:46 +0200 Subject: [PATCH 2/6] added test for pathlib.Path --- tensorboard/plugins/hparams/_keras_test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tensorboard/plugins/hparams/_keras_test.py b/tensorboard/plugins/hparams/_keras_test.py index 570bd15acf..86d7e9d952 100644 --- a/tensorboard/plugins/hparams/_keras_test.py +++ b/tensorboard/plugins/hparams/_keras_test.py @@ -15,6 +15,7 @@ import os +from pathlib import Path from unittest import mock from google.protobuf import text_format @@ -145,6 +146,15 @@ def test_explicit_writer(self): # We'll assume that the contents are correct, as in the case where # the file writer was constructed implicitly. + def test_pathlib_writer(self): + + writer = Path(self.logdir) + self._initialize_model(writer=writer) + self.model.fit(x=[(1,)], y=[(2,)], callbacks=[self.callback]) + + files = os.listdir(self.logdir) + self.assertEqual(len(files), 1, files) + def test_non_eager_failure(self): with tf.compat.v1.Graph().as_default(): assert not tf.executing_eagerly() From 331b6d20e1f2dcb20e3d91ed6b2517e2373e3ca3 Mon Sep 17 00:00:00 2001 From: mspils <111068086+mspils@users.noreply.github.com> Date: Mon, 5 Sep 2022 11:52:39 +0200 Subject: [PATCH 3/6] remove empty line, fsdecode -> fspath --- tensorboard/plugins/hparams/_keras.py | 2 +- tensorboard/plugins/hparams/_keras_test.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorboard/plugins/hparams/_keras.py b/tensorboard/plugins/hparams/_keras.py index e51dae3108..8ae7057567 100644 --- a/tensorboard/plugins/hparams/_keras.py +++ b/tensorboard/plugins/hparams/_keras.py @@ -67,7 +67,7 @@ def __init__(self, writer, hparams, trial_id=None): elif isinstance(writer, str): self._writer = tf.compat.v2.summary.create_file_writer(writer) elif isinstance(writer, os.PathLike): - self._writer = tf.compat.v2.summary.create_file_writer(os.fsdecode(writer)) + self._writer = tf.compat.v2.summary.create_file_writer(os.fspath(writer)) else: self._writer = writer diff --git a/tensorboard/plugins/hparams/_keras_test.py b/tensorboard/plugins/hparams/_keras_test.py index 86d7e9d952..52fd6ef20e 100644 --- a/tensorboard/plugins/hparams/_keras_test.py +++ b/tensorboard/plugins/hparams/_keras_test.py @@ -147,7 +147,6 @@ def test_explicit_writer(self): # the file writer was constructed implicitly. def test_pathlib_writer(self): - writer = Path(self.logdir) self._initialize_model(writer=writer) self.model.fit(x=[(1,)], y=[(2,)], callbacks=[self.callback]) From 9ea743a586c425aca42e7d3750153aec328e28ca Mon Sep 17 00:00:00 2001 From: mspils <111068086+mspils@users.noreply.github.com> Date: Thu, 22 Sep 2022 14:34:57 +0200 Subject: [PATCH 4/6] Update tensorboard/plugins/hparams/_keras.py additional linebreak Co-authored-by: Riley Jones <78179109+rileyajones@users.noreply.github.com> --- tensorboard/plugins/hparams/_keras.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tensorboard/plugins/hparams/_keras.py b/tensorboard/plugins/hparams/_keras.py index 8ae7057567..f2cf897308 100644 --- a/tensorboard/plugins/hparams/_keras.py +++ b/tensorboard/plugins/hparams/_keras.py @@ -67,7 +67,9 @@ def __init__(self, writer, hparams, trial_id=None): elif isinstance(writer, str): self._writer = tf.compat.v2.summary.create_file_writer(writer) elif isinstance(writer, os.PathLike): - self._writer = tf.compat.v2.summary.create_file_writer(os.fspath(writer)) + self._writer = tf.compat.v2.summary.create_file_writer( + os.fspath(writer) + ) else: self._writer = writer From 72ba0bf424e2ab9859491f6632b9f17f170dac44 Mon Sep 17 00:00:00 2001 From: mspils <111068086+mspils@users.noreply.github.com> Date: Thu, 22 Sep 2022 14:35:32 +0200 Subject: [PATCH 5/6] Update tensorboard/plugins/hparams/_keras.py removed trailing whitespace Co-authored-by: Riley Jones <78179109+rileyajones@users.noreply.github.com> --- tensorboard/plugins/hparams/_keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorboard/plugins/hparams/_keras.py b/tensorboard/plugins/hparams/_keras.py index f2cf897308..29a86fd086 100644 --- a/tensorboard/plugins/hparams/_keras.py +++ b/tensorboard/plugins/hparams/_keras.py @@ -17,7 +17,7 @@ Use `tensorboard.plugins.hparams.api` to access this module's contents. """ -import os +import os import tensorflow as tf from tensorboard.plugins.hparams import api_pb2 From c11c94ada20968cab5dfb43e4fb5c9266db92a61 Mon Sep 17 00:00:00 2001 From: mspils <111068086+mspils@users.noreply.github.com> Date: Thu, 22 Sep 2022 14:43:36 +0200 Subject: [PATCH 6/6] Changed test for error message invalid writer --- tensorboard/plugins/hparams/_keras_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorboard/plugins/hparams/_keras_test.py b/tensorboard/plugins/hparams/_keras_test.py index 52fd6ef20e..1cef8ffc80 100644 --- a/tensorboard/plugins/hparams/_keras_test.py +++ b/tensorboard/plugins/hparams/_keras_test.py @@ -174,7 +174,7 @@ def test_reuse_failure(self): def test_invalid_writer(self): with self.assertRaisesRegex( TypeError, - "writer must be a `SummaryWriter` or `str`, not None", + "writer must be a `SummaryWriter`, `str` or `PathLike`, not None", ): _keras.Callback(writer=None, hparams={})