forked from physionetchallenges/python-example-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_code.py
557 lines (456 loc) · 17.4 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/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 numpy as np
import os
import sys
### Challenge data I/O functions
# Find the records in a folder and its subfolders.
def find_records(folder):
records = set()
for root, directories, files in os.walk(folder):
for file in files:
extension = os.path.splitext(file)[1]
if extension == '.hea':
record = os.path.relpath(os.path.join(root, file), folder)[:-4]
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 signal(s) for a record.
def load_signal(record):
import wfdb
signal_files = get_signal_files(record)
if signal_files:
signal, fields = wfdb.rdsamp(record)
else:
signal, fields = None, None
return signal, fields
def load_signals(record):
return load_signal(record)
# Load the image(s) for a record.
def load_image(record):
from PIL import Image
path = os.path.split(record)[0]
image_files = get_image_files(record)
images = list()
for image_file in image_files:
image_file_path = os.path.join(path, image_file)
if os.path.isfile(image_file_path):
image = Image.open(image_file_path)
images.append(image)
return images
def load_images(record):
return load_image(record)
# Load the dx class(es) for a record.
def load_dx(record):
header = load_header(record)
dx = get_dxs_from_header(header)
return dx
def load_dxs(record):
return load_dx(record)
# Save the header for a record.
def save_header(record, header):
header_file = get_header_file(record)
save_text(header_file, header)
# Save the signal(s) for a record.
def save_signal(record, signal, comments=list()):
header = load_header(record)
path, record = os.path.split(record)
sampling_frequency = get_sampling_frequency(header)
signal_formats = get_signal_formats(header)
adc_gains = get_adc_gains(header)
baselines = get_baselines(header)
signal_units = get_signal_units(header)
signal_names = get_signal_names(header)
if all(signal_format == '16' for signal_format in signal_formats):
signal = np.clip(signal, -2**15 + 1, 2**15 - 1)
signal = np.asarray(signal, dtype=np.int16)
else:
signal_format_string = ', '.join(sorted(set(signal_formats)))
raise NotImplementedError(f'{signal_format_string} not implemented')
import wfdb
wfdb.wrsamp(record, fs=sampling_frequency, units=signal_units, sig_name=signal_names, \
d_signal=signal, fmt=signal_formats, adc_gain=adc_gains, baseline=baselines, comments=comments, \
write_dir=path)
def save_signals(record, signals):
save_signal(record, signals)
# Save the dx class(es) for a record.
def save_dx(record, dx):
header_file = get_header_file(record)
header = load_text(header_file)
header += '#Dx: ' + ', '.join(dx) + '\n'
save_text(header_file, header)
return header
def save_dxs(record, dxs):
return save_dx(record, dxs)
### 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 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 signal units from a header file.
def get_signal_formats(string):
num_signals = get_num_signals(string)
values = list()
for i, l in enumerate(string.split('\n')):
if 1 <= i <= num_signals:
field = l.split(' ')[1]
if 'x' in field:
field = field.split('x')[0]
if ':' in field:
field = field.split(':')[0]
if '+' in field:
field = field.split('+')[0]
value = field
values.append(value)
return values
# Get signal units from a header file.
def get_adc_gains(string):
num_signals = get_num_signals(string)
values = list()
for i, l in enumerate(string.split('\n')):
if 1 <= i <= num_signals:
field = l.split(' ')[2]
if '/' in field:
field = field.split('/')[0]
if '(' in field and ')' in field:
field = field.split('(')[0]
value = float(field)
values.append(value)
return values
# Get signal units from a header file.
def get_baselines(string):
num_signals = get_num_signals(string)
values = list()
for i, l in enumerate(string.split('\n')):
if 1 <= i <= num_signals:
field = l.split(' ')[2]
if '/' in field:
field = field.split('/')[0]
if '(' in field and ')' in field:
field = field.split('(')[1].split(')')[0]
value = int(field)
values.append(value)
return values
# Get signal units from a header file.
def get_signal_units(string):
num_signals = get_num_signals(string)
values = list()
for i, l in enumerate(string.split('\n')):
if 1 <= i <= num_signals:
field = l.split(' ')[2]
if '/' in field:
value = field.split('/')[1]
else:
value = 'mV'
values.append(value)
return values
# Get the number of samples 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
# 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 variables from a text file.
def get_variables(string, variable_name, sep=','):
variables = list()
has_variable = False
for l in string.split('\n'):
if l.startswith(variable_name):
variables += [variable.strip() for variable in l[len(variable_name):].strip().split(sep)]
has_variable = True
return variables, has_variable
# Get the signal file(s) from a header or a similar string.
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 image file(s) from a header or a similar string.
def get_image_files_from_header(string):
images, has_image = get_variables(string, '#Image:')
if not has_image:
raise Exception('No images available: did you forget to generate or include the images?')
return images
# Get the dx class(es) from a header or a similar string.
def get_dxs_from_header(string):
dxs, has_dx = get_variables(string, '#Dx:')
if not has_dx:
raise Exception('No dx classes available: are you trying to load the classes from the held-out dataset, or did you forget to prepare the data to include the classes?')
return dxs
# 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 file(s) for a record.
def get_signal_files(record):
header_file = get_header_file(record)
header = load_text(header_file)
signal_files = get_signal_files_from_header(header)
return signal_files
# Get the image file(s) for a record.
def get_image_files(record):
header_file = get_header_file(record)
header = load_text(header_file)
image_files = get_image_files_from_header(header)
return image_files
### Evaluation functions
# Construct the binary one-vs-rest confusion matrices, where the columns are the expert labels and the rows are the classifier
# for the given classes.
def compute_one_vs_rest_confusion_matrix(labels, outputs, classes):
assert np.shape(labels) == np.shape(outputs)
num_instances = len(labels)
num_classes = len(classes)
A = np.zeros((num_classes, 2, 2))
for i in range(num_instances):
for j in range(num_classes):
if labels[i, j] == 1 and outputs[i, j] == 1: # TP
A[j, 0, 0] += 1
elif labels[i, j] == 0 and outputs[i, j] == 1: # FP
A[j, 0, 1] += 1
elif labels[i, j] == 1 and outputs[i, j] == 0: # FN
A[j, 1, 0] += 1
elif labels[i, j] == 0 and outputs[i, j] == 0: # TN
A[j, 1, 1] += 1
return A
# Compute macro F-measure.
def compute_f_measure(labels, outputs):
# Compute confusion matrix.
classes = sorted(set.union(*map(set, labels)))
labels = compute_one_hot_encoding(labels, classes)
outputs = compute_one_hot_encoding(outputs, classes)
A = compute_one_vs_rest_confusion_matrix(labels, outputs, classes)
num_classes = len(classes)
per_class_f_measure = np.zeros(num_classes)
for k in range(num_classes):
tp, fp, fn, tn = A[k, 0, 0], A[k, 0, 1], A[k, 1, 0], A[k, 1, 1]
if 2 * tp + fp + fn > 0:
per_class_f_measure[k] = float(2 * tp) / float(2 * tp + fp + fn)
else:
per_class_f_measure[k] = float('nan')
if np.any(np.isfinite(per_class_f_measure)):
macro_f_measure = np.nanmean(per_class_f_measure)
else:
macro_f_measure = float('nan')
return macro_f_measure, per_class_f_measure, classes
# Reorder channels in signal.
def reorder_signal(input_signal, input_channels, output_channels):
if input_signal is None:
return None
if input_channels == output_channels and len(set(input_channels)) == len(set(output_channels)) == len(output_channels):
output_signal = input_signal
else:
input_channels = [channel.strip().casefold() for channel in input_channels]
output_channels = [channel.strip().casefold() for channel in output_channels]
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
# Pad or truncate signal.
def trim_signal(input_signal, num_samples):
if input_signal is None:
return None
cur_samples, num_channels = np.shape(input_signal)
data_type = input_signal.dtype
if cur_samples == num_samples:
output_signal = input_signal
else:
output_signal = np.zeros((num_samples, num_channels), dtype=data_type)
if cur_samples < num_samples: # Zero-pad the signals.
output_signal[:cur_samples, :] = input_signal
else: # Truncate the signals.
output_signal = input_signal[:num_samples, :]
return output_signal
# Compute SNR.
def compute_snr(label_signal, output_signal):
label_signal = np.asarray(label_signal)
output_signal = np.asarray(output_signal)
assert(np.all(np.shape(label_signal) == np.shape(output_signal)))
label_signal[np.isnan(label_signal)] = 0
output_signal[np.isnan(output_signal)] = 0
noise_signal = output_signal - label_signal
x = np.sum(label_signal**2)
y = np.sum(noise_signal**2)
if x > 0 and y > 0:
snr = 10 * np.log10(x / y)
elif x > 0 and y == 0:
snr = float('inf')
else:
snr = float('nan')
return snr
# Compute the mean signal power to median noise power metric.
def compute_snr_median(label_signal, output_signal):
label_signal = np.asarray(label_signal)
output_signal = np.asarray(output_signal)
assert(np.all(np.shape(label_signal) == np.shape(output_signal)))
label_signal[np.isnan(label_signal)] = 0
output_signal[np.isnan(output_signal)] = 0
noise_signal = output_signal - label_signal
x = np.mean(label_signal**2)
y = np.median(noise_signal**2)
if y > 0:
snr = 10 * np.log10(x / y)
else:
snr = float('inf')
return snr
# Compute a metric inspired by the Kolmogorov-Smirnov test statistic.
def compute_ks_metric(label_signal, output_signal):
label_signal = np.asarray(label_signal)
output_signal = np.asarray(output_signal)
assert(np.all(np.shape(label_signal) == np.shape(output_signal)))
label_signal[np.isnan(label_signal)] = 0
output_signal[np.isnan(output_signal)] = 0
label_signal_cdf = np.cumsum(np.abs(label_signal))
output_signal_cdf = np.cumsum(np.abs(output_signal))
if label_signal_cdf[-1] > 0:
label_signal_cdf = label_signal_cdf / label_signal_cdf[-1]
if output_signal_cdf[-1] > 0:
output_signal_cdf = output_signal_cdf / output_signal_cdf[-1]
goodness_of_fit = 1.0 - np.max(np.abs(label_signal_cdf - output_signal_cdf))
return goodness_of_fit
# Compute the adaptive signed correlation index (ASCI) metric.
def compute_asci_metric(label_signal, output_signal, beta=0.05):
label_signal = np.asarray(label_signal)
output_signal = np.asarray(output_signal)
assert(np.all(np.shape(label_signal) == np.shape(output_signal)))
label_signal[np.isnan(label_signal)] = 0
output_signal[np.isnan(output_signal)] = 0
if beta <= 0 or beta > 1:
raise ValueError('The beta value should be greater than 0 and less than or equal to 1.')
threshold = beta * np.std(label_signal)
noise_signal = np.abs(label_signal - output_signal)
discrete_noise = np.zeros_like(noise_signal)
discrete_noise[noise_signal <= threshold] = 1
discrete_noise[noise_signal > threshold] = -1
asci = np.mean(discrete_noise)
return asci
# Compute a weighted absolute difference metric.
def compute_weighted_absolute_difference(label_signal, output_signal, fs):
label_signal = np.asarray(label_signal)
output_signal = np.asarray(output_signal)
assert(label_signal.ndim == 1 and np.size(label_signal) == np.size(output_signal))
from scipy.signal import filtfilt
label_signal[np.isnan(label_signal)] = 0
output_signal[np.isnan(output_signal)] = 0
m = np.size(label_signal)
w = filtfilt(np.ones(m), m, label_signal, method='gust')
w = 1 - 0.5/np.max(w) * w
n = np.sum(w)
weighted_absolute_difference_metric = np.sum(np.abs(label_signal-output_signal) * w)/n
return weighted_absolute_difference_metric
### 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
# Cast a value to an integer if an integer, a float if a non-integer float, and an unknown value otherwise.
def cast_int_float_unknown(x):
if is_integer(x):
x = int(x)
elif is_finite_number(x):
x = float(x)
elif is_number(x):
x = 'Unknown'
else:
raise NotImplementedError(f'Unable to cast {x}.')
return x
# Construct the one-hot encoding of data for the given classes.
def compute_one_hot_encoding(data, classes):
num_instances = len(data)
num_classes = len(classes)
one_hot_encoding = np.zeros((num_instances, num_classes), dtype=np.bool_)
unencoded_data = list()
for i, x in enumerate(data):
for y in x:
for j, z in enumerate(classes):
if (y == z) or (is_nan(y) and is_nan(z)):
one_hot_encoding[i, j] = 1
return one_hot_encoding