From 1e83a1bd32a29e1c8802d0593dbcd6e6b173a8d3 Mon Sep 17 00:00:00 2001 From: Etay Livne <53942171+EtayLivne@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:15:12 +0300 Subject: [PATCH] Check if CometLogger experiment is alive (#19915) Co-authored-by: Etay Livne --- src/lightning/pytorch/loggers/comet.py | 2 +- tests/tests_pytorch/loggers/test_comet.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/lightning/pytorch/loggers/comet.py b/src/lightning/pytorch/loggers/comet.py index ccb6d62de8..277af5c85f 100644 --- a/src/lightning/pytorch/loggers/comet.py +++ b/src/lightning/pytorch/loggers/comet.py @@ -268,7 +268,7 @@ class CometLogger(Logger): self.logger.experiment.some_comet_function() """ - if self._experiment is not None: + if self._experiment is not None and self._experiment.alive: return self._experiment if self._future_experiment_key is not None: diff --git a/tests/tests_pytorch/loggers/test_comet.py b/tests/tests_pytorch/loggers/test_comet.py index 791089c47c..e467c63543 100644 --- a/tests/tests_pytorch/loggers/test_comet.py +++ b/tests/tests_pytorch/loggers/test_comet.py @@ -66,6 +66,20 @@ def test_comet_logger_online(comet_mock): api.assert_called_once_with("rest") +@mock.patch.dict(os.environ, {}) +def test_comet_experiment_resets_if_not_alive(comet_mock): + """Test that the CometLogger creates a new experiment if the old one is not alive anymore.""" + logger = CometLogger() + assert logger._experiment is None + alive_experiment = Mock(alive=True) + logger._experiment = alive_experiment + assert logger.experiment is alive_experiment + + unalive_experiment = Mock(alive=False) + logger._experiment = unalive_experiment + assert logger.experiment is not unalive_experiment + + @mock.patch.dict(os.environ, {}) def test_comet_logger_no_api_key_given(comet_mock): """Test that CometLogger fails to initialize if both api key and save_dir are missing."""