Skip to content

Commit 865d355

Browse files
authored
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
1 parent 84712c4 commit 865d355

File tree

5 files changed

+123
-18
lines changed

5 files changed

+123
-18
lines changed

dvc/command/metrics.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def run(self):
8787
return 0
8888

8989

90-
def _show_diff(diff, markdown=False):
90+
def _show_diff(diff, markdown=False, no_path=False, old=False):
9191
from collections import OrderedDict
9292

9393
from dvc.utils.diff import table
@@ -96,16 +96,23 @@ def _show_diff(diff, markdown=False):
9696
for fname, mdiff in diff.items():
9797
sorted_mdiff = OrderedDict(sorted(mdiff.items()))
9898
for metric, change in sorted_mdiff.items():
99-
rows.append(
100-
[
101-
fname,
102-
metric,
103-
change["new"],
104-
change.get("diff", "diff not supported"),
105-
]
106-
)
99+
row = [] if no_path else [fname]
100+
row.append(metric)
101+
if old:
102+
row.append(change.get("old"))
103+
row.append(change["new"])
104+
row.append(change.get("diff", "diff not supported"))
105+
rows.append(row)
106+
107+
header = [] if no_path else ["Path"]
108+
header.append("Metric")
109+
if old:
110+
header.extend(["Old", "New"])
111+
else:
112+
header.append("Value")
113+
header.append("Change")
107114

108-
return table(["Path", "Metric", "Value", "Change"], rows, markdown)
115+
return table(header, rows, markdown)
109116

110117

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

125132
logger.info(json.dumps(diff))
126133
else:
127-
table = _show_diff(diff, self.args.show_md)
134+
table = _show_diff(
135+
diff, self.args.show_md, self.args.no_path, self.args.old
136+
)
128137
if table:
129138
logger.info(table)
130139

@@ -269,6 +278,18 @@ def add_parser(subparsers, parent_parser):
269278
default=False,
270279
help="Show tabulated output in the Markdown format (GFM).",
271280
)
281+
metrics_diff_parser.add_argument(
282+
"--no-path",
283+
action="store_true",
284+
default=False,
285+
help="Don't show metric path.",
286+
)
287+
metrics_diff_parser.add_argument(
288+
"--old",
289+
action="store_true",
290+
default=False,
291+
help="Show old metric value.",
292+
)
272293
metrics_diff_parser.set_defaults(func=CmdMetricsDiff)
273294

274295
METRICS_REMOVE_HELP = "Remove metric mark on a DVC-tracked file."

dvc/command/params.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@
88
logger = logging.getLogger(__name__)
99

1010

11-
def _show_diff(diff, markdown=False):
11+
def _show_diff(diff, markdown=False, no_path=False):
1212
from dvc.utils.diff import table
1313

1414
rows = []
1515
for fname, pdiff in diff.items():
1616
sorted_pdiff = OrderedDict(sorted(pdiff.items()))
1717
for param, change in sorted_pdiff.items():
18-
rows.append([fname, param, change["old"], change["new"]])
18+
row = [] if no_path else [fname]
19+
row.append(param)
20+
row.append(change["old"])
21+
row.append(change["new"])
22+
rows.append(row)
1923

20-
return table(["Path", "Param", "Old", "New"], rows, markdown)
24+
header = [] if no_path else ["Path"]
25+
header.append("Param")
26+
header.append("Old")
27+
header.append("New")
28+
29+
return table(header, rows, markdown)
2130

2231

2332
class CmdParamsDiff(CmdBase):
@@ -34,7 +43,7 @@ def run(self):
3443

3544
logger.info(json.dumps(diff))
3645
else:
37-
table = _show_diff(diff, self.args.show_md)
46+
table = _show_diff(diff, self.args.show_md, self.args.no_path)
3847
if table:
3948
logger.info(table)
4049

@@ -100,4 +109,10 @@ def add_parser(subparsers, parent_parser):
100109
default=False,
101110
help="Show tabulated output in the Markdown format (GFM).",
102111
)
112+
params_diff_parser.add_argument(
113+
"--no-path",
114+
action="store_true",
115+
default=False,
116+
help="Don't show params path.",
117+
)
103118
params_diff_parser.set_defaults(func=CmdParamsDiff)

scripts/completion/dvc.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ _dvc_lock_COMPGEN=_dvc_compgen_DVCFiles
3838
_dvc_metrics='add diff modify remove show'
3939
_dvc_metrics_add='-t --type -x --xpath'
4040
_dvc_metrics_add_COMPGEN=_dvc_compgen_files
41-
_dvc_metrics_diff='--targets -t --type -x --xpath -R --show-json --show-md'
41+
_dvc_metrics_diff='--targets -t --type -x --xpath -R --show-json --show-md --no-path --old'
4242
_dvc_metrics_modify='-t --type -x --xpath'
4343
_dvc_metrics_modify_COMPGEN=_dvc_compgen_files
4444
_dvc_metrics_remove=''
@@ -48,7 +48,7 @@ _dvc_metrics_show_COMPGEN=_dvc_compgen_files
4848
_dvc_move=''
4949
_dvc_move_COMPGEN=_dvc_compgen_files
5050
_dvc_params='diff'
51-
_dvc_params_diff='--all --show-json --show-md'
51+
_dvc_params_diff='--all --show-json --show-md --no-path'
5252
_dvc_pipeline='list show'
5353
_dvc_pipeline_list=''
5454
_dvc_pipeline_show='-c --commands -o --outs --ascii --dot --tree -l --locked'

tests/unit/command/test_metrics.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def test_metrics_diff(dvc, mocker):
1717
"--targets",
1818
"target1",
1919
"target2",
20+
"--show-md",
21+
"--no-path",
22+
"--old",
2023
]
2124
)
2225
assert cli_args.func == CmdMetricsDiff
@@ -174,3 +177,41 @@ def test_metrics_diff_markdown():
174177
| metrics.yaml | a.d.e | 4 | 1 |
175178
| metrics.yaml | x.b | 6 | diff not supported |"""
176179
)
180+
181+
182+
def test_metrics_diff_no_path():
183+
assert _show_diff(
184+
{
185+
"metrics.yaml": {
186+
"x.b": {"old": 5, "new": 6, "diff": 1},
187+
"a.d.e": {"old": 3, "new": 4, "diff": 1},
188+
"a.b.c": {"old": 1, "new": 2, "diff": 1},
189+
}
190+
},
191+
no_path=True,
192+
) == textwrap.dedent(
193+
"""\
194+
Metric Value Change
195+
a.b.c 2 1
196+
a.d.e 4 1
197+
x.b 6 1"""
198+
)
199+
200+
201+
def test_metrics_diff_with_old():
202+
assert _show_diff(
203+
{
204+
"metrics.yaml": {
205+
"x.b": {"old": 5, "new": 6, "diff": 1},
206+
"a.d.e": {"old": 3, "new": 4, "diff": 1},
207+
"a.b.c": {"old": 1, "new": 2, "diff": 1},
208+
}
209+
},
210+
old=True,
211+
) == textwrap.dedent(
212+
"""\
213+
Path Metric Old New Change
214+
metrics.yaml a.b.c 1 2 1
215+
metrics.yaml a.d.e 3 4 1
216+
metrics.yaml x.b 5 6 1"""
217+
)

tests/unit/command/test_params.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77

88
def test_params_diff(dvc, mocker):
99
cli_args = parse_args(
10-
["params", "diff", "HEAD~10", "HEAD~1", "--all", "--show-json"]
10+
[
11+
"params",
12+
"diff",
13+
"HEAD~10",
14+
"HEAD~1",
15+
"--all",
16+
"--show-json",
17+
"--show-md",
18+
"--no-path",
19+
]
1120
)
1221
assert cli_args.func == CmdParamsDiff
1322

@@ -142,3 +151,22 @@ def test_params_diff_markdown():
142151
| params.yaml | a.d.e | None | 4 |
143152
| params.yaml | x.b | 5 | 6 |"""
144153
)
154+
155+
156+
def test_params_diff_no_path():
157+
assert _show_diff(
158+
{
159+
"params.yaml": {
160+
"x.b": {"old": 5, "new": 6},
161+
"a.d.e": {"old": 3, "new": 4},
162+
"a.b.c": {"old": 1, "new": 2},
163+
}
164+
},
165+
no_path=True,
166+
) == textwrap.dedent(
167+
"""\
168+
Param Old New
169+
a.b.c 1 2
170+
a.d.e 3 4
171+
x.b 5 6"""
172+
)

0 commit comments

Comments
 (0)