-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_code.py
385 lines (320 loc) · 13.7 KB
/
helper_code.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
# Do *not* edit this script.
# These are helper functions that you can use with your code.
# Check the example code to see how to import these functions to your code.
import os, numpy as np, scipy as sp, scipy.io
### Challenge data I/O functions
# Find the folders with data files.
def find_data_folders(root_folder):
data_folders = list()
for x in os.listdir(root_folder):
data_folder = os.path.join(root_folder, x)
if os.path.isdir(data_folder):
data_folders.append(x)
return sorted(data_folders)
def load_challenge_data(data_folder, patient_id):
# Define file location.
patient_metadata_file = os.path.join(data_folder, patient_id, patient_id + '.txt')
recording_metadata_file = os.path.join(data_folder, patient_id, patient_id + '.tsv')
# Load non-recording data.
patient_metadata = load_text_file(patient_metadata_file)
recording_metadata = load_text_file(recording_metadata_file)
# Load recordings.
recordings = list()
recording_ids = get_recording_ids(recording_metadata)
for recording_id in recording_ids:
if recording_id != 'nan':
recording_location = os.path.join(data_folder, patient_id, recording_id)
recording_data, sampling_frequency, channels = load_recording(recording_location)
else:
recording_data = None
sampling_frequency = None
channels = None
recordings.append((recording_data, sampling_frequency, channels))
return patient_metadata, recording_metadata, recordings
# Load the WFDB data for the Challenge (but not all possible WFDB files).
def load_recording(record_name):
# Allow either the record name or the header filename.
root, ext = os.path.splitext(record_name)
if ext=='':
header_file = record_name + '.hea'
else:
header_file = record_name
# Load the header file.
if not os.path.isfile(header_file):
raise FileNotFoundError('{} recording not found.'.format(record_name))
with open(header_file, 'r') as f:
header = [l.strip() for l in f.readlines() if l.strip()]
# Parse the header file.
record_name = None
num_signals = None
sampling_frequency = None
num_samples = None
signal_files = list()
gains = list()
offsets = list()
channels = list()
initial_values = list()
checksums = list()
for i, l in enumerate(header):
arrs = [arr.strip() for arr in l.split(' ')]
# Parse the record line.
if i==0:
record_name = arrs[0]
num_signals = int(arrs[1])
sampling_frequency = float(arrs[2])
num_samples = int(arrs[3])
# Parse the signal specification lines.
else:
signal_file = arrs[0]
gain = float(arrs[2].split('/')[0])
offset = int(arrs[4])
initial_value = int(arrs[5])
checksum = int(arrs[6])
channel = arrs[8]
signal_files.append(signal_file)
gains.append(gain)
offsets.append(offset)
initial_values.append(initial_value)
checksums.append(checksum)
channels.append(channel)
# Check that the header file only references one signal file. WFDB format allows for multiple signal files, but we have not
# implemented that here for simplicity.
num_signal_files = len(set(signal_files))
if num_signal_files!=1:
raise NotImplementedError('The header file {}'.format(header_file) \
+ ' references {} signal files; one signal file expected.'.format(num_signal_files))
# Load the signal file.
head, tail = os.path.split(header_file)
signal_file = os.path.join(head, list(signal_files)[0])
data = np.asarray(sp.io.loadmat(signal_file)['val'])
# Check that the dimensions of the signal data in the signal file is consistent with the dimensions for the signal data given
# in the header file.
num_channels = len(channels)
if np.shape(data)!=(num_channels, num_samples):
raise ValueError('The header file {}'.format(header_file) \
+ ' is inconsistent with the dimensions of the signal file.')
# Check that the initial value and checksums for the signal data in the signal file are consistent with the initial value and
# checksums for the signal data given in the header file.
for i in range(num_channels):
if data[i, 0]!=initial_values[i]:
raise ValueError('The initial value in header file {}'.format(header_file) \
+ ' is inconsistent with the initial value for channel'.format(channels[i]))
if np.sum(data[i, :])!=checksums[i]:
raise ValueError('The checksum in header file {}'.format(header_file) \
+ ' is inconsistent with the initial value for channel'.format(channels[i]))
# Rescale the signal data using the ADC gains and ADC offsets.
rescaled_data = np.zeros(np.shape(data), dtype=np.float32)
for i in range(num_channels):
rescaled_data[i, :] = (data[i, :]-offsets[i])/gains[i]
return rescaled_data, sampling_frequency, channels
# Reorder/reselect the channels.
def reorder_recording_channels(current_data, current_channels, reordered_channels):
if current_channels == reordered_channels:
return current_data
else:
indices = list()
for channel in reordered_channels:
if channel in current_channels:
i = current_channels.index(channel)
indices.append(i)
num_channels = len(reordered_channels)
num_samples = np.shape(current_data)[1]
reordered_data = np.zeros((num_channels, num_samples))
reordered_data[:, :] = current_data[indices, :]
return reordered_data
### Helper Challenge data I/O functions
# Load text file as a string.
def load_text_file(filename):
with open(filename, 'r') as f:
data = f.read()
return data
# Get a variable from the patient metadata.
def get_variable(text, variable_name, variable_type, preserve_nan=True):
variable = None
for l in text.split('\n'):
if l.startswith(variable_name):
variable = l.split(':')[1].strip()
if preserve_nan and variable.lower() == 'nan':
variable = float('nan')
else:
variable = variable_type(variable)
return variable
# Get a column from the recording metadata.
def get_column(string, column, variable_type, sep='\t'):
variables = list()
for i, l in enumerate(string.split('\n')):
arrs = [arr.strip() for arr in l.split(sep) if arr.strip()]
if i==0:
column_index = arrs.index(column)
elif arrs:
variable = arrs[column_index]
variable = variable_type(variable)
variables.append(variable)
return np.asarray(variables)
# Get the patient ID variable from the patient data.
def get_patient_id(string):
return get_variable(string, 'Patient', str)
# Get the age variable (in years) from the patient data.
def get_age(string):
return get_variable(string, 'Age', int)
# Get the sex variable from the patient data.
def get_sex(string):
return get_variable(string, 'Sex', str)
# Get the ROSC variable (in minutes) from the patient data.
def get_rosc(string):
return get_variable(string, 'ROSC', int)
# Get the OHCA variable from the patient data.
def get_ohca(string):
return get_variable(string, 'OHCA', bool)
# Get the VFib variable from the patient data.
def get_vfib(string):
return get_variable(string, 'VFib', bool)
# Get the TTM variable (in Celsius) from the patient data.
def get_ttm(string):
return get_variable(string, 'TTM', int)
# Get the Outcome variable from the patient data.
def get_outcome(string):
variable = get_variable(string, 'Outcome', str)
if variable is None or is_nan(variable):
raise ValueError('No outcome available. Is your code trying to load labels from the hidden data?')
if variable == 'Good':
variable = 0
elif variable == 'Poor':
variable = 1
return variable
# Get the Outcome probability variable from the patient data.
def get_outcome_probability(string):
variable = sanitize_scalar_value(get_variable(string, 'Outcome probability', str))
if variable is None or is_nan(variable):
raise ValueError('No outcome available. Is your code trying to load labels from the hidden data?')
return variable
# Get the CPC variable from the patient data.
def get_cpc(string):
variable = sanitize_scalar_value(get_variable(string, 'CPC', str))
if variable is None or is_nan(variable):
raise ValueError('No CPC score available. Is your code trying to load labels from the hidden data?')
return variable
# Get the hour number column from the patient data.
def get_hours(string):
return get_column(string, 'Hour', int)
# Get the time column from the patient data.
def get_times(string):
return get_column(string, 'Time', str)
# Get the quality score column from the patient data.
def get_quality_scores(string):
return get_column(string, 'Quality', float)
# Get the recording IDs column from the patient data.
def get_recording_ids(string):
return get_column(string, 'Record', str)
### Challenge label and output I/O functions
# Load the Challenge labels for one file.
def load_challenge_label(string):
if os.path.isfile(string):
string = load_text_file(string)
outcome = get_outcome(string)
cpc = get_cpc(string)
return outcome, cpc
# Load the Challenge labels for all of the files in a folder.
def load_challenge_labels(folder):
patient_folders = find_data_folders(folder)
num_patients = len(patient_folders)
patient_ids = list()
outcomes = np.zeros(num_patients, dtype=np.bool_)
cpcs = np.zeros(num_patients, dtype=np.float64)
for i in range(num_patients):
patient_metadata_file = os.path.join(folder, patient_folders[i], patient_folders[i] + '.txt')
patient_metadata = load_text_file(patient_metadata_file)
patient_ids.append(get_patient_id(patient_metadata))
outcomes[i] = get_outcome(patient_metadata)
cpcs[i] = get_cpc(patient_metadata)
return patient_ids, outcomes, cpcs
# Save the Challenge outputs for one file.
def save_challenge_outputs(filename, patient_id, outcome, outcome_probability, cpc):
# Sanitize values, e.g., in case they are a singleton array.
outcome = sanitize_boolean_value(outcome)
outcome_probability = sanitize_scalar_value(outcome_probability)
cpc = sanitize_scalar_value(cpc)
# Format Challenge outputs.
patient_string = 'Patient: {}'.format(patient_id)
if outcome == 0:
outcome = 'Good'
elif outcome == 1:
outcome = 'Poor'
outcome_string = 'Outcome: {}'.format(outcome)
outcome_probability_string = 'Outcome probability: {:.3f}'.format(float(outcome_probability))
cpc_string = 'CPC: {:.3f}'.format(int(float(cpc)) if is_integer(cpc) else float(cpc))
output_string = patient_string + '\n' + \
outcome_string + '\n' + outcome_probability_string + '\n' + cpc_string + '\n'
# Write the Challenge outputs.
if filename is not None:
with open(filename, 'w') as f:
f.write(output_string)
return output_string
# Load the Challenge outputs for one file.
def load_challenge_output(string):
if os.path.isfile(string):
string = load_text_file(string)
patient_id = get_patient_id(string)
outcome = get_outcome(string)
outcome_probability = get_outcome_probability(string)
cpc = get_cpc(string)
return patient_id, outcome, outcome_probability, cpc
# Load the Challenge outputs for all of the files in folder.
def load_challenge_outputs(folder, patient_ids):
num_patients = len(patient_ids)
outcomes = np.zeros(num_patients, dtype=np.bool_)
outcome_probabilities = np.zeros(num_patients, dtype=np.float64)
cpcs = np.zeros(num_patients, dtype=np.float64)
for i in range(num_patients):
output_file = os.path.join(folder, patient_ids[i], patient_ids[i] + '.txt')
patient_id, outcome, outcome_probability, cpc = load_challenge_output(output_file)
outcomes[i] = outcome
outcome_probabilities[i] = outcome_probability
cpcs[i] = cpc
return outcomes, outcome_probabilities, cpcs
### Other helper functions
# Check if a variable is a number or represents a number.
def is_number(x):
try:
float(x)
return True
except (ValueError, TypeError):
return False
# Check if a variable is an integer or represents an integer.
def is_integer(x):
if is_number(x):
return float(x).is_integer()
else:
return False
# Check if a variable is a finite number or represents a finite number.
def is_finite_number(x):
if is_number(x):
return np.isfinite(float(x))
else:
return False
# Check if a variable is a NaN (not a number) or represents a NaN.
def is_nan(x):
if is_number(x):
return np.isnan(float(x))
else:
return False
# Remove any quotes, brackets (for singleton arrays), and/or invisible characters.
def remove_extra_characters(x):
return str(x).replace('"', '').replace("'", "").replace('[', '').replace(']', '').replace(' ', '').strip()
# Sanitize boolean values, e.g., from the Challenge outputs.
def sanitize_boolean_value(x):
x = remove_extra_characters(x)
if (is_finite_number(x) and float(x)==0) or (x in ('False', 'false', 'F', 'f')):
return 0
elif (is_finite_number(x) and float(x)==1) or (x in ('True', 'true', 'T', 't')):
return 1
else:
return float('nan')
# Santize scalar values, e.g., from the Challenge outputs.
def sanitize_scalar_value(x):
x = remove_extra_characters(x)
if is_number(x):
return float(x)
else:
return float('nan')