-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.py
318 lines (288 loc) · 9.6 KB
/
callbacks.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: utf-8 -*-
"""Define the callbacks of the Dash application."""
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd
import numpy as np
from app import app, df
from constants import COLOR_MAP, LABELS, RESEARCH_CATEGORIES
# Set alternative color scheme
color_list = px.colors.qualitative.Antique
# Move grey to fifth position
color_list.insert(4, color_list.pop(10))
# --- HELPER FUNCTIONS ---
def filter_dataframe(filter_categories, year_range):
"""Init mask with only False values, add selected categories and filter by year range."""
mask = pd.Series(index=df.index, dtype=bool)
for category in filter_categories:
mask = mask | df[category].astype(bool)
return df[mask & (df['PY'] >= year_range[0]) & (df['PY'] <= year_range[1])]
def calc_country_org_count(dff):
"""Calculate the count of organisation by country."""
# Count of organisation by country
counts = dff.groupby(['CountryCode', 'Organisation']).size().unstack()
# Flatten hierarchical columns
counts.columns = counts.columns.tolist()
# Add country names from dataset
counts = counts.merge(
dff[['CountryCode', 'Country']],
how='left',
on='CountryCode'
).drop_duplicates().reset_index(drop=True)
# Calculate fractions
counts['CompanyAcademiaFraction'] = 100 / (
counts['Academia'] / counts['Company'] + 1)
counts['CompanyCollaborationFraction'] = 100 / (
counts['Collaboration'] / counts['Company'] + 1)
counts['CollaborationAcademiaFraction'] = 100 / (
counts['Academia'] / counts['Collaboration'] + 1)
counts['CompanyAcademiaCollabFraction'] = 100 / ((
counts['Academia'] +
counts['Collaboration']
) / (
counts['Company'] +
counts['Collaboration']
) + 1)
return counts
def draw_histogram(dff):
"""Draw the histogram chart."""
# Count of organisation by year
year_org_count = pd.DataFrame({'Count': dff.groupby(['PY', 'Organisation']).size()}).reset_index()
fig = px.bar(
year_org_count,
x='PY',
y='Count',
barmode='group',
color='Organisation',
color_discrete_map=COLOR_MAP,
labels=LABELS,
title='Publications of Organisations by Year'
).update_layout(
title_x=0.5
)
return fig
def draw_pie(dff):
"""Draw the pie chart."""
# Count of organisation
org_count = pd.DataFrame({'Count': dff.groupby(['Organisation']).size()}).reset_index()
fig = px.pie(
org_count,
values='Count',
names='Organisation',
color='Organisation',
color_discrete_map=COLOR_MAP,
title='Distribution of Publications'
).update_layout(
title_x=0.5
)
return fig
def draw_category_pies(dff):
"""Draw the category pie charts."""
# Count of organisation type for each category
category_org_count = dff[[
*RESEARCH_CATEGORIES,
'Organisation'
]].replace(0, np.nan).groupby('Organisation').agg('count').T
# Flatten categorical columns
category_org_count.columns = category_org_count.columns.tolist()
# Set names properly and reset the index
category_org_count['Total'] = category_org_count.sum(axis='columns')
category_org_count = category_org_count.reset_index().rename({'index': 'Category'}, axis='columns').replace(LABELS)
pie_cat_all = px.pie(
category_org_count,
values='Total',
names='Category',
color='Category',
color_discrete_sequence=color_list,
labels=LABELS,
title='Overall Distribution'
).update_layout(
title_x=0.5
)
pie_cat_academia = px.pie(
category_org_count,
values='Academia',
names='Category',
color='Category',
color_discrete_sequence=color_list,
labels=LABELS,
title='Academia'
).update_layout(
showlegend=False,
title_x=0.5
)
pie_cat_companies = px.pie(
category_org_count,
values='Company',
names='Category',
color='Category',
color_discrete_sequence=color_list,
labels=LABELS,
title='Companies'
).update_layout(
showlegend=False,
title_x=0.5
)
pie_cat_collaborations = px.pie(
category_org_count,
values='Collaboration',
names='Category',
color='Category',
color_discrete_sequence=color_list,
labels=LABELS,
title='Collaborations'
).update_layout(
showlegend=False,
title_x=0.5
)
return [pie_cat_all, pie_cat_academia, pie_cat_companies, pie_cat_collaborations]
# --- CALLBACKS ---
@app.callback(Output('choropleth-map', 'figure'),
Input('map-tabs', 'value'),
Input('map-data', 'children'))
def draw_map(tab, counts_json):
"""Draw the four different choropleth maps."""
# Import jsonified saved map-data
country_org_count = pd.read_json(counts_json, orient='split')
choro_map_comp_acad = px.choropleth(
country_org_count,
locations='CountryCode',
color='CompanyAcademiaFraction',
hover_name='Country',
hover_data=['Academia', 'Company', 'Collaboration'],
labels=LABELS,
color_continuous_scale=[
(0, COLOR_MAP['Academia']),
(1, COLOR_MAP['Company'])
],
range_color=[0, 20],
title='Company to Academia Publication Fractions',
center={'lat': 20}
).update_layout(
title_x=0.5,
height=800,
coloraxis_colorbar=dict(
title='Company Fraction',
ticks='outside',
ticksuffix='%'
)
).update_geos(
visible=False,
showland=True,
landcolor='#ccc',
showcoastlines=True,
projection_type='natural earth'
)
choro_map_comp_collab = px.choropleth(
country_org_count,
locations='CountryCode',
color='CompanyCollaborationFraction',
hover_name='Country',
hover_data=['Academia', 'Company', 'Collaboration'],
labels=LABELS,
color_continuous_scale=[
(0, COLOR_MAP['Collaboration']),
(1, COLOR_MAP['Company'])
],
range_color=[0, 16],
title='Company to Collaboration Publication Fractions',
center={'lat': 20}
).update_layout(
title_x=0.5,
height=800,
coloraxis_colorbar=dict(
title='Company Fraction',
ticks='outside',
ticksuffix='%'
)
).update_geos(
visible=False,
showland=True,
landcolor='#ccc',
showcoastlines=True,
projection_type='natural earth'
)
choro_map_collab_acad = px.choropleth(
country_org_count,
locations='CountryCode',
color='CollaborationAcademiaFraction',
hover_name='Country',
hover_data=['Academia', 'Company', 'Collaboration'],
labels=LABELS,
color_continuous_scale=[
(0, COLOR_MAP['Academia']),
(1, COLOR_MAP['Collaboration'])
],
range_color=[40, 100],
title='Collaboration to Academia Publication Fractions',
center={'lat': 20}
).update_layout(
title_x=0.5,
height=800,
coloraxis_colorbar=dict(
title='Collabor. Fraction',
ticks='outside',
ticksuffix='%'
)
).update_geos(
visible=False,
showland=True,
landcolor='#ccc',
showcoastlines=True,
projection_type='natural earth'
)
choro_map_comp_acad_collab = px.choropleth(
country_org_count,
locations='CountryCode',
color='CompanyAcademiaCollabFraction',
hover_name='Country',
hover_data=['Academia', 'Company', 'Collaboration'],
labels=LABELS,
color_continuous_scale=[
(0, COLOR_MAP['Academia']),
(1, COLOR_MAP['Company'])
],
range_color=[30, 50],
title='Company to Academia Publication Fractions (Collab. count for both)',
center={'lat': 20}
).update_layout(
title_x=0.5,
height=800,
coloraxis_colorbar=dict(
title='Company Fraction',
ticks='outside',
ticksuffix='%'
)
).update_geos(
visible=False,
showland=True,
landcolor='#ccc',
showcoastlines=True,
projection_type='natural earth'
)
if tab == 'comp-acad-collab':
return choro_map_comp_acad_collab
elif tab == 'comp-acad':
return choro_map_comp_acad
elif tab == 'comp-collab':
return choro_map_comp_collab
elif tab == 'collab-acad':
return choro_map_collab_acad
@app.callback(Output('histogram-year', 'figure'),
Output('pie-org', 'figure'),
Output('map-data', 'children'),
Output('pie-cat-all', 'figure'),
Output('pie-cat-academia', 'figure'),
Output('pie-cat-companies', 'figure'),
Output('pie-cat-collaborations', 'figure'),
Input('submit-button-state', 'n_clicks'),
State('category-filter', 'value'),
State('year-slider', 'value'))
def create_charts(_n_clicks, filter_categories, year_range):
"""Calls functions for creating/updating charts and outputs them."""
del _n_clicks # n_clicks is only used for triggering this function
dff = filter_dataframe(filter_categories, year_range)
return (draw_histogram(dff),
draw_pie(dff),
calc_country_org_count(dff).to_json(orient='split'),
*draw_category_pies(dff))