Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/dvc_render/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,23 @@ def generate_markdown(self, report_path=None) -> str:
data[x] = list(map(float, data[x]))
data[y] = list(map(float, data[y]))

plt.title(self.properties.get("title", Path(self.name).stem))
plt.xlabel(self.properties.get("x_label", x))
plt.ylabel(self.properties.get("y_label", y))
plt.plot(x, y, data=data)
plt.tight_layout()
plt.savefig(output_file)
plt.close()

if report_path:
return f"\n![{self.name}]({output_file.relative_to(report_folder)})"

base64_str = base64.b64encode(
output_file.getvalue() # type: ignore
).decode()
src = f"data:image/png;base64,{base64_str}"

return f"\n![{self.name}]({src})"
if x is not None and y is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] I came across the same issue in #142. The error only happens in the CI. Please LMK if you want me to split this into another PR.

plt.title(self.properties.get("title", Path(self.name).stem))
plt.xlabel(self.properties.get("x_label", x))
plt.ylabel(self.properties.get("y_label", y))
plt.plot(x, y, data=data)
plt.tight_layout()
plt.savefig(output_file)
plt.close()

if report_path:
return f"\n![{self.name}]({output_file.relative_to(report_folder)})"

base64_str = base64.b64encode(
output_file.getvalue() # type: ignore
).decode()
src = f"data:image/png;base64,{base64_str}"

return f"\n![{self.name}]({src})"

return ""
2 changes: 2 additions & 0 deletions src/dvc_render/vega_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def dict_replace_value(d: dict, name: str, value: Any) -> dict:
if v == name:
x[k] = value
continue
if name in v and isinstance(value, str):
v = v.replace(name, value)
x[k] = v
return x

Expand Down
38 changes: 38 additions & 0 deletions tests/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,41 @@ def test_escape_special_characters():
assert filled["encoding"]["x"]["title"] == "foo.bar[0]"
assert filled["encoding"]["y"]["field"] == "foo\\.bar\\[1\\]"
assert filled["encoding"]["y"]["title"] == "foo.bar[1]"


def test_fill_anchor_in_string(tmp_dir):
y = "lab"
x = "SR"
tmp_dir.gen(
"custom.json",
json.dumps(
{
"data": {"values": Template.anchor("data")},
"transform": [
{"joinaggregate": [{"op": "mean", "field": "lab", "as": "mean_y"}]},
{
"calculate": "pow("
+ "datum.<DVC_METRIC_Y> - datum.<DVC_METRIC_X>,2"
+ ")",
"as": "SR",
},
{"joinaggregate": [{"op": "sum", "field": "SR", "as": "SSR"}]},
],
"encoding": {
"x": {"field": Template.anchor("x")},
"y": {"field": Template.anchor("y")},
},
},
),
)
datapoints = [
{x: "B", y: "A"},
{x: "A", y: "A"},
]
props = {"template": "custom.json", "x": x, "y": y}

renderer = VegaRenderer(datapoints, "foo", **props)
filled = renderer.get_filled_template(as_string=False)
assert filled["transform"][1]["calculate"] == "pow(datum.lab - datum.SR,2)"
assert filled["encoding"]["x"]["field"] == x
assert filled["encoding"]["y"]["field"] == y