Skip to content

Commit

Permalink
refactor: Update Live class to handle pathlib.Path object for `dv…
Browse files Browse the repository at this point in the history
…cyaml` argument.
  • Loading branch information
AbdullahMakhdoom committed Sep 9, 2024
1 parent 6888462 commit 190bd91
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(
resume: bool = False,
report: Literal["md", "notebook", "html", None] = None,
save_dvc_exp: bool = True,
dvcyaml: Optional[str] = "dvc.yaml",
dvcyaml: Optional[str, Path] = "dvc.yaml",
cache_images: bool = False,
exp_name: Optional[str] = None,
exp_message: Optional[str] = None,
Expand All @@ -104,11 +104,11 @@ def __init__(
part of `Live.end()`. Defaults to `True`. If you are using DVCLive
inside a DVC Pipeline and running with `dvc exp run`, the option will be
ignored.
dvcyaml (str | None): where to write dvc.yaml file, which adds DVC
dvcyaml (str | Path | None): where to write dvc.yaml file, which adds DVC
configuration for metrics, plots, and parameters as part of
`Live.next_step()` and `Live.end()`. If `None`, no dvc.yaml file is
written. Defaults to `"dvc.yaml"`. See `Live.make_dvcyaml()`.
If a string like `"subdir/dvc.yaml"`, DVCLive will write the
If a string or Path like `"subdir/dvc.yaml"`, DVCLive will write the
configuration to that path (file must be named "dvc.yaml").
If `False`, DVCLive will not write to "dvc.yaml" (useful if you are
tracking DVCLive metrics, plots, and parameters independently and
Expand Down Expand Up @@ -265,10 +265,14 @@ def _init_dvc(self): # noqa: C901
self._include_untracked.append(self.dir)

def _init_dvc_file(self) -> str:
if isinstance(self._dvcyaml, Path):
self._dvcyaml = str(self._dvcyaml)
if isinstance(self._dvcyaml, str):
if os.path.basename(self._dvcyaml) == "dvc.yaml":
return self._dvcyaml
raise InvalidDvcyamlError
if self._dvcyaml:
raise InvalidDvcyamlError("DVC yaml path is invalid. Must be a string or a Path object.")
return "dvc.yaml"

def _init_dvc_pipeline(self):
Expand Down Expand Up @@ -334,6 +338,8 @@ def _init_test(self):
"""
with tempfile.TemporaryDirectory() as dirpath:
self._dir = os.path.join(dirpath, self._dir)
if isinstance(self._dvcyaml, Path):
self._dvcyaml = str(self._dvcyaml)
if isinstance(self._dvcyaml, str):
self._dvc_file = os.path.join(dirpath, self._dvcyaml)
self._save_dvc_exp = False
Expand Down
8 changes: 5 additions & 3 deletions tests/test_dvc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest
from pathlib import Path
from dvc.exceptions import DvcException
from dvc.repo import Repo
from dvc.scm import NoSCM
Expand Down Expand Up @@ -220,12 +221,13 @@ def test_get_exp_name_duplicate(tmp_dir, mocked_dvc_repo, mocker, caplog):
assert msg in caplog.text


def test_test_mode(tmp_dir, monkeypatch, mocked_dvc_repo):
@pytest.mark.parametrize("dvcyaml_path", ["dvc.yaml", Path("dvcyaml/dvc.yaml")])
def test_test_mode(tmp_dir, monkeypatch, mocked_dvc_repo, dvcyaml_path):
monkeypatch.setenv(DVCLIVE_TEST, "true")
live = Live("dir", dvcyaml="dvc.yaml")
live = Live("dir", dvcyaml=dvcyaml_path)
live.make_dvcyaml()
assert live._dir != "dir"
assert live._dvc_file != "dvc.yaml"
assert live._save_dvc_exp is False
assert not os.path.exists("dir")
assert not os.path.exists("dvc.yaml")
assert not os.path.exists(dvcyaml_path)

0 comments on commit 190bd91

Please sign in to comment.