-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwittergraph.py
98 lines (88 loc) · 2.75 KB
/
twittergraph.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
#%%
# import
import pandas as pd
import networkx as nx
from pyvis.network import Network
from igraph import Graph
from matplotlib.cm import get_cmap
from matplotlib.colors import rgb2hex
# download orcid data
orc_list = pd.read_csv("https://opencheck.is/scitwitter/orcidgraph", header=None)
# create graph (networkx because the easiest)
nx_graph = nx.Graph()
nx_graph.add_edges_from(orc_list.to_numpy())
n_edges = nx_graph.number_of_edges()
n_nodes = nx_graph.number_of_nodes()
print("Number of edges:", n_edges)
print("Number of nodes:", n_nodes)
# estimate layout (igraph because fast)
ig_graph = Graph.from_networkx(nx_graph)
layout = ig_graph.layout("fr")
layout_factor = 1200 # factor to multiply the node positions with (maybe adjust with more nodes)
# convert to pyvis graph (because nice HTML plotting)
pv_graph = Network(height="700px", width="100%", select_menu=True, bgcolor="#000e1e")
pv_graph.from_nx(nx_graph)
# global layout
pv_layout = """
const options = {
"nodes": {
"scaling": {
"min": 10,
"max": 60
},
"color": {
"border": "rgb(43, 124, 233, 1)",
"background": "rgb(160, 198, 247)"
},
"font": {
"color": "rgba(255, 255, 255, 0.8)"
}
},
"edges": {
"color": {
"color": "rgba(43, 124, 233, 0.2)",
"highlight": "rgba(139, 0, 0, 1)"
},
"smooth": {
"type": "continuous",
"roundness": 0.75
}
},
"physics": {
"enabled": false
}
}
"""
pv_graph.set_options(pv_layout)
# specific layout
for i, node in enumerate(pv_graph.get_nodes()):
# position from igraph
pv_graph.get_node(node)["x"] = layout.coords[i][0] * layout_factor
pv_graph.get_node(node)["y"] = layout.coords[i][1] * layout_factor
# node size
degree = nx_graph.degree()[node]
pv_graph.get_node(node)["value"] = degree
# hover information
pv_graph.get_node(node)["title"] = f'<a href=https://orcid.org/{node} target="_parent">{node}</a><br>{degree} edges'
# color when selected
pv_graph.get_node(node)["color"] = {
"highlight": {
"border": 'darkred',
"background": 'red'
}
}
# colored communites
#communities = nx.algorithms.community.louvain_communities(nx_graph)
#comm_colors = get_cmap("tab20").colors
#comm_colors = [rgb2hex(c) for c in comm_colors]
#i = 0
#for comm in communities:
# for node in list(comm):
# pv_graph.get_node(node)["color"] = {
# "border": comm_colors[i],
# "background": comm_colors[i+1]
# }
# i += 2
# save as HTML
pv_graph.show('graph.html')
# %%