Skip to content

Commit 19a9c51

Browse files
committed
standardise plot data output
1 parent 784a5b5 commit 19a9c51

File tree

8 files changed

+140
-239
lines changed

8 files changed

+140
-239
lines changed

dvc/commands/plots.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,6 @@ def _show_json(
5959
ui.write_json(compact({"errors": all_errors, "data": data}), highlight=False)
6060

6161

62-
def _adjust_vega_renderers(renderers):
63-
from dvc.render import REVISION_FIELD, VERSION_FIELD
64-
from dvc_render import VegaRenderer
65-
66-
for r in renderers:
67-
if isinstance(r, VegaRenderer):
68-
if _data_versions_count(r) > 1:
69-
summary = _summarize_version_infos(r)
70-
for dp in r.datapoints:
71-
vi = dp.pop(VERSION_FIELD, {})
72-
keys = list(vi.keys())
73-
for key in keys:
74-
if not (len(summary.get(key, set())) > 1):
75-
vi.pop(key)
76-
if vi:
77-
dp["rev"] = "::".join(vi.values())
78-
else:
79-
for dp in r.datapoints:
80-
dp[REVISION_FIELD] = dp[VERSION_FIELD]["revision"]
81-
dp.pop(VERSION_FIELD, {})
82-
83-
84-
def _summarize_version_infos(renderer):
85-
from collections import defaultdict
86-
87-
from dvc.render import VERSION_FIELD
88-
89-
result = defaultdict(set)
90-
91-
for dp in renderer.datapoints:
92-
for key, value in dp.get(VERSION_FIELD, {}).items():
93-
result[key].add(value)
94-
return dict(result)
95-
96-
97-
def _data_versions_count(renderer):
98-
from itertools import product
99-
100-
summary = _summarize_version_infos(renderer)
101-
x = product(summary.get("filename", {None}), summary.get("field", {None}))
102-
return len(set(x))
103-
104-
10562
class CmdPlots(CmdBase):
10663
def _func(self, *args, **kwargs):
10764
raise NotImplementedError
@@ -175,7 +132,6 @@ def run(self) -> int: # noqa: C901, PLR0911, PLR0912
175132
return 0
176133

177134
renderers = [r.renderer for r in renderers_with_errors]
178-
_adjust_vega_renderers(renderers)
179135
if self.args.show_vega:
180136
renderer = first(filter(lambda r: r.TYPE == "vega", renderers))
181137
if renderer:

dvc/render/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
INDEX_FIELD = "step"
22
REVISION_FIELD = "rev"
33
FILENAME_FIELD = "filename"
4-
VERSION_FIELD = "dvc_data_version_info"
4+
FIELD_FIELD = "field"
55
REVISIONS_KEY = "revisions"
66
TYPE_KEY = "type"
77
SRC_FIELD = "src"

dvc/render/convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import defaultdict
22
from typing import Dict, List, Union
33

4-
from dvc.render import REVISION_FIELD, REVISIONS_KEY, SRC_FIELD, TYPE_KEY, VERSION_FIELD
4+
from dvc.render import REVISION_FIELD, REVISIONS_KEY, SRC_FIELD, TYPE_KEY
55
from dvc.render.converter.image import ImageConverter
66
from dvc.render.converter.vega import VegaConverter
77

@@ -22,7 +22,7 @@ def _get_converter(
2222
def _group_by_rev(datapoints):
2323
grouped = defaultdict(list)
2424
for datapoint in datapoints:
25-
rev = datapoint.get(VERSION_FIELD, {}).get("revision")
25+
rev = datapoint.get(REVISION_FIELD)
2626
grouped[rev].append(datapoint)
2727
return dict(grouped)
2828

dvc/render/converter/vega.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from funcy import first, last
55

66
from dvc.exceptions import DvcException
7-
from dvc.render import FILENAME_FIELD, INDEX_FIELD, VERSION_FIELD
7+
from dvc.render import FIELD_FIELD, FILENAME_FIELD, INDEX_FIELD, REVISION_FIELD
88

99
from . import Converter
1010

@@ -274,11 +274,9 @@ def flat_datapoints(self, revision): # noqa: C901, PLR0912
274274
_update_all(
275275
datapoints,
276276
update_dict={
277-
VERSION_FIELD: {
278-
"revision": revision,
279-
FILENAME_FIELD: y_file_short,
280-
"field": y_field,
281-
}
277+
REVISION_FIELD: revision,
278+
FILENAME_FIELD: y_file_short,
279+
FIELD_FIELD: y_field,
282280
},
283281
)
284282

tests/integration/plots/test_plots.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from funcy import first
1212

1313
from dvc.cli import main
14-
from dvc.render import REVISION_FIELD, VERSION_FIELD
14+
from dvc.render import REVISION_FIELD
1515

1616
JSON_OUT = "vis_data"
1717

@@ -193,35 +193,35 @@ def test_repo_with_plots(tmp_dir, scm, dvc, capsys, run_copy_metrics, repo_with_
193193
] == _update_datapoints(
194194
linear_v1,
195195
{
196-
VERSION_FIELD: {
197-
"revision": "workspace",
198-
"filename": "linear.json",
199-
"field": "y",
200-
},
196+
"rev": "workspace",
197+
"filename": "linear.json",
198+
"field": "y",
201199
},
202200
)
203201
assert html_result["linear.json"]["data"]["values"] == _update_datapoints(
204202
linear_v1,
205203
{
206204
REVISION_FIELD: "workspace",
205+
"filename": "linear.json",
206+
"field": "y",
207207
},
208208
)
209209
assert json_data["confusion.json"][0]["content"]["data"][
210210
"values"
211211
] == _update_datapoints(
212212
confusion_v1,
213213
{
214-
VERSION_FIELD: {
215-
"revision": "workspace",
216-
"filename": "confusion.json",
217-
"field": "actual",
218-
},
214+
"rev": "workspace",
215+
"filename": "confusion.json",
216+
"field": "actual",
219217
},
220218
)
221219
assert html_result["confusion.json"]["data"]["values"] == _update_datapoints(
222220
confusion_v1,
223221
{
224-
REVISION_FIELD: "workspace",
222+
"rev": "workspace",
223+
"filename": "confusion.json",
224+
"field": "actual",
225225
},
226226
)
227227
verify_image(tmp_dir, "workspace", "image.png", image_v1, html_path, json_data)
@@ -277,63 +277,63 @@ def test_repo_with_plots(tmp_dir, scm, dvc, capsys, run_copy_metrics, repo_with_
277277
] == _update_datapoints(
278278
linear_v2,
279279
{
280-
VERSION_FIELD: {
281-
"revision": "workspace",
282-
"filename": "../linear.json",
283-
"field": "y",
284-
},
280+
"rev": "workspace",
281+
"filename": "../linear.json",
282+
"field": "y",
285283
},
286284
) + _update_datapoints(
287285
linear_v1,
288286
{
289-
VERSION_FIELD: {
290-
"revision": "HEAD",
291-
"filename": "../linear.json",
292-
"field": "y",
293-
},
287+
"rev": "HEAD",
288+
"filename": "../linear.json",
289+
"field": "y",
294290
},
295291
)
296292
assert html_result["../linear.json"]["data"]["values"] == _update_datapoints(
297293
linear_v2,
298294
{
299295
REVISION_FIELD: "workspace",
296+
"filename": "../linear.json",
297+
"field": "y",
300298
},
301299
) + _update_datapoints(
302300
linear_v1,
303301
{
304302
REVISION_FIELD: "HEAD",
303+
"filename": "../linear.json",
304+
"field": "y",
305305
},
306306
)
307307
assert json_data["../confusion.json"][0]["content"]["data"][
308308
"values"
309309
] == _update_datapoints(
310310
confusion_v2,
311311
{
312-
VERSION_FIELD: {
313-
"revision": "workspace",
314-
"filename": "../confusion.json",
315-
"field": "actual",
316-
},
312+
"rev": "workspace",
313+
"filename": "../confusion.json",
314+
"field": "actual",
317315
},
318316
) + _update_datapoints(
319317
confusion_v1,
320318
{
321-
VERSION_FIELD: {
322-
"revision": "HEAD",
323-
"filename": "../confusion.json",
324-
"field": "actual",
325-
},
319+
"rev": "HEAD",
320+
"filename": "../confusion.json",
321+
"field": "actual",
326322
},
327323
)
328324
assert html_result["../confusion.json"]["data"]["values"] == _update_datapoints(
329325
confusion_v2,
330326
{
331327
REVISION_FIELD: "workspace",
328+
"filename": "../confusion.json",
329+
"field": "actual",
332330
},
333331
) + _update_datapoints(
334332
confusion_v1,
335333
{
336334
REVISION_FIELD: "HEAD",
335+
"filename": "../confusion.json",
336+
"field": "actual",
337337
},
338338
)
339339

@@ -434,7 +434,7 @@ def test_repo_with_config_plots(tmp_dir, capsys, repo_with_config_plots):
434434
repo_state = repo_with_config_plots()
435435
plots = next(repo_state)
436436

437-
html_path, _, __ = call(capsys)
437+
html_path, json_result, __ = call(capsys)
438438

439439
assert os.path.exists(html_path)
440440
html_result = extract_vega_specs(
@@ -444,15 +444,20 @@ def test_repo_with_config_plots(tmp_dir, capsys, repo_with_config_plots):
444444
"confusion_train_vs_test",
445445
],
446446
)
447+
447448
ble = _update_datapoints(
448449
plots["data"]["linear_train.json"],
449450
{
450-
REVISION_FIELD: "linear_train.json",
451+
"rev": "workspace",
452+
"filename": "linear_train.json",
453+
"field": "y",
451454
},
452455
) + _update_datapoints(
453456
plots["data"]["linear_test.json"],
454457
{
455-
REVISION_FIELD: "linear_test.json",
458+
"rev": "workspace",
459+
"filename": "linear_test.json",
460+
"field": "y",
456461
},
457462
)
458463

tests/unit/render/test_convert.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dvc.render import REVISION_FIELD, REVISIONS_KEY, SRC_FIELD, TYPE_KEY, VERSION_FIELD
1+
from dvc.render import REVISION_FIELD, REVISIONS_KEY, SRC_FIELD, TYPE_KEY
22
from dvc.render.convert import to_json
33

44

@@ -10,13 +10,13 @@ def test_to_json_vega(mocker):
1010
{
1111
"x": 1,
1212
"y": 2,
13-
VERSION_FIELD: {"revision": "foo"},
13+
"rev": "foo",
1414
"filename": "foo.json",
1515
},
1616
{
1717
"x": 2,
1818
"y": 1,
19-
VERSION_FIELD: {"revision": "bar"},
19+
"rev": "bar",
2020
"filename": "foo.json",
2121
},
2222
]
@@ -31,15 +31,15 @@ def test_to_json_vega(mocker):
3131
"x": 1,
3232
"y": 2,
3333
"filename": "foo.json",
34-
VERSION_FIELD: {"revision": "foo"},
34+
"rev": "foo",
3535
},
3636
],
3737
"bar": [
3838
{
3939
"x": 2,
4040
"y": 1,
4141
"filename": "foo.json",
42-
VERSION_FIELD: {"revision": "bar"},
42+
"rev": "bar",
4343
},
4444
],
4545
},
@@ -55,13 +55,13 @@ def test_to_json_vega_split(mocker):
5555
{
5656
"x": 1,
5757
"y": 2,
58-
VERSION_FIELD: {"revision": "foo"},
58+
"rev": "foo",
5959
"filename": "foo.json",
6060
},
6161
{
6262
"x": 2,
6363
"y": 1,
64-
VERSION_FIELD: {"revision": "bar"},
64+
"rev": "bar",
6565
"filename": "foo.json",
6666
},
6767
]
@@ -76,15 +76,15 @@ def test_to_json_vega_split(mocker):
7676
"x": 1,
7777
"y": 2,
7878
"filename": "foo.json",
79-
VERSION_FIELD: {"revision": "foo"},
79+
"rev": "foo",
8080
}
8181
],
8282
"bar": [
8383
{
8484
"x": 2,
8585
"y": 1,
8686
"filename": "foo.json",
87-
VERSION_FIELD: {"revision": "bar"},
87+
"rev": "bar",
8888
}
8989
],
9090
},

tests/unit/render/test_match.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from funcy import set_in
33

4-
from dvc.render import VERSION_FIELD
54
from dvc.render.converter.vega import VegaConverter
65
from dvc.render.match import PlotsData, _squash_plots_properties, match_defs_renderers
76

@@ -158,20 +157,16 @@ def test_match_renderers(M):
158157
renderer = renderer_with_errors[0]
159158
assert renderer.datapoints == [
160159
{
161-
VERSION_FIELD: {
162-
"revision": "v1",
163-
"filename": "file.json",
164-
"field": "y",
165-
},
160+
"rev": "v1",
161+
"filename": "file.json",
162+
"field": "y",
166163
"x": 1,
167164
"y": 1,
168165
},
169166
{
170-
VERSION_FIELD: {
171-
"revision": "v1",
172-
"filename": "file.json",
173-
"field": "y",
174-
},
167+
"rev": "v1",
168+
"filename": "file.json",
169+
"field": "y",
175170
"x": 2,
176171
"y": 2,
177172
},

0 commit comments

Comments
 (0)