-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.py
More file actions
291 lines (221 loc) · 9.71 KB
/
Copy pathutil.py
File metadata and controls
291 lines (221 loc) · 9.71 KB
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
from datetime import datetime
from pathlib import Path
import pandas as pd
import json
def get_consent(arg):
if isinstance(arg,pd.DataFrame):
df=arg
else:
df=pd.read_csv(arg, dtype=str)
# df can contain Young Patient's and/or Guardian's rows
# sort in ascending order
df_sorted= df.sort_values('interview_date',
key=lambda dates: [datetime.strptime(x,'%m/%d/%Y') for x in dates])
chr_hc= df_sorted.iloc[-1]['group']
return df_sorted, chr_hc
def get_study(ampscz_id: str,
data_from_nda_path: str = '/data/predict1/data_from_nda/') -> str:
'''Get name of the study based on the AMP-SCZ ID
Key arguments:
ampscz_id: AMP-SCZ ID, str.
data_from_nda_path: Root path of Prescient and Pronet PHOENIX
directories, str
Returns:
str, either 'Prescient' or 'Pronet'
'''
site = ampscz_id[:2]
data_from_nda_path = Path(data_from_nda_path)
metadata_paths = data_from_nda_path.glob(
'*/PHOENIX/GENERAL/*/*_metadata.csv')
for metadata_path in metadata_paths:
study = metadata_path.parent.parent.parent.parent.name
if site in metadata_path.name:
return study
return None
def str_date_minus_str_date(date_str1: str, date_str2: str) -> int:
'''Get time delta between dates in string: (date_str2 - date_str1)'''
date1 = datetime.strptime(date_str1, '%Y-%m-%d')
date2 = datetime.strptime(date_str2, '%Y-%m-%d')
time_delta = (date2 - date1)
diff_days = time_delta.days+1
return diff_days
def days_from_today_to_str_date(date_str: str) -> int:
'''Get time delta from today to a date in string format'''
diff_days = str_date_minus_str_date(datetime.today().strftime('%Y-%m-%d'),
date_str)
return diff_days
def check_file_delay(file: str, date_str: str) -> int:
'''Get time delta from today to a date in string format'''
if Path(file).is_file():
return 1
else:
return days_from_today_to_str_date(date_str)
def get_guid_prescient(
ampscz_id: str,
PHOENIX_root: str = '/data/predict1/data_from_nda/Prescient/PHOENIX') \
-> str:
'''Get GUID for a Prescient subject ID'''
prescient_protected = Path(PHOENIX_root) / 'PROTECTED'
site = ampscz_id[:2]
study_protected = prescient_protected / f'Prescient{site}'
study_protected_raw = study_protected / 'raw'
subject_protected_raw = study_protected_raw / ampscz_id
survey_protected_raw = subject_protected_raw / 'surveys'
guid_form = survey_protected_raw / f'{ampscz_id}_guid_form.csv'
df = pd.read_csv(guid_form)
unique_guid_list = df['chrguid_guid'].unique()
# make usre there is only one guid
assert len(unique_guid_list) == 1, 'There is more than one GUID'
guid = unique_guid_list[0]
return guid
def get_guid_pronet(
ampscz_id: str,
PHOENIX_root: str = '/data/predict1/data_from_nda/Pronet/PHOENIX') \
-> str:
'''Get GUID for a Pronet subject ID'''
pronet_protected = Path(PHOENIX_root) / 'PROTECTED'
site = ampscz_id[:2]
study_protected = pronet_protected / f'Pronet{site}'
study_protected_raw = study_protected / 'raw'
subject_protected_raw = study_protected_raw / ampscz_id
survey_protected_raw = subject_protected_raw / 'surveys'
survey_json_path = survey_protected_raw / f'{ampscz_id}.Pronet.json'
with open(survey_json_path, 'r') as fp:
data = json.load(fp)
df = pd.DataFrame(data)
unique_guid_list = df['chrguid_guid'].map(
lambda x: pd.NA if x=='' else x).dropna().unique()
# make usre there is only one guid
assert len(unique_guid_list) == 1, 'There is more than one GUID'
guid = unique_guid_list[0]
return guid
def get_guid(ampscz_id: str) -> str:
'''Get GUID for a AMP-SCZ ID'''
if get_study(ampscz_id) == 'Pronet':
return get_guid_pronet(ampscz_id)
elif get_study(ampscz_id) == 'Prescient':
return get_guid_prescient(ampscz_id)
else:
return None
def get_survey_pronet(ampscz_id: str) -> str:
site = ampscz_id[:2]
data_location = '/data/predict1/data_from_nda'
pronet_root = Path(data_location) / 'Pronet'
pronet_protected = pronet_root / 'PHOENIX' / 'PROTECTED'
study_protected = pronet_protected / f'Pronet{site}'
study_protected_raw = study_protected / 'raw'
subject_protected_raw = study_protected_raw / ampscz_id
survey_protected_raw = subject_protected_raw / 'surveys'
survey_json_path = survey_protected_raw / f'{ampscz_id}.Pronet.json'
return survey_json_path
def get_mri_df_pronet(ampscz_id: str, timepoint: str):
survey_json_path = get_survey_pronet(ampscz_id)
with open(survey_json_path, 'r') as fp:
data = json.load(fp)
df = pd.DataFrame(data)
if timepoint == 'baseline':
df = df[df['redcap_event_name'] == 'baseline_arm_1']
df.redcap_event_name = 'baseline'
elif timepoint == 'followup':
df = df[df['redcap_event_name'] == 'month_2_arm_1']
df.redcap_event_name = 'followup'
mri_avail_df = df[df.chrmri_consent != '']
mri_avail_df = mri_avail_df[
['redcap_event_name'] +
[x for x in mri_avail_df.columns if x.startswith('chrmri')]
].set_index('redcap_event_name').T
return mri_avail_df[timepoint]
def get_survey_mri_df_pronet(ampscz_id: str, fields_to_check: list,
timepoint: str) -> str:
survey_json_path = get_survey_pronet(ampscz_id)
with open(survey_json_path, 'r') as fp:
data = json.load(fp)
df = pd.DataFrame(data)
if timepoint == 'baseline':
df = df[df['redcap_event_name'] == 'baseline_arm_1']
elif timepoint == 'followup':
df = df[df['redcap_event_name'] == 'month_2_arm_1']
mri_avail_df = df[df.chrmri_consent != '']
df_tmp = mri_avail_df[
['redcap_event_name'] +
[x for x in df.columns if x in fields_to_check]
].set_index('redcap_event_name').T
# update Fail binary variables to 3
df_tmp.loc['chrmri_confirm'] = df_tmp.loc['chrmri_confirm'].apply(
lambda x: '1' if x == '-3' else x)
for binary_field in ['chrmri_metal',
'chrmri_physicalmetal',
'chrmri_dental']:
df_tmp.loc[binary_field] = df_tmp.loc[binary_field].apply(
lambda x: '1' if x == '1' else '3')
out_df = df_tmp.isin(['1', '2', '']).all(axis=0)
return out_df.loc[timepoint]
def get_survey_mri_df_prescient(ampscz_id: str, fields_to_check: list,
timepoint: str) -> str:
if timepoint == 'baseline':
timepoint = 2
elif timepoint == 'followup':
timepoint = 4
site = ampscz_id[:2]
data_location = '/data/predict1/data_from_nda'
prescient_root = Path(data_location) / 'Prescient'
prescient_protected = prescient_root / 'PHOENIX' / 'PROTECTED'
site = 'ME'
study_protected = prescient_protected / f'Prescient{site}'
study_protected_raw = study_protected / 'raw'
subject_protected_raw = study_protected_raw / ampscz_id
survey_protected_raw = subject_protected_raw / 'surveys'
mri_run_sheet = survey_protected_raw / f'{ampscz_id}_mri_run_sheet.csv'
mri_df = pd.read_csv(mri_run_sheet)
mri_df = mri_df[mri_df.visit == timepoint]
# ignore -3
df_tmp = mri_df.T
# -3 columns
to_ignore_col_names = mri_df[(df_tmp == -3).T].dropna(axis=1).columns
mri_df = mri_df.drop(to_ignore_col_names, axis=1).fillna('')
out_df = mri_df[
[x for x in fields_to_check if x in mri_df.columns]].T.isin(
[1, 2, '']).all(axis=0)
return out_df.iloc[0]
def get_pass_fail_survey_mri_df(ampscz_id: str, timepoint: str) -> bool:
'''
Key Arguments:
ampscz_id:
timepoint: 'baseline' or 'followup'
'''
fields_to_check = [
'chrmri_confirm', 'chrmri_metal',
'chrmri_physicalmetal', 'chrmri_dental', 'chrmri_consent',
'chrmri_aahscout',
'chrmri_calib_ge', 'chrmri_calib_ge_2', 'chrmri_calib_ge_3',
'chrmri_localizeraligned', 'chrmri_localizerseq',
'chrmri_localizerseq_ge',
'chrmri_dmap', 'chrmri_dmap2', 'chrmri_dmap3',
'chrmri_dmap_qc', 'chrmri_dmap_qc_2', 'chrmri_dmap_qc_3',
'chrmri_dmpa', 'chrmri_dmpa2', 'chrmri_dmpa3',
'chrmri_dmpa_qc', 'chrmri_dmpa_qc_2', 'chrmri_dmpa_qc_3',
'chrmri_t1', 'chrmri_t1_qc',
'chrmri_t2', 'chrmri_t2_qc', 'chrmri_t2_ge', 'chrmri_t2_qc_ge',
'chrmri_dmri126', 'chrmri_dmri126_qc',
'chrmri_dmri176', 'chrmri_dmri176_qc',
'chrmri_dmri_b0', 'chrmri_dmri_b0_2',
'chrmri_dmri_b0_qc', 'chrmri_dmri_b0_qc_2',
'chrmri_rfmriap', 'chrmri_rfmriap2',
'chrmri_rfmriap2_qc', 'chrmri_rfmriap_qc',
'chrmri_rfmriap_ref_num', 'chrmri_rfmriap_ref_num_2',
'chrmri_rfmriap_ref_qc', 'chrmri_rfmriap_ref_qc_2',
'chrmri_rfmripa', 'chrmri_rfmripa2',
'chrmri_rfmripa2_qc', 'chrmri_rfmripa_qc',
'chrmri_rfmripa_ref_num', 'chrmri_rfmripa_ref_num_2',
'chrmri_rfmripa_ref_qc', 'chrmri_rfmripa_ref_qc_2'
]
if get_study(ampscz_id) == 'Pronet':
qc_var = get_survey_mri_df_pronet(ampscz_id,
fields_to_check,
timepoint)
return qc_var
elif get_study(ampscz_id) == 'Prescient':
qc_var = get_survey_mri_df_prescient(ampscz_id,
fields_to_check,
timepoint)
return qc_var