-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_parallel_coordinates.py
182 lines (140 loc) · 4.74 KB
/
test_parallel_coordinates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import json
from dvc_render.html import render_html
from dvc_render.plotly import ParallelCoordinatesRenderer
# pylint: disable=C0116, W1514, W0212, R0903
def expected_format(result):
assert "data" in result
assert "layout" in result
assert isinstance(result["data"], list)
assert result["data"][0]["type"] == "parcoords"
assert isinstance(result["data"][0]["dimensions"], list)
return True
def test_scalar_columns():
datapoints = [
{"col-1": "0.1", "col-2": "1", "col-3": ""},
{"col-1": "2", "col-2": "0.2", "col-3": "0"},
]
renderer = ParallelCoordinatesRenderer(datapoints)
result = json.loads(renderer.partial_html())
assert expected_format(result)
assert result["data"][0]["dimensions"][0] == {
"label": "col-1",
"values": [0.1, 2.0],
}
assert result["data"][0]["dimensions"][1] == {
"label": "col-2",
"values": [1.0, 0.2],
}
assert result["data"][0]["dimensions"][2] == {
"label": "col-3",
"values": [None, 0],
}
def test_categorical_columns():
datapoints = [
{"col-1": "foo", "col-2": ""},
{"col-1": "bar", "col-2": "foobar"},
{"col-1": "foo", "col-2": ""},
]
renderer = ParallelCoordinatesRenderer(datapoints)
result = json.loads(renderer.partial_html())
assert expected_format(result)
assert result["data"][0]["dimensions"][0] == {
"label": "col-1",
"values": [1, 0, 1],
"tickvals": [1, 0, 1],
"ticktext": ["foo", "bar", "foo"],
}
assert result["data"][0]["dimensions"][1] == {
"label": "col-2",
"values": [1, 0, 1],
"tickvals": [1, 0, 1],
"ticktext": ["Missing", "foobar", "Missing"],
}
def test_mixed_columns():
datapoints = [
{"categorical": "foo", "scalar": "0.1"},
{"categorical": "bar", "scalar": "2"},
]
renderer = ParallelCoordinatesRenderer(datapoints)
result = json.loads(renderer.partial_html())
assert expected_format(result)
assert result["data"][0]["dimensions"][0] == {
"label": "categorical",
"values": [1, 0],
"tickvals": [1, 0],
"ticktext": ["foo", "bar"],
}
assert result["data"][0]["dimensions"][1] == {
"label": "scalar",
"values": [0.1, 2.0],
}
def test_color_by_scalar():
datapoints = [
{"categorical": "foo", "scalar": "0.1"},
{"categorical": "bar", "scalar": "2"},
]
renderer = ParallelCoordinatesRenderer(datapoints, color_by="scalar")
result = json.loads(renderer.partial_html())
assert expected_format(result)
assert result["data"][0]["line"] == {
"color": [0.1, 2.0],
"showscale": True,
"colorbar": {"title": "scalar"},
}
def test_color_by_categorical():
datapoints = [
{"categorical": "foo", "scalar": "0.1"},
{"categorical": "bar", "scalar": "2"},
]
renderer = ParallelCoordinatesRenderer(datapoints, color_by="categorical")
result = json.loads(renderer.partial_html())
assert expected_format(result)
assert result["data"][0]["line"] == {
"color": [1, 0],
"showscale": True,
"colorbar": {
"title": "categorical",
"tickmode": "array",
"tickvals": [1, 0],
"ticktext": ["foo", "bar"],
},
}
def test_write_parallel_coordinates(tmp_path):
datapoints = [
{"categorical": "foo", "scalar": "0.1"},
{"categorical": "bar", "scalar": "2"},
]
renderer = ParallelCoordinatesRenderer(datapoints)
html_path = render_html(renderers=[renderer], output_file=tmp_path / "index.html")
html_text = html_path.read_text()
assert ParallelCoordinatesRenderer.SCRIPTS in html_text
div = ParallelCoordinatesRenderer.DIV.format(
id="pcp", partial=renderer.partial_html()
)
assert div in html_text
def test_fill_value():
datapoints = [
{"categorical": "foo", "scalar": "-"},
{"categorical": "-", "scalar": "2"},
]
renderer = ParallelCoordinatesRenderer(datapoints, fill_value="-")
result = json.loads(renderer.partial_html())
assert expected_format(result)
assert result["data"][0]["dimensions"][0] == {
"label": "categorical",
"values": [0, 1],
"tickvals": [0, 1],
"ticktext": ["foo", "Missing"],
}
assert result["data"][0]["dimensions"][1] == {
"label": "scalar",
"values": [None, 2.0],
}
def test_str_cast():
class Foo:
def __str__(self):
return "1"
datapoints = [{"foo": Foo()}]
renderer = ParallelCoordinatesRenderer(datapoints, fill_value="-")
result = renderer._get_plotly_data()
assert result["data"][0]["dimensions"][0]["values"] == [1.0]