Skip to content

Commit 2fbdafa

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

File tree

2 files changed

+71
-62
lines changed

2 files changed

+71
-62
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+
OPTION_ANCHOR_RANGES = {
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] = OPTION_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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from dvc_render.vega import BadTemplateError, VegaRenderer
77
from dvc_render.vega_templates import NoFieldInDataError, Template
8+
from src.dvc_render.vega import OPTION_ANCHOR_RANGES
89

910
# pylint: disable=missing-function-docstring, C1803, C0302
1011

@@ -339,7 +340,10 @@ def test_fill_anchor_in_string(tmp_dir):
339340
["rev", "acc", "step", "filename"],
340341
{
341342
"field": "filename",
342-
"scale": {"domain": ["test", "train"], "range": [[1, 0], [8, 8]]},
343+
"scale": {
344+
"domain": ["test", "train"],
345+
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
346+
},
343347
"legend": {
344348
"symbolFillColor": "transparent",
345349
"symbolStrokeColor": "grey",
@@ -388,7 +392,10 @@ def test_fill_anchor_in_string(tmp_dir):
388392
["rev", "dvc_inferred_y_value", "step", "field"],
389393
{
390394
"field": "field",
391-
"scale": {"domain": ["acc", "acc_norm"], "range": [[1, 0], [8, 8]]},
395+
"scale": {
396+
"domain": ["acc", "acc_norm"],
397+
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
398+
},
392399
"legend": {
393400
"symbolFillColor": "transparent",
394401
"symbolStrokeColor": "grey",
@@ -454,7 +461,7 @@ def test_fill_anchor_in_string(tmp_dir):
454461
"field": "filename::field",
455462
"scale": {
456463
"domain": ["test::acc", "test::acc_norm", "train::acc"],
457-
"range": [[1, 0], [8, 8], [8, 4]],
464+
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:3],
458465
},
459466
"legend": {
460467
"symbolFillColor": "transparent",
@@ -492,7 +499,7 @@ def test_optional_anchors_linear(
492499
assert plot_content["data"]["values"] == expected_datapoints
493500
assert plot_content["encoding"]["color"] == {
494501
"field": "rev",
495-
"scale": {"domain": ["B"], "range": ["#945dd6"]},
502+
"scale": {"domain": ["B"], "range": OPTION_ANCHOR_RANGES["color"][0:1]},
496503
}
497504
assert plot_content["encoding"]["strokeDash"] == stroke_dash_encoding
498505
assert plot_content["layer"][3]["transform"][0]["calculate"] == pivot_field
@@ -763,7 +770,7 @@ def test_optional_anchors_confusion(
763770
},
764771
"scale": {
765772
"domain": ["test", "train"],
766-
"range": ["circle", "square"],
773+
"range": OPTION_ANCHOR_RANGES["shape"][0:2],
767774
},
768775
},
769776
[
@@ -831,7 +838,7 @@ def test_optional_anchors_confusion(
831838
},
832839
"scale": {
833840
"domain": ["test_acc", "train_acc"],
834-
"range": ["circle", "square"],
841+
"range": OPTION_ANCHOR_RANGES["shape"][0:2],
835842
},
836843
},
837844
[
@@ -895,7 +902,7 @@ def test_optional_anchors_confusion(
895902
},
896903
"scale": {
897904
"domain": ["test::test_acc", "train::train_acc"],
898-
"range": ["circle", "square"],
905+
"range": OPTION_ANCHOR_RANGES["shape"][0:2],
899906
},
900907
},
901908
[
@@ -932,7 +939,7 @@ def test_optional_anchors_scatter(
932939
assert plot_content["data"]["values"] == expected_datapoints
933940
assert plot_content["encoding"]["color"] == {
934941
"field": "rev",
935-
"scale": {"domain": ["B", "C"], "range": ["#945dd6", "#13adc7"]},
942+
"scale": {"domain": ["B", "C"], "range": OPTION_ANCHOR_RANGES["color"][0:2]},
936943
}
937944
assert plot_content["encoding"]["shape"] == shape_encoding
938945
assert plot_content["encoding"]["tooltip"] == tooltip_encoding
@@ -996,7 +1003,10 @@ def test_optional_anchors_scatter(
9961003
["rev", "acc", "step", "field"],
9971004
{
9981005
"field": "filename",
999-
"scale": {"domain": ["test", "train"], "range": [[1, 0], [8, 8]]},
1006+
"scale": {
1007+
"domain": ["test", "train"],
1008+
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
1009+
},
10001010
"legend": {
10011011
"symbolFillColor": "transparent",
10021012
"symbolStrokeColor": "grey",
@@ -1029,7 +1039,10 @@ def test_optional_anchors_scatter(
10291039
["rev", "dvc_inferred_y_value", "step", "field"],
10301040
{
10311041
"field": "field",
1032-
"scale": {"domain": ["acc", "acc_norm"], "range": [[1, 0], [8, 8]]},
1042+
"scale": {
1043+
"domain": ["acc", "acc_norm"],
1044+
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
1045+
},
10331046
"legend": {
10341047
"symbolFillColor": "transparent",
10351048
"symbolStrokeColor": "grey",
@@ -1072,7 +1085,7 @@ def test_optional_anchors_scatter(
10721085
"field": "filename::field",
10731086
"scale": {
10741087
"domain": ["test::acc", "test::acc_norm", "train::acc"],
1075-
"range": [[1, 0], [8, 8], [8, 4]],
1088+
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:3],
10761089
},
10771090
"legend": {
10781091
"symbolFillColor": "transparent",
@@ -1103,7 +1116,7 @@ def test_partial_filled_template(
11031116
expected_split = {
11041117
Template.anchor("color"): {
11051118
"field": "rev",
1106-
"scale": {"domain": ["B"], "range": ["#945dd6"]},
1119+
"scale": {"domain": ["B"], "range": OPTION_ANCHOR_RANGES["color"][0:1]},
11071120
},
11081121
Template.anchor("data"): _get_expected_datapoints(datapoints, expected_dp_keys),
11091122
Template.anchor("plot_height"): 300,

0 commit comments

Comments
 (0)