Skip to content
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

Merged
merged 9 commits into from
Oct 27, 2020

Conversation

Vozf
Copy link
Contributor

@Vozf Vozf commented Oct 19, 2020

What does this PR do?

Fixes #4229 (issue)

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

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 🙃

@mergify mergify bot requested a review from a team October 19, 2020 10:49
Copy link
Contributor

@Lothiraldan Lothiraldan left a 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.

pytorch_lightning/loggers/comet.py Outdated Show resolved Hide resolved
@Borda Borda added bug Something isn't working logger Related to the Loggers labels Oct 19, 2020
Borda
Borda previously requested changes Oct 19, 2020
Copy link
Member

@Borda Borda left a 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... 🐰

@mergify mergify bot requested a review from a team October 19, 2020 21:03
@edenlightning edenlightning added this to the 1.0.3 milestone Oct 19, 2020
@tchaton
Copy link
Contributor

tchaton commented Oct 20, 2020

Hey @Vozf, Could you add a tests by any chance ?

@Vozf
Copy link
Contributor Author

Vozf commented Oct 20, 2020

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

@Lothiraldan
Copy link
Contributor

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
Copy link

codecov bot commented Oct 23, 2020

Codecov Report

Merging #4230 into master will increase coverage by 0%.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #4230   +/-   ##
======================================
  Coverage      93%     93%           
======================================
  Files         111     111           
  Lines        8130    8102   -28     
======================================
- Hits         7524    7512   -12     
+ Misses        606     590   -16     

@mergify mergify bot requested a review from a team October 24, 2020 02:08
@awaelchli awaelchli requested a review from Borda October 27, 2020 09:16
@SeanNaren SeanNaren merged commit 4106e2f into Lightning-AI:master Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working logger Related to the Loggers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comet logger overrides COMET_EXPERIMENT_KEY env variable
10 participants