-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.py
306 lines (249 loc) · 8.39 KB
/
functions.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
import requests
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import json
import datetime
import altair as alt
# import vega
from IPython.core.display import display, HTML
import regex
_transport = RequestsHTTPTransport(
url='https://api.datacite.org/graphql',
use_json=True,
)
client = Client(
transport=_transport,
fetch_schema_from_transport=True,
)
def get_events_by_doi_and_relation_type(doi, relation_type):
params = {'doi': doi , 'relation_type_id': relation_type, 'page[size]': 100}
try:
events = requests.get('https://api.datacite.org/events', params=params)
events.raise_for_status()
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
return None
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
return None
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
return None
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)
return None
return events.json()
def get_metadata_display(doi):
query_params = {
"instrumentId" : doi
}
query = gql("""query getInstrument($instrumentId: ID!)
{
work(id: $instrumentId) {
id
citationCount
formattedCitation
repository {
uid
name
}
}
}
""")
try:
data = client.execute(query, variable_values=json.dumps(query_params))
except Exception as e:
print("Unable to get metadata for %s: %s" % (doi, e))
return None
return data
def format_citations(events, include_authors=False):
ids = extract_ids(events)
if len(ids) == 0: return None
query_params = {
"instrumentIds" : ids
}
author_query ="""authors {
title
id
}""" if include_authors else ''
query = gql("""query getInstruments($instrumentIds: [String!])
{
works(ids: $instrumentIds) {
""" + author_query + """
nodes {
formattedCitation
}
}
}
""")
try:
formatted_citations = client.execute(query, variable_values=json.dumps(query_params))
except Exception as e:
print("Unable to get metadata for %s: %s" % ("events", e))
return None
return formatted_citations
def extract_ids(events):
"""Return the property sub_id of array of events."""
ids = []
for event in events['data']:
ids.append(get_doi_name_from_doi_url(event['attributes']['subj-id']))
ids.append(get_doi_name_from_doi_url(event['attributes']['obj-id']))
return [x for x in ids if x is not None]
def get_doi_name_from_doi_url(doi_url):
m = regex.search(r'(?<=https://doi.org/)\b\S+', doi_url)
if m:
# Return the DOI suffix if it is found
return m.group()
else:
# Return None if the DOI suffix is not found
return None
def generate_histogram_spec(data):
"""Return a vega-lite specification for a bar chart with the citation counts."""
## data['meta']['occurred']
chartWidth = 500
thisYear = datetime.datetime.now().year + 1
lowerBoundYear = int(((thisYear - 10) / 5) * 5)
spec = {
"$schema": 'https://vega.github.io/schema/vega-lite/v4.json',
"data": {
"values": data
},
"padding": { "left": 5, "top": 5, "right": 5, "bottom": 5 },
"transform": [
{
"calculate": 'toNumber(datum.title)',
"as": 'period'
},
{
"calculate": 'toNumber(datum.title)+1',
"as": 'bin_end'
},
{
"filter": f"toNumber(datum.title) >= {lowerBoundYear}"
}
],
"width": chartWidth,
"mark": {
"type": 'bar',
"cursor": 'pointer',
"tooltip": True
},
"selection": {
"highlight": {
"type": 'single',
"empty": 'none',
"on": 'mouseover'
}
},
"encoding": {
"x": {
"field": 'period',
"bin": {
"binned": True,
"step": 1,
"maxbins": thisYear - lowerBoundYear
},
"type": 'quantitative',
"axis": {
"format": '1',
"labelExpr": 'datum.label % 2 === 0 ? datum.label : ""'
},
"scale": {
"domain": [lowerBoundYear, thisYear]
}
},
"x2": {
"field": 'bin_end'
},
"y": {
"field": 'count',
"type": 'quantitative',
"axis": {
"format": ',f',
"tickMinStep": 1
}
},
"color": {
"field": 'count',
"scale": { "range": ["#1abc9c"] },
"type": 'nominal',
"legend": None,
"condition": [{ "selection": 'highlight', "value": '#34495e' }]
}
},
"config": {
"view": {
"stroke": None
},
'axis': {
"grid": False,
"title": None,
"labelFlush": False
}
}
}
return spec
def render_histogram(spec):
chart = alt.Chart.from_dict(spec)
return chart
def generate_html(metadata, datasets_table_html, publications_table_html, related_works_table_html, authors_table_html):
HTML_TEMPLATE_FILE = './nfdi-template.html'
STYLES_FILE = './nfdi-styles.css'
html = ''
styles = ''
with open(HTML_TEMPLATE_FILE, 'r') as html_template, open(STYLES_FILE) as styles_file:
html = html_template.read()
styles = styles_file.read()
html = html.format(
style=styles,
formatted_citation=metadata['work']['formattedCitation'],
citation_count=metadata['work']['citationCount'],
repository_link=metadata["work"]["repository"]["uid"],
repository_name=metadata["work"]["repository"]["name"],
datasets=datasets_table_html,
publications=publications_table_html,
related_works=related_works_table_html,
authors=authors_table_html
)
return html
def generate_html_table(title, data):
rows = ''
for item in data:
rows += '<tr><td>' + item + '</td></tr>'
html = f"""<table>
<tr><th><h3>{title}</h3></tr></th>
{rows}
</table>"""
return html
def main(doi):
# Instrument metadata display
metadata = get_metadata_display(doi)
if metadata is None:
return f'Unable to get metadata for {doi}'
doiSet = set([metadata['work']['formattedCitation']])
# Data that used an instrument
datasets_events = get_events_by_doi_and_relation_type(doi, 'is-compiled-by')
formatted_citations = format_citations(datasets_events)
datasets_data = map(lambda item: item['formattedCitation'], formatted_citations['works']['nodes']) if formatted_citations is not None else []
datasets_html = generate_html_table('Datasets', set(datasets_data) - doiSet)
# Publications that used an instrument
publications_events = get_events_by_doi_and_relation_type(doi, 'is-referenced-by')
formatted_citations = format_citations(publications_events)
publications_data = map(lambda item: item['formattedCitation'], formatted_citations['works']['nodes']) if formatted_citations is not None else []
publications_html = generate_html_table('Publications', set(publications_data) - doiSet)
# Related works
related_works_events = get_events_by_doi_and_relation_type(doi, '')
formatted_citations = format_citations(related_works_events, include_authors=True)
related_works_data = map(lambda item: item['formattedCitation'], formatted_citations['works']['nodes']) if formatted_citations is not None else [] # Need to filter out duplicates
related_works_html = generate_html_table('Related Works', set(related_works_data) - doiSet)
# Co-authors List
authors_data = map(lambda item: item['title'], formatted_citations['works']['authors']) if formatted_citations is not None else []
authors_html = generate_html_table('Authors', set(authors_data))
# Generate and save full HTML
html = generate_html(metadata, datasets_html, publications_html, related_works_html, authors_html)
display(HTML(html))
# with open('./nfdi.html', 'w') as file: file.write(html)
# Histogram
spec = generate_histogram_spec(related_works_events['meta']['occurred'])
chart = render_histogram(spec)
return chart.display()