-
Notifications
You must be signed in to change notification settings - Fork 0
/
citations.py
144 lines (114 loc) · 5.04 KB
/
citations.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
#!/usr/bin/env python3
"""
Given an author identified by his/her BAI, this Python3 script counts the number of
citations and the number of citations excluding self cites in the Inspirehep database
(https://inspirehep.net/) for each author's paper.
Additionally, it allows saving a snapshot for later detection of new/removed papers and
change in the number of citations of individual papers
Built on & inspired by https://github.com/efranzin/python
"""
AUTHOR = 'P.Motloch.2'
MAX_NUM_PAPERS = 1000 #Number of papers requested from INSPIRE-HEP
SHORT_TITLE_LENGTH = 50 #Shorten long paper titles
NEED_WRITE_CONFIRM = True #Whether to ask user for permission to save to disk
FILENAME = 'old_biblio.npy'
# Import the modules to open and reading URLs and the JSON encoder
import urllib.request, json
# Open the INSPIRE-HEP profile
inspirehep_profile = 'https://inspirehep.net/api/literature?sort=mostrecent&size=' + \
str(MAX_NUM_PAPERS) + '&q=a%20' + AUTHOR
# Load the data
data = json.loads(urllib.request.urlopen(inspirehep_profile).read())
num_hits = data['hits']['total']
# Data type to store paper id, beginning of the title, number of citations and number of
# citations without self-citations
import numpy as np
bibliography_dtype = np.dtype([
('id', np.int64),
('title', np.unicode_, SHORT_TITLE_LENGTH),
('cits', np.int64),
('cits_noself', np.int64),
])
# Fill in information about author's papers from the website response
biblio = np.zeros(num_hits, dtype = bibliography_dtype)
for i in range(num_hits):
biblio[i]['id'] = data['hits']['hits'][i]['id']
biblio[i]['title'] = data['hits']['hits'][i]['metadata']['titles'][0]['title']
biblio[i]['cits'] = data['hits']['hits'][i]['metadata']['citation_count']
biblio[i]['cits_noself'] = data['hits']['hits'][i]['metadata']['citation_count_without_self_citations']
# Print the total number of citations and the total number of citations excluding self cites
print(
'\nTotal number of citations: ',
sum(biblio['cits']),
'; Excluding self cites: ',
sum(biblio['cits_noself']),
'\n',
sep=''
)
# Function to save current snapshot of the author's citations
def save_snapshot():
"""
Saves a current snapshot of the bibliography.
If NEED_WRITE_CONFIRM is True, asks the user for permission first.
"""
if NEED_WRITE_CONFIRM:
rewrite = input('\nDo you want to save a snapshot [y/n]? ')
if rewrite != 'y':
print('Not saved.')
return
np.save(FILENAME, biblio)
print('Saved.')
return
#If snapshot does not exist, create it (potentially confirming with the user) and exit
from os.path import exists
if not exists(FILENAME):
save_snapshot()
exit()
#Load snapshot
old_biblio = np.load(FILENAME)
#Get a set of paper IDs that were added/removed/stayed
new_paper_ids = set( biblio['id'])
old_paper_ids = set(old_biblio['id'])
added_paper_ids = new_paper_ids.difference(old_paper_ids)
removed_paper_ids = old_paper_ids.difference(new_paper_ids)
stayed_paper_ids = new_paper_ids.intersection(old_paper_ids)
#Keep track of whether we had any changes
changes_present = False
#Print information about papers that were added or removed
for i in removed_paper_ids:
changes_present = True
idx = np.argmax(old_biblio['id'] == i)
title = old_biblio[idx]['title']
num_cites = old_biblio[idx]['cits']
if num_cites == 1:
print('Removed paper: "' + title + '" with ' + str(num_cites) + ' citation')
else:
print('Removed paper: "' + title + '" with ' + str(num_cites) + ' citations')
for i in added_paper_ids:
changes_present = True
idx = np.argmax(biblio['id'] == i)
title = biblio[idx]['title']
num_cites = biblio[idx]['cits']
if num_cites == 1:
print('Added paper: "' + title + '" with ' + str(num_cites) + ' citation')
else:
print('Added paper: "' + title + '" with ' + str(num_cites) + ' citations')
#For papers not added or removed, check if number of citations has changed
for i in stayed_paper_ids:
idx_old = np.argmax(old_biblio['id'] == i)
idx_new = np.argmax( biblio['id'] == i)
title = biblio[idx_new]['title']
num_new_cites = biblio[idx_new]['cits'] - old_biblio[idx_old]['cits']
if num_new_cites != 0:
changes_present = True
if num_new_cites == 1:
print('1 new citation: "' + title + '"')
elif num_new_cites == -1:
print('1 citation removed: "' + title + '"')
elif num_new_cites > 1:
print(str(num_new_cites) + ' new citations: "' + title + '"')
elif num_new_cites < -1:
print(str(abs(num_new_cites)) + ' citations removed: "' + title + '"')
#Save current snapshot if anything changed (potentially confirming with the user)
if changes_present:
save_snapshot()