Skip to content

metrics: accept any viable target in DvcRepo #4590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions dvc/repo/metrics/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,31 @@


def _collect_metrics(repo, targets, recursive):
metrics = set()

if targets:
target_infos = [
PathInfo(os.path.abspath(target)) for target in targets
]
tree = RepoTree(repo)

rec_files = []
if recursive:
for target_info in target_infos:
if tree.isdir(target_info):
rec_files.extend(list(tree.walk_files(target_info)))

result = [t for t in target_infos if tree.isfile(t)]
result.extend(rec_files)

return result

metrics = set()
for stage in repo.stages:
for out in stage.outs:
if not out.metric:
continue

metrics.add(out.path_info)

if not targets:
return list(metrics)

target_infos = [PathInfo(os.path.abspath(target)) for target in targets]

def _filter(path_info):
for info in target_infos:
func = path_info.isin_or_eq if recursive else path_info.__eq__
if func(info):
return True
return False

return list(filter(_filter, metrics))
return list(metrics)


def _extract_metrics(metrics, path, rev):
Expand Down
14 changes: 14 additions & 0 deletions tests/func/metrics/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,17 @@ def _gen(val):
"Path Metric Old New Change\n"
"m.yaml foo 1.23457 3.45679 2.22222"
)


def test_metrics_diff_non_metrics(tmp_dir, scm, dvc):
def _gen(val):
tmp_dir.scm_gen({"some_file.yaml": f"foo: {val}"}, commit=str(val))

_gen(1)
_gen(2)
_gen(3)

result = dvc.metrics.diff(targets=["some_file.yaml"], a_rev="HEAD~2")
assert result == {
"some_file.yaml": {"foo": {"old": 1, "new": 3, "diff": 2}}
}
38 changes: 38 additions & 0 deletions tests/func/metrics/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,41 @@ def test_missing_cache(tmp_dir, dvc, run_copy_metrics):
remove(stage.outs[0].cache_path)

assert dvc.metrics.show() == {"": {"metrics.yaml": 1.1}}


def test_show_non_metric(tmp_dir, dvc):
tmp_dir.gen("metrics.yaml", "foo: 1.1")

assert dvc.metrics.show(targets=["metrics.yaml"]) == {
"": {"metrics.yaml": {"foo": 1.1}}
}


def test_show_non_metric_branch(tmp_dir, scm, dvc):
tmp_dir.scm_gen("metrics.yaml", "foo: 1.1", commit="init")
with tmp_dir.branch("branch", new=True):
tmp_dir.scm_gen("metrics.yaml", "foo: 2.2", commit="other")

assert dvc.metrics.show(targets=["metrics.yaml"], revs=["branch"]) == {
"workspace": {"metrics.yaml": {"foo": 1.1}},
"branch": {"metrics.yaml": {"foo": 2.2}},
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a test for recursive directory with unofficial metrics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since both official and unofficial metrics are gathered the same way now, I would argue that test_non_metric_and_recurisve_show already covers that. Anyhow, I modified it so that it checks both official and unofficial metrics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! πŸ™

def test_non_metric_and_recurisve_show(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen(
{"metrics_t.yaml": "foo: 1.1", "metrics": {"metric1.yaml": "bar: 1.2"}}
)

metric2 = os.fspath(tmp_dir / "metrics" / "metric2.yaml")
run_copy_metrics("metrics_t.yaml", metric2, metrics=[metric2])

assert dvc.metrics.show(
targets=["metrics_t.yaml", "metrics"], recursive=True
) == {
"": {
os.path.join("metrics", "metric1.yaml"): {"bar": 1.2},
os.path.join("metrics", "metric2.yaml"): {"foo": 1.1},
"metrics_t.yaml": {"foo": 1.1},
}
}