Skip to content

Commit 36970c0

Browse files
committed
api: Add exp_show.
Closes #8775 Closes #7167
1 parent b814531 commit 36970c0

File tree

5 files changed

+110
-10
lines changed

5 files changed

+110
-10
lines changed

dvc/api/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from .data import open # pylint: disable=redefined-builtin
44
from .data import get_url, read
5-
from .experiments import exp_save, make_checkpoint
5+
from .experiments import exp_save, exp_show, make_checkpoint
66
from .show import metrics_show, params_show
77

88
__all__ = [
99
"exp_save",
10+
"exp_show",
1011
"get_url",
1112
"make_checkpoint",
1213
"open",

dvc/api/experiments.py

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import builtins
22
import os
33
from time import sleep
4-
from typing import List, Optional
4+
from typing import Dict, List, Optional
5+
6+
from rich.text import Text
57

68
from dvc.env import DVC_CHECKPOINT, DVC_ROOT
79
from dvc.repo import Repo
10+
from dvc.repo.experiments.show import tabulate
811
from dvc.stage.monitor import CheckpointTask
912

1013

@@ -69,3 +72,99 @@ def exp_save(
6972
return repo.experiments.save(
7073
name=name, force=force, include_untracked=include_untracked
7174
)
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())

dvc/compare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def with_value(value, default):
3535

3636

3737
class TabularData(MutableSequence[Sequence["CellT"]]):
38-
def __init__(self, columns: Sequence[str], fill_value: str = ""):
38+
def __init__(self, columns: Sequence[str], fill_value: Optional[str] = ""):
3939
self._columns: Dict[str, Column] = {name: Column() for name in columns}
4040
self._keys: List[str] = list(columns)
4141
self._fill_value = fill_value

dvc/repo/experiments/show.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def show(
6262

6363
def tabulate(
6464
baseline_states: Iterable["ExpState"],
65-
fill_value: str = "-",
65+
fill_value: Optional[str] = "-",
6666
error_value: str = "!",
6767
**kwargs,
6868
) -> Tuple["TabularData", Dict[str, Iterable[str]]]:
@@ -124,7 +124,7 @@ def _build_rows(
124124
baseline_states: Iterable["ExpState"],
125125
*,
126126
all_headers: Iterable[str],
127-
fill_value: str,
127+
fill_value: Optional[str],
128128
sort_by: Optional[str] = None,
129129
sort_order: Optional[Literal["asc", "desc"]] = None,
130130
**kwargs,
@@ -234,7 +234,7 @@ def _exp_range_rows(
234234
exp_range: "ExpRange",
235235
*,
236236
all_headers: Iterable[str],
237-
fill_value: str,
237+
fill_value: Optional[str],
238238
is_base: bool = False,
239239
**kwargs,
240240
) -> Iterator[Tuple["CellT", ...]]:
@@ -276,7 +276,7 @@ def _data_cells(
276276
metrics_names: Mapping[str, Iterable[str]],
277277
params_names: Mapping[str, Iterable[str]],
278278
deps_names: Iterable[str],
279-
fill_value: str = "-",
279+
fill_value: Optional[str] = "-",
280280
error_value: str = "!",
281281
precision: Optional[int] = None,
282282
**kwargs,
@@ -317,10 +317,10 @@ def _d_cells(
317317

318318
def format_time(
319319
timestamp: Optional[datetime],
320-
fill_value: str = "-",
320+
fill_value: Optional[str] = "-",
321321
iso: bool = False,
322322
**kwargs,
323-
) -> str:
323+
) -> Optional[str]:
324324
if not timestamp:
325325
return fill_value
326326
if iso:

dvc/ui/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
SHOW_MAX_WIDTH = 1024
1515

1616

17-
CellT = Union[str, "RichText"] # RichText is mostly compatible with str
17+
CellT = Union[str, "RichText", None] # RichText is mostly compatible with str
1818
Row = Sequence[CellT]
1919
TableData = Sequence[Row]
2020
Headers = Sequence[str]

0 commit comments

Comments
 (0)