Skip to content

Commit 050a436

Browse files
committed
Enable log_* functions
1 parent eb883a4 commit 050a436

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

pytorch_lightning/loggers/neptune.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@
2020
]
2121

2222
import logging
23-
import operator
2423
import os
24+
import warnings
2525
from argparse import Namespace
2626
from functools import reduce
27-
from typing import Any, Dict, Generator, Optional, Set, Union
27+
from typing import Any, Dict, Generator, List, Optional, Set, Union
2828
from weakref import ReferenceType
2929

3030
import torch
31+
from neptune.new.types import File as NeptuneFile
3132

3233
from pytorch_lightning import __version__
34+
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint
3335
from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_experiment
3436
from pytorch_lightning.utilities import rank_zero_only
3537
from pytorch_lightning.utilities.imports import _NEPTUNE_AVAILABLE, _NEPTUNE_GREATER_EQUAL_0_9
3638
from pytorch_lightning.utilities.model_summary import ModelSummary
37-
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint
3839

3940
if _NEPTUNE_AVAILABLE and _NEPTUNE_GREATER_EQUAL_0_9:
4041
try:
@@ -49,12 +50,11 @@
4950
# needed for test mocks, and function signatures
5051
neptune, Run = None, None
5152

52-
5353
log = logging.getLogger(__name__)
5454

5555
INTEGRATION_VERSION_KEY = "source_code/integrations/pytorch-lightning"
5656

57-
# kwargs used in NeptuneLogger for legacy client (current NeptuneLegacyLogger)
57+
# kwargs used in previous NeptuneLogger version, now deprecated
5858
LEGACY_NEPTUNE_INIT_KWARGS = [
5959
"project_name",
6060
"offline_mode",
@@ -568,43 +568,50 @@ def version(self) -> str:
568568
return self.run._short_id # skipcq: PYL-W0212
569569

570570
@staticmethod
571-
def _raise_deprecated_api_usage(f_name, sample_code):
572-
raise ValueError(
573-
f"The function you've used is deprecated.\n"
574-
f"If you are looking for the Neptune logger using legacy Python API,"
575-
f" it's still available as part of neptune-contrib package:\n"
576-
f" - https://docs-legacy.neptune.ai/integrations/pytorch_lightning.html\n"
577-
f"The NeptuneLogger was re-written to use the neptune.new Python API\n"
578-
f" - https://neptune.ai/blog/neptune-new\n"
579-
f" - https://docs.neptune.ai/integrations-and-supported-tools/model-training/pytorch-lightning\n"
571+
def _log_experimental_feature_info(f_name, sample_code):
572+
warnings.warn(
573+
"The function you've used is experimental, it may change or be abandoned in the future.\n"
574+
"If you are looking for the Neptune logger using legacy Python API,"
575+
" it's still available as part of neptune-contrib package:\n"
576+
" - https://docs-legacy.neptune.ai/integrations/pytorch_lightning.html\n"
577+
"The NeptuneLogger was re-written to use the neptune.new Python API\n"
578+
" - https://neptune.ai/blog/neptune-new\n"
579+
" - https://docs.neptune.ai/integrations-and-supported-tools/model-training/pytorch-lightning\n"
580580
f"Instead of `logger.{f_name}` you can use:\n"
581581
f"\t{sample_code}"
582582
)
583583

584584
@rank_zero_only
585-
def log_metric(self, *args, **kwargs):
586-
self._raise_deprecated_api_usage("log_metric", f"logger.run['{self._prefix}/key'].log(42)")
585+
def log_metric(self, key: str, metric):
586+
self._log_experimental_feature_info("log_metric", f"logger.run['{self._prefix}/key'].log(42)")
587+
self.run[f'{self._prefix}/{key}'].log(metric)
587588

588589
@rank_zero_only
589-
def log_text(self, *args, **kwargs):
590-
self._raise_deprecated_api_usage("log_text", f"logger.run['{self._prefix}/key'].log('text')")
590+
def log_text(self, key: str, value):
591+
self._log_experimental_feature_info("log_text", f"logger.run['{self._prefix}/key'].log('text')")
592+
self.run[f'{self._prefix}/{key}'].log(str(value))
591593

592594
@rank_zero_only
593-
def log_image(self, *args, **kwargs):
594-
self._raise_deprecated_api_usage("log_image",
595-
f"logger.run['{self._prefix}/key'].log(File('path_to_image'))")
595+
def log_image(self, key: str, img):
596+
self._log_experimental_feature_info("log_image", f"logger.run['{self._prefix}/key'].log(File('path_to_image'))")
597+
if isinstance(img, str):
598+
# if `img` is path to file, convert it to file object
599+
img = NeptuneFile(img)
600+
self.run[f'{self._prefix}/{key}'].log(img)
596601

597602
@rank_zero_only
598-
def log_artifact(self, *args, **kwargs):
599-
self._raise_deprecated_api_usage("log_artifact",
600-
f"logger.run['{self._prefix}/{self.ARTIFACTS_KEY}/key'].log('path_to_file')")
603+
def log_artifact(self, key: str, artifact: str):
604+
self._log_experimental_feature_info("log_artifact",
605+
f"logger.run['{self._prefix}/{self.ARTIFACTS_KEY}/key'].log('path_to_file')")
606+
self.run[f'{self._prefix}/{self.ARTIFACTS_KEY}/{key}'].log(artifact)
601607

602608
@rank_zero_only
603-
def set_property(self, *args, **kwargs):
604-
self._raise_deprecated_api_usage("log_artifact",
605-
f"logger.run['{self._prefix}/{self.PARAMETERS_KEY}/key'].log(value)")
609+
def set_property(self, key: str, prop):
610+
self._log_experimental_feature_info("log_artifact",
611+
f"logger.run['{self._prefix}/{self.PARAMETERS_KEY}/key'] = value")
612+
self.run[f'{self._prefix}/{self.ARTIFACTS_KEY}/{key}'] = prop
606613

607614
@rank_zero_only
608-
def append_tags(self, *args, **kwargs):
609-
self._raise_deprecated_api_usage("append_tags",
610-
"logger.run['sys/tags'].add(['foo', 'bar'])")
615+
def append_tags(self, tags: List[str]):
616+
self._log_experimental_feature_info("append_tags", "logger.run['sys/tags'].add(['foo', 'bar'])")
617+
self.run['sys/tags'].add(tags)

0 commit comments

Comments
 (0)