Skip to content

Commit

Permalink
plot: accept any viable target in dvc repo (iterative#4602)
Browse files Browse the repository at this point in the history
  • Loading branch information
pared authored Sep 23, 2020
1 parent 38dd1c9 commit e1b3444
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 deletions.
49 changes: 31 additions & 18 deletions dvc/repo/plots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging
import os

from funcy import first, project

from dvc.exceptions import DvcException, NoPlotsError, OutputNotFoundError
from dvc.exceptions import DvcException, NoPlotsError
from dvc.path_info import PathInfo
from dvc.schema import PLOT_PROPS
from dvc.tree.repo import RepoTree
from dvc.utils import relpath
Expand Down Expand Up @@ -142,23 +144,34 @@ def modify(self, path, props=None, unset=None):


def _collect_plots(repo, targets=None, rev=None):
def _targets_to_outs(targets):
for t in targets:
try:
(out,) = repo.find_outs_by_path(t)
yield out
except OutputNotFoundError:
logger.warning(
"File '{}' was not found at: '{}'. It will not be "
"plotted.".format(t, rev)
)

if targets:
outs = _targets_to_outs(targets)
else:
outs = (out for stage in repo.stages for out in stage.outs if out.plot)

return {out.path_info: _plot_props(out) for out in outs}
plots = {out for stage in repo.stages for out in stage.outs if out.plot}

def to_result(plots):
return {plot.path_info: _plot_props(plot) for plot in plots}

if not targets:
return to_result(plots)

target_infos = {PathInfo(os.path.abspath(target)) for target in targets}

target_plots = set()
for p in plots:
if p.path_info in target_infos:
target_plots.add(p)
target_infos.remove(p.path_info)

tree = RepoTree(repo)
result = to_result(target_plots)

for t in target_infos:
if tree.isfile(t):
result[t] = {}
else:
logger.warning(
"'%s' was not found at: '%s'. It will not be plotted.", t, rev,
)

return result


def _plot_props(out):
Expand Down
55 changes: 54 additions & 1 deletion tests/func/plots/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def test_plot_even_if_metric_missing(
with caplog.at_level(logging.WARNING, "dvc"):
plots = dvc.plots.show(revs=["v1", "v2"], targets=["metric.json"])
assert (
"File 'metric.json' was not found at: 'v1'. "
"'metric.json' was not found at: 'v1'. "
"It will not be plotted." in caplog.text
)

Expand Down Expand Up @@ -602,3 +602,56 @@ def test_multiple_plots(tmp_dir, scm, dvc, run_copy_metrics):
)

assert len(dvc.plots.show().keys()) == 2


def test_show_non_plot(tmp_dir, scm, dvc):
metric = [
{"first_val": 100, "val": 2},
{"first_val": 200, "val": 3},
]
_write_json(tmp_dir, metric, "metric.json")
plot_string = dvc.plots.show(targets=["metric.json"])["metric.json"]

plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{
"val": 2,
PlotData.INDEX_FIELD: 0,
"first_val": 100,
"rev": "workspace",
},
{
"val": 3,
PlotData.INDEX_FIELD: 1,
"first_val": 200,
"rev": "workspace",
},
]
assert plot_content["encoding"]["x"]["field"] == PlotData.INDEX_FIELD
assert plot_content["encoding"]["y"]["field"] == "val"


def test_show_non_plot_and_plot_with_params(
tmp_dir, scm, dvc, run_copy_metrics
):
metric = [
{"first_val": 100, "val": 2},
{"first_val": 200, "val": 3},
]
_write_json(tmp_dir, metric, "metric.json")
run_copy_metrics(
"metric.json", "metric2.json", plots_no_cache=["metric2.json"]
)

dvc.plots.modify("metric2.json", props={"title": "TITLE"})
result = dvc.plots.show(targets=["metric.json", "metric2.json"])

plot_content = json.loads(result["metric.json"])
plot2_content = json.loads(result["metric2.json"])

assert plot2_content["title"] == "TITLE"

assert plot_content != plot2_content
plot_content.pop("title")
plot2_content.pop("title")
assert plot_content == plot2_content

0 comments on commit e1b3444

Please sign in to comment.