-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_code.py
484 lines (408 loc) · 14.8 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/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 use these functions in your code.
import numpy as np
import os
import scipy as sp
import sys
import wfdb
### Challenge variables
age_string = '# Age:'
sex_string = '# Sex:'
label_string = '# Chagas label:'
probability_string = '# Chagas probability:'
### Challenge data I/O functions
# Find the records in a folder and its subfolders.
def find_records(folder, header_extension='.hea'):
records = set()
for root, directories, files in os.walk(folder):
for file in files:
extension = os.path.splitext(file)[1]
if extension == header_extension:
record = os.path.relpath(os.path.join(root, file), folder)[:-len(header_extension)]
records.add(record)
records = sorted(records)
return records
# Load the header for a record.
def load_header(record):
header_file = get_header_file(record)
header = load_text(header_file)
return header
# Load the signals for a record.
def load_signals(record):
signal, fields = wfdb.rdsamp(record)
return signal, fields
# Load the label for a record.
def load_label(record):
header = load_header(record)
label = get_label(header)
return label
# Load the probability for a record.
def load_probability(record):
header = load_header(record)
label = get_probability(header)
return label
# Save the model outputs for a record.
def save_outputs(output_file, record_name, label, probability):
output_string = f'{record_name}\n{label_string} {label}\n{probability_string} {probability}\n'
save_text(output_file, output_string)
return output_string
### Helper Challenge functions
# Load a text file as a string.
def load_text(filename):
with open(filename, 'r') as f:
string = f.read()
return string
# Save a string as a text file.
def save_text(filename, string):
with open(filename, 'w') as f:
f.write(string)
# Get a variable from a string.
def get_variable(string, variable_name):
variable = ''
has_variable = False
for l in string.split('\n'):
if l.startswith(variable_name):
variable = l[len(variable_name):].strip()
has_variable = True
return variable, has_variable
# Get the header file for a record.
def get_header_file(record):
if not record.endswith('.hea'):
header_file = record + '.hea'
else:
header_file = record
return header_file
# Get the signal files for a record.
def get_signal_files(record):
header_file = get_header_file(record)
if os.path.isfile(header_file):
header = load_text(header_file)
signal_files = get_signal_files_from_header(header)
else:
signal_files = list()
return signal_files
def get_signal_files_from_header(string):
signal_files = list()
for i, l in enumerate(string.split('\n')):
arrs = [arr.strip() for arr in l.split(' ')]
if i==0 and not l.startswith('#'):
num_channels = int(arrs[1])
elif i<=num_channels and not l.startswith('#'):
signal_file = arrs[0]
if signal_file not in signal_files:
signal_files.append(signal_file)
else:
break
return signal_files
# Get the age from a header or a similar string.
def get_age(string):
age, has_age = get_variable(string, age_string)
if not has_age:
age = float('nan')
elif is_number(age):
age = float(age)
return age
# Get the sex from a header or a similar string.
def get_sex(string):
sex, has_sex = get_variable(string, sex_string)
if not has_sex:
sex = None
return sex
# Get the label from a header or a similar string.
def get_label(string, allow_missing=False):
label, has_label = get_variable(string, label_string)
if not has_label and not allow_missing:
raise Exception('No label is available: are you trying to load the labels from the held-out data?')
label = sanitize_boolean_value(label)
return label
# Get the probability from a header or a similar string.
def get_probability(string, allow_missing=False):
probability, has_probability = get_variable(string, probability_string)
if not has_probability and not allow_missing:
raise Exception('No probability is available: are you trying to load the labels from the held-out data?')
probability = sanitize_scalar_value(probability)
return probability
### WFDB functions
# Get the record name from a header file.
def get_record_name(string):
value = string.split('\n')[0].split(' ')[0].split('/')[0].strip()
return value
# Get the number of signals from a header file.
def get_num_signals(string):
value = string.split('\n')[0].split(' ')[1].strip()
if is_integer(value):
value = int(value)
else:
value = None
return value
# Get the sampling frequency from a header file.
def get_sampling_frequency(string):
value = string.split('\n')[0].split(' ')[2].split('/')[0].strip()
if is_number(value):
value = float(value)
else:
value = None
return value
# Get the number of samples from a header file.
def get_num_samples(string):
value = string.split('\n')[0].split(' ')[3].strip()
if is_integer(value):
value = int(value)
else:
value = None
return value
# Get the signal names from a header file.
def get_signal_names(string):
num_signals = get_num_signals(string)
values = list()
for i, l in enumerate(string.split('\n')):
if 1 <= i <= num_signals:
value = l.split(' ')[8]
values.append(value)
return values
### Evaluation functions
# Compute the Challenge score.
def compute_challenge_score(labels, outputs, max_fraction_positive = 0.05):
# Check the data.
assert len(labels) == len(outputs)
num_instances = len(labels)
max_num_positive_instances = int(max_fraction_positive * num_instances)
# Convert the data to NumPy arrays, as needed, for easier indexing.
labels = np.asarray(labels, dtype=np.float64)
outputs = np.asarray(outputs, dtype=np.float64)
# Collect the unique output values as the thresholds for the positive and negative classes.
thresholds = np.unique(outputs)
thresholds = np.append(thresholds, thresholds[-1] + 1)
thresholds = thresholds[::-1]
num_thresholds = len(thresholds)
idx = np.argsort(outputs)[::-1]
# Initialize the TPs, FPs, FNs, and TNs with no positive outputs.
tp = np.zeros(num_thresholds)
fp = np.zeros(num_thresholds)
fn = np.zeros(num_thresholds)
tn = np.zeros(num_thresholds)
tp[0] = 0
fp[0] = 0
fn[0] = np.sum(labels == 1)
tn[0] = np.sum(labels == 0)
# Update the TPs, FPs, FNs, and TNs using the values at the previous threshold.
i = 0
for j in range(1, num_thresholds):
tp[j] = tp[j-1]
fp[j] = fp[j-1]
fn[j] = fn[j-1]
tn[j] = tn[j-1]
while i < num_instances and outputs[idx[i]] >= thresholds[j]:
if labels[idx[i]] == 1:
tp[j] += 1
fn[j] -= 1
else:
fp[j] += 1
tn[j] -= 1
i += 1
# Find the true positive rate so that the number of positive model outputs are no more than 5% of the total instances.
k = num_thresholds
for j in range(1, num_thresholds):
if tp[j] + fp[j] > max_num_positive_instances:
k = j - 1
break
if tp[k] + fn[k] > 0:
tpr = tp[k] / (tp[k] + fn[k])
else:
tpr = float('nan')
return tpr
# Compute area under the receiver operating characteristic curve (AUROC) and area under the precision recall curve (AUPRC).
def compute_auc(labels, outputs):
assert len(labels) == len(outputs)
num_instances = len(labels)
# Convert the data to NumPy arrays for easier indexing.
labels = np.asarray(labels, dtype=np.float64)
outputs = np.asarray(outputs, dtype=np.float64)
# Collect the unique output values as the thresholds for the positive and negative classes.
thresholds = np.unique(outputs)
thresholds = np.append(thresholds, thresholds[-1]+1)
thresholds = thresholds[::-1]
num_thresholds = len(thresholds)
idx = np.argsort(outputs)[::-1]
# Initialize the TPs, FPs, FNs, and TNs with no positive outputs.
tp = np.zeros(num_thresholds)
fp = np.zeros(num_thresholds)
fn = np.zeros(num_thresholds)
tn = np.zeros(num_thresholds)
tp[0] = 0
fp[0] = 0
fn[0] = np.sum(labels == 1)
tn[0] = np.sum(labels == 0)
# Update the TPs, FPs, FNs, and TNs using the values at the previous threshold.
i = 0
for j in range(1, num_thresholds):
tp[j] = tp[j-1]
fp[j] = fp[j-1]
fn[j] = fn[j-1]
tn[j] = tn[j-1]
while i < num_instances and outputs[idx[i]] >= thresholds[j]:
if labels[idx[i]] == 1:
tp[j] += 1
fn[j] -= 1
else:
fp[j] += 1
tn[j] -= 1
i += 1
# Compute the TPRs, TNRs, and PPVs at each threshold.
tpr = np.zeros(num_thresholds)
tnr = np.zeros(num_thresholds)
ppv = np.zeros(num_thresholds)
for j in range(num_thresholds):
if tp[j] + fn[j] > 0:
tpr[j] = tp[j] / (tp[j] + fn[j])
else:
tpr[j] = float('nan')
if fp[j] + tn[j] > 0:
tnr[j] = tn[j] / (fp[j] + tn[j])
else:
tnr[j] = float('nan')
if tp[j] + fp[j] > 0:
ppv[j] = tp[j] / (tp[j] + fp[j])
else:
ppv[j] = float('nan')
# Compute AUROC as the area under a piecewise linear function with TPR/sensitivity (x-axis) and TNR/specificity (y-axis) and
# AUPRC as the area under a piecewise constant with TPR/recall (x-axis) and PPV/precision (y-axis).
auroc = 0.0
auprc = 0.0
for j in range(num_thresholds-1):
auroc += 0.5 * (tpr[j+1] - tpr[j]) * (tnr[j+1] + tnr[j])
auprc += (tpr[j+1] - tpr[j]) * ppv[j+1]
return auroc, auprc
# Compute the binary confusion matrix, where the columns are the expert labels and the rows are the classifier labels for the given
# classes.
def compute_confusion_matrix(labels, outputs):
assert np.shape(labels) == np.shape(outputs)
num_instances = len(labels)
A = np.zeros((2, 2))
for i in range(num_instances):
if labels[i] == 1 and outputs[i] == 1:
A[0, 0] += 1
elif labels[i] == 1 and outputs[i] == 0:
A[0, 1] += 1
elif labels[i] == 0 and outputs[i] == 1:
A[1, 0] += 1
elif labels[i] == 0 and outputs[i] == 0:
A[1, 1] += 1
else:
raise ValueError(f'{labels[i]} and/or {outputs[i]} not valid.')
return A
# Compute accuracy.
def compute_accuracy(labels, outputs):
# Compute the confusion matrix.
A = compute_confusion_matrix(labels, outputs)
# Compute accuracy.
if np.sum(A) > 0:
accuracy = np.trace(A) / np.sum(A)
else:
accuracy = float('nan')
return accuracy
# Compute macro F-measure.
def compute_f_measure(labels, outputs):
# Compute the confusion matrix.
A = compute_confusion_matrix(labels, outputs)
tp, fp, fn, tn = A[0, 0], A[0, 1], A[1, 0], A[1, 1]
if 2 * tp + fp + fn > 0:
f_measure = float(2 * tp) / float(2 * tp + fp + fn)
else:
f_measure = float('nan')
return f_measure
# Normalize the channel names.
def normalize_names(names_ref, names_est):
tmp = list()
for a in names_est:
for b in names_ref:
if a.casefold() == b.casefold():
tmp.append(b)
break
return tmp
# Reorder channels in signal.
def reorder_signal(input_signal, input_channels, output_channels):
# Do not allow repeated channels with potentially different values in a signal.
assert(len(set(input_channels)) == len(input_channels))
assert(len(set(output_channels)) == len(output_channels))
if input_channels == output_channels:
output_signal = input_signal
else:
output_channels = normalize_names(input_channels, output_channels)
input_signal = np.asarray(input_signal)
num_samples = np.shape(input_signal)[0]
num_channels = len(output_channels)
data_type = input_signal.dtype
output_signal = np.zeros((num_samples, num_channels), dtype=data_type)
for i, output_channel in enumerate(output_channels):
for j, input_channel in enumerate(input_channels):
if input_channel == output_channel:
output_signal[:, i] = input_signal[:, j]
return output_signal
### Other helper functions
# Remove any single or double quotes; parentheses, braces, and brackets (for singleton arrays); and spaces and tabs from a string.
def remove_extra_characters(x):
x = str(x)
x = x.replace('"', '').replace("'", "")
x = x.replace('(', '').replace(')', '').replace('[', '').replace(']', '').replace('{', '').replace('}', '')
x = x.replace(' ', '').replace('\t', '')
x = x.strip()
return x
# 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, i.e., not a number, or represents a NaN.
def is_nan(x):
if is_number(x):
return np.isnan(float(x))
else:
return False
# Check if a variable is a boolean or represents a boolean.
def is_boolean(x):
if (is_number(x) and float(x)==0) or (remove_extra_characters(x).casefold() in ('false', 'f', 'no', 'n')):
return True
elif (is_number(x) and float(x)==1) or (remove_extra_characters(x).casefold() in ('true', 't', 'yes', 'y')):
return True
else:
return False
# Sanitize integer values.
def sanitize_integer_value(x):
x = remove_extra_characters(x)
if is_integer(x):
return int(float(x))
else:
return float('nan')
# Sanitize scalar values.
def sanitize_scalar_value(x):
x = remove_extra_characters(x)
if is_number(x):
return float(x)
else:
return float('nan')
# Sanitize boolean values.
def sanitize_boolean_value(x):
x = remove_extra_characters(x)
if (is_number(x) and float(x)==0) or (remove_extra_characters(x).casefold() in ('false', 'f', 'no', 'n')):
return 0
elif (is_number(x) and float(x)==1) or (remove_extra_characters(x).casefold() in ('true', 't', 'yes', 'y')):
return 1
else:
return float('nan')