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

Add initial version of PlotlyRenderer. #88

Closed
wants to merge 6 commits into from
Closed
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
18 changes: 9 additions & 9 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ markdown =
%(table)s
matplotlib
docs =
mkdocs==1.3.1
mkdocs-gen-files==0.3.5
mkdocs-material==8.4.1
mkdocs-section-index==0.3.4
mkdocstrings-python==0.7.1
mkdocs==1.5.2
mkdocs-gen-files==0.5.0
mkdocs-material==9.2.2
mkdocs-section-index==0.3.5
mkdocstrings-python==1.5.0
tests =
%(table)s
%(markdown)s
funcy>=1.17
pytest==7.2.0
pytest-sugar>=0.9.6,<1.0
pytest-cov==3.0.0
pytest-mock==3.8.2
pytest==7.4.0
pytest-sugar==0.9.7
pytest-cov==4.1.0
pytest-mock==3.11.1
pylint==2.15.0
mypy==0.981
pytest-test-utils>=0.0.6
Expand Down
4 changes: 2 additions & 2 deletions src/dvc_render/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
from .html import render_html # noqa: F401
from .image import ImageRenderer
from .plotly import ParallelCoordinatesRenderer # noqa: F401
from .plotly import PlotlyRenderer # noqa: F401
from .vega import VegaRenderer
from .vega_templates import TEMPLATES # noqa: F401

RENDERERS = [ImageRenderer, VegaRenderer]
RENDERERS = [ImageRenderer, PlotlyRenderer, VegaRenderer]
102 changes: 36 additions & 66 deletions src/dvc_render/plotly.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import json
from typing import Any, Dict, Optional
from typing import List

from .base import Renderer
from .utils import list_dict_to_dict_list


class ParallelCoordinatesRenderer(Renderer):
"""
Renderer for Parallel Coordinates plot.

Using Plotly.
"""
class PlotlyRenderer(Renderer):
"""Renderer using plotly.js mimicking VegaRenderer"""

TYPE = "plotly"

Expand All @@ -30,63 +25,38 @@ class ParallelCoordinatesRenderer(Renderer):
"""

# pylint: disable=W0231
def __init__(
self,
datapoints,
name="pcp",
color_by: Optional[str] = None,
fill_value: str = "",
):
self.datapoints = datapoints
self.color_by = color_by
self.name = name
self.fill_value = fill_value
def __init__(self, datapoints: List, name: str, **properties):
super().__init__(datapoints, name, **properties)

def convert_datapoints(self, datapoints):
"""Convert from dvc-render format to plotly json format"""
traces = {}
for datapoint in datapoints:
revision = datapoint.get("dvc_data_version_info", {}).get(
"revision", "workspace"
)
if revision not in traces:
traces[revision] = {"name": revision, "x": [], "y": []}
for axis in ("x", "y"):
value = datapoint[self.properties[axis]]
traces[revision][axis].append(value)
traces = list(traces.values())

template = self.properties.pop("template", "linear")
for trace in traces:
trace["type"] = template

layout = {
"title": self.properties.get("title", ""),
"xaxis": {
"title": self.properties.get("x_label", self.properties.get("x"))
},
"yaxis": {
"title": self.properties.get("y_label", self.properties.get("y"))
},
}

return {"data": traces, "layout": layout}

def partial_html(self, **kwargs) -> str:
return json.dumps(self._get_plotly_data())

def _get_plotly_data(self):
tabular_dict = list_dict_to_dict_list(self.datapoints)

trace: Dict[str, Any] = {"type": "parcoords", "dimensions": []}
for label, values in tabular_dict.items():
values = list(map(str, values))
is_categorical = False
try:
float_values = [
float(x) if x != self.fill_value else None for x in values
]
except ValueError:
is_categorical = True

if is_categorical:
non_missing = [x for x in values if x != self.fill_value]
unique_values = sorted(set(non_missing))
unique_values.append(self.fill_value)

dummy_values = [unique_values.index(x) for x in values]

values = [x if x != self.fill_value else "Missing" for x in values]
trace["dimensions"].append(
{
"label": label,
"values": dummy_values,
"tickvals": dummy_values,
"ticktext": values,
}
)
else:
trace["dimensions"].append({"label": label, "values": float_values})

if label == self.color_by:
trace["line"] = {
"color": dummy_values if is_categorical else float_values,
"showscale": True,
"colorbar": {"title": self.color_by},
}
if is_categorical:
trace["line"]["colorbar"]["tickmode"] = "array"
trace["line"]["colorbar"]["tickvals"] = dummy_values
trace["line"]["colorbar"]["ticktext"] = values

return {"data": [trace], "layout": {}}
return json.dumps(self.convert_datapoints(self.datapoints))
182 changes: 0 additions & 182 deletions tests/test_parallel_coordinates.py

This file was deleted.

Loading