From 865d355554b27be5ea49640c14556c46f7906327 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 22 May 2020 04:35:42 +0300 Subject: [PATCH] params/metrics: introduce --no-path and --old (#3845) * metrics: diff: add --no-path Part of #3690 * params: diff: introduce --no-path * metrics: diff: introduce --old Part of #3690 --- dvc/command/metrics.py | 43 ++++++++++++++++++++++-------- dvc/command/params.py | 23 +++++++++++++--- scripts/completion/dvc.bash | 4 +-- tests/unit/command/test_metrics.py | 41 ++++++++++++++++++++++++++++ tests/unit/command/test_params.py | 30 ++++++++++++++++++++- 5 files changed, 123 insertions(+), 18 deletions(-) diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index 8cd9befd6..c0e317748 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -87,7 +87,7 @@ class CmdMetricsRemove(CmdBase): return 0 -def _show_diff(diff, markdown=False): +def _show_diff(diff, markdown=False, no_path=False, old=False): from collections import OrderedDict from dvc.utils.diff import table @@ -96,16 +96,23 @@ def _show_diff(diff, markdown=False): for fname, mdiff in diff.items(): sorted_mdiff = OrderedDict(sorted(mdiff.items())) for metric, change in sorted_mdiff.items(): - rows.append( - [ - fname, - metric, - change["new"], - change.get("diff", "diff not supported"), - ] - ) + row = [] if no_path else [fname] + row.append(metric) + if old: + row.append(change.get("old")) + row.append(change["new"]) + row.append(change.get("diff", "diff not supported")) + rows.append(row) - return table(["Path", "Metric", "Value", "Change"], rows, markdown) + header = [] if no_path else ["Path"] + header.append("Metric") + if old: + header.extend(["Old", "New"]) + else: + header.append("Value") + header.append("Change") + + return table(header, rows, markdown) class CmdMetricsDiff(CmdBase): @@ -124,7 +131,9 @@ class CmdMetricsDiff(CmdBase): logger.info(json.dumps(diff)) else: - table = _show_diff(diff, self.args.show_md) + table = _show_diff( + diff, self.args.show_md, self.args.no_path, self.args.old + ) if table: logger.info(table) @@ -269,6 +278,18 @@ def add_parser(subparsers, parent_parser): default=False, help="Show tabulated output in the Markdown format (GFM).", ) + metrics_diff_parser.add_argument( + "--no-path", + action="store_true", + default=False, + help="Don't show metric path.", + ) + metrics_diff_parser.add_argument( + "--old", + action="store_true", + default=False, + help="Show old metric value.", + ) metrics_diff_parser.set_defaults(func=CmdMetricsDiff) METRICS_REMOVE_HELP = "Remove metric mark on a DVC-tracked file." diff --git a/dvc/command/params.py b/dvc/command/params.py index ddf954b32..f980ff69b 100644 --- a/dvc/command/params.py +++ b/dvc/command/params.py @@ -8,16 +8,25 @@ from dvc.exceptions import DvcException logger = logging.getLogger(__name__) -def _show_diff(diff, markdown=False): +def _show_diff(diff, markdown=False, no_path=False): from dvc.utils.diff import table rows = [] for fname, pdiff in diff.items(): sorted_pdiff = OrderedDict(sorted(pdiff.items())) for param, change in sorted_pdiff.items(): - rows.append([fname, param, change["old"], change["new"]]) + row = [] if no_path else [fname] + row.append(param) + row.append(change["old"]) + row.append(change["new"]) + rows.append(row) - return table(["Path", "Param", "Old", "New"], rows, markdown) + header = [] if no_path else ["Path"] + header.append("Param") + header.append("Old") + header.append("New") + + return table(header, rows, markdown) class CmdParamsDiff(CmdBase): @@ -34,7 +43,7 @@ class CmdParamsDiff(CmdBase): logger.info(json.dumps(diff)) else: - table = _show_diff(diff, self.args.show_md) + table = _show_diff(diff, self.args.show_md, self.args.no_path) if table: logger.info(table) @@ -100,4 +109,10 @@ def add_parser(subparsers, parent_parser): default=False, help="Show tabulated output in the Markdown format (GFM).", ) + params_diff_parser.add_argument( + "--no-path", + action="store_true", + default=False, + help="Don't show params path.", + ) params_diff_parser.set_defaults(func=CmdParamsDiff) diff --git a/scripts/completion/dvc.bash b/scripts/completion/dvc.bash index cc12e982a..f8bce9fc2 100644 --- a/scripts/completion/dvc.bash +++ b/scripts/completion/dvc.bash @@ -38,7 +38,7 @@ _dvc_lock_COMPGEN=_dvc_compgen_DVCFiles _dvc_metrics='add diff modify remove show' _dvc_metrics_add='-t --type -x --xpath' _dvc_metrics_add_COMPGEN=_dvc_compgen_files -_dvc_metrics_diff='--targets -t --type -x --xpath -R --show-json --show-md' +_dvc_metrics_diff='--targets -t --type -x --xpath -R --show-json --show-md --no-path --old' _dvc_metrics_modify='-t --type -x --xpath' _dvc_metrics_modify_COMPGEN=_dvc_compgen_files _dvc_metrics_remove='' @@ -48,7 +48,7 @@ _dvc_metrics_show_COMPGEN=_dvc_compgen_files _dvc_move='' _dvc_move_COMPGEN=_dvc_compgen_files _dvc_params='diff' -_dvc_params_diff='--all --show-json --show-md' +_dvc_params_diff='--all --show-json --show-md --no-path' _dvc_pipeline='list show' _dvc_pipeline_list='' _dvc_pipeline_show='-c --commands -o --outs --ascii --dot --tree -l --locked' diff --git a/tests/unit/command/test_metrics.py b/tests/unit/command/test_metrics.py index cd4bb7c2a..d0eb89e1b 100644 --- a/tests/unit/command/test_metrics.py +++ b/tests/unit/command/test_metrics.py @@ -17,6 +17,9 @@ def test_metrics_diff(dvc, mocker): "--targets", "target1", "target2", + "--show-md", + "--no-path", + "--old", ] ) assert cli_args.func == CmdMetricsDiff @@ -174,3 +177,41 @@ def test_metrics_diff_markdown(): | metrics.yaml | a.d.e | 4 | 1 | | metrics.yaml | x.b | 6 | diff not supported |""" ) + + +def test_metrics_diff_no_path(): + assert _show_diff( + { + "metrics.yaml": { + "x.b": {"old": 5, "new": 6, "diff": 1}, + "a.d.e": {"old": 3, "new": 4, "diff": 1}, + "a.b.c": {"old": 1, "new": 2, "diff": 1}, + } + }, + no_path=True, + ) == textwrap.dedent( + """\ + Metric Value Change + a.b.c 2 1 + a.d.e 4 1 + x.b 6 1""" + ) + + +def test_metrics_diff_with_old(): + assert _show_diff( + { + "metrics.yaml": { + "x.b": {"old": 5, "new": 6, "diff": 1}, + "a.d.e": {"old": 3, "new": 4, "diff": 1}, + "a.b.c": {"old": 1, "new": 2, "diff": 1}, + } + }, + old=True, + ) == textwrap.dedent( + """\ + Path Metric Old New Change + metrics.yaml a.b.c 1 2 1 + metrics.yaml a.d.e 3 4 1 + metrics.yaml x.b 5 6 1""" + ) diff --git a/tests/unit/command/test_params.py b/tests/unit/command/test_params.py index 8ebfac5fd..787110145 100644 --- a/tests/unit/command/test_params.py +++ b/tests/unit/command/test_params.py @@ -7,7 +7,16 @@ from dvc.command.params import CmdParamsDiff, _show_diff def test_params_diff(dvc, mocker): cli_args = parse_args( - ["params", "diff", "HEAD~10", "HEAD~1", "--all", "--show-json"] + [ + "params", + "diff", + "HEAD~10", + "HEAD~1", + "--all", + "--show-json", + "--show-md", + "--no-path", + ] ) assert cli_args.func == CmdParamsDiff @@ -142,3 +151,22 @@ def test_params_diff_markdown(): | params.yaml | a.d.e | None | 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""" + )