Skip to content

Commit

Permalink
minor changes to smooth template (#112)
Browse files Browse the repository at this point in the history
* minor changes to smooth template

* add  to loess transform groupby

* add back simple template

* add filename and field to loess groupby

* fix tests
  • Loading branch information
dberenbaum authored Feb 1, 2023
1 parent 011bdb6 commit d11af41
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
77 changes: 50 additions & 27 deletions src/dvc_render/vega_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ class BarHorizontalSortedTemplate(Template):
"sort": "-x",
},
"yOffset": {"field": "rev"},
"color": {"field": "rev", "type": "nominal"},
"color": {
"field": "rev",
"type": "nominal",
"legend": {"orient": "top", "direction": "vertical"},
},
},
}

Expand Down Expand Up @@ -141,7 +145,11 @@ class BarHorizontalTemplate(Template):
"title": Template.anchor("y_label"),
},
"yOffset": {"field": "rev"},
"color": {"field": "rev", "type": "nominal"},
"color": {
"field": "rev",
"type": "nominal",
"legend": {"orient": "top", "direction": "vertical"},
},
},
}

Expand Down Expand Up @@ -393,7 +401,11 @@ class ScatterTemplate(Template):
"title": Template.anchor("y_label"),
"scale": {"zero": False},
},
"color": {"field": "rev", "type": "nominal"},
"color": {
"field": "rev",
"type": "nominal",
"legend": {"orient": "top", "direction": "vertical"},
},
},
"layer": [
{"mark": "point"},
Expand Down Expand Up @@ -473,25 +485,18 @@ class SmoothLinearTemplate(Template):
"params": [
{
"name": "smooth",
"value": 0.2,
"value": 0.001,
"bind": {
"input": "range",
"min": 0.001,
"max": 1,
"step": 0.01,
"step": 0.001,
},
},
],
"transform": [
{
"loess": Template.anchor("y"),
"on": Template.anchor("x"),
"groupby": ["rev", "filename"],
"bandwidth": {"signal": "smooth"},
}
],
"layer": [
{
"mark": "line",
"encoding": {
"x": {
"field": Template.anchor("x"),
Expand All @@ -510,23 +515,46 @@ class SmoothLinearTemplate(Template):
"legend": {"orient": "top", "direction": "vertical"},
},
},
"layer": [
"transform": [
{
"mark": {
"type": "line",
"point": True,
"tooltip": {"content": "data"},
}
}
"loess": Template.anchor("y"),
"on": Template.anchor("x"),
"groupby": ["rev", "filename", "field", "filename::field"],
"bandwidth": {"signal": "smooth"},
},
],
}
},
{
"mark": {
"type": "point",
"tooltip": {"content": "data"},
},
"encoding": {
"x": {
"field": Template.anchor("x"),
"type": "quantitative",
"title": Template.anchor("x_label"),
},
"y": {
"field": Template.anchor("y"),
"type": "quantitative",
"title": Template.anchor("y_label"),
"scale": {"zero": False},
},
"color": {"field": "rev", "type": "nominal"},
},
},
],
}


class LinearTemplate(Template):
class LinearTemplate(SmoothLinearTemplate):
DEFAULT_NAME = "linear"


class SimpleLinearTemplate(Template):
DEFAULT_NAME = "simple"

DEFAULT_CONTENT = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"values": Template.anchor("data")},
Expand All @@ -535,7 +563,6 @@ class LinearTemplate(Template):
"height": 300,
"mark": {
"type": "line",
"point": True,
"tooltip": {"content": "data"},
},
"encoding": {
Expand All @@ -559,10 +586,6 @@ class LinearTemplate(Template):
}


class SimpleLinearTemplate(LinearTemplate):
DEFAULT_NAME = "simple"


TEMPLATES = [
SimpleLinearTemplate,
LinearTemplate,
Expand Down
11 changes: 6 additions & 5 deletions tests/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ def test_default_template_mark():

plot_content = json.loads(VegaRenderer(datapoints, "foo").partial_html())

assert plot_content["mark"] == {
"type": "line",
"point": True,
assert plot_content["layer"][0]["mark"] == "line"

assert plot_content["layer"][1]["mark"] == {
"type": "point",
"tooltip": {"content": "data"},
}

Expand All @@ -73,8 +74,8 @@ def test_choose_axes():
"second_val": 300,
},
]
assert plot_content["encoding"]["x"]["field"] == "first_val"
assert plot_content["encoding"]["y"]["field"] == "second_val"
assert plot_content["layer"][0]["encoding"]["x"]["field"] == "first_val"
assert plot_content["layer"][0]["encoding"]["y"]["field"] == "second_val"


def test_confusion():
Expand Down

0 comments on commit d11af41

Please sign in to comment.