refactor diff and show to use new table ui and new data structure (#5872)

* use new table data structure for diff and show

* fix params diff showing changes

* metrics show: use default precision

* skip precision format for exp diff params

* exp diff: separate tables in --quiet mode

* change precision to signify significants digits

changed f specifier to g, so that we show significant digits
instead of fractional digits. This does changes how precision
worked for metrics/params diff/show.
This commit is contained in:
Saugat Pachhai
2021-04-27 11:05:52 +05:45
committed by GitHub
parent 0860e419e4
commit f38d163d5c
11 changed files with 792 additions and 675 deletions
+15 -136
View File
@@ -1,8 +1,7 @@
import logging
import textwrap
from dvc.cli import parse_args
from dvc.command.params import CmdParamsDiff, _show_diff
from dvc.command.params import CmdParamsDiff
def test_params_diff(dvc, mocker):
@@ -24,11 +23,12 @@ def test_params_diff(dvc, mocker):
assert cli_args.func == CmdParamsDiff
cmd = cli_args.func(cli_args)
m = mocker.patch("dvc.repo.params.diff.diff", return_value={})
params_diff = mocker.patch("dvc.repo.params.diff.diff", return_value={})
show_diff_mock = mocker.patch("dvc.compare.show_diff")
assert cmd.run() == 0
m.assert_called_once_with(
params_diff.assert_called_once_with(
cmd.repo,
a_rev="HEAD~10",
b_rev="HEAD~1",
@@ -36,6 +36,7 @@ def test_params_diff(dvc, mocker):
all=True,
deps=True,
)
show_diff_mock.assert_not_called()
def test_params_diff_from_cli(dvc, mocker):
@@ -43,80 +44,20 @@ def test_params_diff_from_cli(dvc, mocker):
assert cli_args.func == CmdParamsDiff
cmd = cli_args.func(cli_args)
m = mocker.patch("dvc.repo.params.diff.diff", return_value={})
params_diff = mocker.patch("dvc.repo.params.diff.diff", return_value={})
show_diff_mock = mocker.patch("dvc.compare.show_diff")
assert cmd.run() == 0
m.assert_called_once_with(
params_diff.assert_called_once_with(
cmd.repo, a_rev=None, b_rev=None, all=False, targets=None, deps=False,
)
def test_params_diff_changed():
assert _show_diff(
{"params.yaml": {"a.b.c": {"old": 1, "new": 2}}}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml a.b.c 1 2"""
show_diff_mock.assert_called_once_with(
{}, title="Param", markdown=False, no_path=False, show_changes=False
)
def test_params_diff_list():
assert _show_diff(
{"params.yaml": {"a.b.c": {"old": 1, "new": [2, 3]}}}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml a.b.c 1 [2, 3]"""
)
def test_params_diff_unchanged():
assert _show_diff(
{"params.yaml": {"a.b.d": {"old": "old", "new": "new"}}}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml a.b.d old new"""
)
def test_params_diff_no_changes():
assert _show_diff({}) == ""
def test_params_diff_new():
assert _show_diff(
{"params.yaml": {"a.b.d": {"old": None, "new": "new"}}}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml a.b.d — new"""
)
def test_params_diff_deleted():
assert _show_diff(
{"params.yaml": {"a.b.d": {"old": "old", "new": None}}}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml a.b.d old —"""
)
def test_params_diff_prec():
assert _show_diff(
{"params.yaml": {"train.lr": {"old": 0.0042, "new": 0.0043}}}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml train.lr 0.0042 0.0043"""
)
def test_params_diff_show_json(dvc, mocker, caplog):
def test_params_diff_show_json(dvc, mocker, caplog, capsys):
cli_args = parse_args(
["params", "diff", "HEAD~10", "HEAD~1", "--show-json"]
)
@@ -124,73 +65,11 @@ def test_params_diff_show_json(dvc, mocker, caplog):
mocker.patch(
"dvc.repo.params.diff.diff", return_value={"params.yaml": {"a": "b"}}
)
show_diff_mock = mocker.patch("dvc.compare.show_diff")
with caplog.at_level(logging.INFO, logger="dvc"):
assert cmd.run() == 0
assert '{"params.yaml": {"a": "b"}}\n' in caplog.text
def test_params_diff_sorted():
assert _show_diff(
{
"params.yaml": {
"x.b": {"old": 5, "new": 6},
"a.d.e": {"old": 3, "new": 4},
"a.b.c": {"old": 1, "new": 2},
}
}
) == textwrap.dedent(
"""\
Path Param Old New
params.yaml a.b.c 1 2
params.yaml a.d.e 3 4
params.yaml x.b 5 6"""
)
def test_params_diff_markdown_empty():
assert _show_diff({}, markdown=True) == textwrap.dedent(
"""\
| Path | Param | Old | New |
|--------|---------|-------|-------|
"""
)
def test_params_diff_markdown():
assert _show_diff(
{
"params.yaml": {
"x.b": {"old": 5, "new": 6},
"a.d.e": {"old": None, "new": 4},
"a.b.c": {"old": 1, "new": None},
}
},
markdown=True,
) == textwrap.dedent(
"""\
| Path | Param | Old | New |
|-------------|---------|-------|-------|
| params.yaml | a.b.c | 1 | — |
| params.yaml | a.d.e | — | 4 |
| params.yaml | x.b | 5 | 6 |
"""
)
def test_params_diff_no_path():
assert _show_diff(
{
"params.yaml": {
"x.b": {"old": 5, "new": 6},
"a.d.e": {"old": 3, "new": 4},
"a.b.c": {"old": 1, "new": 2},
}
},
no_path=True,
) == textwrap.dedent(
"""\
Param Old New
a.b.c 1 2
a.d.e 3 4
x.b 5 6"""
)
show_diff_mock.assert_not_called()
assert capsys.readouterr() == ("", "")