From 554c6efbe09781558afc4cf8e2a401a33473d707 Mon Sep 17 00:00:00 2001 From: Anunaya Srivastava Date: Wed, 4 Sep 2024 18:01:29 +0530 Subject: [PATCH 1/5] Fix: Empty string as template in CustomPlot.init Fixed dvc issue #10482 at https://github.com/iterative/dvc/issues/10482 --- src/dvclive/plots/custom.py | 3 +++ tests/plots/test_custom.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/dvclive/plots/custom.py b/src/dvclive/plots/custom.py index 0ea1527..431bf74 100644 --- a/src/dvclive/plots/custom.py +++ b/src/dvclive/plots/custom.py @@ -23,6 +23,9 @@ def __init__( ) -> None: super().__init__(name, output_folder) self.name = self.name.replace(".json", "") + if template.strip() == "": + template = None + config = { "template": template, "x": x, diff --git a/tests/plots/test_custom.py b/tests/plots/test_custom.py index 349c726..c87d662 100644 --- a/tests/plots/test_custom.py +++ b/tests/plots/test_custom.py @@ -56,3 +56,30 @@ 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", + } From 41997a61bfecf67f8a03d40ddb5f3663b4f5b804 Mon Sep 17 00:00:00 2001 From: Anunaya Srivastava Date: Thu, 5 Sep 2024 12:49:11 +0530 Subject: [PATCH 2/5] Fix None template in Custom Plot constructor --- src/dvclive/plots/custom.py | 2 +- tests/plots/test_custom.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/dvclive/plots/custom.py b/src/dvclive/plots/custom.py index 431bf74..e4b1228 100644 --- a/src/dvclive/plots/custom.py +++ b/src/dvclive/plots/custom.py @@ -23,7 +23,7 @@ def __init__( ) -> None: super().__init__(name, output_folder) self.name = self.name.replace(".json", "") - if template.strip() == "": + if template is not None and template.strip() == "": template = None config = { diff --git a/tests/plots/test_custom.py b/tests/plots/test_custom.py index c87d662..295c6d2 100644 --- a/tests/plots/test_custom.py +++ b/tests/plots/test_custom.py @@ -83,3 +83,30 @@ def test_log_custom_plot_with_template_as_empty_string(tmp_dir): "x_label": "x_label", "y_label": "y_label", } + + +def test_default_template_in_log_custom_plot(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", + 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", + } From 9ccab9eb5f331b66b307fd879e41833c31bf0c77 Mon Sep 17 00:00:00 2001 From: Anunaya Srivastava Date: Thu, 5 Sep 2024 12:59:58 +0530 Subject: [PATCH 3/5] Update src/dvclive/plots/custom.py Co-authored-by: skshetry <18718008+skshetry@users.noreply.github.com> --- src/dvclive/plots/custom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dvclive/plots/custom.py b/src/dvclive/plots/custom.py index e4b1228..2d7ecd3 100644 --- a/src/dvclive/plots/custom.py +++ b/src/dvclive/plots/custom.py @@ -23,7 +23,7 @@ def __init__( ) -> None: super().__init__(name, output_folder) self.name = self.name.replace(".json", "") - if template is not None and template.strip() == "": + if not template: template = None config = { From 374f69ca9631de23873b859d9030d5694745bc29 Mon Sep 17 00:00:00 2001 From: Anunaya Srivastava Date: Thu, 5 Sep 2024 13:59:47 +0530 Subject: [PATCH 4/5] Remove unnecesary tests in plots/test_custom.py --- tests/plots/test_custom.py | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/tests/plots/test_custom.py b/tests/plots/test_custom.py index 295c6d2..a367851 100644 --- a/tests/plots/test_custom.py +++ b/tests/plots/test_custom.py @@ -84,29 +84,3 @@ def test_log_custom_plot_with_template_as_empty_string(tmp_dir): "y_label": "y_label", } - -def test_default_template_in_log_custom_plot(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", - 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", - } From bf8ff740c6ce29a7b510173cf615f1138a3d618d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Sep 2024 08:30:18 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/plots/test_custom.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/plots/test_custom.py b/tests/plots/test_custom.py index a367851..c87d662 100644 --- a/tests/plots/test_custom.py +++ b/tests/plots/test_custom.py @@ -83,4 +83,3 @@ def test_log_custom_plot_with_template_as_empty_string(tmp_dir): "x_label": "x_label", "y_label": "y_label", } -