Skip to content

Commit

Permalink
add constants and use in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Dec 4, 2023
1 parent 86ae6c5 commit 2fbdafa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 62 deletions.
96 changes: 46 additions & 50 deletions src/dvc_render/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,48 @@
FILENAME_FIELD = [FILENAME, FIELD]
CONCAT_FIELDS = FIELD_SEPARATOR.join(FILENAME_FIELD)

SPLIT_ANCHORS = [
"color",
"data",
"plot_height",
"plot_width",
"shape",
"stroke_dash",
"title",
"tooltip",
"x_label",
"y_label",
"zoom_and_pan",
]
OPTIONAL_ANCHORS = [
"color",
"column",
"group_by_x",
"group_by_y",
"group_by",
"pivot_field",
"plot_height",
"plot_width",
"row",
"shape",
"stroke_dash",
"tooltip",
"zoom_and_pan",
]
OPTION_ANCHOR_RANGES = {
"stroke_dash": [[1, 0], [8, 8], [8, 4], [4, 4], [4, 2], [2, 1], [1, 1]],
"color": [
"#945dd6",
"#13adc7",
"#f46837",
"#48bb78",
"#4299e1",
"#ed8936",
"#f56565",
],
"shape": ["circle", "square", "triangle", "diamond"],
}


class VegaRenderer(Renderer):
"""Renderer for vega plots."""
Expand Down Expand Up @@ -46,25 +88,6 @@ def __init__(self, datapoints: List, name: str, **properties):
self.properties.get("template", None),
self.properties.get("template_dir", None),
)
self._optional_anchor_ranges: Dict[
str,
Union[
List[str],
List[List[int]],
],
] = {
"stroke_dash": [[1, 0], [8, 8], [8, 4], [4, 4], [4, 2], [2, 1], [1, 1]],
"color": [
"#945dd6",
"#13adc7",
"#f46837",
"#48bb78",
"#4299e1",
"#ed8936",
"#f56565",
],
"shape": ["circle", "square", "triangle", "diamond"],
}

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

Expand Down Expand Up @@ -126,19 +149,7 @@ def get_partial_filled_template(self):
Returns a partially filled template along with the split out anchor content
"""
content = self.get_filled_template(
split_anchors=[
"color",
"data",
"plot_height",
"plot_width",
"shape",
"stroke_dash",
"title",
"tooltip",
"x_label",
"y_label",
"zoom_and_pan",
],
split_anchors=SPLIT_ANCHORS,
strict=True,
)
return content, {"anchor_definitions": self._split_content}
Expand Down Expand Up @@ -206,23 +217,7 @@ def get_revs(self):

def _process_optional_anchors(self, split_anchors: List[str]):
optional_anchors = [
anchor
for anchor in [
"color",
"column",
"group_by_x",
"group_by_y",
"group_by",
"pivot_field",
"plot_height",
"plot_width",
"row",
"shape",
"stroke_dash",
"tooltip",
"zoom_and_pan",
]
if self.template.has_anchor(anchor)
anchor for anchor in OPTIONAL_ANCHORS if self.template.has_anchor(anchor)
]
if not optional_anchors:
return None
Expand Down Expand Up @@ -443,7 +438,7 @@ def _get_optional_anchor_mapping(
name: str,
domain: List[str],
):
full_range_values: List[Any] = self._optional_anchor_ranges.get(name, [])
full_range_values: List[Any] = OPTION_ANCHOR_RANGES.get(name, [])
anchor_range_values = full_range_values.copy()

anchor_range = []
Expand All @@ -454,6 +449,7 @@ def _get_optional_anchor_mapping(
anchor_range.append(range_value)

legend = (
# fix stroke dash and shape legend entry appearance (use empty shapes)
{"legend": {"symbolFillColor": "transparent", "symbolStrokeColor": "grey"}}
if name != "color"
else {}
Expand Down
37 changes: 25 additions & 12 deletions tests/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from dvc_render.vega import BadTemplateError, VegaRenderer
from dvc_render.vega_templates import NoFieldInDataError, Template
from src.dvc_render.vega import OPTION_ANCHOR_RANGES

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

Expand Down Expand Up @@ -339,7 +340,10 @@ def test_fill_anchor_in_string(tmp_dir):
["rev", "acc", "step", "filename"],
{
"field": "filename",
"scale": {"domain": ["test", "train"], "range": [[1, 0], [8, 8]]},
"scale": {
"domain": ["test", "train"],
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
},
"legend": {
"symbolFillColor": "transparent",
"symbolStrokeColor": "grey",
Expand Down Expand Up @@ -388,7 +392,10 @@ def test_fill_anchor_in_string(tmp_dir):
["rev", "dvc_inferred_y_value", "step", "field"],
{
"field": "field",
"scale": {"domain": ["acc", "acc_norm"], "range": [[1, 0], [8, 8]]},
"scale": {
"domain": ["acc", "acc_norm"],
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
},
"legend": {
"symbolFillColor": "transparent",
"symbolStrokeColor": "grey",
Expand Down Expand Up @@ -454,7 +461,7 @@ def test_fill_anchor_in_string(tmp_dir):
"field": "filename::field",
"scale": {
"domain": ["test::acc", "test::acc_norm", "train::acc"],
"range": [[1, 0], [8, 8], [8, 4]],
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:3],
},
"legend": {
"symbolFillColor": "transparent",
Expand Down Expand Up @@ -492,7 +499,7 @@ def test_optional_anchors_linear(
assert plot_content["data"]["values"] == expected_datapoints
assert plot_content["encoding"]["color"] == {
"field": "rev",
"scale": {"domain": ["B"], "range": ["#945dd6"]},
"scale": {"domain": ["B"], "range": OPTION_ANCHOR_RANGES["color"][0:1]},
}
assert plot_content["encoding"]["strokeDash"] == stroke_dash_encoding
assert plot_content["layer"][3]["transform"][0]["calculate"] == pivot_field
Expand Down Expand Up @@ -763,7 +770,7 @@ def test_optional_anchors_confusion(
},
"scale": {
"domain": ["test", "train"],
"range": ["circle", "square"],
"range": OPTION_ANCHOR_RANGES["shape"][0:2],
},
},
[
Expand Down Expand Up @@ -831,7 +838,7 @@ def test_optional_anchors_confusion(
},
"scale": {
"domain": ["test_acc", "train_acc"],
"range": ["circle", "square"],
"range": OPTION_ANCHOR_RANGES["shape"][0:2],
},
},
[
Expand Down Expand Up @@ -895,7 +902,7 @@ def test_optional_anchors_confusion(
},
"scale": {
"domain": ["test::test_acc", "train::train_acc"],
"range": ["circle", "square"],
"range": OPTION_ANCHOR_RANGES["shape"][0:2],
},
},
[
Expand Down Expand Up @@ -932,7 +939,7 @@ def test_optional_anchors_scatter(
assert plot_content["data"]["values"] == expected_datapoints
assert plot_content["encoding"]["color"] == {
"field": "rev",
"scale": {"domain": ["B", "C"], "range": ["#945dd6", "#13adc7"]},
"scale": {"domain": ["B", "C"], "range": OPTION_ANCHOR_RANGES["color"][0:2]},
}
assert plot_content["encoding"]["shape"] == shape_encoding
assert plot_content["encoding"]["tooltip"] == tooltip_encoding
Expand Down Expand Up @@ -996,7 +1003,10 @@ def test_optional_anchors_scatter(
["rev", "acc", "step", "field"],
{
"field": "filename",
"scale": {"domain": ["test", "train"], "range": [[1, 0], [8, 8]]},
"scale": {
"domain": ["test", "train"],
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
},
"legend": {
"symbolFillColor": "transparent",
"symbolStrokeColor": "grey",
Expand Down Expand Up @@ -1029,7 +1039,10 @@ def test_optional_anchors_scatter(
["rev", "dvc_inferred_y_value", "step", "field"],
{
"field": "field",
"scale": {"domain": ["acc", "acc_norm"], "range": [[1, 0], [8, 8]]},
"scale": {
"domain": ["acc", "acc_norm"],
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:2],
},
"legend": {
"symbolFillColor": "transparent",
"symbolStrokeColor": "grey",
Expand Down Expand Up @@ -1072,7 +1085,7 @@ def test_optional_anchors_scatter(
"field": "filename::field",
"scale": {
"domain": ["test::acc", "test::acc_norm", "train::acc"],
"range": [[1, 0], [8, 8], [8, 4]],
"range": OPTION_ANCHOR_RANGES["stroke_dash"][0:3],
},
"legend": {
"symbolFillColor": "transparent",
Expand Down Expand Up @@ -1103,7 +1116,7 @@ def test_partial_filled_template(
expected_split = {
Template.anchor("color"): {
"field": "rev",
"scale": {"domain": ["B"], "range": ["#945dd6"]},
"scale": {"domain": ["B"], "range": OPTION_ANCHOR_RANGES["color"][0:1]},
},
Template.anchor("data"): _get_expected_datapoints(datapoints, expected_dp_keys),
Template.anchor("plot_height"): 300,
Expand Down

0 comments on commit 2fbdafa

Please sign in to comment.