-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_ldopa.py
260 lines (198 loc) · 8 KB
/
process_ldopa.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
import os
import pandas as pd
from tqdm import tqdm
from scipy import constants
from glob import glob
from joblib import Parallel, delayed
from utils import parse_datetime_from_timestamp
RAW_DIR = "Data/LDOPA_DATA/"
PROCESSED_DIR = "Data/Ldopa_Processed/"
N_JOBS = 8
def build_metadata(datadir=RAW_DIR, processeddir=PROCESSED_DIR):
outFile = os.path.join(processeddir, "Metadata.csv")
if os.path.exists(outFile):
return pd.read_csv(outFile, index_col=[0])
metadata = pd.read_csv(
os.path.join(datadir, "MetadataOfPatientOnboarding.csv"), index_col=[0]
)
updrs_cols = [
"updrs_score_p1",
"updrs_score_p2",
"updrs_score_p3",
"updrs_score_p4",
"updrs_second_visit_score_p3",
]
metadata["MeanUPDRS"] = metadata[updrs_cols].mean(axis=1)
metadata = metadata[["gender", "MeanUPDRS"]]
metadata.to_csv(outFile)
return metadata
def build_acc_data(datadir=RAW_DIR, processeddir=PROCESSED_DIR, n_jobs=N_JOBS):
subjects = build_task_reference_file(datadir, processeddir)["subject_id"].unique()
outdir = os.path.join(processeddir, "acc_data")
os.makedirs(outdir, exist_ok=True)
if len(glob(os.path.join(outdir, "*.csv"))) != len(subjects):
Parallel(n_jobs=n_jobs)(
delayed(build_participant_acc_data)(subject, datadir, outdir)
for subject in tqdm(subjects)
)
else:
print("Acceleration data already compiled...\n")
def build_task_reference_file(datadir=RAW_DIR, outdir=PROCESSED_DIR, overwrite=False):
outFile = os.path.join(outdir, "TaskReferenceFile.csv")
if os.path.exists(outFile) and not overwrite:
taskRefFile = pd.read_csv(
outFile, parse_dates=["timestamp_start", "timestamp_end"]
)
print(f'Using saved task reference file saved at "{outFile}".')
else:
os.makedirs(outdir, exist_ok=True)
taskScoreFile1 = os.path.join(datadir, "TaskScoresPartI.csv")
taskScoreFile2 = os.path.join(datadir, "TaskScoresPartII.csv")
homeTaskFile = os.path.join(datadir, "HomeTasks.csv")
taskScore1 = pd.read_csv(
taskScoreFile1,
parse_dates=["timestamp_start", "timestamp_end"],
date_parser=parse_datetime_from_timestamp,
)
taskScore2 = pd.read_csv(
taskScoreFile2,
parse_dates=["timestamp_start", "timestamp_end"],
date_parser=parse_datetime_from_timestamp,
)
taskScores = pd.concat([taskScore1, taskScore2])[
["subject_id", "visit", "task_code", "timestamp_start", "timestamp_end"]
]
visit_to_day = {1: 1, 2: 4}
taskScores["participant_day"] = taskScores["visit"].map(visit_to_day)
taskScores.drop("visit", axis=1, inplace=True)
homeTasks = pd.read_csv(
homeTaskFile,
parse_dates=["timestamp_start", "timestamp_end"],
date_parser=parse_datetime_from_timestamp,
)
homeTasks = homeTasks[
[
"subject_id",
"participant_day",
"task_code",
"timestamp_start",
"timestamp_end",
]
]
taskRefFile = (
pd.concat([taskScores, homeTasks]).drop_duplicates().reset_index(drop=True)
)
taskRefFile.to_csv(outFile)
return taskRefFile
def build_participant_acc_data(subject, datadir, outdir):
os.makedirs(outdir, exist_ok=True)
accFile = os.path.join(outdir, f"{subject}.csv")
if not os.path.exists(accFile):
dataFiles = [
pd.read_csv(
build_patient_file_path(datadir, "GENEActiv", subject, i),
delimiter="\t",
index_col="timestamp",
parse_dates=True,
skipinitialspace=True,
date_parser=parse_datetime_from_timestamp,
)
for i in range(1, 5)
]
subjectFile = pd.concat(dataFiles).dropna().drop_duplicates()
subjectFile = subjectFile / constants.g
subjectFile.index.name = "timestamp"
subjectFile.rename(
columns={
"GENEActiv_X": "x",
"GENEActiv_Y": "y",
"GENEActiv_Z": "z",
"GENEActiv_Magnitude": "mag",
},
inplace=True,
)
subjectFile.to_csv(accFile)
else:
print(f'Using saved subject accelerometery data at "{accFile}".')
def build_patient_file_path(dataFolder, device, subject_id, index):
return os.path.join(
dataFolder, device, get_patient_folder(subject_id), f"rawdata_day{index}.txt"
)
def get_patient_folder(subject_id):
subject_num, subject_loc = subject_id.split("_", 1)
if subject_loc == "BOS":
return "patient" + subject_num
elif subject_loc == "NYC":
return "patient" + subject_num + "_NY"
else:
raise AssertionError("Invalid subject id")
def label_acc_data(label, datadir=RAW_DIR, processeddir=PROCESSED_DIR, n_jobs=N_JOBS):
taskRefFile = build_task_reference_file(datadir, processeddir)
subjects = taskRefFile["subject_id"].unique()
outdir = os.path.join(PROCESSED_DIR, "raw_labels")
os.makedirs(outdir, exist_ok=True)
if len(glob(os.path.join(outdir, "*.csv"))) != len(subjects):
taskDictionary = build_task_dictionary(datadir, processeddir)
accdir = os.path.join(processeddir, "acc_data")
Parallel(n_jobs=n_jobs)(
delayed(label_participant_data)(
subject, taskRefFile, taskDictionary, accdir, outdir, label
)
for subject in tqdm(subjects)
)
else:
print("Label data already compiled...\n")
def build_task_dictionary(datadir=RAW_DIR, outdir=PROCESSED_DIR):
processedDictionaryPath = os.path.join(outdir, "TaskDictionary.csv")
if os.path.exists(processedDictionaryPath):
taskDictionary = pd.read_csv(processedDictionaryPath, index_col="task_code")
else:
os.makedirs(os.path.dirname(outdir), exist_ok=True)
taskDictionary = pd.read_csv(os.path.join(datadir, "TaskCodeDictionary.csv"))
taskDictionary["is-walking"] = taskDictionary["description"].apply(
is_walking_given_description
)
taskDictionary["activity"] = taskDictionary["task_code"].apply(
activity_given_task_code
)
taskDictionary.set_index("task_code", inplace=True)
taskDictionary.to_csv(processedDictionaryPath)
return taskDictionary
def is_walking_given_description(description):
return (
"walking"
if (("WALKING" in description.upper()) or ("STAIRS" in description.upper()))
else "not-walking"
)
def activity_given_task_code(task_code):
if "wlkg" in task_code:
return "wlkg"
elif "ftn" in task_code:
return "ftn"
elif "ram" in task_code:
return "ram"
else:
return task_code
def label_participant_data(
subject, taskRefFile, taskDictionary, accdir, outdir, label="is-walking"
):
os.makedirs(outdir, exist_ok=True)
labelFilePath = os.path.join(outdir, f"{subject}.csv")
accFilePath = os.path.join(accdir, f"{subject}.csv")
if not os.path.exists(labelFilePath):
accFile = pd.read_csv(accFilePath, index_col=[0], parse_dates=True)
participantTasks = taskRefFile[taskRefFile["subject_id"] == subject]
accFile["annotation"] = -1
accFile["day"] = -1
for _, task in participantTasks.iterrows():
startTime, endTime = task[["timestamp_start", "timestamp_end"]]
mask = (accFile.index > startTime) & (accFile.index <= endTime)
accFile.loc[mask, "annotation"] = taskDictionary.loc[
task["task_code"], label
]
accFile.loc[mask, "day"] = task["participant_day"]
walkingLabels = accFile["annotation"]
walkingLabels.to_csv(labelFilePath)
accFile.to_csv(accFilePath)
else:
print(f'Using saved subject labelled accelerometery data at "{accFilePath}".')