-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcal.py
600 lines (580 loc) · 19.4 KB
/
abcal.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 12 22:37:23 2022
@author: LS Le Clercq
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import statsmodels.api as sm
import io
import folium
import scipy.stats as stats
from scipy.stats import shapiro
from PIL import Image
print('ABC: Author Bias Computation (1.0.2)')
print('Copyright (C) 2023 Le Clercq')
print('This is free software. There is NO warranty; not even for',
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.')
print()
def menu_choice():
""" Find out what the user wants to do next. """
print("MAIN MENU:")
print("----------------------------------------")
print("Choose one of the following options?")
print(" a) Calculate Author bias")
print(" b) Calibrate Author bias")
print(" c) Check normality of Author bias distribution")
print(" d) Get upper/lower quartiles of Author bias")
print(" e) Create plots")
print(" f) Get descriptive/summary statistics")
print(" q) Quit")
print("----------------------------------------")
choice = input("Choice: ")
print()
if choice.lower() in ['a','b','c','d','e','f','q']:
return choice.lower()
else:
print(choice +"?")
print("Invalid option")
print()
return None
def author_bias():
''''Calculates the abundance of contributions from the same author to
test for possible bias'''
infile = input("Authors file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file " + infile + " does not exist."
print()
print(msg)
print()
return
Authors = pd.read_csv(infile)
length = len(Authors)
Author_list = list(Authors.iloc[0,1:length])
rows = len(Authors.index)
Num = 1
for i in range(1,rows):
AuthorAdd = Authors.iloc[i, 1:length]
Add = list(AuthorAdd)
Num = Num + 1
Author_list = Author_list + Add
Author_list = list(Author_list)
Cleaned_Authors = [x for x in Author_list if str(x) != 'nan']
Author_list_Unique = list(set(Cleaned_Authors))
AuthorDF = pd.DataFrame(columns = ['Author','Pubs'])
for x in Author_list_Unique:
occurrence = Cleaned_Authors.count(x)
AuthorDF = AuthorDF.append({'Author': x, 'Pubs':occurrence}, ignore_index=True)
output_name = input("File name (.csv) for Author counts: ")
if output_name.endswith('.csv') is True:
pass
elif output_name.endswith('.csv') is False:
output_name = output_name + '.csv'
AuthorDF.to_csv(output_name, index=False)
Sum = AuthorDF['Pubs'].sum()
Normlist = []
for x in AuthorDF['Author']:
PubsNorm = AuthorDF['Pubs']/Sum
Normlist = list(PubsNorm)
AuthorDF['Pubs.Norm'] = Normlist
length_2 = len(AuthorDF.index)
Names = []
Values = []
for i in range(0, length_2):
old_value = AuthorDF.iloc[i]['Author']
Names.append(old_value)
new_value = AuthorDF.iloc[i]['Pubs.Norm']
Values.append(new_value)
Authors.replace(to_replace=old_value, value=new_value, inplace=True)
Individual_bias = pd.DataFrame(list(zip(Names, Values)),
columns = ['Author', 'Ind.Bias'])
output_name_2 = input("File name (.csv) for calculated values (Individual): ")
if output_name_2.endswith('.csv') is True:
pass
elif output_name_2.endswith('.csv') is False:
output_name_2 = output_name_2 + '.csv'
Individual_bias.to_csv(output_name_2, index=False)
Bias = Authors.sum(axis=1)
Authors['Bias'] = Bias
Authors_Out = pd.DataFrame(Authors) #columns=['Paper', 'Bias'])
output_name_3 = input("File name (.csv) for calculated values (Paper): ")
if output_name_3.endswith('.csv') is True:
pass
elif output_name_3.endswith('.csv') is False:
output_name_3 = output_name_3 + '.csv'
Authors_Out.to_csv(output_name_3, index=False)
print()
print()
def calibrate():
''''Calibrate to Author number'''
infile = input("Authors with bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file " + infile + " does not exist."
print()
print(msg)
print()
return
Authors = pd.read_csv(infile)
Normlist = []
for i in range(0,len(Authors.index)):
Author_list = list(Authors.iloc[i][1:-1])
Author_list = [x for x in Author_list if str(x) != 'nan']
Auth_num = len(Author_list)
PubsNorm_1 = Authors.iloc[i]['Bias']
PubsNorm_2 = PubsNorm_1/Auth_num
Normlist.append(PubsNorm_2)
Authors['Cal.Bias'] = Normlist
Authors_Out = pd.DataFrame(Authors, columns=['Paper', 'Bias', 'Cal.Bias'])
output_name = input("File name (.csv) for calibrated values: ")
if output_name.endswith('.csv') is True:
pass
elif output_name.endswith('.csv') is False:
output_name = output_name + '.csv'
Authors_Out.to_csv(output_name, index=False)
print()
print()
def normality_menu():
"""Brings up the menu options for testing normality"""
print("NORMALITY MENU:")
print("----------------------------------------")
print("Choose one of the following options?")
print(" a) Shapiro-Wilk")
print(" b) QQ Plot")
print(" c) Histogram")
print(" q) Quit")
print("----------------------------------------")
choice = input("Choice: ")
print()
if choice.lower() in ['a','b','c','q']:
return choice.lower()
else:
print(choice +"?")
print("Invalid option")
print()
return None
def normality():
'''Returns function to test normality'''
choice = normality_menu()
if choice == 'a':
check_norm_shapiro()
if choice == 'b':
check_norm_QQ()
if choice == 'c':
check_norm_histo()
def check_norm_shapiro():
'''Verify normal distribution by Shapiro-Wilk'''
infile = input("Authors with computed bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file " + infile + " does not exist."
print()
print(msg)
print()
return
Data = pd.read_csv(infile)
Bias = Data['Cal.Bias']
Normality = shapiro(Bias)
Statistic = Normality.statistic
PValue = Normality.pvalue
file = open('Normality.txt','w')
file.write('Test of Normality:\n')
file.write('--------------------\n')
file.write('Shapiro-Wilk: ' + "{:.3f}".format(Statistic) + '\n')
if PValue <= 0.01:
file.write('P-value: ' + str(PValue) + ' ***\n')
file.write('Result: Not Normal')
elif PValue <= 0.02:
file.write('P-value: ' + str(PValue) + ' **\n')
file.write('Result: Not Normal')
elif PValue <= 0.05:
file.write('P-value: ' + str(PValue) + ' *\n')
file.write('Result: Not Normal')
elif PValue > 0.05:
file.write('P-value: ' + str(PValue) + '\n')
file.write('Result: Normal')
file.close()
print()
print()
def check_norm_QQ():
'''Plots the QQ plot to check normality'''
infile = input("Authors with computed bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file " + infile + " does not exist."
print()
print(msg)
print()
return
Data = pd.read_csv(infile)
Bias = Data['Cal.Bias']
Bias = Bias.to_frame()
Values = Bias["Cal.Bias"].values.tolist()
Values = np.array(Values)
fig, ax = plt.subplots(figsize =(10, 7))
plt.rcParams['axes.facecolor'] = '#f2f2f5'
font = {'family': 'serif',
'color': 'black',
'weight': 'normal',
}
ax.set_facecolor('#f2f2f5')
ax.set_title("Quantile plot for bias values", fontdict=font, fontsize=20, pad=15)
ax.set_xlabel("Theoretical quantile", fontdict=font, fontsize=15, labelpad=10)
ax.set_ylabel("Sample quantile", fontdict=font, fontsize=15, labelpad=10)
sm.qqplot(Values, line ='45', scale=2, ax=ax)
fig.savefig('QQ Plot.png')
def check_norm_histo():
'''Plots the histograms to check normality'''
infile = input("Authors with computed bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file "+ infile + " does not exist."
print()
print(msg)
print()
return
Data = pd.read_csv(infile)
Bias = Data['Cal.Bias']
Bias = Bias.to_frame()
Values = Bias["Cal.Bias"].values.tolist()
Values = np.array(Values)
fig, ax = plt.subplots(figsize =(10, 7))
plt.rcParams['axes.facecolor'] = '#f2f2f5'
font = {'family': 'serif',
'color': 'black',
'weight': 'normal',
}
ax.set_facecolor('#f2f2f5')
ax.set_title("Histogram of distribution for bias values", fontdict=font, fontsize=20, pad=15)
ax.set_xlabel("Bias value", fontdict=font, fontsize=15, labelpad=10)
ax.set_ylabel("Count", fontdict=font, fontsize=15, labelpad=10)
ax.hist(Values, color='#bc5090')
plt.savefig("Histogram of bias values.png")
print()
print('Done!')
print()
def get_quantiles():
'''Get the quantiles and determine level of bias'''
infile = input("Authors with bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file "+ infile + " does not exist."
print()
print(msg)
print()
return
Data = pd.read_csv(infile)
Bias = Data['Cal.Bias']
Quantiles = np.quantile(Bias, [0,0.33,0.5,0.66,1])
Upper = Quantiles[3]
Lower = Quantiles[1]
file = open('Quantiles.txt','w')
file.write('The Quatiles are:\n')
file.write('--------------------\n')
file.write('\n')
file.write('Min ' + "{:.4f}".format(Quantiles[0]) + '\n')
file.write('Max: ' + "{:.4f}".format(Quantiles[4]) + '\n')
file.write('\n')
file.write('Q33: ' + "{:.4f}".format(Quantiles[1]) + '\n')
file.write('Q50: ' + "{:.4f}".format(Quantiles[2]) + '\n')
file.write('Q66: ' + "{:.4f}".format(Quantiles[3]) + '\n')
file.write('\n')
file.write('The Levels are:\n')
file.write('--------------------\n')
file.write('\n')
file.write('Low: ' + "{:.4f}".format(Quantiles[0]) +
' to ' + "{:.4f}".format(Quantiles[1]) + '\n')
file.write('Medium: ' + "{:.4f}".format(Quantiles[1]) +
' to ' + "{:.4f}".format(Quantiles[3]) + '\n')
file.write('High: ' + "{:.4f}".format(Quantiles[3]) +
' to ' + "{:.4f}".format(Quantiles[4]))
file.close()
Level = []
len(Bias)
for items in Bias:
value = items
if value >= Upper:
Add = 'High'
Level.append(Add)
elif value <= Lower:
Add = 'Low'
Level.append(Add)
else:
Add = 'Medium'
Level.append(Add)
Data['Level'] = Level
output_name = input("File name (.csv) for quantile leveled values: ")
if output_name.endswith('.csv') is True:
pass
elif output_name.endswith('.csv') is False:
output_name = output_name + '.csv'
Data.to_csv(output_name, index=False)
print()
print()
def plots_menu():
"""Brings up the menu options for generating plots"""
print("PLOTS MENU:")
print("----------------------------------------")
print("Choose one of the following options?")
print(" a) Plot by Year")
print(" b) Plot by Authors")
print(" c) Plot by Location")
print(" d) Plot z-values of Calibrated Bias")
print(" q) Quit")
print("----------------------------------------")
choice = input("Choice: ")
print()
if choice.lower() in ['a','b','c','d','q']:
return choice.lower()
else:
print(choice +"?")
print("Invalid option")
print()
return None
def plots():
'''Brings up funtion to call for plot'''
choice = plots_menu()
if choice == 'a':
year_plot()
if choice == 'b':
author_plot()
if choice == 'c':
location_plot()
if choice == 'd':
bias_plot()
def year_plot():
'''plots publications by year'''
infile = input("Authors file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file "+ infile + " does not exist."
print()
print(msg)
print()
return
Authors = pd.read_csv(infile)
Years = Authors['Year']
Papers = Authors['Paper']
df2 = pd.DataFrame(list(zip(Papers, Years)),
columns=['Paper','Year'])
df3 = df2.groupby(['Year']).size()
YearsData = list(set(Years))
YearsData = sorted(YearsData)
fig, ax = plt.subplots(figsize =(16, 10))
tick_spacing = 1
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.rcParams['axes.facecolor'] = '#f2f2f5'
font = {'family': 'serif',
'color': 'black',
'weight': 'normal',
}
ax.set_facecolor('#f2f2f5')
ax.set_title("Publications by year", fontdict=font, fontsize=20, pad=15)
ax.set_xlabel("Year", fontdict=font, fontsize=15, labelpad=10)
ax.set_ylabel("Publications", fontdict=font, fontsize=15, labelpad=10)
ax.bar(YearsData,df3, color='#003f5c')
plt.savefig("Year Plot.png")
plt.show()
print()
print("Done!")
print()
def author_plot():
"""Plots the top Authors for a given analysis"""
infile = input("Authors file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file "+ infile + " does not exist."
print()
print(msg)
print()
return
Authors = pd.read_csv(infile)
Number = int(input("Number of Authors: "))
df4 = Authors[['Author','Pubs']].nlargest(n=Number, columns='Pubs')
TopAuthors = df4['Author']
Pubs = df4['Pubs']
fig_2, ax = plt.subplots(figsize =(16, 9))
font = {'family': 'serif',
'color': 'black',
'weight': 'normal',
}
colors = ['#003f5c','#58508d','#bc5090','#ff6361','#ffa600']
ax.barh(TopAuthors, Pubs, color=colors)
ax.set_facecolor('#f2f2f5')
ax.set_title("Publications by author", fontdict=font, fontsize=20, pad=15)
ax.set_xlabel("Number of publications", fontdict=font, fontsize=15, labelpad=10)
ax.set_ylabel("Authors", fontdict=font, fontsize=15, labelpad=10)
for s in ['top', 'bottom', 'left', 'right']:
ax.spines[s].set_visible(False)
plt.savefig("Author Plot.png")
plt.show()
print()
print("Done!")
print()
def location_plot():
'''Plots the publications per location'''
infile = input("Locations file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file "+ infile + " does not exist."
print()
print(msg)
print()
return
Locations = pd.read_csv(infile)
Location = Locations['Location']
Papers = Locations['Paper']
df4 = pd.DataFrame(list(zip(Papers, Location)),
columns=['Paper','Location'])
df5 = df4.groupby(['Location']).size()
df5 = pd.Series.to_frame(df5)
df5 = df5.reset_index()
df5.columns = ['Location', 'Publications']
End = max(df5['Publications']) + 1
political_countries_url = (
"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson"
)
world_map = folium.Map(location=(30, 10), tiles="cartodbpositron",
control_scale=True, zoom_start=2)
folium.Choropleth(
geo_data=political_countries_url,
data=df5,
columns=["Location", "Publications"],
key_on="feature.properties.name",
fill_color="RdYlGn_r",
fill_opacity=0.8,
line_opacity=0.3,
nan_fill_color="white",
legend_name="Publications by Country",
bins=list(range(0,End,1))
).add_to(world_map)
world_map.save("Locations plot.html")
img_data = world_map._to_png(5)
img = Image.open(io.BytesIO(img_data))
img.save('Location plot.png', dpi=(600,600))
print()
print('Done!')
print()
def bias_plot():
'''Plot of bias measure computed for authors'''
infile = input("Computed bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
try:
pd.read_csv(infile)
except FileNotFoundError:
msg = "Sorry, the file "+ infile + " does not exist."
print()
print(msg)
print()
return
Data = pd.read_csv(infile)
Data = pd.DataFrame(Data)
Bias = Data['Cal.Bias']
ZBias = stats.zscore(Bias)
Data['Z-Score'] = ZBias
Data.to_csv(infile, index=False)
fig, ax = plt.subplots(figsize =(10, 7))
font = {'family': 'serif',
'color': 'black',
'weight': 'normal',
}
bplot1 = ax.boxplot(ZBias, patch_artist = True,
notch ='True')
colorbar = ['#58508d']
for bplot in (bplot1):
for patch, color in zip(bplot1['boxes'], colorbar):
patch.set_facecolor(color)
ax.set_facecolor('#f2f2f5')
ax.set_title("Publications bias", fontdict=font, fontsize=20, pad=15)
ax.set_xlabel("Bias", fontdict=font, fontsize=15, labelpad=10)
ax.set_ylabel("Z-values", fontdict=font, fontsize=15, labelpad=10)
plt.savefig("Bias plot.png")
def descriptive_stats():
"""Generates descriptive statistics for values"""
infile = input("Authors with computed bias file (.csv): ")
if infile.endswith('.csv') is True:
pass
elif infile.endswith('.csv') is False:
infile = infile + '.csv'
Data = pd.read_csv(infile)
Data = pd.DataFrame(Data)
Data_2 = Data.describe()
print(Data_2)
Data_2.to_csv("Descriptive Stats.csv", index=True)
print()
def main_loop():
"""The main loop of the script"""
while True:
choice = menu_choice()
if choice is None:
continue
elif choice == 'q':
print( "Exiting...")
break # jump out of while loop
elif choice == 'a':
author_bias()
elif choice == 'b':
calibrate()
elif choice == 'c':
normality()
elif choice == 'd':
get_quantiles()
elif choice == 'e':
plots()
elif choice == 'f':
descriptive_stats()
else:
print("Invalid choice.")
if __name__ == '__main__':
main_loop()