Esempio n. 1
0
def diff(repo, *args, a_rev=None, b_rev=None, param_deps=False, **kwargs):
    from dvc.repo.experiments.show import _collect_experiment_commit
    from dvc.scm import resolve_rev

    if repo.scm.no_commits:
        return {}

    if a_rev:
        a_rev = fix_exp_head(repo.scm, a_rev)
        rev = resolve_rev(repo.scm, a_rev)
        old = _collect_experiment_commit(repo, rev, param_deps=param_deps)
    else:
        old = _collect_experiment_commit(
            repo, fix_exp_head(repo.scm, "HEAD"), param_deps=param_deps
        )

    if b_rev:
        b_rev = fix_exp_head(repo.scm, b_rev)
        rev = resolve_rev(repo.scm, b_rev)
        new = _collect_experiment_commit(repo, rev, param_deps=param_deps)
    else:
        new = _collect_experiment_commit(
            repo, "workspace", param_deps=param_deps
        )

    with_unchanged = kwargs.pop("all", False)

    return {
        key: _diff(
            format_dict(old.get("data", {}).get(key, {})),
            format_dict(new.get("data", {}).get(key, {})),
            with_unchanged=with_unchanged,
        )
        for key in ["metrics", "params"]
    }
Esempio n. 2
0
File: diff.py Progetto: zang3tsu/dvc
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
    from dvc.repo.experiments.show import _collect_experiment

    if repo.scm.no_commits:
        return {}

    if a_rev:
        with repo.experiments.chdir():
            old = _collect_experiment(repo.experiments.exp_dvc, a_rev)
    else:
        old = _collect_experiment(repo, "HEAD")

    if b_rev:
        with repo.experiments.chdir():
            new = _collect_experiment(repo.experiments.exp_dvc, b_rev)
    else:
        new = _collect_experiment(repo, "workspace")

    with_unchanged = kwargs.pop("all", False)

    return {
        key: _diff(
            format_dict(old[key]),
            format_dict(new[key]),
            with_unchanged=with_unchanged,
        )
        for key in ["metrics", "params"]
    }
Esempio n. 3
0
def diff(repo, *args, a_rev=None, b_rev=None, param_deps=False, **kwargs):
    from dvc.repo.experiments.show import _collect_experiment_commit

    if repo.scm.no_commits:
        return {}

    if a_rev:
        rev = repo.scm.resolve_rev(a_rev)
        old = _collect_experiment_commit(repo, rev, param_deps=param_deps)
    else:
        old = _collect_experiment_commit(repo, "HEAD", param_deps=param_deps)

    if b_rev:
        rev = repo.scm.resolve_rev(b_rev)
        new = _collect_experiment_commit(repo, rev, param_deps=param_deps)
    else:
        new = _collect_experiment_commit(repo,
                                         "workspace",
                                         param_deps=param_deps)

    with_unchanged = kwargs.pop("all", False)

    return {
        key: _diff(
            format_dict(old[key]),
            format_dict(new[key]),
            with_unchanged=with_unchanged,
        )
        for key in ["metrics", "params"]
    }
Esempio n. 4
0
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
    with_unchanged = kwargs.pop("all", False)

    old = _get_params(repo, *args, **kwargs, rev=(a_rev or "HEAD"))
    new = _get_params(repo, *args, **kwargs, rev=b_rev)

    return _diff(format_dict(old),
                 format_dict(new),
                 with_unchanged=with_unchanged)
Esempio n. 5
0
def _show_metrics(
    metrics,
    markdown=False,
    all_branches=False,
    all_tags=False,
    all_commits=False,
    precision=None,
):
    from dvc.utils.diff import format_dict, table
    from dvc.utils.flatten import flatten

    # When `metrics` contains a `None` key, it means that some files
    # specified as `targets` in `repo.metrics.show` didn't contain any metrics.
    missing = metrics.pop(None, None)
    with_rev = any([all_branches, all_tags, all_commits])
    header_set = set()
    rows = []

    if precision is None:
        precision = DEFAULT_PRECISION

    def _round(val):
        if isinstance(val, float):
            return round(val, precision)
        return val

    for _branch, val in metrics.items():
        for _fname, metric in val.items():
            if not isinstance(metric, dict):
                header_set.add("")
                continue
            for key, _val in flatten(format_dict(metric)).items():
                header_set.add(key)
    header = sorted(header_set)
    for branch, val in metrics.items():
        for fname, metric in val.items():
            row = []
            if with_rev:
                row.append(branch)
            row.append(fname)
            if not isinstance(metric, dict):
                row.append(str(metric))
                rows.append(row)
                continue
            flattened_val = flatten(format_dict(metric))

            for i in header:
                row.append(_round(flattened_val.get(i)))
            rows.append(row)
    header.insert(0, "Path")
    if with_rev:
        header.insert(0, "Revision")

    if missing:
        raise BadMetricError(missing)
    return table(header, rows, markdown)
Esempio n. 6
0
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
    if repo.scm.no_commits:
        return {}

    with_unchanged = kwargs.pop("all", False)

    old = _get_metrics(repo, *args, **kwargs, rev=(a_rev or "HEAD"))
    new = _get_metrics(repo, *args, **kwargs, rev=b_rev)

    return _diff(
        format_dict(old), format_dict(new), with_unchanged=with_unchanged
    )
Esempio n. 7
0
def _show_metrics(metrics,
                  all_branches=False,
                  all_tags=False,
                  all_commits=False):
    from dvc.utils.diff import format_dict
    from dvc.utils.flatten import flatten

    # When `metrics` contains a `None` key, it means that some files
    # specified as `targets` in `repo.metrics.show` didn't contain any metrics.
    missing = metrics.pop(None, None)

    lines = []
    for branch, val in metrics.items():
        if all_branches or all_tags or all_commits:
            lines.append(f"{branch}:")

        for fname, metric in val.items():
            if not isinstance(metric, dict):
                lines.append("\t{}: {}".format(fname, str(metric)))
                continue

            lines.append(f"\t{fname}:")
            for key, value in flatten(format_dict(metric)).items():
                lines.append(f"\t\t{key}: {value}")

    if missing:
        raise BadMetricError(missing)

    return "\n".join(lines)
Esempio n. 8
0
def metrics_table(
    metrics,
    all_branches: bool = False,
    all_tags: bool = False,
    all_commits: bool = False,
    precision: int = None,
    round_digits: bool = False,
):
    from dvc.utils.diff import format_dict
    from dvc.utils.flatten import flatten

    td = TabularData(["Revision", "Path"], fill_value="-")

    for branch, val in metrics.items():
        for fname, metric in val.get("data", {}).items():
            row_data: Dict[str, str] = {"Revision": branch, "Path": fname}
            metric = metric.get("data", {})
            flattened = (flatten(format_dict(metric)) if isinstance(
                metric, dict) else {
                    "": metric
                })
            row_data.update({
                k: _format_field(v, precision, round_digits)
                for k, v in flattened.items()
            })
            td.row_from_dict(row_data)

    rev, path, *metrics_headers = td.keys()
    td.project(rev, path, *sorted(metrics_headers))

    if not any([all_branches, all_tags, all_commits]):
        td.drop("Revision")

    return td
Esempio n. 9
0
def show_metrics(metrics,
                 all_branches=False,
                 all_tags=False,
                 all_commits=False):
    from flatten_json import flatten
    from dvc.utils.diff import format_dict

    # When `metrics` contains a `None` key, it means that some files
    # specified as `targets` in `repo.metrics.show` didn't contain any metrics.
    missing = metrics.pop(None, None)

    for branch, val in metrics.items():
        if all_branches or all_tags or all_commits:
            logger.info("{branch}:".format(branch=branch))

        for fname, metric in val.items():
            if not isinstance(metric, dict):
                logger.info("\t{}: {}".format(fname, str(metric)))
                continue

            logger.info("\t{}:".format(fname))
            for key, value in flatten(format_dict(metric), ".").items():
                logger.info("\t\t{}: {}".format(key, value))

    if missing:
        raise BadMetricError(missing)
Esempio n. 10
0
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
    if repo.scm.no_commits:
        return {}

    with_unchanged = kwargs.pop("all", False)

    a_rev = a_rev or "HEAD"
    b_rev = b_rev or "workspace"

    params = _get_params(repo, *args, **kwargs, revs=[a_rev, b_rev])
    old = params.get(a_rev, {})
    new = params.get(b_rev, {})

    return _diff(format_dict(old),
                 format_dict(new),
                 with_unchanged=with_unchanged)
Esempio n. 11
0
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
    if repo.scm.no_commits:
        return {}

    with_unchanged = kwargs.pop("all", False)

    a_rev = a_rev or "HEAD"
    a_rev = fix_exp_head(repo.scm, a_rev)
    b_rev = fix_exp_head(repo.scm, b_rev) or "workspace"

    metrics = _get_metrics(repo, *args, **kwargs, revs=[a_rev, b_rev])
    old = metrics.get(a_rev, {})
    new = metrics.get(b_rev, {})

    return _diff(format_dict(old),
                 format_dict(new),
                 with_unchanged=with_unchanged)