Datasets:

AstroCLIMB / code /wasp26_lib /big_query_citations_refs.py
fgrezes's picture
notebooks and code for wasp 2026
4528bac verified
Raw History Blame Contribute Delete
3.2 kB
import pandas as pd
import requests
import math
from tqdm.auto import tqdm
def big_query_citations_refs(DOIs, scix_token):
"""
Given a list of DOIs and a valid SciX API token returns a Pandas dataframe associating DOIs with their references and citing bibcodes
DOIs of the shape: 10.3847/1538-4357/aca0dd
"""
ref_cit_df = pd.DataFrame(columns=['Paper DOI', 'Paper Bibcode', 'References Bibcodes', 'Citing Bibcodes'])
# go through DOIs 2000 at a time
for i in tqdm(range(math.ceil(len(DOIs)/2000))):
DOIs_list = DOIs[2000*i:2000*(i+1)]
DOIs_set = set(DOIs_list)
DOIs_string = 'doi\n'+'\n'.join(DOIs_set)
try:
req = requests.post('https://api.adsabs.harvard.edu/v1/search/bigquery',
params={'q':'*:*', 'wt':'json', 'fq':'{!bitset}', 'fl':'doi, bibcode, reference, citation', 'rows':2000},
headers={'Authorization': f'Bearer {scix_token}'},
data=DOIs_string)
req.raise_for_status()
except:
print(f'biq query failed. Status code {req.status_code}. Exiting')
return None
else:
# concatenate to full dataframe
temp_df = pd.DataFrame(columns=['Paper DOI', 'References Bibcodes', 'Citing Bibcodes'])
temp_DOIs = [set(d['doi']).intersection(DOIs_set).pop() for d in req.json()['response']['docs']]
temp_bibs = [d['bibcode'] for d in req.json()['response']['docs']]
temp_refs = [d['reference'] if 'reference' in d.keys() else [] for d in req.json()['response']['docs']]
temp_cits = [d['citation'] if 'citation' in d.keys() else [] for d in req.json()['response']['docs']]
# temp_DOIs = []
# for d in req.json()['response']['docs']:
# if 'doi' in d.keys():
# temp_DOIs.append(set(d['doi']).intersection(set(DOIs_list)).pop())
# else:
# print(d)
# temp_DOIs = [d['doi'][0] if 'doi' in d.keys() and d['doi'][0] in DOIs_list else [] for d in req.json()['response']['docs']:
# # sort by DOI
# DOIs_list = sorted(DOIs_list)
# temp_refs = [x for _, x in sorted(zip(temp_DOIs, temp_refs), key=lambda pair: pair[0])]
# temp_cits = [x for _, x in sorted(zip(temp_DOIs, temp_cits), key=lambda pair: pair[0])]
# temp_DOIs = sorted(temp_DOIs)
# check that every DOI returned is in the original
# assert DOIs_list == temp_DOIs
# assert set(temp_DOIs).difference(set(DOIs_list))==set()
# for i,b in enumerate(temp_bibs):
# if '2023ApJS..267...47S'==b:
# print(req.json()['response']['docs'][i])
temp_df['Paper DOI'] = temp_DOIs
temp_df['Paper Bibcode'] = temp_bibs
temp_df['References Bibcodes'] = temp_refs
temp_df['Citing Bibcodes'] = temp_cits
ref_cit_df = pd.concat([ref_cit_df, temp_df], axis=0, ignore_index=True)
return(ref_cit_df)