-
Notifications
You must be signed in to change notification settings - Fork 0
/
collabgraph.py
184 lines (130 loc) · 5.11 KB
/
collabgraph.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
"""
Module containing CollabGraph to generate network and associated visualization
"""
import os
import plotly.graph_objects as go
import networkx as nx
class CollabNetwork(nx.Graph):
def __init__(self, collab_dict):
nx.Graph.__init__(self)
self.collab_dict = collab_dict
self._generate_network(self.collab_dict)
def _generate_network(self, collab_dict):
# POPULATE GRAPH WITH NODES AND EDGES
# Add nodes and edges recursively
self._make_nodes(collab_dict)
self._make_edges(collab_dict)
# Resize nodes
main_artist = list(collab_dict.keys())[0].name
self._resize_nodes(main_artist)
def _make_nodes(self, collab_dict):
for artist, collaborators in collab_dict.items():
self.add_node(artist.name)
self._make_nodes(collaborators)
def _make_edges(self, collab_dict):
for artist, collaborators in collab_dict.items():
for artist1, collaborators1 in collaborators.items():
self.add_edge(
artist.name, artist1.name, weight=artist1.parent_collab_count
)
self._make_edges(collaborators)
def _resize_nodes(self, center, scale=5):
for node in self.nodes():
current_distance = nx.shortest_path_length(self, center, node)
current_size = 1 / (current_distance + 1) * 14
self.nodes[node]["size"] = current_size
def position_network(self, parameters):
# Position nodes and edges
return nx.spring_layout(
self, scale=None, k=parameters["k"], iterations=parameters["iterations"]
)
class CollabGraph(go.Figure):
def __init__(self):
go.Figure.__init__(self)
self.layout = go.Layout(
paper_bgcolor="rgba(255,255,255,1)",
plot_bgcolor="rgba(255,255,255,1)",
xaxis={"showgrid": False, "zeroline": False, "showticklabels": False},
yaxis={"showgrid": False, "zeroline": False, "showticklabels": False},
showlegend=False,
)
def draw_graph(self, network, position, save=False, filename=None):
# Create node traces, edge traces and add to figure
node_trace = self._make_node_trace(network, position)
edge_traces = self._make_edge_traces(network, position)
# Add node trace and add all edge traces
self._add_nodes_and_edges_trace(node_trace, edge_traces)
# TO SEE
self.show()
if save:
if not os.path.exists("data"):
os.mkdir("data")
self.write_image(f"data/{filename}.png", width=1200, height=750, scale=1.5)
def _make_node_trace(self, graph, position):
node_trace = go.Scatter(
x=[],
y=[],
text=[],
textposition="top center",
textfont_size=[],
mode="markers+text",
hoverinfo="none",
marker=dict(color=[], size=[], opacity=[], line=None),
)
# For each node, get the position and size and add to the node_trace
for node in graph.nodes():
x, y = position[node]
node_trace["x"] += tuple([x])
node_trace["y"] += tuple([y])
node_trace["textfont_size"] += tuple([2 * graph.nodes[node]["size"]])
marker_size = 3 * graph.nodes[node]["size"]
marker_opacity = 1 - (1 / marker_size)
node_trace["marker"]["color"] += tuple(["cornflowerblue"])
node_trace["marker"]["size"] += tuple([marker_size])
node_trace["marker"]["opacity"] += tuple([marker_opacity])
node_trace["text"] += tuple(["<b>" + node + "</b>"])
return node_trace
def _make_edge_traces(self, graph, position):
"""
For each edge, make an edge_trace, append to list
Input: graph object, position object
Output: list of edge traces
Functions used: _make_edge
"""
edge_traces = []
for edge in graph.edges():
artist_1 = edge[0]
artist_2 = edge[1]
x0, y0 = position[artist_1]
x1, y1 = position[artist_2]
text = (
artist_1 + "--" + artist_2 + ": " + str(graph.edges()[edge]["weight"])
)
trace = self._make_edge(
[x0, x1, None],
[y0, y1, None],
text,
width=0.3 * graph.edges()[edge]["weight"] ** 1.1,
)
edge_traces.append(trace)
return edge_traces
def _make_edge(self, x, y, text, width):
"""
Custom function to create an edge between node x and node y, with a given text and width
"""
return go.Scatter(
x=x,
y=y,
line=dict(width=width, color="cornflowerblue"),
hoverinfo="text",
text=([text]),
mode="lines",
)
def _add_nodes_and_edges_trace(self, node_trace, edge_traces):
self.add_trace(node_trace)
for edge_trace in edge_traces:
self.add_trace(edge_trace)
"""
TODO
-Opacity is off
"""