Files
2023-03-15 14:31:15 +01:00

28 KiB

In [1]:
import dimcli
from dimcli.utils import *

import sys, json, time
import pandas as pd
In [13]:
dsl = dimcli.login()
Searching config file credentials for default 'live' instance..
Dimcli - Dimensions API Client (v0.9.9.1)
Connected to: <https://app.dimensions.ai/api/dsl/v2> - DSL v2.3
Method: dsl.ini file
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
Time: 5.80s
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 ...
getting data for year 2005 with 6971 records
0-500 / 6971 (16.84s)
500-1000 / 6971 (17.37s)
1000-1500 / 6971 (17.83s)
1500-2000 / 6971 (18.05s)
2000-2500 / 6971 (19.11s)
2500-3000 / 6971 (14.79s)
3000-3500 / 6971 (17.62s)
3500-4000 / 6971 (15.43s)
4000-4500 / 6971 (21.59s)
4500-5000 / 6971 (17.07s)
5000-5500 / 6971 (10.88s)
5500-6000 / 6971 (18.43s)
6000-6500 / 6971 (18.78s)
6500-6971 / 6971 (22.14s)
===
Records extracted: 6971
Starting iteration with limit=500 skip=0 ...
getting data for year 2004 with 6277 records
0-500 / 6277 (17.51s)
500-1000 / 6277 (16.70s)
1000-1500 / 6277 (16.27s)
1500-2000 / 6277 (16.20s)
2000-2500 / 6277 (23.17s)
2500-3000 / 6277 (16.02s)
3000-3500 / 6277 (18.05s)
3500-4000 / 6277 (18.96s)
4000-4500 / 6277 (19.80s)
4500-5000 / 6277 (14.16s)
5000-5500 / 6277 (12.01s)
5500-6000 / 6277 (13.83s)
6000-6277 / 6277 (12.06s)
===
Records extracted: 6277
Starting iteration with limit=500 skip=0 ...
getting data for year 2003 with 5665 records
0-500 / 5665 (25.20s)
500-1000 / 5665 (23.27s)
1000-1500 / 5665 (18.42s)
1500-2000 / 5665 (21.45s)
2000-2500 / 5665 (19.57s)
2500-3000 / 5665 (13.88s)
3000-3500 / 5665 (22.37s)
3500-4000 / 5665 (21.89s)
4000-4500 / 5665 (12.69s)
4500-5000 / 5665 (18.08s)
5000-5500 / 5665 (14.02s)
5500-5665 / 5665 (4.39s)
===
Records extracted: 5665
Starting iteration with limit=500 skip=0 ...
getting data for year 2002 with 5312 records
0-500 / 5312 (18.12s)
500-1000 / 5312 (15.87s)
1000-1500 / 5312 (32.86s)
1500-2000 / 5312 (18.36s)
2000-2500 / 5312 (26.14s)
2500-3000 / 5312 (25.01s)
3000-3500 / 5312 (25.73s)
3500-4000 / 5312 (27.17s)
4000-4500 / 5312 (12.25s)
4500-5000 / 5312 (14.05s)
5000-5312 / 5312 (19.90s)
===
Records extracted: 5312
Starting iteration with limit=500 skip=0 ...
getting data for year 2001 with 5029 records
0-500 / 5029 (30.58s)
500-1000 / 5029 (17.30s)
1000-1500 / 5029 (20.61s)
1500-2000 / 5029 (16.46s)
2000-2500 / 5029 (14.86s)
2500-3000 / 5029 (18.14s)
3000-3500 / 5029 (17.12s)
3500-4000 / 5029 (21.76s)
4000-4500 / 5029 (17.36s)
4500-5000 / 5029 (12.86s)
5000-5029 / 5029 (1.97s)
===
Records extracted: 5029
Starting iteration with limit=500 skip=0 ...
getting data for year 2000 with 4861 records
0-500 / 4861 (14.67s)
500-1000 / 4861 (18.31s)
1000-1500 / 4861 (13.52s)
1500-2000 / 4861 (14.42s)
2000-2500 / 4861 (13.11s)
2500-3000 / 4861 (15.43s)
3000-3500 / 4861 (15.74s)
3500-4000 / 4861 (16.47s)
4000-4500 / 4861 (7.27s)
4500-4861 / 4861 (11.13s)
===
Records extracted: 4861
Starting iteration with limit=500 skip=0 ...
getting data for year 1999 with 4615 records
0-500 / 4615 (17.20s)
500-1000 / 4615 (21.19s)
1000-1500 / 4615 (20.08s)
1500-2000 / 4615 (13.44s)
2000-2500 / 4615 (11.97s)
2500-3000 / 4615 (16.50s)
3000-3500 / 4615 (15.89s)
3500-4000 / 4615 (12.26s)
4000-4500 / 4615 (10.00s)
4500-4615 / 4615 (5.47s)
===
Records extracted: 4615
Starting iteration with limit=500 skip=0 ...
getting data for year 1998 with 4333 records
0-500 / 4333 (17.36s)
500-1000 / 4333 (24.32s)
1000-1500 / 4333 (14.55s)
1500-2000 / 4333 (12.48s)
2000-2500 / 4333 (13.05s)
2500-3000 / 4333 (13.58s)
3000-3500 / 4333 (15.55s)
3500-4000 / 4333 (6.63s)
4000-4333 / 4333 (9.78s)
===
Records extracted: 4333
Starting iteration with limit=500 skip=0 ...
getting data for year 1996 with 3936 records
0-500 / 3936 (16.31s)
500-1000 / 3936 (11.61s)
1000-1500 / 3936 (13.88s)
1500-2000 / 3936 (12.99s)
2000-2500 / 3936 (13.95s)
2500-3000 / 3936 (12.66s)
3000-3500 / 3936 (14.95s)
3500-3936 / 3936 (17.25s)
===
Records extracted: 3936
Starting iteration with limit=500 skip=0 ...
getting data for year 1997 with 3806 records
0-500 / 3806 (17.76s)
500-1000 / 3806 (15.04s)
1000-1500 / 3806 (20.03s)
1500-2000 / 3806 (18.83s)
2000-2500 / 3806 (19.77s)
2500-3000 / 3806 (16.21s)
3000-3500 / 3806 (23.08s)
3500-3806 / 3806 (9.50s)
===
Records extracted: 3806
Starting iteration with limit=500 skip=0 ...
getting data for year 1995 with 3685 records
0-500 / 3685 (22.80s)
500-1000 / 3685 (16.05s)
1000-1500 / 3685 (15.35s)
1500-2000 / 3685 (14.90s)
2000-2500 / 3685 (11.33s)
2500-3000 / 3685 (12.52s)
3000-3500 / 3685 (8.04s)
3500-3685 / 3685 (12.09s)
===
Records extracted: 3685
Starting iteration with limit=500 skip=0 ...
getting data for year 1994 with 3432 records
0-500 / 3432 (18.66s)
500-1000 / 3432 (18.97s)
1000-1500 / 3432 (12.81s)
1500-2000 / 3432 (21.12s)
2000-2500 / 3432 (16.16s)
2500-3000 / 3432 (7.24s)
3000-3432 / 3432 (10.08s)
===
Records extracted: 3432
Starting iteration with limit=500 skip=0 ...
getting data for year 1991 with 3414 records
0-500 / 3414 (14.84s)
500-1000 / 3414 (48.89s)
1000-1500 / 3414 (13.27s)
1500-2000 / 3414 (17.41s)
2000-2500 / 3414 (12.35s)
2500-3000 / 3414 (8.74s)
3000-3414 / 3414 (9.22s)
===
Records extracted: 3414
Starting iteration with limit=500 skip=0 ...
getting data for year 1993 with 3356 records
0-500 / 3356 (13.69s)
500-1000 / 3356 (11.22s)
1000-1500 / 3356 (19.55s)
1500-2000 / 3356 (11.22s)
2000-2500 / 3356 (10.47s)
2500-3000 / 3356 (9.90s)
3000-3356 / 3356 (8.00s)
===
Records extracted: 3356
Starting iteration with limit=500 skip=0 ...
getting data for year 1990 with 3181 records
0-500 / 3181 (14.93s)
500-1000 / 3181 (14.69s)
1000-1500 / 3181 (16.78s)
1500-2000 / 3181 (14.39s)
2000-2500 / 3181 (18.94s)
2500-3000 / 3181 (12.47s)
3000-3181 / 3181 (6.24s)
===
Records extracted: 3181
Starting iteration with limit=500 skip=0 ...
getting data for year 1992 with 3146 records
0-500 / 3146 (23.38s)
500-1000 / 3146 (27.98s)
1000-1500 / 3146 (20.40s)
1500-2000 / 3146 (17.12s)
2000-2500 / 3146 (11.89s)
2500-3000 / 3146 (10.20s)
3000-3146 / 3146 (7.41s)
===
Records extracted: 3146
Starting iteration with limit=500 skip=0 ...
getting data for year 1989 with 3120 records
0-500 / 3120 (12.52s)
500-1000 / 3120 (12.80s)
1000-1500 / 3120 (13.53s)
1500-2000 / 3120 (15.30s)
2000-2500 / 3120 (15.28s)
2500-3000 / 3120 (11.71s)
3000-3120 / 3120 (8.86s)
===
Records extracted: 3120
Starting iteration with limit=500 skip=0 ...
getting data for year 1988 with 2934 records
0-500 / 2934 (14.97s)
500-1000 / 2934 (13.25s)
1000-1500 / 2934 (14.42s)
1500-2000 / 2934 (12.42s)
2000-2500 / 2934 (20.41s)
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
Cell In [22], line 9
      7 for idx in years_tupel:
      8     print(f"getting data for year {idx[0]} with {idx[1]} records")
----> 9     pub_year = dsl.query_iterative(query_template.format(GRIDID,idx[0]), limit=500)
     10     pub_year.as_dataframe().to_csv(f"stanford/publications_year_{idx[0]}.csv")
     11     time.sleep(2)

File c:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:331, in Dsl.query_iterative(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
    328     output = self.query_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)                    
    330 elif not IS_UNNEST and len(res[sourcetype]) == limit and not flag_last_round:
--> 331     output = res[sourcetype] + self.query_iterative(q, show_results, limit, new_skip, pause, force,maxlimit, verbose, tot, _warnings_tot)
    333 elif IS_UNNEST and len(res[sourcetype]) > 0 and not flag_last_round:
    334     # unnest returns a number of records that don't relate to actual data left
    335     # hence can't match the lenght of results to limit in this case
    336     output = res[sourcetype] + self.query_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, tot, _warnings_tot)

File c:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:331, in Dsl.query_iterative(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
    328     output = self.query_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)                    
    330 elif not IS_UNNEST and len(res[sourcetype]) == limit and not flag_last_round:
--> 331     output = res[sourcetype] + self.query_iterative(q, show_results, limit, new_skip, pause, force,maxlimit, verbose, tot, _warnings_tot)
    333 elif IS_UNNEST and len(res[sourcetype]) > 0 and not flag_last_round:
    334     # unnest returns a number of records that don't relate to actual data left
    335     # hence can't match the lenght of results to limit in this case
    336     output = res[sourcetype] + self.query_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, tot, _warnings_tot)

    [... skipping similar frames: Dsl.query_iterative at line 331 (2 times)]

File c:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:331, in Dsl.query_iterative(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
    328     output = self.query_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)                    
    330 elif not IS_UNNEST and len(res[sourcetype]) == limit and not flag_last_round:
--> 331     output = res[sourcetype] + self.query_iterative(q, show_results, limit, new_skip, pause, force,maxlimit, verbose, tot, _warnings_tot)
    333 elif IS_UNNEST and len(res[sourcetype]) > 0 and not flag_last_round:
    334     # unnest returns a number of records that don't relate to actual data left
    335     # hence can't match the lenght of results to limit in this case
    336     output = res[sourcetype] + self.query_iterative(q, show_results, limit, new_skip, pause, force, maxlimit, verbose, tot, _warnings_tot)

File c:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:292, in Dsl.query_iterative(self, q, show_results, limit, skip, pause, force, maxlimit, verbose, _tot_count_prev_query, _warnings_tot)
    289 q2 = q + " limit %d skip %d" % (limit, skip)
    291 start = time.time()
--> 292 res = self.query(q2, show_results=False, retry=0, verbose=False)
    293 end = time.time()
    294 elapsed = end - start

File c:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\dimcli\core\api.py:196, in Dsl.query(self, q, show_results, retry, verbose)
    194 if verbose: printDebug("Response.header\n---\n" + str(response.headers), "red")
    195 if verbose: printDebug("Response.content\n---\n" +str(response.content), "red")
--> 196 response.raise_for_status()

File c:\Users\NES\anaconda3\envs\dimensions.ai\lib\site-packages\requests\models.py:1021, in Response.raise_for_status(self)
   1016     http_error_msg = (
   1017         f"{self.status_code} Server Error: {reason} for url: {self.url}"
   1018     )
   1020 if http_error_msg:
-> 1021     raise HTTPError(http_error_msg, response=self)

HTTPError: 502 Server Error: Bad Gateway for url: https://app.dimensions.ai/api/dsl/v2