-
Notifications
You must be signed in to change notification settings - Fork 67
/
plots.py
504 lines (423 loc) · 13.6 KB
/
plots.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
from ..core.non_aggregate import (
BaseScatter,
BaseLine,
BaseStackedLine,
BaseGraph,
)
from .custom_extensions import (
CustomInspectTool,
calc_connected_edges,
InteractiveDatashaderPoints,
InteractiveDatashaderLine,
InteractiveDatashaderGraph,
InteractiveDatashaderMultiLine,
)
from packaging.version import Version
import datashader as ds
import dask_cudf
import dask.dataframe as dd
import cupy as cp
import cudf
import pandas as pd
import holoviews as hv
from bokeh import events
from PIL import Image
import requests
from io import BytesIO
ds_version = Version(ds.__version__)
def load_image(url):
response = requests.get(url)
return Image.open(BytesIO(response.content))
class Scatter(BaseScatter):
"""
Description:
"""
reset_event = events.Reset
data_y_axis = "y"
data_x_axis = "x"
def format_source_data(self, data):
"""
Description:
format source
-------------------------------------------
Input:
source_dict = {
'X': [],
'Y': []
}
-------------------------------------------
Ouput:
"""
self.source = data
def generate_chart(self):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if not self.title:
self.title = (
"Scatter plot for "
+ (self.aggregate_col or "")
+ " "
+ self.aggregate_fn
)
self.chart = InteractiveDatashaderPoints(
source_df=self.source,
x=self.x,
y=self.y,
aggregate_col=self.aggregate_col,
aggregate_fn=self.aggregate_fn,
color_palette=self.color_palette,
pixel_shade_type=self.pixel_shade_type,
tile_provider=self.tile_provider,
legend=self.legend,
legend_position=self.legend_position,
spread_threshold=self.pixel_density,
point_shape=self.point_shape,
max_px=self.point_size,
unselected_alpha=self.unselected_alpha,
title=self.title,
xaxis=self.library_specific_params["xaxis"],
yaxis=self.library_specific_params["yaxis"],
)
def reload_chart(self, data=None):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if data is not None:
if len(data) == 0:
data = cudf.DataFrame({k: cp.nan for k in data.columns})
self.chart.update_data(data)
def apply_theme(self, theme):
"""
apply thematic changes to the chart based on the theme
"""
if not self.colors_set:
self.chart.color_palette = theme.color_palette
self.chart._compute_datashader_assets()
class Graph(BaseGraph):
"""
Description:
"""
reset_event = events.Reset
data_y_axis = "node_y"
data_x_axis = "node_x"
def format_source_data(self, dataframe):
"""
Description:
format source
-------------------------------------------
Input:
source_dict = {
'X': [],
'Y': []
}
-------------------------------------------
Ouput:
"""
if isinstance(dataframe, (cudf.DataFrame, pd.DataFrame)):
self.nodes = dataframe
else:
self.nodes = dataframe.data
self.edges = dataframe.edges
if self.edges is not None:
# update connected_edges value for datashaded edges
self.connected_edges = calc_connected_edges(
self.nodes,
self.edges,
self.node_x,
self.node_y,
self.node_id,
self.edge_source,
self.edge_target,
self.edge_aggregate_col,
self.x_dtype,
self.y_dtype,
self.edge_render_type,
self.curve_params,
)
def generate_chart(self):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if not self.title:
self.title = "Graph"
impath = (
"https://raw.githubusercontent.com/rapidsai/cuxfilter/"
+ "branch-0.15/python/cuxfilter/charts/datashader/icons/graph.png"
)
self.inspect_neighbors = CustomInspectTool(
icon=load_image(impath),
_active=True,
description="Inspect Neighboring Edges",
)
# loading icon from a url
impath = (
"https://raw.githubusercontent.com/rapidsai/cuxfilter/"
+ "branch-0.15/python/cuxfilter/charts/datashader/icons/XPan.png"
)
self.display_edges = CustomInspectTool(
icon=load_image(impath), _active=True, description="Display Edges"
)
def cb(attr, old, new):
if not new:
self.chart.update_data(edges=self.connected_edges.head(0))
else:
self.chart.update_data(edges=self.connected_edges)
self.display_edges.on_change("_active", cb)
self.chart = InteractiveDatashaderGraph(
nodes_df=self.nodes,
edges_df=self.connected_edges,
node_x=self.node_x,
node_y=self.node_y,
node_aggregate_col=self.node_aggregate_col,
node_aggregate_fn=self.node_aggregate_fn,
node_color_palette=self.node_color_palette,
node_pixel_shade_type=self.node_pixel_shade_type,
tile_provider=self.tile_provider,
legend=self.legend,
legend_position=self.legend_position,
node_spread_threshold=self.node_pixel_density,
node_point_shape=self.node_point_shape,
node_max_px=self.node_point_size,
edge_source=self.node_x,
edge_target=self.node_y,
edge_color=self.edge_color_palette[0],
edge_transparency=self.edge_transparency,
inspect_neighbors=self.inspect_neighbors,
display_edges=self.display_edges,
unselected_alpha=self.unselected_alpha,
title=self.title,
xaxis=self.library_specific_params["xaxis"],
yaxis=self.library_specific_params["yaxis"],
)
def reload_chart(self, data, edges=None):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if data is not None:
if len(data) == 0:
data = cudf.DataFrame({k: cp.nan for k in self.nodes.columns})
# update connected_edges value for datashaded edges
# if display edge toggle is active
if self.display_edges._active:
self.connected_edges = calc_connected_edges(
data,
self.edges if edges is None else edges,
self.node_x,
self.node_y,
self.node_id,
self.edge_source,
self.edge_target,
self.edge_aggregate_col,
self.x_dtype,
self.y_dtype,
self.edge_render_type,
self.curve_params,
)
self.chart.update_data(data, self.connected_edges)
else:
self.chart.update_data(data)
def apply_theme(self, theme):
"""
apply thematic changes to the chart based on the theme
"""
if not self.colors_set:
self.default_palette = theme.color_palette
self.chart.update_color_palette(theme.color_palette)
class Line(BaseLine):
"""
Description:
"""
reset_event = events.Reset
data_y_axis = "y"
data_x_axis = "x"
use_data_tiles = False
def calculate_source(self, data):
"""
Description:
-------------------------------------------
Input:
data = cudf.DataFrame
-------------------------------------------
Ouput:
"""
self.format_source_data(data)
def format_source_data(self, data):
"""
Description:
format source
-------------------------------------------
Input:
source_dict = {
'X': [],
'Y': []
}
-------------------------------------------
Ouput:
"""
self.source = data
self.x_range = (self.source[self.x].min(), self.source[self.x].max())
self.y_range = (self.source[self.y].min(), self.source[self.y].max())
if isinstance(data, dask_cudf.DataFrame):
self.x_range = dd.compute(*self.x_range)
self.y_range = dd.compute(*self.y_range)
def generate_chart(self):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if not self.title:
if self.x == self.y:
self.title = "Line plot for " + self.x
else:
self.title = "Line plot for (" + self.x + "," + self.y + ")"
self.chart = InteractiveDatashaderLine(
source_df=self.source,
x=self.x,
y=self.y,
color=self.color,
pixel_shade_type=self.pixel_shade_type,
unselected_alpha=self.unselected_alpha,
title=self.title,
)
def reload_chart(self, data):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if data is not None:
if len(data) == 0:
data = cudf.DataFrame({k: cp.nan for k in data.columns})
self.chart.update_data(data)
def apply_theme(self, theme):
"""
apply thematic changes to the chart based on the theme
"""
if not self.color_set:
self.default_color = theme.chart_color
self.chart.color = theme.chart_color
class StackedLines(BaseStackedLine):
"""
Description:
"""
reset_event = events.Reset
data_y_axis = "y"
data_x_axis = "x"
def compute_legend(self, colors=None):
colors = colors or self.colors
if self.legend:
res = []
for i, val in enumerate(colors):
res.append((self.y[i], val))
return hv.NdOverlay(
{
k: hv.Curve(
self.source.head(1),
label=str(k),
kdims=[self.x],
vdims=[self.y[0]],
).opts(color=v)
for k, v in res
}
).opts(legend_position=self.legend_position)
return None
def calculate_source(self, data):
"""
Description:
-------------------------------------------
Input:
data = cudf.DataFrame
-------------------------------------------
Ouput:
"""
self.format_source_data(data)
def format_source_data(self, data):
"""
Description:
format source
-------------------------------------------
Input:
source_dict = {
'X': [],
'Y': []
}
-------------------------------------------
Ouput:
"""
self.source = data
if self.x_range is None:
self.x_range = (
self.source[self.x].min(),
self.source[self.x].max(),
)
if self.y_range is None:
# cudf_df[['a','b','c']].min().min() gives min value
# between all values in columns a,b and c
self.y_range = (
self.source[self.y].min().min(),
self.source[self.y].max().max(),
)
if isinstance(data, dask_cudf.DataFrame):
self.x_range = dd.compute(*self.x_range)
self.y_range = dd.compute(*self.y_range)
def generate_chart(self):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if not self.title:
self.title = "Stacked Line plots on x-axis: " + self.x
self.chart = InteractiveDatashaderMultiLine(
source_df=self.source,
x=self.x,
line_dims=self.y,
colors=self.colors,
legend=self.compute_legend(),
unselected_alpha=self.unselected_alpha,
title=self.title,
)
def reload_chart(self, data):
"""
Description:
-------------------------------------------
Input:
-------------------------------------------
Ouput:
"""
if data is not None:
if len(data) == 0:
data = cudf.DataFrame({k: cp.nan for k in data.columns})
self.chart.update_data(data)
def apply_theme(self, theme):
"""
apply thematic changes to the chart based on the theme
"""
if not self.colors_set:
self.default_colors = [theme.chart_color]
self.chart.legend = self.compute_legend(self.default_colors)
self.chart.colors = self.default_colors