Skip to content

Commit

Permalink
plots: find metric data in yaml dictionary (iterative#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
pared authored Jun 9, 2020
1 parent 593e673 commit 5683338
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dvc/repo/plots/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def construct_mapping(loader, node):

return yaml.load(self.content, OrderedLoader)

def _processors(self):
parent_processors = super()._processors()
return [_find_data] + parent_processors


def _load_from_revision(repo, datafile, revision):
from dvc.repo.tree import RepoTree
Expand Down
28 changes: 28 additions & 0 deletions tests/func/plots/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from funcy import first

from dvc.repo.plots.data import (
JSONPlotData,
NoMetricInHistoryError,
PlotData,
PlotMetricTypeError,
YAMLPlotData,
)
from dvc.repo.plots.template import (
NoDataForTemplateError,
Expand Down Expand Up @@ -581,3 +583,29 @@ def test_raise_on_wrong_field(tmp_dir, scm, dvc, run_copy_metrics):

with pytest.raises(NoFieldInDataError):
dvc.plots.show("metric.json", props={"y": "no_val"})


def test_load_metric_from_dict_json(tmp_dir):
metric = [{"acccuracy": 1, "loss": 2}, {"accuracy": 3, "loss": 4}]
dmetric = {"train": metric}

plot_data = JSONPlotData("-", "revision", json.dumps(dmetric))

expected = metric
for d in expected:
d["rev"] = "revision"

assert list(map(dict, plot_data.to_datapoints())) == expected


def test_load_metric_from_dict_yaml(tmp_dir):
metric = [{"acccuracy": 1, "loss": 2}, {"accuracy": 3, "loss": 4}]
dmetric = {"train": metric}

plot_data = YAMLPlotData("-", "revision", yaml.dump(dmetric))

expected = metric
for d in expected:
d["rev"] = "revision"

assert list(map(dict, plot_data.to_datapoints())) == expected

0 comments on commit 5683338

Please sign in to comment.