|
1 | 1 | import builtins
|
2 | 2 | import os
|
3 | 3 | from time import sleep
|
4 |
| -from typing import List, Optional |
| 4 | +from typing import Dict, List, Optional |
| 5 | + |
| 6 | +from rich.text import Text |
5 | 7 |
|
6 | 8 | from dvc.env import DVC_CHECKPOINT, DVC_ROOT
|
7 | 9 | from dvc.repo import Repo
|
| 10 | +from dvc.repo.experiments.show import tabulate |
8 | 11 | from dvc.stage.monitor import CheckpointTask
|
9 | 12 |
|
10 | 13 |
|
@@ -69,3 +72,99 @@ def exp_save(
|
69 | 72 | return repo.experiments.save(
|
70 | 73 | name=name, force=force, include_untracked=include_untracked
|
71 | 74 | )
|
| 75 | + |
| 76 | + |
| 77 | +def _postprocess(exp_rows): |
| 78 | + for exp_row in exp_rows: |
| 79 | + for k, v in exp_row.items(): |
| 80 | + if isinstance(v, Text): |
| 81 | + v_str = str(v) |
| 82 | + try: |
| 83 | + exp_row[k] = float(v_str) |
| 84 | + except ValueError: |
| 85 | + exp_row[k] = v_str |
| 86 | + return exp_rows |
| 87 | + |
| 88 | + |
| 89 | +def exp_show( # noqa: PLR0913 |
| 90 | + repo: Optional[str] = None, |
| 91 | + all_branches: bool = False, |
| 92 | + all_tags: bool = False, |
| 93 | + all_commits: bool = False, |
| 94 | + rev: Optional[str] = None, |
| 95 | + num: int = 1, |
| 96 | + hide_queued: bool = False, |
| 97 | + hide_failed: bool = False, |
| 98 | + sha: bool = False, |
| 99 | + param_deps: bool = False, |
| 100 | + force: bool = False, |
| 101 | +) -> List[Dict]: |
| 102 | + """Get DVC experiments tracked in `repo`. |
| 103 | +
|
| 104 | + Without arguments, this function will retrieve all experiments derived from |
| 105 | + the Git `HEAD`. |
| 106 | +
|
| 107 | + See the options below to customize the experiments retrieved. |
| 108 | +
|
| 109 | + Args: |
| 110 | + repo (str, optional): location of the DVC repository. |
| 111 | + Defaults to the current project (found by walking up from the |
| 112 | + current working directory tree). |
| 113 | + It can be a URL or a file system path. |
| 114 | + Both HTTP and SSH protocols are supported for online Git repos |
| 115 | + (e.g. [user@]server:project.git). |
| 116 | + all_branches (bool, optional): get experiments derived from all Git |
| 117 | + branches, as well as from the last commit (`HEAD`). |
| 118 | + Defaults to `False`. |
| 119 | + all_tags (bool, optional): get experiments derived from all Git tags, |
| 120 | + as well as from the last commit (`HEAD`). |
| 121 | + Defaults to `False`. |
| 122 | + all_commits (bool, optional): get experiments derived from all Git |
| 123 | + commits, as well as from the last commit (HEAD). |
| 124 | + rev (str, optional): Git revision (e.g. branch, tag, SHA commit) to |
| 125 | + use as a reference point to start listing experiments. |
| 126 | + Defaults to `HEAD`. |
| 127 | + num (int, optional): show experiments from the last `num` commits |
| 128 | + (first parents) starting from the --rev baseline. |
| 129 | + Give a negative value to include all first-parent commits (similar |
| 130 | + to `git log -n`). |
| 131 | + Defaults to 1. |
| 132 | + hide_queued (bool, optional): hide experiments that are queued for |
| 133 | + execution. |
| 134 | + Defaults to `False`. |
| 135 | + hide_failed (bool, optional): hide experiments that have failed. |
| 136 | + sha (bool, optional): show the Git commit SHAs of the experiments |
| 137 | + instead of branch, tag, or experiment names. |
| 138 | + Defaults to `False`. |
| 139 | + param_deps (bool, optional): include only parameters that are stage |
| 140 | + dependencies. |
| 141 | + Defaults to `False`. |
| 142 | + force (bool, optional): force re-collection of experiments instead of |
| 143 | + loading from internal experiments cache. |
| 144 | + DVC caches `exp_show` data for completed experiments to improve |
| 145 | + performance of subsequent calls. |
| 146 | + When `force` is specified, DVC will reload all experiment data and |
| 147 | + ignore any previously cached results. |
| 148 | + Defaults to `False`. |
| 149 | +
|
| 150 | + Returns: |
| 151 | + List[Dict]: Each item in the list will contain a dictionary with |
| 152 | + the info for an individual experiment. |
| 153 | + See Examples below. |
| 154 | + """ |
| 155 | + with Repo.open(repo) as _repo: |
| 156 | + experiments = _repo.experiments.show( |
| 157 | + all_branches=all_branches, |
| 158 | + all_tags=all_tags, |
| 159 | + all_commits=all_commits, |
| 160 | + hide_queued=hide_queued, |
| 161 | + hide_failed=hide_failed, |
| 162 | + revs=rev, |
| 163 | + num=num, |
| 164 | + sha_only=sha, |
| 165 | + param_deps=param_deps, |
| 166 | + force=force, |
| 167 | + ) |
| 168 | + td, _ = tabulate(experiments, fill_value=None) |
| 169 | + |
| 170 | + return _postprocess(td.as_dict()) |
0 commit comments