Skip to content

Commit

Permalink
metrics: adds support for strings in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
paulourbano committed Apr 29, 2023
1 parent 2a04263 commit dc8108d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dvc/repo/metrics/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _collect_metrics(repo, targets, recursive):


def _extract_metrics(metrics, path, rev):
if isinstance(metrics, (int, float)):
if isinstance(metrics, (int, float, str)):
return metrics

if not isinstance(metrics, dict):
Expand Down
2 changes: 2 additions & 0 deletions dvc/utils/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def _diff_vals(old, new, with_unchanged):
res = {"old": old, "new": new}
if isinstance(new, (int, float)) and isinstance(old, (int, float)):
res["diff"] = new - old
elif isinstance(new, str) and isinstance(old, str):
res["diff"] = ""
return res


Expand Down
17 changes: 15 additions & 2 deletions tests/func/metrics/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ def _gen(val):
_gen(2)
_gen(3)

expected = {"m.yaml": {"a.b.c": {"old": 1, "new": 3, "diff": 2}}}
expected = {
"m.yaml": {
"a.b.e": {"old": "1", "new": "3", "diff": ""},
"a.b.c": {"old": 1, "new": 3, "diff": 2},
}
}

assert dvc.metrics.diff(a_rev="HEAD~2") == expected

Expand All @@ -47,7 +52,12 @@ def _gen(val):
_gen(2)
_gen(3)

expected = {"m.json": {"a.b.c": {"old": 1, "new": 3, "diff": 2}}}
expected = {
"m.json": {
"a.b.e": {"old": "1", "new": "3", "diff": ""},
"a.b.c": {"old": 1, "new": 3, "diff": 2},
}
}
assert dvc.metrics.diff(a_rev="HEAD~2") == expected


Expand Down Expand Up @@ -78,6 +88,7 @@ def test_metrics_diff_broken_json(tmp_dir, scm, dvc, run_copy_metrics):

assert dvc.metrics.diff() == {
"m.json": {
"a.b.e": {"old": "3", "new": None},
"a.b.c": {"old": 1, "new": None},
"a.b.d": {"old": 1, "new": None},
}
Expand All @@ -96,6 +107,7 @@ def test_metrics_diff_new_metric(tmp_dir, scm, dvc, run_copy_metrics):

assert dvc.metrics.diff() == {
"m.json": {
"a.b.e": {"old": None, "new": "3"},
"a.b.c": {"old": None, "new": 1},
"a.b.d": {"old": None, "new": 1},
}
Expand All @@ -116,6 +128,7 @@ def test_metrics_diff_deleted_metric(tmp_dir, scm, dvc, run_copy_metrics):

assert dvc.metrics.diff() == {
"m.json": {
"a.b.e": {"old": "3", "new": None},
"a.b.c": {"old": 1, "new": None},
"a.b.d": {"old": 1, "new": None},
}
Expand Down

0 comments on commit dc8108d

Please sign in to comment.