Skip to content

Commit

Permalink
Add experiment name to DVCLive only signal file (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon authored Aug 24, 2023
1 parent 9face75 commit c1707ac
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/dvclive/dvc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# ruff: noqa: SLF001
import copy
import json
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from dvclive.plots import Image, Metric
from dvclive.serialize import dump_yaml
Expand Down Expand Up @@ -45,13 +46,13 @@ def _find_dvc_root(root: Optional[StrPath] = None) -> Optional[str]:
return None


def _write_file(file: str, contents=""):
def _write_file(file: str, contents: Dict[str, Union[str, int]]):
import builtins

with builtins.open(file, "w", encoding="utf-8") as fobj:
# NOTE: force flushing/writing empty file to disk, otherwise when
# run in certain contexts (pytest) file may not actually be written
fobj.write(str(contents))
fobj.write(json.dumps(contents, sort_keys=True, ensure_ascii=False))
fobj.flush()
os.fsync(fobj.fileno())

Expand Down Expand Up @@ -105,7 +106,7 @@ def make_dvcyaml(live) -> None:
dump_yaml(dvcyaml, live.dvc_file)


def mark_dvclive_only_started() -> None:
def mark_dvclive_only_started(exp_name: str) -> None:
"""
Signal DVC VS Code extension that
an experiment is running in the workspace.
Expand All @@ -119,7 +120,7 @@ def mark_dvclive_only_started() -> None:

signal_file = _dvclive_only_signal_file(root_dir)

_write_file(signal_file, os.getpid())
_write_file(signal_file, {"pid": os.getpid(), "exp_name": exp_name})


def mark_dvclive_only_ended() -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _init_dvc(self):
self._baseline_rev = self._dvc_repo.scm.get_rev()
if self._save_dvc_exp:
self._exp_name = get_random_exp_name(self._dvc_repo.scm, self._baseline_rev)
mark_dvclive_only_started()
mark_dvclive_only_started(self._exp_name)
self._include_untracked.append(self.dir)

def _init_studio(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def test_vscode_dvclive_only_signal_file(tmp_dir, dvc_root, mocker):
if dvc_root:
assert os.path.exists(signal_file)
with open(signal_file, encoding="utf-8") as f:
assert f.read() == str(test_pid)
assert json.load(f) == {"pid": test_pid, "exp_name": dvclive._exp_name}

else:
assert not os.path.exists(signal_file)
Expand Down

0 comments on commit c1707ac

Please sign in to comment.