28 KiB
28 KiB
In [1]:
import dimcli
from dimcli.utils import *
import sys, json, time
import pandas as pdIn [13]:
dsl = dimcli.login()[2mSearching config file credentials for default 'live' instance..[0m
[2mDimcli - Dimensions API Client (v0.9.9.1)[0m [2mConnected to: <https://app.dimensions.ai/api/dsl/v2> - DSL v2.3[0m [2mMethod: dsl.ini file[0m
In [14]:
dsl = dimcli.Dsl()In [4]:
GRIDID = "grid.168010.e"In [19]:
query= """
search publications
where research_orgs.id="{}"
return year
limit 70
"""
years_tupel = dsl.query(query.format(GRIDID)).as_dataframe().to_records(index=False) Returned Year: 70 [2mTime: 5.80s[0m
In [20]:
years_tupel = years_tupel[17:]In [22]:
query_template= """
search publications
where research_orgs.id="{}" and year="{}"
return publications[abstract+acknowledgements+altmetric+altmetric_id+arxiv_id+authors+authors_count+book_doi+book_series_title+book_title+category_bra+category_for+category_hra+category_hrcs_hc+category_hrcs_rac+category_icrp_cso+category_icrp_ct+category_rcdc+category_sdg+category_uoa+clinical_trial_ids+concepts+concepts_scores+date+date_inserted+date_online+date_print+dimensions_url+doi+field_citation_ratio+funder_countries+funders+id+issn+issue+journal+journal_lists+journal_title_raw+linkout+mesh_terms+open_access+pages+pmcid+pmid+proceedings_title+publisher+recent_citations+reference_ids+referenced_pubs+relative_citation_ratio+research_org_cities+research_org_countries+research_org_country_names+research_org_names+research_org_state_codes+research_org_state_names+research_orgs+researchers+resulting_publication_doi+source_title+subtitles+supporting_grant_ids+times_cited+title+type+volume+year]
"""
for idx in years_tupel:
print(f"getting data for year {idx[0]} with {idx[1]} records")
pub_year = dsl.query_iterative(query_template.format(GRIDID,idx[0]), limit=500)
pub_year.as_dataframe().to_csv(f"stanford/publications_year_{idx[0]}.csv")
time.sleep(2)Starting iteration with limit=500 skip=0 ...[0m
getting data for year 2005 with 6971 records
0-500 / 6971 (16.84s)[0m 500-1000 / 6971 (17.37s)[0m 1000-1500 / 6971 (17.83s)[0m 1500-2000 / 6971 (18.05s)[0m 2000-2500 / 6971 (19.11s)[0m 2500-3000 / 6971 (14.79s)[0m 3000-3500 / 6971 (17.62s)[0m 3500-4000 / 6971 (15.43s)[0m 4000-4500 / 6971 (21.59s)[0m 4500-5000 / 6971 (17.07s)[0m 5000-5500 / 6971 (10.88s)[0m 5500-6000 / 6971 (18.43s)[0m 6000-6500 / 6971 (18.78s)[0m 6500-6971 / 6971 (22.14s)[0m === Records extracted: 6971[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 2004 with 6277 records
0-500 / 6277 (17.51s)[0m 500-1000 / 6277 (16.70s)[0m 1000-1500 / 6277 (16.27s)[0m 1500-2000 / 6277 (16.20s)[0m 2000-2500 / 6277 (23.17s)[0m 2500-3000 / 6277 (16.02s)[0m 3000-3500 / 6277 (18.05s)[0m 3500-4000 / 6277 (18.96s)[0m 4000-4500 / 6277 (19.80s)[0m 4500-5000 / 6277 (14.16s)[0m 5000-5500 / 6277 (12.01s)[0m 5500-6000 / 6277 (13.83s)[0m 6000-6277 / 6277 (12.06s)[0m === Records extracted: 6277[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 2003 with 5665 records
0-500 / 5665 (25.20s)[0m 500-1000 / 5665 (23.27s)[0m 1000-1500 / 5665 (18.42s)[0m 1500-2000 / 5665 (21.45s)[0m 2000-2500 / 5665 (19.57s)[0m 2500-3000 / 5665 (13.88s)[0m 3000-3500 / 5665 (22.37s)[0m 3500-4000 / 5665 (21.89s)[0m 4000-4500 / 5665 (12.69s)[0m 4500-5000 / 5665 (18.08s)[0m 5000-5500 / 5665 (14.02s)[0m 5500-5665 / 5665 (4.39s)[0m === Records extracted: 5665[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 2002 with 5312 records
0-500 / 5312 (18.12s)[0m 500-1000 / 5312 (15.87s)[0m 1000-1500 / 5312 (32.86s)[0m 1500-2000 / 5312 (18.36s)[0m 2000-2500 / 5312 (26.14s)[0m 2500-3000 / 5312 (25.01s)[0m 3000-3500 / 5312 (25.73s)[0m 3500-4000 / 5312 (27.17s)[0m 4000-4500 / 5312 (12.25s)[0m 4500-5000 / 5312 (14.05s)[0m 5000-5312 / 5312 (19.90s)[0m === Records extracted: 5312[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 2001 with 5029 records
0-500 / 5029 (30.58s)[0m 500-1000 / 5029 (17.30s)[0m 1000-1500 / 5029 (20.61s)[0m 1500-2000 / 5029 (16.46s)[0m 2000-2500 / 5029 (14.86s)[0m 2500-3000 / 5029 (18.14s)[0m 3000-3500 / 5029 (17.12s)[0m 3500-4000 / 5029 (21.76s)[0m 4000-4500 / 5029 (17.36s)[0m 4500-5000 / 5029 (12.86s)[0m 5000-5029 / 5029 (1.97s)[0m === Records extracted: 5029[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 2000 with 4861 records
0-500 / 4861 (14.67s)[0m 500-1000 / 4861 (18.31s)[0m 1000-1500 / 4861 (13.52s)[0m 1500-2000 / 4861 (14.42s)[0m 2000-2500 / 4861 (13.11s)[0m 2500-3000 / 4861 (15.43s)[0m 3000-3500 / 4861 (15.74s)[0m 3500-4000 / 4861 (16.47s)[0m 4000-4500 / 4861 (7.27s)[0m 4500-4861 / 4861 (11.13s)[0m === Records extracted: 4861[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1999 with 4615 records
0-500 / 4615 (17.20s)[0m 500-1000 / 4615 (21.19s)[0m 1000-1500 / 4615 (20.08s)[0m 1500-2000 / 4615 (13.44s)[0m 2000-2500 / 4615 (11.97s)[0m 2500-3000 / 4615 (16.50s)[0m 3000-3500 / 4615 (15.89s)[0m 3500-4000 / 4615 (12.26s)[0m 4000-4500 / 4615 (10.00s)[0m 4500-4615 / 4615 (5.47s)[0m === Records extracted: 4615[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1998 with 4333 records
0-500 / 4333 (17.36s)[0m 500-1000 / 4333 (24.32s)[0m 1000-1500 / 4333 (14.55s)[0m 1500-2000 / 4333 (12.48s)[0m 2000-2500 / 4333 (13.05s)[0m 2500-3000 / 4333 (13.58s)[0m 3000-3500 / 4333 (15.55s)[0m 3500-4000 / 4333 (6.63s)[0m 4000-4333 / 4333 (9.78s)[0m === Records extracted: 4333[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1996 with 3936 records
0-500 / 3936 (16.31s)[0m 500-1000 / 3936 (11.61s)[0m 1000-1500 / 3936 (13.88s)[0m 1500-2000 / 3936 (12.99s)[0m 2000-2500 / 3936 (13.95s)[0m 2500-3000 / 3936 (12.66s)[0m 3000-3500 / 3936 (14.95s)[0m 3500-3936 / 3936 (17.25s)[0m === Records extracted: 3936[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1997 with 3806 records
0-500 / 3806 (17.76s)[0m 500-1000 / 3806 (15.04s)[0m 1000-1500 / 3806 (20.03s)[0m 1500-2000 / 3806 (18.83s)[0m 2000-2500 / 3806 (19.77s)[0m 2500-3000 / 3806 (16.21s)[0m 3000-3500 / 3806 (23.08s)[0m 3500-3806 / 3806 (9.50s)[0m === Records extracted: 3806[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1995 with 3685 records
0-500 / 3685 (22.80s)[0m 500-1000 / 3685 (16.05s)[0m 1000-1500 / 3685 (15.35s)[0m 1500-2000 / 3685 (14.90s)[0m 2000-2500 / 3685 (11.33s)[0m 2500-3000 / 3685 (12.52s)[0m 3000-3500 / 3685 (8.04s)[0m 3500-3685 / 3685 (12.09s)[0m === Records extracted: 3685[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1994 with 3432 records
0-500 / 3432 (18.66s)[0m 500-1000 / 3432 (18.97s)[0m 1000-1500 / 3432 (12.81s)[0m 1500-2000 / 3432 (21.12s)[0m 2000-2500 / 3432 (16.16s)[0m 2500-3000 / 3432 (7.24s)[0m 3000-3432 / 3432 (10.08s)[0m === Records extracted: 3432[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1991 with 3414 records
0-500 / 3414 (14.84s)[0m 500-1000 / 3414 (48.89s)[0m 1000-1500 / 3414 (13.27s)[0m 1500-2000 / 3414 (17.41s)[0m 2000-2500 / 3414 (12.35s)[0m 2500-3000 / 3414 (8.74s)[0m 3000-3414 / 3414 (9.22s)[0m === Records extracted: 3414[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1993 with 3356 records
0-500 / 3356 (13.69s)[0m 500-1000 / 3356 (11.22s)[0m 1000-1500 / 3356 (19.55s)[0m 1500-2000 / 3356 (11.22s)[0m 2000-2500 / 3356 (10.47s)[0m 2500-3000 / 3356 (9.90s)[0m 3000-3356 / 3356 (8.00s)[0m === Records extracted: 3356[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1990 with 3181 records
0-500 / 3181 (14.93s)[0m 500-1000 / 3181 (14.69s)[0m 1000-1500 / 3181 (16.78s)[0m 1500-2000 / 3181 (14.39s)[0m 2000-2500 / 3181 (18.94s)[0m 2500-3000 / 3181 (12.47s)[0m 3000-3181 / 3181 (6.24s)[0m === Records extracted: 3181[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1992 with 3146 records
0-500 / 3146 (23.38s)[0m 500-1000 / 3146 (27.98s)[0m 1000-1500 / 3146 (20.40s)[0m 1500-2000 / 3146 (17.12s)[0m 2000-2500 / 3146 (11.89s)[0m 2500-3000 / 3146 (10.20s)[0m 3000-3146 / 3146 (7.41s)[0m === Records extracted: 3146[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1989 with 3120 records
0-500 / 3120 (12.52s)[0m 500-1000 / 3120 (12.80s)[0m 1000-1500 / 3120 (13.53s)[0m 1500-2000 / 3120 (15.30s)[0m 2000-2500 / 3120 (15.28s)[0m 2500-3000 / 3120 (11.71s)[0m 3000-3120 / 3120 (8.86s)[0m === Records extracted: 3120[0m Starting iteration with limit=500 skip=0 ...[0m
getting data for year 1988 with 2934 records
0-500 / 2934 (14.97s)[0m 500-1000 / 2934 (13.25s)[0m 1000-1500 / 2934 (14.42s)[0m 1500-2000 / 2934 (12.42s)[0m 2000-2500 / 2934 (20.41s)[0m
[1;31m---------------------------------------------------------------------------[0m
[1;31mHTTPError[0m Traceback (most recent call last)
Cell [1;32mIn [22], line 9[0m
[0;32m 7[0m [38;5;28;01mfor[39;00m idx [38;5;129;01min[39;00m years_tupel:
[0;32m 8[0m [38;5;28mprint[39m([38;5;124mf[39m[38;5;124m"[39m[38;5;124mgetting data for year [39m[38;5;132;01m{[39;00midx[[38;5;241m0[39m][38;5;132;01m}[39;00m[38;5;124m with [39m[38;5;132;01m{[39;00midx[[38;5;241m1[39m][38;5;132;01m}[39;00m[38;5;124m records[39m[38;5;124m"[39m)
[1;32m----> 9[0m pub_year [38;5;241m=[39m [43mdsl[49m[38;5;241;43m.[39;49m[43mquery_iterative[49m[43m([49m[43mquery_template[49m[38;5;241;43m.[39;49m[43mformat[49m[43m([49m[43mGRIDID[49m[43m,[49m[43midx[49m[43m[[49m[38;5;241;43m0[39;49m[43m][49m[43m)[49m[43m,[49m[43m [49m[43mlimit[49m[38;5;241;43m=[39;49m[38;5;241;43m500[39;49m[43m)[49m
[0;32m 10[0m pub_year[38;5;241m.[39mas_dataframe()[38;5;241m.[39mto_csv([38;5;124mf[39m[38;5;124m"[39m[38;5;124mstanford/publications_year_[39m[38;5;132;01m{[39;00midx[[38;5;241m0[39m][38;5;132;01m}[39;00m[38;5;124m.csv[39m[38;5;124m"[39m)
[0;32m 11[0m time[38;5;241m.[39msleep([38;5;241m2[39m)
File [1;32mc:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:331[0m, in [0;36mDsl.query_iterative[1;34m(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)[0m
[0;32m 328[0m output [39m=[39m [39mself[39m[39m.[39mquery_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
[0;32m 330[0m [39melif[39;00m [39mnot[39;00m IS_UNNEST [39mand[39;00m [39mlen[39m(res[sourcetype]) [39m==[39m limit [39mand[39;00m [39mnot[39;00m flag_last_round:
[1;32m--> 331[0m output [39m=[39m res[sourcetype] [39m+[39m [39mself[39;49m[39m.[39;49mquery_iterative(q, show_results, limit, new_skip, pause, force,maxlimit, verbose, tot, _warnings_tot)
[0;32m 333[0m [39melif[39;00m IS_UNNEST [39mand[39;00m [39mlen[39m(res[sourcetype]) [39m>[39m [39m0[39m [39mand[39;00m [39mnot[39;00m flag_last_round:
[0;32m 334[0m [39m# unnest returns a number of records that don't relate to actual data left[39;00m
[0;32m 335[0m [39m# hence can't match the lenght of results to limit in this case[39;00m
[0;32m 336[0m output [39m=[39m res[sourcetype] [39m+[39m [39mself[39m[39m.[39mquery_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, tot, _warnings_tot)
File [1;32mc:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:331[0m, in [0;36mDsl.query_iterative[1;34m(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)[0m
[0;32m 328[0m output [39m=[39m [39mself[39m[39m.[39mquery_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
[0;32m 330[0m [39melif[39;00m [39mnot[39;00m IS_UNNEST [39mand[39;00m [39mlen[39m(res[sourcetype]) [39m==[39m limit [39mand[39;00m [39mnot[39;00m flag_last_round:
[1;32m--> 331[0m output [39m=[39m res[sourcetype] [39m+[39m [39mself[39;49m[39m.[39;49mquery_iterative(q, show_results, limit, new_skip, pause, force,maxlimit, verbose, tot, _warnings_tot)
[0;32m 333[0m [39melif[39;00m IS_UNNEST [39mand[39;00m [39mlen[39m(res[sourcetype]) [39m>[39m [39m0[39m [39mand[39;00m [39mnot[39;00m flag_last_round:
[0;32m 334[0m [39m# unnest returns a number of records that don't relate to actual data left[39;00m
[0;32m 335[0m [39m# hence can't match the lenght of results to limit in this case[39;00m
[0;32m 336[0m output [39m=[39m res[sourcetype] [39m+[39m [39mself[39m[39m.[39mquery_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, tot, _warnings_tot)
[1;31m[... skipping similar frames: Dsl.query_iterative at line 331 (2 times)][0m
File [1;32mc:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:331[0m, in [0;36mDsl.query_iterative[1;34m(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)[0m
[0;32m 328[0m output [39m=[39m [39mself[39m[39m.[39mquery_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
[0;32m 330[0m [39melif[39;00m [39mnot[39;00m IS_UNNEST [39mand[39;00m [39mlen[39m(res[sourcetype]) [39m==[39m limit [39mand[39;00m [39mnot[39;00m flag_last_round:
[1;32m--> 331[0m output [39m=[39m res[sourcetype] [39m+[39m [39mself[39;49m[39m.[39;49mquery_iterative(q, show_results, limit, new_skip, pause, force,maxlimit, verbose, tot, _warnings_tot)
[0;32m 333[0m [39melif[39;00m IS_UNNEST [39mand[39;00m [39mlen[39m(res[sourcetype]) [39m>[39m [39m0[39m [39mand[39;00m [39mnot[39;00m flag_last_round:
[0;32m 334[0m [39m# unnest returns a number of records that don't relate to actual data left[39;00m
[0;32m 335[0m [39m# hence can't match the lenght of results to limit in this case[39;00m
[0;32m 336[0m output [39m=[39m res[sourcetype] [39m+[39m [39mself[39m[39m.[39mquery_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, tot, _warnings_tot)
File [1;32mc:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:292[0m, in [0;36mDsl.query_iterative[1;34m(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)[0m
[0;32m 289[0m q2 [39m=[39m q [39m+[39m [39m"[39m[39m limit [39m[39m%d[39;00m[39m skip [39m[39m%d[39;00m[39m"[39m [39m%[39m (limit, skip)
[0;32m 291[0m start [39m=[39m time[39m.[39mtime()
[1;32m--> 292[0m res [39m=[39m [39mself[39;49m[39m.[39;49mquery(q2, show_results[39m=[39;49m[39mFalse[39;49;00m, retry[39m=[39;49m[39m0[39;49m, verbose[39m=[39;49m[39mFalse[39;49;00m)
[0;32m 293[0m end [39m=[39m time[39m.[39mtime()
[0;32m 294[0m elapsed [39m=[39m end [39m-[39m start
File [1;32mc:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:196[0m, in [0;36mDsl.query[1;34m(self, q, show_results, retry, verbose)[0m
[0;32m 194[0m [39mif[39;00m verbose: printDebug([39m"[39m[39mResponse.header[39m[39m\n[39;00m[39m---[39m[39m\n[39;00m[39m"[39m [39m+[39m [39mstr[39m(response[39m.[39mheaders), [39m"[39m[39mred[39m[39m"[39m)
[0;32m 195[0m [39mif[39;00m verbose: printDebug([39m"[39m[39mResponse.content[39m[39m\n[39;00m[39m---[39m[39m\n[39;00m[39m"[39m [39m+[39m[39mstr[39m(response[39m.[39mcontent), [39m"[39m[39mred[39m[39m"[39m)
[1;32m--> 196[0m response[39m.[39;49mraise_for_status()
File [1;32mc:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\requests\models.py:1021[0m, in [0;36mResponse.raise_for_status[1;34m(self)[0m
[0;32m 1016[0m http_error_msg [39m=[39m (
[0;32m 1017[0m [39mf[39m[39m"[39m[39m{[39;00m[39mself[39m[39m.[39mstatus_code[39m}[39;00m[39m Server Error: [39m[39m{[39;00mreason[39m}[39;00m[39m for url: [39m[39m{[39;00m[39mself[39m[39m.[39murl[39m}[39;00m[39m"[39m
[0;32m 1018[0m )
[0;32m 1020[0m [39mif[39;00m http_error_msg:
[1;32m-> 1021[0m [39mraise[39;00m HTTPError(http_error_msg, response[39m=[39m[39mself[39m)
[1;31mHTTPError[0m: 502 Server Error: Bad Gateway for url: https://app.dimensions.ai/api/dsl/v2