Skip to content

Commit

Permalink
TabularData: Add to_parallel_coordinates.
Browse files Browse the repository at this point in the history
Uses `ParallelCoordinatesRenderer` and `dvc.render.html.write`.

pre-requisite #4455
  • Loading branch information
daavoo committed Dec 1, 2021
1 parent b1b7d39 commit e878e36
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions dvc/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ def to_csv(self) -> str:
writer.writerow(row)
return buff.getvalue()

def to_parallel_coordinates(self, output_path, color_by):
from dvc.render.html import write
from dvc.render.plotly import ParallelCoordinatesRenderer

index_path = write(
output_path,
renderers=[ParallelCoordinatesRenderer(self, color_by)],
)
return index_path.as_uri()

def add_column(self, name: str) -> None:
self._columns[name] = Column([self._fill_value] * len(self))
self._keys.append(name)
Expand All @@ -173,6 +183,14 @@ def render(self, **kwargs: Any):

if kwargs.pop("csv", False):
ui.write(self.to_csv(), end="")

elif kwargs.pop("html", False):
ui.write(
self.to_parallel_coordinates(
kwargs["output_path"], kwargs.get("color_by")
)
)

else:
ui.table(self, headers=self.keys(), **kwargs)

Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_tabular_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,21 @@ def test_drop_duplicates_invalid_axis():

with pytest.raises(ValueError, match="Invalid 'axis' value foo."):
td.drop_duplicates("foo")


def test_to_parallel_coordinates(tmp_dir, mocker):
(tmp_dir / "foo").mkdir()
td = TabularData(["categorical", "scalar"])
td.extend([["foo", "0.1"], ["bar", "2"]])

write = mocker.patch("dvc.render.html.write")
renderer_class = mocker.patch(
"dvc.render.plotly.ParallelCoordinatesRenderer"
)
renderer = renderer_class.return_value

td.render(html=True, output_path="foo")

renderer_class.assert_called_with(td, None)

write.assert_called_with("foo", renderers=[renderer])

0 comments on commit e878e36

Please sign in to comment.