Skip to content

Commit

Permalink
studio: add subdir to live metrics post messages to support live expe…
Browse files Browse the repository at this point in the history
…riments in monorepos (#787)
  • Loading branch information
mattseddon committed Feb 21, 2024
1 parent 7305452 commit 9eb04c2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ classifiers = [
]
dynamic = ["version"]
dependencies = [
"dvc>=3.33.3",
"dvc>=3.47.0",
"dvc-render>=1.0.0,<2",
"dvc-studio-client>=0.17.1,<1",
"dvc-studio-client>=0.20,<1",
"funcy",
"gto",
"ruamel.yaml",
"scmrepo"
"scmrepo>=3,<4"
]

[project.optional-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import PIL

from dvc.exceptions import DvcException
from dvc.utils.studio import get_subrepo_relpath
from funcy import set_in
from ruamel.yaml.representer import RepresenterError

Expand Down Expand Up @@ -141,6 +142,7 @@ def __init__(
self._baseline_rev: str = os.getenv(env.DVC_EXP_BASELINE_REV, NULL_SHA)
self._exp_name: Optional[str] = exp_name or os.getenv(env.DVC_EXP_NAME)
self._exp_message: Optional[str] = exp_message
self._subdir: Optional[str] = None
self._experiment_rev: Optional[str] = None
self._inside_dvc_exp: bool = False
self._inside_dvc_pipeline: bool = False
Expand Down Expand Up @@ -240,6 +242,8 @@ def _init_dvc(self): # noqa: C901
if self._inside_dvc_pipeline:
return

self._subdir = get_subrepo_relpath(self._dvc_repo)

if self._save_dvc_exp:
mark_dvclive_only_started(self._exp_name)
self._include_untracked.append(self.dir)
Expand Down
9 changes: 6 additions & 3 deletions src/dvclive/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ def get_dvc_studio_config(live: Live):
return get_studio_config(dvc_studio_config=config)


def post_to_studio(live: Live, event: Literal["start", "data", "done"]):
def post_to_studio(live: Live, event: Literal["start", "data", "done"]): # noqa: C901
if event in live._studio_events_to_skip:
return

kwargs = {}
if event == "start" and live._exp_message:
kwargs["message"] = live._exp_message
if event == "start":
if message := live._exp_message:
kwargs["message"] = message
if subdir := live._subdir:
kwargs["subdir"] = subdir
elif event == "data":
metrics, params, plots = get_studio_updates(live)
kwargs["step"] = live.step # type: ignore
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pytest
from dvc_studio_client.env import DVC_STUDIO_TOKEN, DVC_STUDIO_URL, STUDIO_REPO_URL

from dvclive.utils import rel_path


@pytest.fixture()
def tmp_dir(tmp_path, monkeypatch):
Expand All @@ -19,12 +21,19 @@ def mocked_dvc_repo(tmp_dir, mocker):
_dvc_repo.scm.get_ref.return_value = None
_dvc_repo.scm.no_commits = False
_dvc_repo.experiments.save.return_value = "e" * 40
_dvc_repo.root_dir = tmp_dir
_dvc_repo.root_dir = _dvc_repo.scm.root_dir = tmp_dir
_dvc_repo.fs.relpath = rel_path
_dvc_repo.config = {}
mocker.patch("dvclive.live.get_dvc_repo", return_value=_dvc_repo)
return _dvc_repo


@pytest.fixture()
def mocked_dvc_subrepo(tmp_dir, mocker, mocked_dvc_repo):
mocked_dvc_repo.root_dir = tmp_dir / "subdir"
return mocked_dvc_repo


@pytest.fixture()
def dvc_repo(tmp_dir):
from dvc.repo import Repo
Expand Down
12 changes: 12 additions & 0 deletions tests/test_post_to_studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def test_post_to_studio(tmp_dir, mocked_dvc_repo, mocked_studio_post):
)


def test_post_to_studio_subrepo(tmp_dir, mocked_dvc_subrepo, mocked_studio_post):
live = Live()
live.log_param("fooparam", 1)

mocked_post, _ = mocked_studio_post

mocked_post.assert_called_with(
"https://0.0.0.0/api/live",
**get_studio_call("start", exp_name=live._exp_name, subdir="subdir"),
)


def test_post_to_studio_failed_data_request(
tmp_dir, mocker, mocked_dvc_repo, mocked_studio_post
):
Expand Down

0 comments on commit 9eb04c2

Please sign in to comment.