Skip to content

Commit

Permalink
dvcyaml: warn if tracked as stage output (#654)
Browse files Browse the repository at this point in the history
* dvcyaml: warn if tracked as stage output

* tests

* fix tests

* clean up tests

* rename dvcyaml overlap check and refactor test
  • Loading branch information
Dave Berenbaum authored Aug 8, 2023
1 parent 82b7554 commit c473575
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ def _init_cleanup(self):
if self.dvc_file and os.path.exists(self.dvc_file):
os.remove(self.dvc_file)

def _init_check_dvcyaml_overlap(self):
for stage in self._dvc_repo.index.stages:
for out in stage.outs:
if str(out.fs_path) in str(Path(self.dvc_file).absolute()):
msg = (
f"'{self.dvc_file}' is in outputs of stage "
f"'{stage.addressing}'.\n"
f"Remove it from outputs to make DVCLive work as expected."
)
logger.warning(msg)

def _init_dvc(self):
from dvc.scm import NoSCM

Expand Down Expand Up @@ -157,6 +168,9 @@ def _init_dvc(self):
self._save_dvc_exp = False
return

if self._dvcyaml:
self._init_check_dvcyaml_overlap()

if self._inside_dvc_exp:
return

Expand Down
19 changes: 19 additions & 0 deletions tests/test_dvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,22 @@ def test_no_scm_repo(tmp_dir, mocker):

live = Live(save_dvc_exp=True)
assert live._save_dvc_exp is False


@pytest.mark.parametrize("dvcyaml", [True, False])
def test_warn_on_dvcyaml_output_overlap(tmp_dir, mocker, mocked_dvc_repo, dvcyaml):
logger = mocker.patch("dvclive.live.logger")
dvc_stage = mocker.MagicMock()
dvc_stage.addressing = "train"
dvc_out = mocker.MagicMock()
dvc_out.fs_path = tmp_dir / "dvclive"
dvc_stage.outs = [dvc_out]
mocked_dvc_repo.index.stages = [dvc_stage]
live = Live(dvcyaml=dvcyaml)

if dvcyaml:
msg = f"'{live.dvc_file}' is in outputs of stage 'train'.\n"
msg += "Remove it from outputs to make DVCLive work as expected."
logger.warning.assert_called_with(msg)
else:
logger.warning.assert_not_called()

0 comments on commit c473575

Please sign in to comment.