-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotly_functions.py
126 lines (97 loc) · 3.02 KB
/
plotly_functions.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
import networkx as nx
import dash
import pandas as pd
import numpy as np
from typing import Tuple, Any, Union, Collection, Mapping
from plotly import graph_objects as go
def get_edges_df(graph:nx.Graph, node_xy:pd.DataFrame):
edge_x = []
edge_y = []
for edge in graph.edges():
x0, y0 = node_xy.loc[edge[0], ['X', 'Y']]
x1, y1 = node_xy.loc[edge[1], ['X', 'Y']]
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
df = pd.DataFrame(dict(X=edge_x, Y=edge_y))
return df
def get_nodes_df(graph:nx.Graph) -> pd.DataFrame:
return pd.DataFrame([
{'Node':node, 'X':x, 'Y':y}
for (node, (x, y)) in nx.spring_layout(graph).items()
]).set_index('Node')
def get_info_dicts(nodes_df: pd.DataFrame) -> dict[str, dict[str, Any]]:
"""Dicts, keyed by index (node) then column."""
return nodes_df.drop(['X', 'Y', 'AnnotationText'], axis=1, errors='ignore').to_dict('index')
def textify_additional_data(
data: dict[str, dict[str, Any]],
) -> dict[str, str]:
"""Take data in form of {nodeID:{dataKey:dataValue}} and return text string for annotation."""
out = {}
for nodeid, values in data.items():
s = '\n'.join([f"{k}:{v}" for k, v in values.items()])
out[nodeid] = s
return out
def plot_v1(
nodes:pd.DataFrame,
edges:pd.DataFrame
):
"""Plot some simple data."""
node_fmt = dict(
size=2,
color='white',
line=dict(
color='lightslategrey',
width=2,
)
)
nodes_go = go.Scatter(
x=nodes.X,
y=nodes.Y,
ids=nodes.index,
mode='markers',
marker=node_fmt,
)
edges_go = go.Scatter(
x=edges.X,
y=edges.Y,
mode='lines'
)
fig = go.Figure()
fig.add_trace(edges_go)
fig.add_trace(nodes_go)
fig.update_layout(
xaxis=dict(
showticklabels=False,
showgrid=False,
zeroline=False
),
yaxis=dict(
showticklabels=False,
showgrid=False,
zeroline=False
)
)
for k, row in nodes.iterrows():
fig.add_annotation(text=row.AnnotationText, yanchor='bottom', bgcolor='lightgrey', x=row.X, y=row.Y, ax=row.X,
ay=row.Y)
return fig
def basic_graph(data=(('A', 'B'), ('B', 'C'), ('C', 'A'))) -> nx.Graph:
"""Get a nx.Graph with some edges and nodes."""
G = nx.Graph()
G.add_edges_from(data)
return G
def basic_test():
G = basic_graph()
nodes_df = get_nodes_df(G)
nodes_df.loc[:, 'Table'] = ['TableA', 'TableB', 'TableB']
nodes_df.loc[:, 'OtherInfo'] = ['Something', 'Some longish text oadijwoidjaodjw ad', np.nan]
nodes_df.loc[:, 'AnnotationText'] = textify_additional_data(get_info_dicts(nodes_df))
edges_xy = get_edges_df(G, nodes_df)
f = plot_v1(nodes_df, edges_xy)
f.show()
if __name__ == '__main__':
basic_test()