def load_data_api(path_credentials):

    # Try loading objects from disk file; delete saveddata.pkl to force reload
    try:
        path_save = os.path.join(path_credentials, 'saveddata.pkl')
        with open(path_save, 'rb') as f:
            df_study, df_structure, df_report, df_report_structure, \
                df_optiongroup_structure = pickle.load(f)

        print('Loading data from file... Delete saveddata.pkl to force' +
              'reload data from Castor')

    except Exception:
        print('Loading data from PC failed. Reloading from Castor server.')
        df_study, df_structure, df_report, df_report_structure, \
            df_optiongroup_structure = import_data_by_record(path_credentials)

        path_credentials = os.path.join(path_credentials, 'saveddata.pkl')
        with open(path_credentials, 'wb') as f:
            pickle.dump([
                df_study, df_structure, df_report, df_report_structure,
                df_optiongroup_structure
            ], f)

    df_study = df_study.reset_index(drop=True)
    df_report = df_report.reset_index(drop=True)

    df_study = df_study.rename({'Record ID': "Record Id"}, axis=1)
    df_report = df_report.rename({'Record ID': "Record Id"}, axis=1)

    # Remove test records
    df_study = df_study.loc[df_study['Record Id'].astype(int) > 11000, :]

    var_columns = [
        'Form Type', 'Form Collection Name', 'Form Name',
        'Field Variable Name', 'Field Label', 'Field Type', 'Option Name',
        'Option Value'
    ]
    data_struct = get_all_field_information(path_credentials)
    data_struct = data_struct.loc[:, var_columns]

    return df_study, df_report, data_struct
Example #2
0
import configparser
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), '../user_settings.ini'))

# the excel file with all variables and answer options is stored here
target_excel = config['exportresults']['excel_file_variables']

# folder with all figures
figure_dir = config['exportresults']['figures_folder']

# # Get all data from Castor database (without any selection criterium)
# Note that you need export rights for every individual center.

if False:
    study_data,study_struct,reports_data,reports_struct,optiongroups_struct = covid19_import.import_data_by_record(config['CastorCredentials']['local_private_path'])

    with open(str(os.path.join(config['CastorCredentials']['local_private_path'],'objs.pkl')), 'wb') as f:  # Python 3: open(..., 'wb')
        pickle.dump([study_data,study_struct,reports_data,reports_struct,optiongroups_struct], f)
else:
    # Getting back the objects:
    with open(os.path.join(config['CastorCredentials']['local_private_path'],'objs.pkl'),'rb') as f:  # Python 3: open(..., 'rb')
        study_data,study_struct,reports_data,reports_struct,optiongroups_struct = pickle.load(f)
        

# %% Do a patient count on the data so far.
# # Patient counts
# ## Total number of patients
print('Total number of patients (with any data) is: '+str(len(study_data)))

# HOSPITAL ADMISSION	ONSET & ADMISSION	admission_dt	Admission date at this facility: