forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_show.py
411 lines (333 loc) · 11.9 KB
/
test_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import os
import pytest
from dvc.cli import main
from dvc.dvcfile import PIPELINE_FILE
from dvc.exceptions import OverlappingOutputPathsError
from dvc.repo import Repo
from dvc.repo.plots import PlotMetricTypeError
from dvc.utils import onerror_collect
from dvc.utils.fs import remove
from dvc.utils.serialize import EncodingError, YAMLFileCorruptedError
from tests.utils.plots import get_plot
def test_show_targets(tmp_dir, dvc):
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
(tmp_dir / "metric.json").dump_json(metric, sort_keys=True)
plots = dvc.plots.show(targets=["metric.json"])
assert get_plot(plots, "workspace", file="metric.json") == metric
plots = dvc.plots.show(targets=(tmp_dir / "metric.json").fs_path)
assert get_plot(plots, "workspace", file="metric.json") == metric
def test_plot_cache_missing(tmp_dir, scm, dvc, caplog, run_copy_metrics):
metric1 = [{"y": 2}, {"y": 3}]
(tmp_dir / "metric_t.json").dump_json(metric1, sort_keys=True)
run_copy_metrics(
"metric_t.json",
"metric.json",
plots=["metric.json"],
commit="there is metric",
)
scm.tag("v1")
# Make a different plot and then remove its datafile
metric2 = [{"y": 3}, {"y": 4}]
(tmp_dir / "metric_t.json").dump_json(metric2, sort_keys=True)
stage = run_copy_metrics(
"metric_t.json",
"metric.json",
plots=["metric.json"],
commit="there is an another metric",
)
scm.tag("v2")
remove(stage.outs[0].fspath)
remove(stage.outs[0].cache_path)
plots_data = dvc.plots.show(revs=["v1", "v2"], targets=["metric.json"])
assert get_plot(plots_data, "v1", file="metric.json") == metric1
assert isinstance(
get_plot(plots_data, "v2", file="metric.json", endkey="error"),
FileNotFoundError,
)
def test_plot_wrong_metric_type(tmp_dir, scm, dvc, run_copy_metrics):
tmp_dir.gen("metric_t.txt", "some text")
run_copy_metrics(
"metric_t.txt",
"metric.txt",
plots_no_cache=["metric.txt"],
commit="add text metric",
)
result = dvc.plots.show(targets=["metric.txt"], onerror=onerror_collect)
assert isinstance(
get_plot(result, "workspace", file="metric.txt", endkey="error"),
PlotMetricTypeError,
)
@pytest.mark.parametrize("use_dvc", [True, False])
def test_show_non_plot(tmp_dir, scm, use_dvc):
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
(tmp_dir / "metric.json").dump_json(metric, sort_keys=True)
if use_dvc:
dvc = Repo.init()
else:
dvc = Repo(uninitialized=True)
plots = dvc.plots.show(targets=["metric.json"])
assert get_plot(plots, "workspace", file="metric.json") == metric
def test_show_non_plot_and_plot_with_params(
tmp_dir, scm, dvc, run_copy_metrics
):
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
(tmp_dir / "metric.json").dump_json(metric, sort_keys=True)
run_copy_metrics(
"metric.json",
"metric2.json",
plots_no_cache=["metric2.json"],
name="train",
)
props = {"title": "TITLE"}
dvc.plots.modify("metric2.json", props=props)
result = dvc.plots.show(targets=["metric.json", "metric2.json"])
assert get_plot(result, "workspace", file="metric.json") == metric
assert get_plot(result, "workspace", file="metric2.json") == metric
assert (
get_plot(result, "workspace", file="metric2.json", endkey="props")
== props
)
def test_show_from_subdir(tmp_dir, dvc, capsys):
subdir = tmp_dir / "subdir"
subdir.mkdir()
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
(subdir / "metric.json").dump_json(metric, sort_keys=True)
with subdir.chdir():
assert main(["plots", "show", "metric.json"]) == 0
out, _ = capsys.readouterr()
assert subdir.as_uri() in out
assert (subdir / "dvc_plots").is_dir()
assert (subdir / "dvc_plots" / "index.html").is_file()
def test_plots_show_non_existing(tmp_dir, dvc, caplog):
result = dvc.plots.show(targets=["plot.json"])
assert isinstance(
get_plot(result, "workspace", file="plot.json", endkey="error"),
FileNotFoundError,
)
assert "'plot.json' was not found in current workspace." in caplog.text
@pytest.mark.parametrize("clear_before_run", [True, False])
def test_plots_show_overlap(tmp_dir, dvc, run_copy_metrics, clear_before_run):
data_dir = tmp_dir / "data"
data_dir.mkdir()
(data_dir / "m1_temp.yaml").dump({"a": {"b": {"c": 2, "d": 1}}})
run_copy_metrics(
str(data_dir / "m1_temp.yaml"),
str(data_dir / "m1.yaml"),
single_stage=False,
commit="add m1",
name="cp-m1",
plots=[str(data_dir / "m1.yaml")],
)
with (tmp_dir / "dvc.yaml").modify() as d:
# trying to make an output overlaps error
d["stages"]["corrupted-stage"] = {
"cmd": "mkdir data",
"outs": ["data"],
}
# running by clearing and not clearing stuffs
# so as it works even for optimized cases
if clear_before_run:
remove(data_dir)
remove(dvc.odb.local.cache_dir)
dvc._reset()
result = dvc.plots.show(onerror=onerror_collect)
assert isinstance(
get_plot(result, "workspace", endkey="error"),
OverlappingOutputPathsError,
)
def test_dir_plots(tmp_dir, dvc, run_copy_metrics):
subdir = tmp_dir / "subdir"
subdir.mkdir()
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
fname = "file.json"
(tmp_dir / fname).dump_json(metric, sort_keys=True)
p1 = "subdir/p1.json"
p2 = "subdir/p2.json"
tmp_dir.dvc.run(
cmd=(
f"mkdir subdir && python copy.py {fname} {p1} && "
f"python copy.py {fname} {p2}"
),
deps=[fname],
single_stage=False,
plots=["subdir"],
name="copy_double",
)
props = {"title": "TITLE"}
dvc.plots.modify("subdir", props)
result = dvc.plots.show()
assert set(get_plot(result, "workspace")) == {p1, p2}
assert get_plot(result, "workspace", typ="definitions", file="") == {
p1: props,
p2: props,
}
def test_ignore_parsing_error(tmp_dir, dvc, run_copy_metrics):
with open("file", "wb", encoding=None) as fobj:
fobj.write(b"\xc1")
run_copy_metrics("file", "plot_file.json", plots=["plot_file.json"])
result = dvc.plots.show(onerror=onerror_collect)
assert isinstance(
get_plot(result, "workspace", file="plot_file.json", endkey="error"),
EncodingError,
)
@pytest.mark.parametrize(
"file,path_kwargs",
(
(PIPELINE_FILE, {"revision": "workspace", "endkey": "error"}),
(
"plot.yaml",
{"revision": "workspace", "file": "plot.yaml", "endkey": "error"},
),
),
)
def test_log_errors(
tmp_dir, scm, dvc, run_copy_metrics, file, path_kwargs, capsys
):
metric = [{"val": 2}, {"val": 3}]
(tmp_dir / "metric_t.yaml").dump(metric)
run_copy_metrics(
"metric_t.yaml",
"plot.yaml",
plots=["plot.yaml"],
single_stage=False,
name="train",
)
scm.tag("v1")
with open(file, "a", encoding="utf-8") as fd:
fd.write("\nMALFORMED!")
result = dvc.plots.show(onerror=onerror_collect)
_, error = capsys.readouterr()
assert isinstance(get_plot(result, **path_kwargs), YAMLFileCorruptedError)
assert (
"DVC failed to load some plots for following revisions: 'workspace'."
in error
)
def test_plots_binary(tmp_dir, scm, dvc, run_copy_metrics, custom_template):
with open("image.jpg", "wb") as fd:
fd.write(b"content")
dvc.add(["image.jpg"])
run_copy_metrics(
"image.jpg",
"plot.jpg",
commit="run training",
plots=["plot.jpg"],
name="s2",
single_stage=False,
)
scm.add(["dvc.yaml", "dvc.lock"])
scm.commit("initial")
scm.tag("v1")
with open("plot.jpg", "wb") as fd:
fd.write(b"content2")
result = dvc.plots.show(revs=["v1", "workspace"])
assert get_plot(result, "v1", file="plot.jpg") == b"content"
assert get_plot(result, "workspace", file="plot.jpg") == b"content2"
def test_collect_non_existing_dir(tmp_dir, dvc, run_copy_metrics):
subdir = tmp_dir / "subdir"
subdir.mkdir()
metric = [{"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}]
subdir_metric = [{"y": 101, "x": 3}, {"y": 202, "x": 4}]
pname = "source.json"
(tmp_dir / pname).dump_json(metric, sort_keys=True)
sname = "subdir_source.json"
(tmp_dir / sname).dump_json(subdir_metric, sort_keys=True)
p1 = os.path.join("subdir", "p1.json")
p2 = os.path.join("subdir", "p2.json")
subdir_stage = tmp_dir.dvc.run(
cmd=(
f"mkdir subdir && python copy.py {sname} {p1} && "
f"python copy.py {sname} {p2}"
),
deps=[sname],
single_stage=False,
plots=["subdir"],
name="copy_double",
)
run_copy_metrics(
pname,
"plot.json",
plots=["plot.json"],
commit="there is metric",
)
remove(subdir_stage.outs[0].cache_path)
remove(subdir_stage.outs[0].fs_path)
result = dvc.plots.show()
assert get_plot(
result, "workspace", typ="definitions", file="", endkey="error"
)
# make sure others gets loaded
assert get_plot(result, "workspace", file="plot.json") == metric
@pytest.mark.parametrize(
"plot_config,expected_datafiles",
[
# TODO - enable providing data files for x
# (
# {
# "comparison": {
# "x": {"data1.json": "a"},
# "y": {"sub/dir/data2.json": "b"},
# }
# },
# ["data1.json", os.path.join("sub", "dir", "data2.json")],
# ),
(
{"data1.json": {"x": "c", "y": "a", "title": "File as key test"}},
["data1.json"],
),
(
{
"infer_data_from_y": {
"x": "a",
"y": {"data1.json": "b", "sub/dir/data2.json": "c"},
}
},
["data1.json", os.path.join("sub", "dir", "data2.json")],
),
],
)
@pytest.mark.parametrize("separate_config", [True, False])
def test_load_from_config(
tmp_dir,
dvc,
plot_config,
expected_datafiles,
separate_config,
run_copy_metrics,
):
data = {
"data1.json": [
{"a": 1, "b": 0.1, "c": 0.01},
{"a": 2, "b": 0.2, "c": 0.02},
],
os.path.join("sub", "dir", "data.json"): [
{"a": 6, "b": 0.6, "c": 0.06},
{"a": 7, "b": 0.7, "c": 0.07},
],
}
for filename, content in data.items():
dirname = os.path.dirname(filename)
if dirname:
os.makedirs(dirname)
(tmp_dir / filename).dump_json(content, sort_keys=True)
config_files = None
if separate_config:
(tmp_dir / "plot_config.json").dump_json(plot_config, sort_keys=True)
config_file = "plot_config.json"
config_files = {config_file}
else:
# TODO we need that to create any stage, as dvc.yaml plots
# collections bases on existing stages - fix collection
run_copy_metrics("data1.json", "copy.json", name="train")
from dvc.utils.serialize import modify_yaml
config_file = "dvc.yaml"
with modify_yaml(config_file) as dvcfile_content:
dvcfile_content["plots"] = plot_config
result = dvc.plots.show(config_files=config_files)
assert plot_config == get_plot(
result, "workspace", typ="definitions", file=config_file
)
for filename, content in data.items():
if filename in expected_datafiles:
assert content == get_plot(result, "workspace", file=filename)
else:
assert filename not in get_plot(result, "workspace")