Skip to content

Commit e9fa62f

Browse files
committed
add constants and use in tests
1 parent 86ae6c5 commit e9fa62f

File tree

2 files changed

+71
-63
lines changed

2 files changed

+71
-63
lines changed

src/dvc_render/vega.py

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,48 @@
1717
FILENAME_FIELD = [FILENAME, FIELD]
1818
CONCAT_FIELDS = FIELD_SEPARATOR.join(FILENAME_FIELD)
1919

20+
SPLIT_ANCHORS = [
21+
"color",
22+
"data",
23+
"plot_height",
24+
"plot_width",
25+
"shape",
26+
"stroke_dash",
27+
"title",
28+
"tooltip",
29+
"x_label",
30+
"y_label",
31+
"zoom_and_pan",
32+
]
33+
OPTIONAL_ANCHORS = [
34+
"color",
35+
"column",
36+
"group_by_x",
37+
"group_by_y",
38+
"group_by",
39+
"pivot_field",
40+
"plot_height",
41+
"plot_width",
42+
"row",
43+
"shape",
44+
"stroke_dash",
45+
"tooltip",
46+
"zoom_and_pan",
47+
]
48+
OPTIONAL_ANCHOR_RANGES: Dict[str, Union[List[str], List[List[int]]]] = {
49+
"stroke_dash": [[1, 0], [8, 8], [8, 4], [4, 4], [4, 2], [2, 1], [1, 1]],
50+
"color": [
51+
"#945dd6",
52+
"#13adc7",
53+
"#f46837",
54+
"#48bb78",
55+
"#4299e1",
56+
"#ed8936",
57+
"#f56565",
58+
],
59+
"shape": ["circle", "square", "triangle", "diamond"],
60+
}
61+
2062

2163
class VegaRenderer(Renderer):
2264
"""Renderer for vega plots."""
@@ -46,25 +88,6 @@ def __init__(self, datapoints: List, name: str, **properties):
4688
self.properties.get("template", None),
4789
self.properties.get("template_dir", None),
4890
)
49-
self._optional_anchor_ranges: Dict[
50-
str,
51-
Union[
52-
List[str],
53-
List[List[int]],
54-
],
55-
] = {
56-
"stroke_dash": [[1, 0], [8, 8], [8, 4], [4, 4], [4, 2], [2, 1], [1, 1]],
57-
"color": [
58-
"#945dd6",
59-
"#13adc7",
60-
"#f46837",
61-
"#48bb78",
62-
"#4299e1",
63-
"#ed8936",
64-
"#f56565",
65-
],
66-
"shape": ["circle", "square", "triangle", "diamond"],
67-
}
6891

6992
self._split_content: Dict[str, str] = {}
7093

@@ -126,19 +149,7 @@ def get_partial_filled_template(self):
126149
Returns a partially filled template along with the split out anchor content
127150
"""
128151
content = self.get_filled_template(
129-
split_anchors=[
130-
"color",
131-
"data",
132-
"plot_height",
133-
"plot_width",
134-
"shape",
135-
"stroke_dash",
136-
"title",
137-
"tooltip",
138-
"x_label",
139-
"y_label",
140-
"zoom_and_pan",
141-
],
152+
split_anchors=SPLIT_ANCHORS,
142153
strict=True,
143154
)
144155
return content, {"anchor_definitions": self._split_content}
@@ -206,23 +217,7 @@ def get_revs(self):
206217

207218
def _process_optional_anchors(self, split_anchors: List[str]):
208219
optional_anchors = [
209-
anchor
210-
for anchor in [
211-
"color",
212-
"column",
213-
"group_by_x",
214-
"group_by_y",
215-
"group_by",
216-
"pivot_field",
217-
"plot_height",
218-
"plot_width",
219-
"row",
220-
"shape",
221-
"stroke_dash",
222-
"tooltip",
223-
"zoom_and_pan",
224-
]
225-
if self.template.has_anchor(anchor)
220+
anchor for anchor in OPTIONAL_ANCHORS if self.template.has_anchor(anchor)
226221
]
227222
if not optional_anchors:
228223
return None
@@ -443,7 +438,7 @@ def _get_optional_anchor_mapping(
443438
name: str,
444439
domain: List[str],
445440
):
446-
full_range_values: List[Any] = self._optional_anchor_ranges.get(name, [])
441+
full_range_values: List[Any] = OPTIONAL_ANCHOR_RANGES.get(name, [])
447442
anchor_range_values = full_range_values.copy()
448443

449444
anchor_range = []
@@ -454,6 +449,7 @@ def _get_optional_anchor_mapping(
454449
anchor_range.append(range_value)
455450

456451
legend = (
452+
# fix stroke dash and shape legend entry appearance (use empty shapes)
457453
{"legend": {"symbolFillColor": "transparent", "symbolStrokeColor": "grey"}}
458454
if name != "color"
459455
else {}

tests/test_vega.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from dvc_render.vega import BadTemplateError, VegaRenderer
6+
from dvc_render.vega import OPTIONAL_ANCHOR_RANGES, BadTemplateError, VegaRenderer
77
from dvc_render.vega_templates import NoFieldInDataError, Template
88

99
# pylint: disable=missing-function-docstring, C1803, C0302
@@ -339,7 +339,10 @@ def test_fill_anchor_in_string(tmp_dir):
339339
["rev", "acc", "step", "filename"],
340340
{
341341
"field": "filename",
342-
"scale": {"domain": ["test", "train"], "range": [[1, 0], [8, 8]]},
342+
"scale": {
343+
"domain": ["test", "train"],
344+
"range": OPTIONAL_ANCHOR_RANGES["stroke_dash"][0:2],
345+
},
343346
"legend": {
344347
"symbolFillColor": "transparent",
345348
"symbolStrokeColor": "grey",
@@ -388,7 +391,10 @@ def test_fill_anchor_in_string(tmp_dir):
388391
["rev", "dvc_inferred_y_value", "step", "field"],
389392
{
390393
"field": "field",
391-
"scale": {"domain": ["acc", "acc_norm"], "range": [[1, 0], [8, 8]]},
394+
"scale": {
395+
"domain": ["acc", "acc_norm"],
396+
"range": OPTIONAL_ANCHOR_RANGES["stroke_dash"][0:2],
397+
},
392398
"legend": {
393399
"symbolFillColor": "transparent",
394400
"symbolStrokeColor": "grey",
@@ -454,7 +460,7 @@ def test_fill_anchor_in_string(tmp_dir):
454460
"field": "filename::field",
455461
"scale": {
456462
"domain": ["test::acc", "test::acc_norm", "train::acc"],
457-
"range": [[1, 0], [8, 8], [8, 4]],
463+
"range": OPTIONAL_ANCHOR_RANGES["stroke_dash"][0:3],
458464
},
459465
"legend": {
460466
"symbolFillColor": "transparent",
@@ -492,7 +498,7 @@ def test_optional_anchors_linear(
492498
assert plot_content["data"]["values"] == expected_datapoints
493499
assert plot_content["encoding"]["color"] == {
494500
"field": "rev",
495-
"scale": {"domain": ["B"], "range": ["#945dd6"]},
501+
"scale": {"domain": ["B"], "range": OPTIONAL_ANCHOR_RANGES["color"][0:1]},
496502
}
497503
assert plot_content["encoding"]["strokeDash"] == stroke_dash_encoding
498504
assert plot_content["layer"][3]["transform"][0]["calculate"] == pivot_field
@@ -763,7 +769,7 @@ def test_optional_anchors_confusion(
763769
},
764770
"scale": {
765771
"domain": ["test", "train"],
766-
"range": ["circle", "square"],
772+
"range": OPTIONAL_ANCHOR_RANGES["shape"][0:2],
767773
},
768774
},
769775
[
@@ -831,7 +837,7 @@ def test_optional_anchors_confusion(
831837
},
832838
"scale": {
833839
"domain": ["test_acc", "train_acc"],
834-
"range": ["circle", "square"],
840+
"range": OPTIONAL_ANCHOR_RANGES["shape"][0:2],
835841
},
836842
},
837843
[
@@ -895,7 +901,7 @@ def test_optional_anchors_confusion(
895901
},
896902
"scale": {
897903
"domain": ["test::test_acc", "train::train_acc"],
898-
"range": ["circle", "square"],
904+
"range": OPTIONAL_ANCHOR_RANGES["shape"][0:2],
899905
},
900906
},
901907
[
@@ -932,7 +938,7 @@ def test_optional_anchors_scatter(
932938
assert plot_content["data"]["values"] == expected_datapoints
933939
assert plot_content["encoding"]["color"] == {
934940
"field": "rev",
935-
"scale": {"domain": ["B", "C"], "range": ["#945dd6", "#13adc7"]},
941+
"scale": {"domain": ["B", "C"], "range": OPTIONAL_ANCHOR_RANGES["color"][0:2]},
936942
}
937943
assert plot_content["encoding"]["shape"] == shape_encoding
938944
assert plot_content["encoding"]["tooltip"] == tooltip_encoding
@@ -996,7 +1002,10 @@ def test_optional_anchors_scatter(
9961002
["rev", "acc", "step", "field"],
9971003
{
9981004
"field": "filename",
999-
"scale": {"domain": ["test", "train"], "range": [[1, 0], [8, 8]]},
1005+
"scale": {
1006+
"domain": ["test", "train"],
1007+
"range": OPTIONAL_ANCHOR_RANGES["stroke_dash"][0:2],
1008+
},
10001009
"legend": {
10011010
"symbolFillColor": "transparent",
10021011
"symbolStrokeColor": "grey",
@@ -1029,7 +1038,10 @@ def test_optional_anchors_scatter(
10291038
["rev", "dvc_inferred_y_value", "step", "field"],
10301039
{
10311040
"field": "field",
1032-
"scale": {"domain": ["acc", "acc_norm"], "range": [[1, 0], [8, 8]]},
1041+
"scale": {
1042+
"domain": ["acc", "acc_norm"],
1043+
"range": OPTIONAL_ANCHOR_RANGES["stroke_dash"][0:2],
1044+
},
10331045
"legend": {
10341046
"symbolFillColor": "transparent",
10351047
"symbolStrokeColor": "grey",
@@ -1072,7 +1084,7 @@ def test_optional_anchors_scatter(
10721084
"field": "filename::field",
10731085
"scale": {
10741086
"domain": ["test::acc", "test::acc_norm", "train::acc"],
1075-
"range": [[1, 0], [8, 8], [8, 4]],
1087+
"range": OPTIONAL_ANCHOR_RANGES["stroke_dash"][0:3],
10761088
},
10771089
"legend": {
10781090
"symbolFillColor": "transparent",
@@ -1103,7 +1115,7 @@ def test_partial_filled_template(
11031115
expected_split = {
11041116
Template.anchor("color"): {
11051117
"field": "rev",
1106-
"scale": {"domain": ["B"], "range": ["#945dd6"]},
1118+
"scale": {"domain": ["B"], "range": OPTIONAL_ANCHOR_RANGES["color"][0:1]},
11071119
},
11081120
Template.anchor("data"): _get_expected_datapoints(datapoints, expected_dp_keys),
11091121
Template.anchor("plot_height"): 300,

0 commit comments

Comments
 (0)