-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterface_bokeh.py
233 lines (190 loc) · 9.33 KB
/
interface_bokeh.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
from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine, EdgesAndLinkedNodes, NodesAndLinkedEdges, LabelSet, Div, CustomJS, TextInput, RadioButtonGroup
from bokeh.plotting import figure, from_networkx
from bokeh.palettes import Blues8, Reds8, Purples8, Oranges8, Viridis8, Spectral8
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.transform import linear_cmap
from bokeh.models.widgets import Tabs, Panel
from bokeh.layouts import row, layout
from collections import Counter
import networkx as nx
import pandas as pd
def get_degree_distribution(network, title):
degree = [i[1] for i in nx.degree(network)]
values = sorted(degree)
hist = Counter(values)
total = network.number_of_nodes()
l1 = []
l2 = []
for i in hist:
l1.append(i)
l2.append(hist[i])
l2 = [i/total for i in l2]
p = figure(title=title, plot_width=400, plot_height=400,
y_axis_type="log", x_axis_type="log", toolbar_location = None)
p.line(l1, l2, line_width=2)
return p
def generate_year_graph(network, title="Years"):
size_by_this_attribute = 'adjusted_node_size'
color_by_this_attribute = 'adjusted_node_size'
color_palette = Viridis8
HOVER_TOOLTIPS = [("Faculty", "@index"), ("Degree", "@degree"),
("Betweenness Centrality", "@betweenness"),
("Clustering Coefficient", "@clustering")]
plot = figure(tooltips=HOVER_TOOLTIPS, tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',toolbar_location = None,
x_range=Range1d(-11.1, 11.1), y_range=Range1d(-11.1, 11.1), title=str(title), plot_width=400, plot_height=400)
network_graph = from_networkx(
network, nx.spring_layout, scale=10, center=(0, 0))
minimum_value_color = min(
network_graph.node_renderer.data_source.data[color_by_this_attribute])
maximum_value_color = max(
network_graph.node_renderer.data_source.data[color_by_this_attribute])
network_graph.node_renderer.glyph = Circle(size=size_by_this_attribute, fill_color=linear_cmap(
color_by_this_attribute, color_palette, minimum_value_color, maximum_value_color))
network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)
plot.renderers.append(network_graph)
return plot
def generate_giant_componnet(network, title = "Giant Components"):
HOVER_TOOLTIPS = [("Faculty", "@index")]
plot = figure(tooltips = HOVER_TOOLTIPS, toolbar_location = None, x_range=Range1d(-11.1, 11.1), y_range=Range1d(-11.1, 11.1), title=str(title), plot_width=400, plot_height=400)
network_graph = from_networkx(network, nx.spring_layout, scale=10, center=(0, 0))
network_graph.node_renderer.glyph = Circle()
network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)
plot.renderers.append(network_graph)
return plot
def generate_tab_year(graphs):
def get_table(first_col,second_col, third_col, clustering, edge_faculty ):
data = {first_col:[],second_col:[],third_col:[]}
for i in edge_faculty:
data[first_col].append(i[0])
data[second_col].append(i[1])
for i in data[first_col]:
data[third_col].append(clustering[i])
source = ColumnDataSource(data)
columns = [TableColumn(field=first_col, title=first_col),
TableColumn(field=second_col, title=second_col),
TableColumn(field=third_col, title=third_col)]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
return data_table
def get_div():
text = f"""
<div>
<h4>Network Properties</h4>
<ul>
<li>Number of Edges: {temp.year_info['number_of_edges']}</li>
<li>Mean Shortest Path Length: {temp.year_info['avg_dist']}</li>
<li>Average Clustering Coefficient : {temp.year_info['average_clustering_coefficient']}</li>
<li>Average Degree : {temp.year_info['average_degree']}</li>
<li>Number of Connected Components: {temp.year_info['number_of_connected_components']}</li>
<li>Density: {temp.year_info['density']}</li>
</ul>
</div>
"""
div = Div(text=text, width=500, height=100)
table = get_table("Faculty","Most Edge Faculty", "Global Clustering Coefficient", temp.year_info['global_clustering'], temp.year_info['most_edge_faculty'])
return layout([[div],[table]])
def get_first_row(temp):
year_collab = generate_year_graph(
temp.graph_year, f"Collaborations in {i}")
degree_distribution = get_degree_distribution(
temp.graph_year, "Degree Distribution")
return row([year_collab, degree_distribution])
text = """<h3>Collaboration over years</h3><hr/>"""
div = Div(text=text, width=500, height=100)
tabs = []
def connect_tabs(temp):
components = []
for i in temp.year_info['connected_components']:
components.append(generate_giant_componnet(i) )
lay = []
temp = []
for i in range(len(components)):
temp.append(components[i])
if (i+1)%3 == 0:
lay.append(temp)
temp = []
giant_component = layout(lay)
return giant_component
for i in range(2000, 2021):
temp = graphs[i]
stuff = []
year_tab = Panel(child = get_first_row(temp), title = "Year Analysis")
info_tab = Panel(child = get_div(), title = "Information")
connected_component_tab = Panel(child = connect_tabs(temp), title = "Connected Componnents")
till_now = Panel(child = generate_year_graph(temp.graph_previous_years, f"Collaborations from 2000 to {i}"), title = "Cummalative Collaboration")
grid = Tabs(tabs=[year_tab, info_tab, connected_component_tab])
tabs.append(Panel(child=grid, title=str(i)))
tab = Tabs(tabs=tabs)
disp = layout([div, tab])
return disp
def generate_faculty(Faculty, name):
def get_network_plot(network, title):
HOVER_TOOLTIPS = [("Faculty", "@index"), ("Degree", "@degree"),
("Betweenness Centrality", "@betweenness"),
("Clustering Coefficient", "@clustering")]
plot = figure(tooltips=HOVER_TOOLTIPS,tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom', toolbar_location = None,
x_range=Range1d(-11.1, 11.1), y_range=Range1d(-11.1, 11.1), title=str(title), plot_width=400, plot_height=400)
network_graph = from_networkx(
network, nx.spring_layout, scale=10, center=(0, 0))
network_graph.node_renderer.glyph = Circle()
network_graph.edge_renderer.glyph = MultiLine(
line_alpha=0.5, line_width=1)
plot.renderers.append(network_graph)
return plot
tabs = []
for i in range(2000, 2021):
grid = row([get_network_plot(Faculty.graph_years[i], f"Collaborations in {i} from SCSE"), get_network_plot(
Faculty.graph_years_all[i], f"Collaborations in {i} from all the papers")])
tabs.append(Panel(child=grid, title=str(i)))
text = f"""<h3>Collaboration for {name}</h3><hr/>"""
div = Div(text=text, width=500, height=100)
tab = Tabs(tabs=tabs)
disp = layout([div, tab])
return disp
def startingHtml():
text = """<h1>Network Science</h1>
<div>
In an attempt to understand the collaboration between the different professors.
This page contains :
<ul>
<li>Network Graph for collaboration between different professors in SCSE from 2000</li>
<li>Graph and information regarding a particular professor</li>
<ul/>
</div>"""
div = Div(text=text, width=500, height=100)
return div
def overall_information_graphs(year_data):
def get_average_degree_plot(title = "Average Degree"):
l1 = []
l2 = []
for i in range(2000,2021):
l1.append(i)
l2.append(year_data[i].year_info['average_degree'])
p = figure(title=title, plot_width=1000, plot_height=400, toolbar_location = None)
p.line(l1, l2, line_width=2)
return p
def get_average_clustering_plot(title = "Average Clustering Coefficient"):
l1 = []
l2 = []
for i in range(2000,2021):
l1.append(i)
l2.append(year_data[i].year_info['average_clustering_coefficient'])
p = figure(title=title, plot_width=1000, plot_height=400, toolbar_location = None)
p.line(l1, l2, line_width=2)
return p
t1 = get_average_degree_plot()
t2 = get_average_clustering_plot()
return layout([t1,t2])
def show_html(Year_Graph, name=None, Faculty=None):
years_scse = generate_tab_year(Year_Graph)
faculty = generate_faculty(Faculty, name)
properties = overall_information_graphs(Year_Graph)
panel_list = []
panel_list.append(Panel(child = years_scse, title = "Year Wise Analysis"))
panel_list.append(Panel(child = faculty, title = "Faculty Collaborations"))
panel_list.append(Panel(child = properties, title = "Analysis"))
tab = Tabs(tabs = panel_list)
l = [startingHtml(), tab]
disp = layout(l)
show(disp)