-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathScientoPyClass.py
executable file
·589 lines (483 loc) · 23 KB
/
ScientoPyClass.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
# The MIT License (MIT)
# Copyright (c) 2018 - Universidad del Cauca, Juan Ruiz-Rosero
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
import paperUtils
import paperSave
import globalVar
import os
import matplotlib.pyplot as plt
import numpy as np
import graphUtils
import sys
import re
import time
from PIL import Image
class ScientoPyClass:
def __init__(self, from_gui=False):
# Parameters variables
self.criterion = 'authorKeywords'
self.graphType = 'bar_trends'
self.length = 10
self.skipFirst = 0
self.topics = ''
self.startYear = globalVar.DEFAULT_START_YEAR
self.endYear = globalVar.DEFAULT_END_YEAR
self.savePlot = ''
self.noPlot = False
self.agrForGraph = False
self.wordCloudMask = ''
self.windowWidth = 2
self.previousResults = False
self.onlyFirst = False
self.graphTitle = ''
self.pYear = False
self.plotWidth = globalVar.DEFAULT_PLOT_WIDTH
self.plotHeight = globalVar.DEFAULT_PLOT_HEIGHT
self.trend = False
self.yLog = False
self.filter = ""
self.fromGui = from_gui
# Working variables
self.papersDict = []
self.resultsFileName = ''
self.extResultsFileName = ''
self.lastPreviousResults = ''
self.preprocessBriefFileName = os.path.join(globalVar.DATA_OUT_FOLDER, globalVar.PREPROCESS_LOG_FILE)
self.preprocessDatasetFile = os.path.join(globalVar.DATA_OUT_FOLDER, globalVar.OUTPUT_FILE_NAME)
self.topicResults = []
self.yearArray = []
self.startYearIndex = 0
self.endYearIndex = 0
def closePlot(self):
plt.close()
def scientoPy(self, args=''):
globalVar.cancelProcess = False
globalVar.progressText = "Reading dataset"
globalVar.progressPer = 0
# To let progress bar open
if self.fromGui:
time.sleep(0.01)
if args == '':
args = self
print("\n\nScientoPy: %s" % (globalVar.SCIENTOPY_VERSION))
print("================\n")
# Check python version
if sys.version_info[0] < 3:
print("ERROR, you are using Python 2, Python 3.X.X required")
print("")
exit()
# Validate window Width
if args.windowWidth < 1:
print("ERROR: minimum windowWidth 1")
exit()
# Validate start and end years
if args.startYear > args.endYear:
print("ERROR: startYear > endYear")
exit()
# Create output folders if not exist
if not os.path.exists(os.path.join(globalVar.GRAPHS_OUT_FOLDER)):
os.makedirs(os.path.join(globalVar.GRAPHS_OUT_FOLDER))
if not os.path.exists(os.path.join(globalVar.RESULTS_FOLDER)):
os.makedirs(os.path.join(globalVar.RESULTS_FOLDER))
# Select the input file
if args.previousResults:
INPUT_FILE = os.path.join(globalVar.RESULTS_FOLDER, globalVar.OUTPUT_FILE_NAME)
else:
INPUT_FILE = os.path.join(globalVar.DATA_OUT_FOLDER, globalVar.OUTPUT_FILE_NAME)
# Start the output list empty
papersDictOut = []
topicList = []
loadDataSet = False
if len(self.papersDict) == 0 or args.previousResults:
loadDataSet = True
if args.previousResults == False and self.lastPreviousResults == True:
loadDataSet = True
# Open the dataset only if not loaded in papersDict
if loadDataSet:
self.papersDict = []
self.lastPreviousResults = args.previousResults
# Open the storage database and add to sel.fpapersDict
if not os.path.isfile(INPUT_FILE):
print("ERROR: %s file not found" % INPUT_FILE)
print("Make sure that you have run the preprocess step before run scientoPy")
exit()
ifile = open(INPUT_FILE, "r", encoding='utf-8')
print("Reading file: %s" % (INPUT_FILE))
globalVar.progressPer = 10
paperUtils.openFileToDict(ifile, self.papersDict)
ifile.close()
if globalVar.cancelProcess:
return
# If reading previous results, remove possible duplicated from multiple topics
if args.previousResults:
self.papersDict= paperUtils.removeDuplicates(self.papersDict)
print("Scopus papers: %s" % globalVar.papersScopus)
print("WoS papers: %s" % globalVar.papersWoS)
print("Omitted papers: %s" % globalVar.omitedPapers)
print("Total papers: %s" % len(self.papersDict))
# Create a self.yearArray
self.yearArray = range(args.startYear, args.endYear + 1)
yearPapers = {}
for i in range(args.startYear, args.endYear + 1):
yearPapers[i] = 0
# Filter papers with invalid year
self.papersDict = list(filter(lambda x: x["year"].isdigit(), self.papersDict))
# Filter the papers outside the year range
papersDictInside = self.papersDict.copy()
papersDictInside = list(filter(lambda x: int(x["year"]) >= args.startYear, papersDictInside))
papersDictInside = list(filter(lambda x: int(x["year"]) <= args.endYear, papersDictInside))
print("Total papers in range (%s - %s): %s" %
(args.startYear, args.endYear, len(papersDictInside)))
# If no papers in the range exit
if (len(papersDictInside) == 0):
print("ERROR: no papers found in the range.")
del papersDictInside
return
# Find the number of total papers per year
for paper in papersDictInside:
if int(paper["year"]) in yearPapers.keys():
yearPapers[int(paper["year"])] += 1
# Get the filter options
filterSubTopic = ""
if args.filter:
filterSubTopic = args.filter.strip()
print("Filter Sub Topic: %s" % filterSubTopic)
# Parse custom topics
if args.topics:
print("Custom topics entered:")
# Divide the topics by ;
topicsFirst = args.topics.split(";")
for x in topicsFirst:
topicList.append(x.split(","))
# Remove beginning and ending space from topics, and empty topics
for topic in topicList:
for idx, item in enumerate(topic):
topic[idx] = item.strip()
if not topic[idx]:
topic.remove(topic[idx])
if not topic:
topicList.remove(topic)
# Remove for each sub topic, start and end spaces
for item1 in topicList:
for item2 in item1:
item2 = item2.strip()
for topic in topicList:
print(topic)
# Find the top topics
else:
print("Finding the top topics...")
globalVar.progressPer = 30
globalVar.progressText = "Finding the top topics"
topicDic = {}
# For each paper, get the full topicDic
for paper in papersDictInside:
if globalVar.cancelProcess:
return
# For each item in paper criteria
for item in paper[args.criterion].split(";"):
# Strip paper item and upper case
item = item.strip()
item = item.upper()
# If paper item empty continue
if item == "":
continue
# If filter sub topic, omit items outside that do not match with the subtopic
if filterSubTopic != "" and len(item.split(",")) >= 2:
if (item.split(",")[1].strip().upper() != filterSubTopic.upper()):
continue
# If topic already in topicDic
if item in topicDic:
topicDic[item] += 1
# If topic is not in topicDic, create this in topicDic
else:
topicDic[item] = 1
# If onlyFirst, only keep the firt processesing
if args.onlyFirst:
break
# If trending analysis, the top topic list to analyse is bigger
if args.trend:
topicListLength = globalVar.TOP_TREND_SIZE
startList = 0
else:
topicListLength = args.length
startList = args.skipFirst
# Get the top topics by the topDic count
topTopcis = sorted(topicDic.items(),
key=lambda x: -x[1])[startList:(startList + topicListLength)]
# Put the topTopics in topic List
for topic in topTopcis:
topicList.append([topic[0]])
if len(topicList) == 0:
print("\nFINISHED : There is not results with your inputs criteria or filter")
del papersDictInside
return
# print("Topic list:")
# print(topicList)
# Create a dictonary in self.topicResults list per element in topicList
self.topicResults = []
for topics in topicList:
topicItem = {}
topicItem["upperName"] = topics[0].upper()
# If the topic name was given as an argument, use the first one given, else keep empty to use the first one found
if args.topics:
topicItem["name"] = topics[0]
else:
topicItem["name"] = ""
topicItem["allTopics"] = topics
topicItem["year"] = self.yearArray
topicItem["PapersCount"] = [0] * len(self.yearArray)
topicItem["PapersCountAccum"] = [0] * len(self.yearArray)
topicItem["PapersCountRate"] = [0] * len(self.yearArray)
topicItem["PapersTotal"] = 0
topicItem["AverageDocPerYear"] = 0 # ADY
topicItem["PapersInLastYears"] = 0
topicItem["PerInLastYears"] = 0 # PDLY
topicItem["CitedByCount"] = [0] * len(self.yearArray)
topicItem["CitedByCountAccum"] = [0] * len(self.yearArray)
topicItem["CitedByTotal"] = 0
topicItem["papers"] = []
topicItem["topicsFound"] = []
topicItem["hIndex"] = 0
topicItem["agr"] = 0 # Average growth rate
self.topicResults.append(topicItem)
# Find papers within the arguments, and fill the self.topicResults fields per year.
print("Calculating papers statistics...")
globalVar.progressText = "Calculating papers statistics"
papersLen = len(papersDictInside)
papersCounter = 0
# For each paper
for paper in papersDictInside:
papersCounter += 1
progressPer = int(float(papersCounter) / float(papersLen) * 100)
globalVar.progressPer = progressPer
if globalVar.cancelProcess:
return
# For each item in paper criteria
for item in paper[args.criterion].split(";"):
# Strip paper item and upper
item = item.strip()
itemUp = item.upper()
# For each topic in topic results
for topicItem in self.topicResults:
# for each sub topic
for subTopic in topicItem["allTopics"]:
# Check if the sub topic match with the paper item
if args.topics and "*" in subTopic.upper():
subTopicRegex = subTopic.upper().replace("*", ".*")
p = re.compile(subTopicRegex)
match = p.match(itemUp)
else:
match = subTopic.upper() == itemUp
# If match, sum it to the topicItem
if match:
yearIndex = topicItem["year"].index(int(paper["year"]))
topicItem["PapersCount"][yearIndex] += 1
topicItem["PapersTotal"] += 1
topicItem["CitedByCount"][yearIndex] += int(paper["citedBy"])
topicItem["CitedByTotal"] += int(paper["citedBy"])
# If no name in the topicItem, put the first one that was found
if topicItem["name"] == "":
topicItem["name"] = item
topicItem["papers"].append(paper)
# Add the matched paper to the papersDictOut
papersDictOut.append(paper)
# If it is a new topic, add it to topicItem["topicsFound"]
if itemUp not in [x.upper() for x in topicItem["topicsFound"]]:
topicItem["topicsFound"].append(item)
# Only process one (the first one) if args.onlyFirst
if args.onlyFirst:
break
# Print the topics found if the asterisk willcard was used
for topicItem in self.topicResults:
for subTopic in topicItem["allTopics"]:
if args.topics and "*" in subTopic.upper():
print("\nTopics found for %s:" % subTopic)
print('"' + ';'.join(topicItem["topicsFound"]) + '"')
print("")
print("Calculating accumulative ...")
# Extract accumulative
for topicItem in self.topicResults:
citedAccumValue = 0
papersAccumValue = 0
for i in range(0, len(topicItem["CitedByCountAccum"])):
citedAccumValue += topicItem["CitedByCount"][i]
topicItem["CitedByCountAccum"][i] = citedAccumValue
papersAccumValue += topicItem["PapersCount"][i]
topicItem["PapersCountAccum"][i] = papersAccumValue
print("Calculating Average Growth Rate (AGR)...")
# Extract the Average Growth Rate (AGR)
for topicItem in self.topicResults:
# Calculate rates
pastCount = 0
# Per year with papers count data
for i in range(0, len(topicItem["PapersCount"])):
topicItem["PapersCountRate"][i] = topicItem["PapersCount"][i] - pastCount
pastCount = topicItem["PapersCount"][i]
# Calculate AGR from rates
self.endYearIndex = len(topicItem["year"]) - 1
self.startYearIndex = self.endYearIndex - (args.windowWidth - 1)
topicItem["agr"] = \
round(np.mean(topicItem["PapersCountRate"][self.startYearIndex: self.endYearIndex + 1]), 1)
print("Calculating Average Documents per Year (ADY)...")
# Extract the Average Documents per Year (ADY)
for topicItem in self.topicResults:
# Calculate ADY from rates
self.endYearIndex = len(topicItem["year"]) - 1
self.startYearIndex = self.endYearIndex - (args.windowWidth - 1)
topicItem["AverageDocPerYear"] = \
round(np.mean(topicItem["PapersCount"][self.startYearIndex: self.endYearIndex + 1]), 1)
topicItem["PapersInLastYears"] = \
np.sum(topicItem["PapersCount"][self.startYearIndex: self.endYearIndex + 1])
if topicItem["PapersTotal"] > 0:
topicItem["PerInLastYears"] = \
round(100 * topicItem["PapersInLastYears"] / topicItem["PapersTotal"], 1)
# Scale in percentage per year
if args.pYear:
for topicItem in self.topicResults:
for year, value in yearPapers.items():
index = topicItem["year"].index(year)
if value != 0:
topicItem["PapersCount"][index] /= (float(value) / 100.0)
print("Calculating h-index...")
# Calculate h index per topic
for topicItem in self.topicResults:
# print("\n" + topicName)
# Sort papers by cited by count
papersIn = topicItem["papers"]
papersIn = sorted(papersIn, key=lambda x: int(x["citedBy"]), reverse=True)
count = 1
hIndex = 0
for paper in papersIn:
# print(str(count) + ". " + paper["citedBy"])
if int(paper["citedBy"]) >= count:
hIndex = count
count += 1
# print("hIndex: " + str(hIndex))
topicItem["hIndex"] = hIndex
# Sort by PapersTotal, and then by name.
self.topicResults = sorted(self.topicResults, key=lambda x: x["name"], reverse=False)
self.topicResults = sorted(self.topicResults, key=lambda x: int(x["PapersTotal"]), reverse=True)
# If trend analysis, sort by agr, and get the first ones
if args.trend:
self.topicResults = sorted(self.topicResults, key=lambda x: int(x["agr"]), reverse=True)
self.topicResults = self.topicResults[args.skipFirst:(args.skipFirst + args.length)]
# Print top topics
print("\nTop topics:")
print("Average Growth Rate (AGR) and Average Documents per Year (ADY) period: %d - %d\n\r"
% (self.yearArray[self.startYearIndex], self.yearArray[self.endYearIndex]))
print('-' * 87)
print("{:<4s}{:<30s}{:>10s}{:>10s}{:>10s}{:>10s}{:>12s}".format("Pos", args.criterion, "Total", "AGR", "ADY",
"PDLY", "h-index"))
print('-' * 87)
count = 0
for topicItem in self.topicResults:
print("{:<4d}{:<30s}{:>10d}{:>10.1f}{:>10.1f}{:>10.1f}{:>10d}".format(
count + 1, topicItem["name"], topicItem["PapersTotal"], topicItem["agr"],
topicItem["AverageDocPerYear"], topicItem["PerInLastYears"], topicItem["hIndex"]))
count += 1
print('-' * 87)
print("")
if filterSubTopic != "":
for topicItem in self.topicResults:
topicItem["name"] = topicItem["name"].split(",")[0].strip()
globalVar.progressText = "Saving results"
if self.fromGui:
time.sleep(0.01)
if globalVar.cancelProcess:
return
self.resultsFileName = paperSave.saveTopResults(self.topicResults, args.criterion, args.savePlot)
if self.fromGui:
time.sleep(0.01)
if globalVar.cancelProcess:
return
self.extResultsFileName = paperSave.saveExtendedResults(self.topicResults, args.criterion, args.savePlot)
# Only save results if that is result of a not previous result
if not args.previousResults:
paperSave.saveResults(papersDictOut, os.path.join(globalVar.RESULTS_FOLDER,
globalVar.OUTPUT_FILE_NAME))
del papersDictInside
globalVar.progressPer = 101
print("\nAnalysis finished.")
def plotResults(self, args=''):
if args == '':
args = self
if args.noPlot:
return
# If more than 100 results and not wordCloud, no plot.
if len(self.topicResults) > 100 and not args.graphType == "word_cloud" and not args.noPlot:
args.noPlot = True
print("\nERROR: Not allowed to graph more than 100 results")
return
if args.graphType == "evolution":
graphUtils.plot_evolution(plt, self.topicResults, self.yearArray[self.startYearIndex], self.yearArray[self.endYearIndex], args)
if args.graphType == "word_cloud":
from wordcloud import WordCloud
my_dpi = 96
plt.figure(figsize=(1960 / my_dpi, 1080 / my_dpi), dpi=my_dpi)
if args.wordCloudMask:
imageMask = np.array(Image.open(args.wordCloudMask))
wc = WordCloud(background_color="white", max_words=5000, width=1960, height=1080, colormap="tab10",
mask=imageMask)
else:
wc = WordCloud(background_color="white", max_words=5000, width=1960, height=1080, colormap="tab10")
freq = {}
for topicItem in self.topicResults:
freq[topicItem["name"]] = topicItem["PapersTotal"]
# generate word cloud
wc.generate_from_frequencies(freq)
# show
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
fig = plt.gcf()
fig.canvas.set_window_title(args.criterion + ' word cloud graph')
if args.graphType == "bar":
graphUtils.plot_bar_horizontal(plt, self.topicResults, args)
if args.graphType == "bar_trends":
graphUtils.plot_bar_horizontal_trends(plt, self.topicResults,
self.yearArray[self.startYearIndex], self.yearArray[self.endYearIndex], args)
if args.graphType == "time_line":
graphUtils.plot_time_line(plt, self.topicResults, False, args)
fig = plt.gcf()
fig.set_size_inches(args.plotWidth, args.plotHeight)
if args.yLog:
plt.yscale('log')
# TODO: Fix mticker
# plt.gca().yaxis.set_minor_formatter(mticker.ScalarFormatter())
if args.pYear:
plt.ylabel("% of documents per year")
if args.graphTitle:
# plt.title(args.graphTitle)
fig = plt.gcf()
fig.suptitle(args.graphTitle, y=1.0)
plt.tight_layout(rect=[0, 0, 1, 0.95])
else:
plt.tight_layout()
if args.savePlot == "":
if self.fromGui:
plt.show(block=False)
else:
plt.show(block=True)
else:
plt.savefig(os.path.join(globalVar.GRAPHS_OUT_FOLDER, args.savePlot),
bbox_inches='tight', pad_inches=0.01)
print("Plot saved on: " + os.path.join(globalVar.GRAPHS_OUT_FOLDER, args.savePlot))
if args.savePlot == "":
if self.fromGui:
plt.show()