Skip to content

Commit

Permalink
live: get rid of ParameterAlreadyLoggedError
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrifiro committed Sep 29, 2022
1 parent a4ca09e commit 3e6c3db
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 106 deletions.
20 changes: 1 addition & 19 deletions src/dvclive/error.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from .live import Live
Expand Down Expand Up @@ -46,24 +46,6 @@ def __init__(self, name, step):
)


class ParameterAlreadyLoggedError(DvcLiveError):
def __init__(
self, name: str, val: Any, previous_val: Optional[Any] = None
):
self.name = name
self.val = val
self.previous_val = previous_val
super().__init__(
f"Parameter '{name}={val}' has already been logged"
+ (
f" (previous value={self.previous_val})."
if self.previous_val is not None
and self.val != self.previous_val
else "."
)
)


class InvalidParameterTypeError(DvcLiveError):
def __init__(self, val: Any):
self.val = val
Expand Down
13 changes: 0 additions & 13 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
InvalidDataTypeError,
InvalidParameterTypeError,
InvalidPlotTypeError,
ParameterAlreadyLoggedError,
)
from .report import make_report
from .serialize import dump_yaml, load_yaml
Expand Down Expand Up @@ -249,18 +248,6 @@ def _dump_params(self):

def log_params(self, params: Dict[str, ParamLike]):
"""Saves the given set of parameters (dict) to yaml"""
if self._resume and self.get_step():
logger.info(
"Resuming previous dvclive session, not logging params."
)
return

for param_name, param_value in params.items():
if param_name in self._params:
raise ParameterAlreadyLoggedError(
param_name, param_value, self._params[param_name]
)

self._params.update(params)
self._dump_params()
logger.debug(f"Logged {params} parameters to {self.params_path}")
Expand Down
74 changes: 0 additions & 74 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# pylint: disable=protected-access
# pylint: disable=unused-argument
import os
import re

import pytest
from funcy import last
Expand All @@ -13,7 +12,6 @@
DataAlreadyLoggedError,
InvalidDataTypeError,
InvalidParameterTypeError,
ParameterAlreadyLoggedError,
)
from dvclive.serialize import load_yaml
from dvclive.utils import parse_scalars
Expand Down Expand Up @@ -82,25 +80,6 @@ def test_log_param(tmp_dir, param_name, param_value):
assert s[param_name] == param_value


def test_log_param_already_logged(tmp_dir):
dvclive = Live()

dvclive.log_param("param", 42)
with pytest.raises(
ParameterAlreadyLoggedError,
match="Parameter 'param=42' has already been logged.",
):
dvclive.log_param("param", 42)

with pytest.raises(
ParameterAlreadyLoggedError,
match=re.escape(
"Parameter 'param=1' has already been logged (previous value=42)."
),
):
dvclive.log_param("param", 1)


def test_log_params(tmp_dir):
dvclive = Live()
params = {
Expand All @@ -125,59 +104,6 @@ def test_log_params_resume(tmp_dir, resume):
dvclive = Live(resume=resume)
assert ("param" in dvclive._params) == resume

if resume:
with pytest.raises(ParameterAlreadyLoggedError):
dvclive.log_param("param", 42)


@pytest.mark.parametrize("resume", (False, True))
def test_log_params_resume_log(tmp_dir, resume, caplog, mocker):
dvclive = Live(resume=resume)
dvclive.log_param("param", 42)

dvclive = Live(resume=resume)
assert ("param" in dvclive._params) == resume
dvclive.next_step()

if resume:
spy = mocker.spy(dvclive, "_dump_params")
dvclive.log_param("param", 42)
assert (
caplog.messages[-1]
== "Resuming previous dvclive session, not logging params."
)
assert not spy.called


def test_log_params_already_logged(tmp_dir):
dvclive = Live()
params = {
"param_string": "string_value",
"param_int": 42,
"param_float": 42.0,
"param_bool_true": True,
"param_bool_false": False,
}

dvclive.log_params(params)
assert os.path.isfile(dvclive.params_path)

with pytest.raises(
ParameterAlreadyLoggedError,
match="Parameter 'param_string=string_value' has already been logged.",
):

dvclive.log_param("param_string", "string_value")

with pytest.raises(
ParameterAlreadyLoggedError,
match=re.escape(
"Parameter 'param_string=other_value' has already been logged (previous value=string_value)." # noqa
),
):

dvclive.log_param("param_string", "other_value")


def test_log_param_custom_obj(tmp_dir):
dvclive = Live("logs")
Expand Down

0 comments on commit 3e6c3db

Please sign in to comment.