Skip to content

Commit

Permalink
params/metrics: introduce --no-path and --old (iterative#3845)
Browse files Browse the repository at this point in the history
* metrics: diff: add --no-path

Part of iterative#3690

* params: diff: introduce --no-path

* metrics: diff: introduce --old

Part of iterative#3690
  • Loading branch information
efiop authored May 22, 2020
1 parent 84712c4 commit 865d355
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 18 deletions.
43 changes: 32 additions & 11 deletions dvc/command/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def run(self):
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
Expand All @@ -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)

header = [] if no_path else ["Path"]
header.append("Metric")
if old:
header.extend(["Old", "New"])
else:
header.append("Value")
header.append("Change")

return table(["Path", "Metric", "Value", "Change"], rows, markdown)
return table(header, rows, markdown)


class CmdMetricsDiff(CmdBase):
Expand All @@ -124,7 +131,9 @@ def run(self):

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)

Expand Down Expand Up @@ -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."
Expand Down
23 changes: 19 additions & 4 deletions dvc/command/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
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):
Expand All @@ -34,7 +43,7 @@ def run(self):

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)

Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions scripts/completion/dvc.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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=''
Expand All @@ -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'
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/command/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def test_metrics_diff(dvc, mocker):
"--targets",
"target1",
"target2",
"--show-md",
"--no-path",
"--old",
]
)
assert cli_args.func == CmdMetricsDiff
Expand Down Expand Up @@ -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"""
)
30 changes: 29 additions & 1 deletion tests/unit/command/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@

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

Expand Down Expand Up @@ -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"""
)

0 comments on commit 865d355

Please sign in to comment.