sugikey 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sugikey/__init__.py +3 -0
- sugikey/bokeh_sk.py +116 -0
- sugikey/draw.py +721 -0
- sugikey/examples.py +250 -0
- sugikey/optim.py +289 -0
- sugikey/processing.py +579 -0
- sugikey/sankey.py +53 -0
- sugikey/thin.py +22 -0
- sugikey-0.0.0.dist-info/LICENSE +21 -0
- sugikey-0.0.0.dist-info/METADATA +89 -0
- sugikey-0.0.0.dist-info/RECORD +12 -0
- sugikey-0.0.0.dist-info/WHEEL +4 -0
sugikey/__init__.py
ADDED
sugikey/bokeh_sk.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Drawing Sankey diagrams with Bokeh
|
|
2
|
+
|
|
3
|
+
Bokeh allows interactions to be included in plots: zooming, panning, showing things on hover etc.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Any, Dict, Optional
|
|
7
|
+
|
|
8
|
+
import networkx as nx
|
|
9
|
+
import pandas as pd
|
|
10
|
+
from bokeh.models import ColumnDataSource, HoverTool
|
|
11
|
+
from bokeh.plotting import figure
|
|
12
|
+
|
|
13
|
+
from . import draw
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_bokeh_kwargs(pyplot_kwargs: Dict[str, Any]) -> dict:
|
|
17
|
+
"""Translate pyplot keyword arguments in bokeh keyword arguments
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
pyplot_kwargs (Dict[str, Any]): A dictionary of pyplot keyword arguments.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
bk_kwargs: A dictionary of bokeh keyword arguments.
|
|
24
|
+
"""
|
|
25
|
+
bk_kwargs = {}
|
|
26
|
+
kw_translate_dict = {"lw": "line_width"}
|
|
27
|
+
for plt_kw, kw_val in pyplot_kwargs.items():
|
|
28
|
+
bk_kw = kw_translate_dict.get(plt_kw, plt_kw)
|
|
29
|
+
bk_kwargs[bk_kw] = kw_val
|
|
30
|
+
return bk_kwargs
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def draw_sankey_bokeh(dig: nx.DiGraph, draw_config: Optional[draw.DrawConfig] = None):
|
|
34
|
+
"""The actual drawing of a Sankey plot in bokeh
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
dig
|
|
38
|
+
draw_config
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
figure
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
if draw_config is None:
|
|
45
|
+
draw_config = draw.DrawConfig()
|
|
46
|
+
|
|
47
|
+
diagram = draw.get_sankey_diagram(dig, draw_config)
|
|
48
|
+
|
|
49
|
+
x_range, y_range = diagram.coordinate_range
|
|
50
|
+
|
|
51
|
+
plot = figure(
|
|
52
|
+
width=800,
|
|
53
|
+
height=300,
|
|
54
|
+
title="",
|
|
55
|
+
tools="",
|
|
56
|
+
toolbar_location=None,
|
|
57
|
+
match_aspect=True,
|
|
58
|
+
x_range=x_range,
|
|
59
|
+
y_range=y_range,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
for pline in diagram.lines:
|
|
63
|
+
plot.line(
|
|
64
|
+
pline.x_list, pline.y_list, **get_bokeh_kwargs(draw_config.edge_edge_kw)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
filled_df = pd.DataFrame(
|
|
68
|
+
{"x": pline.x_list, "y": pline.y_list, "name": pline.name}
|
|
69
|
+
for pline in diagram.filled
|
|
70
|
+
)
|
|
71
|
+
filled_source = ColumnDataSource(filled_df)
|
|
72
|
+
fill_renderer = plot.patches(
|
|
73
|
+
source=filled_source,
|
|
74
|
+
xs="x",
|
|
75
|
+
ys="y",
|
|
76
|
+
**get_bokeh_kwargs(draw_config.edge_fill_kw)
|
|
77
|
+
)
|
|
78
|
+
fill_hover = HoverTool(renderers=[fill_renderer], tooltips=[("", "@name")])
|
|
79
|
+
plot.add_tools(fill_hover)
|
|
80
|
+
|
|
81
|
+
node_label_df = pd.DataFrame(
|
|
82
|
+
[
|
|
83
|
+
{"x": label.x_txt, "y": label.y_txt, "text": label.text}
|
|
84
|
+
for label in diagram.labels
|
|
85
|
+
if label.category == "node_label"
|
|
86
|
+
]
|
|
87
|
+
)
|
|
88
|
+
node_label_source = ColumnDataSource(node_label_df)
|
|
89
|
+
plot.text(
|
|
90
|
+
source=node_label_source,
|
|
91
|
+
x="x",
|
|
92
|
+
y="y",
|
|
93
|
+
text="text",
|
|
94
|
+
**draw_config.node_label_kwargs
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
edge_label_df = pd.DataFrame(
|
|
98
|
+
[
|
|
99
|
+
{"x": label.x_txt, "y": label.y_txt, "text": label.text}
|
|
100
|
+
for label in diagram.labels
|
|
101
|
+
if label.category == "edge_label"
|
|
102
|
+
]
|
|
103
|
+
)
|
|
104
|
+
edge_label_source = ColumnDataSource(edge_label_df)
|
|
105
|
+
plot.text(
|
|
106
|
+
source=edge_label_source,
|
|
107
|
+
x="x",
|
|
108
|
+
y="y",
|
|
109
|
+
text="text",
|
|
110
|
+
**draw_config.edge_label_kwargs
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
plot.xgrid.grid_line_color = None
|
|
114
|
+
plot.ygrid.grid_line_color = None
|
|
115
|
+
plot.axis.visible = False
|
|
116
|
+
return plot
|