-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafetch.py
331 lines (259 loc) · 9.05 KB
/
datafetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
"""
@author: Justin Okeke
Retrieve relevant data for questionnaire sendout
"""
import pyodbc
import pandas as pd
import requests
import io
import re
def access_data(
path_access, enrolment = True, followup = True, screening = False
):
"""Get data from Access database
Parameters
----------
path_access : str, optional
Path to Access database.
enrolment : bool, optional
Setting to True will return dictionary containing 'enrolment'
(i.e. 'OBS Enrolment Log') pandas dataframe. The default is True.
followup : bool, optional
Setting to True will return dictionary containing 'enrolment'
(i.e. 'OBSFollowupLog') pandas dataframe. The default is True.
screening : bool, optional
Setting to True will return dictionary containing 'enrolment'
(i.e. 'OBS Screening Log') pandas dataframe. The default is False.
Returns
-------
dict of pandas.dataframes
'enrolment' contains 'OBS Enrolment Log' Access table
'followup' contains 'OBSFollowupLog' Access table
'screening' contains 'OBS Screening Log' Access table
"""
cnxn = pyodbc.connect((r'DRIVER={Microsoft Access Driver '
r'(*.mdb, *.accdb)}; DBQ=' + path_access +
r';;UID="";PWD="";'))
access_dict = {}
if enrolment:
access_dict['enrolment'] = pd.read_sql(
"Select * From [OBS Enrolment Log]", cnxn
)
if followup:
access_dict['followup'] = pd.read_sql(
"Select * From [OBSFollowupLog]", cnxn
)
if screening:
access_dict['screening'] = pd.read_sql(
"Select * From [OBS Screening log]", cnxn
)
cnxn.close()
for key, value in access_dict.items():
access_dict[key] = clean_access_table(value)
return access_dict
def clean_access_table(access_table):
"""Clean Access Table
Parameters
----------
access_table : pandas.DataFrame
DESCRIPTION.
Returns
-------
access_table : pandas.DataFrame
DESCRIPTION.
"""
access_table = access_table.drop(
access_table[access_table['OBSEnrolmentID'].isna()
].index)
access_table['OBSEnrolmentID'] = (
access_table['OBSEnrolmentID'].replace('[^0-9]', '', regex = True)
.astype(int)
)
#access_table['OBSEnrolmentID'] = access_table['OBSEnrolmentID'].astype(int)
access_table = access_table.rename(
columns = {'OBSEnrolmentID':'obs_study_id'}
)
return access_table
# remove defaults for previous and multiples in access_exclude
def access_exclude(
access_data_dict, excl_previous = True, excl_multiple = True,
excl_no_use = True, excl_no_contact = False, excl_no_access = False,
excl_fetal_demise = False, excl_neonatal_death = False
):
""" Get list of OBS subject IDs to exclude
Parameters
----------
access_data_dict : dict of pandas dataframes
Expected to be derived from access_data(enrolment = True,
followup = True).
excl_previous : bool, optional
Setting to True will return most recent OBS subject id(s) for
previously enrolled OBS subjects. The default is True.
excl_multiple : bool, optional
Setting to True will return OBS subject ids of subjects who had
multiples. The default is True.
excl_no_use : bool, optional
Setting to True will return OBS subject ids who choose a "No Further
Use" withdrawal. The default is True.
excl_no_contact : bool, optional
Setting to True will return OBS subject ids who choose a "No Further
Contact" withdrawal. The default is True.
excl_no_access : bool, optional
Setting to True will return OBS subject ids who choose a "No Further
Access" withdrawal. The default is False.
excl_fetal_demise : bool, optional
Setting to True will return OBS subject ids who had a fetal demise
during the associated OBS enrolment. The default is False.
excl_neonatal_death : bool, optional
Setting to True will return OBS subject ids who had a neonatal death
during the associated OBS enrolment. The default is False.
Returns
-------
access_exclude : list
OBS subjects to be excluded
"""
# needs to include most up to date acceess database
# if access_data_dict == None:
# access_data_dict = access_data()
followup = access_data_dict['followup']
enrolment = access_data_dict['enrolment']
excl_type_dict = {'NoUse': excl_no_use,
'NoContact': excl_no_contact,
'NoAccess': excl_no_access,
'Fetal Demise/Termination': excl_fetal_demise,
'Neonatal death': excl_neonatal_death}
access_exclude = []
for key, value in excl_type_dict.items():
if value:
access_exclude.extend(
followup.loc[followup[key] == 1, 'obs_study_id'].tolist()
)
if excl_previous:
access_exclude = access_exclude.extend(
enrolment.loc[enrolment['Previous OBS participant'],
'obs_study_id'
].tolist()
)
if excl_multiple:
access_exclude = access_exclude.extend(
followup.loc[followup['TwinBDelivery'].notnull(),
'obs_study_id'
].tolist()
)
access_exclude = list(set(access_exclude))
return access_exclude
#REDCap data
# for clinic, need to get a different way
def redcap_data(api_param, add_param = {}):
"""Get data from AHRC REDCap
Parameters
----------
api_param : str
API token for associated REDCap data.
add_param : TYPE, optional
Add or overwrite request parameters. The default is {}.
Returns
-------
rc_data : pandas.DataFrame
REDCap data.
"""
api_req_data = {
'content': 'record',
'format': 'csv',
'type': 'flat',
'rawOrLabel': 'raw',
'rawOrLabelHeaders': 'raw',
'exportCheckboxLabel': 'false',
'exportSurveyFields': 'true', #necessary for completion date
'returnFormat': 'json',
'exportDataAccessGroups': 'true',
}
api_req_data.update({'token': api_param})
api_req_data.update(add_param)
req = requests.post(
'https://redcap.smh.ca/redcap/api/', api_req_data
).content
rc_data = pd.read_csv(
io.StringIO(req.decode('utf-8')),
sep = ',',
error_bad_lines = False,
index_col = False,
dtype = str
)
return rc_data
def redcap_lsq_summary(api_param, version):
"""Get REDCap LSQ summary data
Parameters
----------
api_param : str
DESCRIPTION.
version : int or str
Version of LSQ requesting (e.g. '1' for LSQ1).
Returns
-------
lsq_summary : pandas.DataFrame
REDCap LSQ summary data.
"""
lsq_summary = redcap_data(
api_param,
{
'fields[0]' : 'obs_study_id',
'fields[1]' : (
'lifestyle_questionnaire_'
+ re.sub(r'^[a-zA-Z]*', '', str(version))
+ '_timestamp'
),
'fields[2]' : (
'lifestyle_questionnaire_'
+ re.sub(r'^[a-zA-Z]*', '', str(version))
+ '_complete'),
}
)
return lsq_summary
def lsq_complete(lsq):
"""Clean LSQ dataframe
Parameters
----------
lsq : pandas.DataFrame
LSQ data derived from redcap_data()
Returns
-------
complete_lsq : pandas.DataFrame
DataFrame only containing completed LSQs with 'password' and
'redcap_survey_identifier' columns removed
"""
for lsq_version in range(1, 4):
complete_col = (
'lifestyle_questionnaire_' + str(lsq_version) +'_complete'
)
if complete_col in list(lsq.columns.values):
complete_lsq = lsq.loc[lsq[complete_col] == '2']
complete_lsq = complete_lsq.drop(columns = [
'password', 'redcap_survey_identifier',
], errors = 'ignore')
break
return complete_lsq
def redcap_clinic(api_param):
"""Get AHRC REDCap clinic data
Parameters
----------
api_param : str
API token for associated REDCap data.
Returns
-------
complete_lsq : pandas.DataFrame
DataFrame only containing REDCap clinic data.
Notes
-----
Clinical data requires special treament due to export size
restrictions. Creates post request for each obs id and merges.
"""
rc_data = redcap_data(api_param, {'fields[0]': 'obs_id'})
obs_id_lst = list(rc_data['obs_id'].unique())
clinic_df_lst = []
for obs_id in obs_id_lst:
rc_data = redcap_data(api_param, {'records[0]': obs_id})
clinic_df_lst.append(rc_data)
merge_rc = pd.concat(clinic_df_lst)
return merge_rc