Example #1
0
def import_data_by_record(path_to_api_creds=None):
    config = configparser.ConfigParser()
    config.read(os.path.join(os.path.dirname(__file__), 'user_settings.ini'))
    
    if path_to_api_creds == None:
        path_to_api_creds = config['CastorCredentials']['local_private_path']
    # alternative for import_data if import_data fails due to server-side timeout errors (i.e. for large datasets)
    # this alternative loops over de records and report instances to load the data
    
    # input: private folder where client & secret files (no extension, 1 string only per file) from castor are saved by the user
    # see also: https://helpdesk.castoredc.com/article/124-application-programming-interface-api
    c = Castor_api(path_to_api_creds) # e.g. in user dir outside of GIT repo
    
    # get study ID for COVID study
    c.select_study_by_name(config['CastorCredentials']['study_name'])    
    
    df_study, df_structure_study, df_report, df_structure_report, df_optiongroups_structure = c.records_reports_all(report_names=['Daily'])
    
    # remove test institute and archived (deleted) records
    test_inst = [i for i in c.request_institutes() if 'test' in i['name'].lower()][0]
    test_records = [r['record_id'] for r in c.request_study_records(institute=test_inst['institute_id'])]
    test_records += [r['record_id'] for r in c.request_study_records() if r['archived']==1]

    df_study.drop(index=df_study[df_study['Record Id'].isin(test_records)].index, inplace=True)
    df_report.drop(index=df_report[df_report['Record Id'].isin(test_records)].index, inplace=True)
    
    return df_study, df_structure_study, df_report, df_structure_report, df_optiongroups_structure
"""
import site
site.addsitedir(
    './../')  # add directory to path to enable import of castor_api
from castor_api import Castor_api

import configparser
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), '../user_settings.ini'))

c = Castor_api(config['CastorCredentials']['local_private_path'])
study_id = c.select_study_by_name(config['CastorCredentials']['study_name'])
varname = 'Outcome'

# select AMC + VUmc + MUMC
institutes = c.request_institutes()
inst_amc_vumc = [
    inst['institute_id'] for inst in c.request_institutes()
    if (inst['name'] == 'AUMC - AMC' or inst['name'] == 'AUMC - VUmc'
        or inst['name'] == 'MUMC')
]
records = c.request_study_records(
    institute=inst_amc_vumc[0]) + c.request_study_records(
        institute=inst_amc_vumc[1])

options = c.request_fieldoptiongroup(
    optiongroup_id=c.field_optiongroup_by_variable_name(varname))
values = c.field_values_by_variable_name(varname, records=records)

print('Total records: ' + str(len(values)))
print(' - records: \'' + varname + '\' data not present: ' +