-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathtest_shapeplots.py
More file actions
414 lines (334 loc) · 15.2 KB
/
Copy pathtest_shapeplots.py
File metadata and controls
414 lines (334 loc) · 15.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import numpy as np
from holoviews.element import (
Bounds,
Box,
HLine,
Path,
Rectangles,
Segments,
Tiles,
VLine,
)
from holoviews.plotting.plotly.util import PLOTLY_MAP, PLOTLY_SCATTERMAP
from .test_plot import TestPlotlyPlot
default_shape_color = '#2a3f5f'
class TestShape(TestPlotlyPlot):
def assert_shape_element_styling(self, element):
props = dict(
fillcolor='orange',
line_color='yellow',
line_dash='dot',
line_width=5,
opacity=0.7
)
element = element.clone().opts(**props)
state = self._get_plot_state(element)
shapes = state['layout']['shapes']
self.assert_property_values(shapes[0], props)
class TestMapboxShape(TestPlotlyPlot):
def setUp(self):
super().setUp()
# Precompute coordinates
self.xs = [3000000, 2000000, 1000000]
self.ys = [-3000000, -2000000, -1000000]
self.x_range = (-5000000, 4000000)
self.x_center = sum(self.x_range) / 2.0
self.y_range = (-3000000, 2000000)
self.y_center = sum(self.y_range) / 2.0
self.lon_range, self.lat_range = Tiles.easting_northing_to_lon_lat(self.x_range, self.y_range)
self.lon_centers, self.lat_centers = Tiles.easting_northing_to_lon_lat(
[self.x_center], [self.y_center]
)
self.lon_center, self.lat_center = self.lon_centers[0], self.lat_centers[0]
self.lons, self.lats = Tiles.easting_northing_to_lon_lat(self.xs, self.ys)
class TestVLineHLine(TestShape):
def assert_vline(self, shape, x, xref='x', ydomain=(0, 1)):
self.assertEqual(shape['type'], 'line')
self.assertEqual(shape['x0'], x)
self.assertEqual(shape['x1'], x)
self.assertEqual(shape['xref'], xref)
self.assertEqual(shape['y0'], ydomain[0])
self.assertEqual(shape['y1'], ydomain[1])
self.assertEqual(shape['yref'], 'paper')
def assert_hline(self, shape, y, yref='y', xdomain=(0, 1)):
self.assertEqual(shape['type'], 'line')
self.assertEqual(shape['y0'], y)
self.assertEqual(shape['y1'], y)
self.assertEqual(shape['yref'], yref)
self.assertEqual(shape['x0'], xdomain[0])
self.assertEqual(shape['x1'], xdomain[1])
self.assertEqual(shape['xref'], 'paper')
def test_single_vline(self):
vline = VLine(3)
state = self._get_plot_state(vline)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 1)
self.assert_vline(shapes[0], 3)
def test_single_hline(self):
hline = HLine(3)
state = self._get_plot_state(hline)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 1)
self.assert_hline(shapes[0], 3)
def test_vline_layout(self):
layout = (VLine(1) + VLine(2) +
VLine(3) + VLine(4)).cols(2).opts(vspacing=0, hspacing=0)
state = self._get_plot_state(layout)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 4)
# Check shapes
self.assert_vline(shapes[0], 3, xref='x', ydomain=[0.0, 0.5])
self.assert_vline(shapes[1], 4, xref='x2', ydomain=[0.0, 0.5])
self.assert_vline(shapes[2], 1, xref='x3', ydomain=[0.5, 1.0])
self.assert_vline(shapes[3], 2, xref='x4', ydomain=[0.5, 1.0])
def test_hline_layout(self):
layout = (HLine(1) + HLine(2) +
HLine(3) + HLine(4)).cols(2).opts(vspacing=0, hspacing=0)
state = self._get_plot_state(layout)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 4)
# Check shapes
self.assert_hline(shapes[0], 3, yref='y', xdomain=[0.0, 0.5])
self.assert_hline(shapes[1], 4, yref='y2', xdomain=[0.5, 1.0])
self.assert_hline(shapes[2], 1, yref='y3', xdomain=[0.0, 0.5])
self.assert_hline(shapes[3], 2, yref='y4', xdomain=[0.5, 1.0])
def test_vline_styling(self):
self.assert_shape_element_styling(VLine(3))
def test_hline_styling(self):
self.assert_shape_element_styling(HLine(3))
class TestPathShape(TestShape):
def assert_path_shape_element(self, shape, element, xref='x', yref='y'):
# Check type
self.assertEqual(shape['type'], 'path')
# Check svg path
expected_path = 'M' + 'L'.join([
f'{x} {y}' for x, y in
zip(element.dimension_values(0), element.dimension_values(1))]) + 'Z'
self.assertEqual(shape['path'], expected_path)
# Check axis references
self.assertEqual(shape['xref'], xref)
self.assertEqual(shape['yref'], yref)
def test_simple_path(self):
path = Path([(0, 0), (1, 1), (0, 1), (0, 0)])
state = self._get_plot_state(path)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 1)
self.assert_path_shape_element(shapes[0], path)
self.assert_shape_element_styling(path)
class TestMapboxPathShape(TestMapboxShape):
def test_simple_path(self):
path = Tiles("") * Path([
(self.x_range[0], self.y_range[0]),
(self.x_range[1], self.y_range[1]),
(self.x_range[0], self.y_range[1]),
(self.x_range[0], self.y_range[0]),
]).redim.range(
x=self.x_range, y=self.y_range
)
state = self._get_plot_state(path)
self.assertEqual(state["data"][1]["type"], PLOTLY_SCATTERMAP)
self.assertEqual(state["data"][1]["mode"], "lines")
self.assertEqual(state["data"][1]["lon"], np.array([
self.lon_range[i] for i in (0, 1, 0, 0)
] + [np.nan]))
self.assertEqual(state["data"][1]["lat"], np.array([
self.lat_range[i] for i in (0, 1, 1, 0)
] + [np.nan]))
self.assertEqual(state["data"][1]["showlegend"], False)
self.assertEqual(state["data"][1]["line"]["color"], default_shape_color)
self.assertEqual(
state['layout'][PLOTLY_MAP]['center'], {
'lat': self.lat_center, 'lon': self.lon_center
}
)
class TestBounds(TestPathShape):
def test_single_bounds(self):
bounds = Bounds((1, 2, 3, 4))
state = self._get_plot_state(bounds)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 1)
self.assert_path_shape_element(shapes[0], bounds)
def test_bounds_layout(self):
bounds1 = Bounds((0, 0, 1, 1))
bounds2 = Bounds((0, 0, 2, 2))
bounds3 = Bounds((0, 0, 3, 3))
bounds4 = Bounds((0, 0, 4, 4))
layout = (bounds1 + bounds2 +
bounds3 + bounds4).cols(2)
state = self._get_plot_state(layout)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 4)
# Check shapes
self.assert_path_shape_element(shapes[0], bounds3, xref='x', yref='y')
self.assert_path_shape_element(shapes[1], bounds4, xref='x2', yref='y2')
self.assert_path_shape_element(shapes[2], bounds1, xref='x3', yref='y3')
self.assert_path_shape_element(shapes[3], bounds2, xref='x4', yref='y4')
def test_bounds_styling(self):
self.assert_shape_element_styling(Bounds((1, 2, 3, 4)))
class TestMapboxBounds(TestMapboxShape):
def test_single_bounds(self):
bounds = Tiles("") * Bounds(
(self.x_range[0], self.y_range[0], self.x_range[1], self.y_range[1])
).redim.range(
x=self.x_range, y=self.y_range
)
state = self._get_plot_state(bounds)
self.assertEqual(state["data"][1]["type"], PLOTLY_SCATTERMAP)
self.assertEqual(state["data"][1]["mode"], "lines")
self.assertEqual(state["data"][1]["lon"], np.array([
self.lon_range[i] for i in (0, 0, 1, 1, 0)
]))
self.assertEqual(state["data"][1]["lat"], np.array([
self.lat_range[i] for i in (0, 1, 1, 0, 0)
]))
self.assertEqual(state["data"][1]["showlegend"], False)
self.assertEqual(state["data"][1]["line"]["color"], default_shape_color)
self.assertEqual(
state['layout'][PLOTLY_MAP]['center'], {
'lat': self.lat_center, 'lon': self.lon_center
}
)
def test_bounds_layout(self):
bounds1 = Bounds((0, 0, 1, 1))
bounds2 = Bounds((0, 0, 2, 2))
bounds3 = Bounds((0, 0, 3, 3))
bounds4 = Bounds((0, 0, 4, 4))
layout = (Tiles("") * bounds1 + Tiles("") * bounds2 +
Tiles("") * bounds3 + Tiles("") * bounds4).cols(2)
state = self._get_plot_state(layout)
self.assertEqual(state['data'][1]["subplot"], PLOTLY_MAP)
self.assertEqual(state['data'][3]["subplot"], PLOTLY_MAP + "2")
self.assertEqual(state['data'][5]["subplot"], PLOTLY_MAP + "3")
self.assertEqual(state['data'][7]["subplot"], PLOTLY_MAP + "4")
self.assertNotIn("xaxis", state['layout'])
self.assertNotIn("yaxis", state['layout'])
class TestBox(TestPathShape):
def test_single_box(self):
box = Box(0, 0, (1, 2), orientation=1)
state = self._get_plot_state(box)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 1)
self.assert_path_shape_element(shapes[0], box)
def test_bounds_layout(self):
box1 = Box(0, 0, (1, 1), orientation=0)
box2 = Box(0, 0, (2, 2), orientation=0.5)
box3 = Box(0, 0, (3, 3), orientation=1.0)
box4 = Box(0, 0, (4, 4), orientation=1.5)
layout = (box1 + box2 +
box3 + box4).cols(2)
state = self._get_plot_state(layout)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 4)
# Check shapes
self.assert_path_shape_element(shapes[0], box3, xref='x', yref='y')
self.assert_path_shape_element(shapes[1], box4, xref='x2', yref='y2')
self.assert_path_shape_element(shapes[2], box1, xref='x3', yref='y3')
self.assert_path_shape_element(shapes[3], box2, xref='x4', yref='y4')
def test_box_styling(self):
self.assert_shape_element_styling(Box(0, 0, (1, 1)))
class TestMapboxBox(TestMapboxShape):
def test_single_box(self):
box = Tiles("") * Box(0, 0, (1000000, 2000000)).redim.range(
x=self.x_range, y=self.y_range
)
x_box_range = [-500000, 500000]
y_box_range = [-1000000, 1000000]
lon_box_range, lat_box_range = Tiles.easting_northing_to_lon_lat(x_box_range, y_box_range)
state = self._get_plot_state(box)
self.assertEqual(state["data"][1]["type"], PLOTLY_SCATTERMAP)
self.assertEqual(state["data"][1]["mode"], "lines")
self.assertEqual(state["data"][1]["showlegend"], False)
self.assertEqual(state["data"][1]["line"]["color"], default_shape_color)
self.assertEqual(state["data"][1]["lon"], np.array([
lon_box_range[i] for i in (0, 0, 1, 1, 0)
]))
self.assertEqual(state["data"][1]["lat"], np.array([
lat_box_range[i] for i in (0, 1, 1, 0, 0)
]))
self.assertEqual(
state['layout'][PLOTLY_MAP]['center'], {
'lat': self.lat_center, 'lon': self.lon_center
}
)
class TestRectangles(TestPathShape):
def test_boxes_simple(self):
boxes = Rectangles([(0, 0, 1, 1), (2, 2, 4, 3)])
state = self._get_plot_state(boxes)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 2)
self.assertEqual(shapes[0], {'type': 'rect', 'x0': 0, 'y0': 0, 'x1': 1,
'y1': 1, 'xref': 'x', 'yref': 'y', 'name': '',
'fillcolor': '#30a2da',
'line': {'color': default_shape_color}})
self.assertEqual(shapes[1], {'type': 'rect', 'x0': 2, 'y0': 2, 'x1': 4,
'y1': 3, 'xref': 'x', 'yref': 'y', 'name': '',
'fillcolor': '#30a2da',
'line': {'color': default_shape_color}})
self.assertEqual(state['layout']['xaxis']['range'], [0, 4])
self.assertEqual(state['layout']['yaxis']['range'], [0, 3])
class TestMapboxRectangles(TestMapboxShape):
def test_rectangles_simple(self):
rectangles = Tiles("") * Rectangles([
(0, 0, self.x_range[1], self.y_range[1]),
(self.x_range[0], self.y_range[0], 0, 0),
]).redim.range(
x=self.x_range, y=self.y_range
)
state = self._get_plot_state(rectangles)
self.assertEqual(state["data"][1]["type"], PLOTLY_SCATTERMAP)
self.assertEqual(state["data"][1]["mode"], "lines")
self.assertEqual(state["data"][1]["showlegend"], False)
self.assertEqual(state["data"][1]["line"]["color"], default_shape_color)
self.assertEqual(state["data"][1]["lon"], np.array([
0, 0, self.lon_range[1], self.lon_range[1], 0, np.nan,
self.lon_range[0], self.lon_range[0], 0, 0, self.lon_range[0], np.nan
]))
self.assertEqual(state["data"][1]["lat"], np.array([
0, self.lat_range[1], self.lat_range[1], 0, 0, np.nan,
self.lat_range[0], 0, 0, self.lat_range[0], self.lat_range[0], np.nan
]))
self.assertEqual(
state['layout'][PLOTLY_MAP]['center'], {
'lat': self.lat_center, 'lon': self.lon_center
}
)
class TestSegments(TestPathShape):
def test_segments_simple(self):
boxes = Segments([(0, 0, 1, 1), (2, 2, 4, 3)])
state = self._get_plot_state(boxes)
shapes = state['layout']['shapes']
self.assertEqual(len(shapes), 2)
self.assertEqual(shapes[0], {'type': 'line', 'x0': 0, 'y0': 0, 'x1': 1,
'y1': 1, 'xref': 'x', 'yref': 'y', 'name': '',
'line': {'color': default_shape_color}})
self.assertEqual(shapes[1], {'type': 'line', 'x0': 2, 'y0': 2, 'x1': 4,
'y1': 3, 'xref': 'x', 'yref': 'y', 'name': '',
'line': {'color': default_shape_color}})
self.assertEqual(state['layout']['xaxis']['range'], [0, 4])
self.assertEqual(state['layout']['yaxis']['range'], [0, 3])
class TestMapboxSegments(TestMapboxShape):
def test_segments_simple(self):
rectangles = Tiles("") * Segments([
(0, 0, self.x_range[1], self.y_range[1]),
(self.x_range[0], self.y_range[0], 0, 0),
]).redim.range(
x=self.x_range, y=self.y_range
)
state = self._get_plot_state(rectangles)
self.assertEqual(state["data"][1]["type"], PLOTLY_SCATTERMAP)
self.assertEqual(state["data"][1]["mode"], "lines")
self.assertEqual(state["data"][1]["showlegend"], False)
self.assertEqual(state["data"][1]["line"]["color"], default_shape_color)
self.assertEqual(state["data"][1]["lon"], np.array([
0, self.lon_range[1], np.nan,
self.lon_range[0], 0, np.nan
]))
self.assertEqual(state["data"][1]["lat"], np.array([
0, self.lat_range[1], np.nan,
self.lat_range[0], 0, np.nan
]))
self.assertEqual(
state['layout'][PLOTLY_MAP]['center'], {
'lat': self.lat_center, 'lon': self.lon_center
}
)