Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Empty string as template in CustomPlot.init #841

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions src/dvclive/plots/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def __init__(
) -> None:
super().__init__(name, output_folder)
self.name = self.name.replace(".json", "")
if template is not None and template.strip() == "":
template = None
Copy link
Member

Choose a reason for hiding this comment

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

I think we should just disallow empty string.

Copy link
Member

Choose a reason for hiding this comment

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

In any case, this template.strip() is unnecessary.

Suggested change
if template is not None and template.strip() == "":
template = None
if not template:
template = 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.

@skshetry Thanks. This is much cleaner.


config = {
"template": template,
"x": x,
Expand Down
54 changes: 54 additions & 0 deletions tests/plots/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,57 @@ def test_log_custom_plot_multi_y(tmp_dir):
"x_label": "x_label",
"y_label": "y_label",
}


def test_log_custom_plot_with_template_as_empty_string(tmp_dir):
live = Live()
out = tmp_dir / live.plots_dir / CustomPlot.subfolder

datapoints = [{"x": 1, "y": 2}, {"x": 3, "y": 4}]
live.log_plot(
"custom_linear",
datapoints,
x="x",
y="y",
template="",
title="custom_title",
x_label="x_label",
y_label="y_label",
)

assert json.loads((out / "custom_linear.json").read_text()) == datapoints
# 'template' should not be in plot_config. Default template will be assigned later.
assert live._plots["custom_linear"].plot_config == {
"title": "custom_title",
"x": "x",
"y": "y",
"x_label": "x_label",
"y_label": "y_label",
}


def test_default_template_in_log_custom_plot(tmp_dir):
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's worth it to add this test, has very less return.

We can't test for every combinations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since template = '' caused a cryptic error, I added this test as a guardrail to ensure that any template = None is handled explicitly.
But on a second thought, I think you are correct. This test is for log_plot() which has default value for template. CustomPlot.__init__() doesn't have a default value for template but that shouldn't be test here.
Will rectify.

live = Live()
out = tmp_dir / live.plots_dir / CustomPlot.subfolder

datapoints = [{"x": 1, "y": 2}, {"x": 3, "y": 4}]
live.log_plot(
"custom_linear",
datapoints,
x="x",
y="y",
title="custom_title",
x_label="x_label",
y_label="y_label",
)

assert json.loads((out / "custom_linear.json").read_text()) == datapoints

assert live._plots["custom_linear"].plot_config == {
"template": "linear",
"title": "custom_title",
"x": "x",
"y": "y",
"x_label": "x_label",
"y_label": "y_label",
}