-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix COMET_EXPERIMENT_KEY environment variable usage in comet logger #4230
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, we should probably add a test for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we pls add a test to show failing master and this is the fixes... 🐰
Hey @Vozf, Could you add a tests by any chance ? |
Not really. Made this pr based on my personal inherited cometLogger class and I don't even have a setup for PytorchLightning project. I am also not familiar with your testing setup |
I've worked on a test for this PR, it's a bit more complex than needed because the experiment key is not passed as a parameter to the Experiment class, I don't know what is the best way to send it to this PR. Here is the patch if someone with the right permission can push it to this PR: diff --git a/tests/loggers/test_comet.py b/tests/loggers/test_comet.py
index dc66f184..3d13c6df 100644
--- a/tests/loggers/test_comet.py
+++ b/tests/loggers/test_comet.py
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
-from unittest.mock import patch
+from unittest.mock import patch, DEFAULT
import pytest
@@ -25,6 +25,7 @@ from tests.base import EvalModelTemplate
def _patch_comet_atexit(monkeypatch):
""" Prevent comet logger from trying to print at exit, since pytest's stdout/stderr redirection breaks it. """
import atexit
+
monkeypatch.setattr(atexit, "register", lambda _: None)
@@ -99,6 +100,37 @@ def test_comet_logger_experiment_name(comet):
comet_experiment().set_name.assert_called_once_with(experiment_name)
+@patch('pytorch_lightning.loggers.comet.comet_ml')
+def test_comet_logger_manual_experiment_key(comet):
+ """Test that Comet Logger respects manually set COMET_EXPERIMENT_KEY."""
+
+ api_key = "key"
+ experiment_key = "96346da91469407a85641afe5766b554"
+
+ instantation_environ = {}
+
+ def save_os_environ(*args, **kwargs):
+ nonlocal instantation_environ
+ instantation_environ = os.environ.copy()
+
+ return DEFAULT
+
+ # Test api_key given
+ with patch.dict(os.environ, {"COMET_EXPERIMENT_KEY": experiment_key}):
+ with patch('pytorch_lightning.loggers.comet.CometExperiment', side_effect=save_os_environ) as comet_experiment:
+ logger = CometLogger(api_key=api_key)
+
+ assert logger.version == experiment_key
+
+ assert logger._experiment is None
+
+ _ = logger.experiment
+
+ comet_experiment.assert_called_once_with(api_key=api_key, project_name=None)
+
+ assert instantation_environ["COMET_EXPERIMENT_KEY"] == experiment_key
+
+
@patch('pytorch_lightning.loggers.comet.CometOfflineExperiment')
@patch('pytorch_lightning.loggers.comet.comet_ml')
def test_comet_logger_dirs_creation(comet, comet_experiment, tmpdir, monkeypatch): |
Codecov Report
@@ Coverage Diff @@
## master #4230 +/- ##
======================================
Coverage 93% 93%
======================================
Files 111 111
Lines 8130 8102 -28
======================================
- Hits 7524 7512 -12
+ Misses 606 590 -16 |
What does this PR do?
Fixes #4229 (issue)
Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃