Skip to content

Commit 75006eb

Browse files
author
Dave Berenbaum
authoredAug 16, 2023
cache: cache in dvc exp if not stage output (#660)
* cache: cache in dvc exp if not stage output * fix tests
1 parent f1b8e2a commit 75006eb

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed
 

‎src/dvclive/live.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -462,31 +462,33 @@ def log_artifact(
462462
def cache(self, path):
463463
try:
464464
if self._inside_dvc_exp:
465-
msg = f"Skipping dvc add {path} because `dvc exp run` is running."
466465
path_stage = None
467466
for stage in self._dvc_repo.index.stages:
468467
for out in stage.outs:
469468
if out.fspath == str(Path(path).absolute()):
470469
path_stage = stage
471470
break
472-
if not path_stage:
473-
msg += (
474-
"\nTo track it automatically during `dvc exp run`, "
475-
"add it as an output of the pipeline stage."
471+
if path_stage and path_stage.cmd:
472+
msg = (
473+
f"Skipping `dvc add {path}` because it is already being tracked"
474+
" automatically as an output of `dvc exp run`."
476475
)
477-
logger.warning(msg)
478-
elif path_stage.cmd:
479-
msg += "\nIt is already being tracked automatically."
480476
logger.info(msg)
481-
else:
482-
msg += (
483-
"\nTo track it automatically during `dvc exp run`:"
477+
return # skip caching
478+
if path_stage:
479+
msg = (
480+
f"\nTo track '{path}' automatically during `dvc exp run`:"
484481
f"\n1. Run `dvc exp remove {path_stage.addressing}` "
485482
"to stop tracking it outside the pipeline."
486483
"\n2. Add it as an output of the pipeline stage."
487484
)
488485
logger.warning(msg)
489-
return
486+
else:
487+
msg = (
488+
f"\nTo track '{path}' automatically during `dvc exp run`, "
489+
"add it as an output of the pipeline stage."
490+
)
491+
logger.warning(msg)
490492

491493
stage = self._dvc_repo.add(str(path))
492494

‎tests/test_log_artifact.py

+21-24
Original file line numberDiff line numberDiff line change
@@ -225,44 +225,41 @@ def test_log_artifact_type_model_when_dvc_add_fails(tmp_dir, mocker, mocked_dvc_
225225
}
226226

227227

228-
def test_log_artifact_inside_exp(tmp_dir, mocked_dvc_repo):
229-
data = tmp_dir / "data"
230-
data.touch()
231-
with Live() as live:
232-
live._inside_dvc_exp = True
233-
live.log_artifact("data")
234-
mocked_dvc_repo.add.assert_not_called()
235-
236-
237228
@pytest.mark.parametrize("tracked", ["data_source", "stage", None])
238-
def test_log_artifact_inside_exp_logger(tmp_dir, mocker, dvc_repo, tracked):
229+
def test_log_artifact_inside_exp(tmp_dir, mocker, dvc_repo, tracked):
239230
logger = mocker.patch("dvclive.live.logger")
231+
data = tmp_dir / "data"
232+
data.touch()
240233
if tracked == "data_source":
241-
data = tmp_dir / "data"
242-
data.touch()
243234
dvc_repo.add(data)
244235
elif tracked == "stage":
245236
dvcyaml_path = tmp_dir / "dvc.yaml"
246237
with open(dvcyaml_path, "w") as f:
247238
f.write(dvcyaml)
248-
with Live() as live:
249-
live._inside_dvc_exp = True
250-
live.log_artifact("data")
251-
msg = "Skipping dvc add data because `dvc exp run` is running."
252-
if tracked == "data_source":
253-
msg += (
254-
"\nTo track it automatically during `dvc exp run`:"
239+
live = Live()
240+
spy = mocker.spy(live._dvc_repo, "add")
241+
live._inside_dvc_exp = True
242+
live.log_artifact("data")
243+
if tracked == "stage":
244+
msg = (
245+
"Skipping `dvc add data` because it is already being tracked"
246+
" automatically as an output of `dvc exp run`."
247+
)
248+
logger.info.assert_called_with(msg)
249+
spy.assert_not_called()
250+
elif tracked == "data_source":
251+
msg = (
252+
"\nTo track 'data' automatically during `dvc exp run`:"
255253
"\n1. Run `dvc exp remove data.dvc` "
256254
"to stop tracking it outside the pipeline."
257255
"\n2. Add it as an output of the pipeline stage."
258256
)
259257
logger.warning.assert_called_with(msg)
260-
elif tracked == "stage":
261-
msg += "\nIt is already being tracked automatically."
262-
logger.info.assert_called_with(msg)
258+
spy.assert_called_once()
263259
else:
264-
msg += (
265-
"\nTo track it automatically during `dvc exp run`, "
260+
msg = (
261+
"\nTo track 'data' automatically during `dvc exp run`, "
266262
"add it as an output of the pipeline stage."
267263
)
268264
logger.warning.assert_called_with(msg)
265+
spy.assert_called_once()

0 commit comments

Comments
 (0)
Please sign in to comment.