plots: add plot markers to DVC files (#3807)

* run: add --plot/--plot-no-cache

Part of #3409

* plot: move show/diff to API

* plots: show marked plots by default

Involves some refactoring to make plots comply with the rest of the code
base.

* dvc.yaml: use top-level metrics and plots

* dvc.yaml: add support for plot template

* plots: add support for templates in dvc.yaml

* dvc.yaml: allow metrics/plots to have persist/cache

* plots: render single page by-default and introduce --show-json

* plot: use "working tree" instead of "workspace"

Just to sync this with the rest of the codebase. We should consider
renaming it to "workspace" everywhere, but for now I opt to not touch
it to not break something that depends on it.

* plots: rename -f|--file to -o|--out

Better corresponds to other commands like `import`.

* plots: use RepoTree and brancher instead of api

* stage: get rid of OutputParams

* tests: plots: add simple diff test

* plots: use `--targets` instead of `--datafile`

Makes it comply with `dvc metrics/params` CLI options.

* tests: unit: plots: add --show-json test

* stage: cleanup exception for format error

Based on feedback from the users.

* lockfile: use relpath in corruption error
This commit is contained in:
Ruslan Kuprieiev
2020-05-20 04:48:53 +03:00
committed by GitHub
parent aaca1401ea
commit 09136f2378
26 changed files with 631 additions and 468 deletions
+24 -41
View File
@@ -1,24 +1,21 @@
import pytest
from dvc.cli import parse_args
from dvc.command.plots import CmdPlotsDiff, CmdPlotsShow
def test_metrics_diff(mocker):
def test_metrics_diff(dvc, mocker):
cli_args = parse_args(
[
"plots",
"diff",
"--file",
"--out",
"result.extension",
"-t",
"template",
"-d",
"--targets",
"datafile",
"--select",
"column1,column2",
"--no-html",
"--stdout",
"--show-json",
"-x",
"x_field",
"-y",
@@ -37,20 +34,17 @@ def test_metrics_diff(mocker):
assert cli_args.func == CmdPlotsDiff
cmd = cli_args.func(cli_args)
m = mocker.patch.object(cmd.repo, "plot", autospec=True)
mocker.patch("builtins.open")
mocker.patch("os.path.join")
m = mocker.patch("dvc.repo.plots.diff.diff", return_value={})
assert cmd.run() == 0
m.assert_called_once_with(
datafile="datafile",
cmd.repo,
targets=["datafile"],
template="template",
revisions=["HEAD", "tag1", "tag2"],
revs=["HEAD", "tag1", "tag2"],
fields={"column1", "column2"},
path=None,
embed=False,
x_field="x_field",
y_field="y_field",
csv_header=True,
@@ -60,19 +54,18 @@ def test_metrics_diff(mocker):
)
def test_metrics_show(mocker):
def test_metrics_show(dvc, mocker):
cli_args = parse_args(
[
"plots",
"show",
"-f",
"-o",
"result.extension",
"-t",
"template",
"-s",
"$.data",
"--no-html",
"--stdout",
"--show-json",
"--no-csv-header",
"datafile",
]
@@ -81,19 +74,16 @@ def test_metrics_show(mocker):
cmd = cli_args.func(cli_args)
m = mocker.patch.object(cmd.repo, "plot", autospec=True)
mocker.patch("builtins.open")
mocker.patch("os.path.join")
m = mocker.patch("dvc.repo.plots.show.show", return_value={})
assert cmd.run() == 0
m.assert_called_once_with(
datafile="datafile",
cmd.repo,
targets=["datafile"],
template="template",
revisions=None,
fields=None,
path="$.data",
embed=False,
x_field=None,
y_field=None,
csv_header=False,
@@ -103,20 +93,13 @@ def test_metrics_show(mocker):
)
@pytest.mark.parametrize(
"arg_revisions,is_dirty,expected_revisions",
[
([], False, ["workspace"]),
([], True, ["HEAD", "workspace"]),
(["v1", "v2", "workspace"], False, ["v1", "v2", "workspace"]),
(["v1", "v2", "workspace"], True, ["v1", "v2", "workspace"]),
],
)
def test_revisions(mocker, arg_revisions, is_dirty, expected_revisions):
args = mocker.MagicMock()
cmd = CmdPlotsDiff(args)
mocker.patch.object(args, "revisions", arg_revisions)
mocker.patch.object(cmd.repo.scm, "is_dirty", return_value=is_dirty)
assert cmd._revisions() == expected_revisions
def test_plots_show_json(dvc, mocker, caplog):
cli_args = parse_args(
["plots", "diff", "HEAD~10", "HEAD~1", "--show-json"]
)
cmd = cli_args.func(cli_args)
mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"plots.csv": "plothtml"}
)
assert cmd.run() == 0
assert '{"plots.csv": "plothtml"}\n' in caplog.text
+10
View File
@@ -18,6 +18,10 @@ def test_run(mocker, dvc):
"metrics",
"--metrics-no-cache",
"metrics-no-cache",
"--plots",
"plots",
"--plots-no-cache",
"plots-no-cache",
"--file",
"file",
"--wdir",
@@ -51,6 +55,8 @@ def test_run(mocker, dvc):
outs_no_cache=["outs-no-cache"],
metrics=["metrics"],
metrics_no_cache=["metrics-no-cache"],
plots=["plots"],
plots_no_cache=["plots-no-cache"],
outs_persist=["outs-persist"],
outs_persist_no_cache=["outs-persist-no-cache"],
params=["file:param1,param2", "param3"],
@@ -78,6 +84,8 @@ def test_run_args_from_cli(mocker, dvc):
outs_no_cache=[],
metrics=[],
metrics_no_cache=[],
plots=[],
plots_no_cache=[],
outs_persist=[],
outs_persist_no_cache=[],
params=[],
@@ -105,6 +113,8 @@ def test_run_args_with_spaces(mocker, dvc):
outs_no_cache=[],
metrics=[],
metrics_no_cache=[],
plots=[],
plots_no_cache=[],
outs_persist=[],
outs_persist_no_cache=[],
params=[],