-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate_12ECG_score.py
596 lines (495 loc) · 23.5 KB
/
evaluate_12ECG_score.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/usr/bin/env python
# This file contains functions for evaluating algorithms for the 2020 PhysioNet/
# Computing in Cardiology Challenge. You can run it as follows:
#
# python evaluate_12ECG_score.py labels outputs scores.csv
#
# where 'labels' is a directory containing files with the labels, 'outputs' is a
# directory containing files with the outputs from your model, and 'scores.csv'
# (optional) is a collection of scores for the algorithm outputs.
#
# Each file of labels or outputs must have the format described on the Challenge
# webpage. The scores for the algorithm outputs include the area under the
# receiver-operating characteristic curve (AUROC), the area under the recall-
# precision curve (AUPRC), accuracy (fraction of correct recordings), macro F-
# measure, and the Challenge metric, which assigns different weights to
# different misclassification errors.
#
# BSD 2-Clause License
# #
# # Copyright (c) 2020, PhysioNet
# # All rights reserved.
# #
# # Redistribution and use in source and binary forms, with or without
# # modification, are permitted provided that the following conditions are met:
# #
# # * Redistributions of source code must retain the above copyright notice, this
# # list of conditions and the following disclaimer.
# #
# # * Redistributions in binary form must reproduce the above copyright notice,
# # this list of conditions and the following disclaimer in the documentation
# # and/or other materials provided with the distribution.
# #
# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as np, os, os.path, sys
from warnings import warn
def evaluate_12ECG_score(label_directory, output_directory):
# Define the weights, the SNOMED CT code for the normal class, and equivalent SNOMED CT codes.
weights_file = 'dx/weights.csv'
normal_class = '426783006'
equivalent_classes = [['713427006', '59118001'], ['284470004', '63593006'], ['427172004', '17338001']]
# Find the label and output files.
print('Finding label and output files...')
label_files, output_files = find_challenge_files(label_directory, output_directory)
# Load the labels and outputs.
print('Loading labels and outputs...')
label_classes, labels = load_labels(label_files, normal_class, equivalent_classes)
output_classes, binary_outputs, scalar_outputs = load_outputs(output_files, normal_class, equivalent_classes)
# Organize/sort the labels and outputs.
print('Organizing labels and outputs...')
classes, labels, binary_outputs, scalar_outputs = organize_labels_outputs(label_classes, output_classes, labels, binary_outputs, scalar_outputs)
# Load the weights for the Challenge metric.
print('Loading weights...')
weights = load_weights(weights_file, classes)
# Only consider classes that are scored with the Challenge metric.
indices = np.any(weights, axis=0) # Find indices of classes in weight matrix.
classes = [x for i, x in enumerate(classes) if indices[i]]
labels = labels[:, indices]
scalar_outputs = scalar_outputs[:, indices]
binary_outputs = binary_outputs[:, indices]
weights = weights[np.ix_(indices, indices)]
# Evaluate the model by comparing the labels and outputs.
print('Evaluating model...')
print('- AUROC and AUPRC...')
auroc, auprc = compute_auc(labels, scalar_outputs)
print('- Accuracy...')
accuracy = compute_accuracy(labels, binary_outputs)
print('- F-measure...')
f_measure = compute_f_measure(labels, binary_outputs)
print('- F-beta and G-beta measures...')
f_beta_measure, g_beta_measure = compute_beta_measures(labels, binary_outputs, beta=2)
print('- Challenge metric...')
challenge_metric = compute_challenge_metric(weights, labels, binary_outputs, classes, normal_class)
print('Done.')
# Return the results.
return auroc, auprc, accuracy, f_measure, f_beta_measure, g_beta_measure, challenge_metric
# Check if the input is a number.
def is_number(x):
try:
float(x)
return True
except ValueError:
return False
# Find Challenge files.
def find_challenge_files(label_directory, output_directory):
label_files = list()
output_files = list()
for f in sorted(os.listdir(label_directory)):
F = os.path.join(label_directory, f) # Full path for label file
if os.path.isfile(F) and F.lower().endswith('.hea') and not f.lower().startswith('.'):
root, ext = os.path.splitext(f)
g = root + '.csv'
G = os.path.join(output_directory, g) # Full path for corresponding output file
if os.path.isfile(G):
label_files.append(F)
output_files.append(G)
else:
raise IOError('Output file {} not found for label file {}.'.format(g, f))
if label_files and output_files:
return label_files, output_files
else:
raise IOError('No label or output files found.')
def prepare_classes(classes, equivalent_classes_collection, labels=None, pred=None, score=None):
# For each set of equivalent class, use only one class as the representative class for the set and discard
# the other classes in the set. The label for the representative class is positive if any of the labels
# in the set is positive.
remove_classes = list()
remove_indices = list()
for equivalent_classes in equivalent_classes_collection:
equivalent_classes = [x for x in equivalent_classes if x in classes]
if len(equivalent_classes)>1:
representative_class = equivalent_classes[0]
other_classes = equivalent_classes[1:]
equivalent_indices = [classes.index(x) for x in equivalent_classes]
representative_index = equivalent_indices[0]
other_indices = equivalent_indices[1:]
if labels is not None:
labels[:, representative_index] = np.any(labels[:, equivalent_indices], axis=1)
if pred is not None:
pred[:, representative_index] = np.any(pred[:, equivalent_indices], axis=1)
if score is not None:
score[:, representative_index] = np.nanmean(score[:, equivalent_indices], axis=1)
remove_classes += other_classes
remove_indices += other_indices
for x in remove_classes:
classes.remove(x)
if labels is not None:
labels = np.delete(labels, remove_indices, axis=1)
if pred is not None:
pred = np.delete(pred, remove_indices, axis=1)
pred[np.isnan(pred)] = 0
if score is not None:
score = np.delete(score, remove_indices, axis=1)
score[np.isnan(score)] = 0
return classes, labels, pred, score
# Load labels from header/label files.
def load_labels(label_files, normal_class, equivalent_classes_collection):
# The labels should have the following form:
#
# Dx: label_1, label_2, label_3
#
num_recordings = len(label_files)
# Load diagnoses.
tmp_labels = list()
for i in range(num_recordings):
with open(label_files[i], 'r') as f:
for l in f:
if l.startswith('#Dx'):
dxs = set(arr.strip() for arr in l.split(': ')[1].split(','))
tmp_labels.append(dxs)
# Identify classes.
classes = set.union(*map(set, tmp_labels))
if normal_class not in classes:
classes.add(normal_class)
print('- The normal class {} is not one of the label classes, so it has been automatically added, but please check that you chose the correct normal class.'.format(normal_class))
classes = sorted(classes)
num_classes = len(classes)
# Use one-hot encoding for labels.
labels = np.zeros((num_recordings, num_classes), dtype=np.bool)
for i in range(num_recordings):
dxs = tmp_labels[i]
for dx in dxs:
j = classes.index(dx)
labels[i, j] = 1
classes, labels, _, __ = prepare_classes(classes, equivalent_classes_collection, labels=labels)
return classes, labels
# Load outputs from output files.
def load_outputs(output_files, normal_class, equivalent_classes_collection):
# The outputs should have the following form:
#
# diagnosis_1, diagnosis_2, diagnosis_3
# 0, 1, 1
# 0.12, 0.34, 0.56
#
num_recordings = len(output_files)
tmp_labels = list()
tmp_binary_outputs = list()
tmp_scalar_outputs = list()
for i in range(num_recordings):
with open(output_files[i], 'r') as f:
for j, l in enumerate(f):
arrs = [arr.strip() for arr in l.split(',')]
if j==1:
row = arrs
tmp_labels.append(row)
elif j==2:
row = list()
for arr in arrs:
number = 1 if arr in ('1', 'True', 'true', 'T', 't') else 0
row.append(number)
tmp_binary_outputs.append(row)
elif j==3:
row = list()
for arr in arrs:
number = float(arr) if is_number(arr) else 0
row.append(number)
tmp_scalar_outputs.append(row)
# Identify classes.
classes = set.union(*map(set, tmp_labels))
if normal_class not in classes:
classes.add(normal_class)
print('- The normal class {} is not one of the output classes, so it has been automatically added, but please check that you identified the correct normal class.'.format(normal_class))
classes = sorted(classes)
num_classes = len(classes)
# Use one-hot encoding for binary outputs and the same order for scalar outputs.
binary_outputs = np.zeros((num_recordings, num_classes), dtype=np.bool)
scalar_outputs = np.zeros((num_recordings, num_classes), dtype=np.float64)
for i in range(num_recordings):
dxs = tmp_labels[i]
for k, dx in enumerate(dxs):
j = classes.index(dx)
binary_outputs[i, j] = tmp_binary_outputs[i][k]
scalar_outputs[i, j] = tmp_scalar_outputs[i][k]
classes, _, binary_outputs, scalar_outputs = prepare_classes(classes, equivalent_classes_collection,
pred=binary_outputs, score=scalar_outputs)
return classes, binary_outputs, scalar_outputs
# Organize labels and outputs.
def organize_labels_outputs(label_classes, output_classes, tmp_labels, tmp_binary_outputs, tmp_scalar_outputs):
# Include all classes from either the labels or the outputs.
classes = sorted(set(label_classes) | set(output_classes))
num_classes = len(classes)
# Check that the labels and outputs have the same numbers of recordings.
assert(len(tmp_labels)==len(tmp_binary_outputs)==len(tmp_scalar_outputs))
num_recordings = len(tmp_labels)
# Rearrange the columns of the labels and the outputs to be consistent with the order of the classes.
labels = np.zeros((num_recordings, num_classes), dtype=np.bool)
for k, dx in enumerate(label_classes):
j = classes.index(dx)
labels[:, j] = tmp_labels[:, k]
binary_outputs = np.zeros((num_recordings, num_classes), dtype=np.bool)
scalar_outputs = np.zeros((num_recordings, num_classes), dtype=np.float64)
for k, dx in enumerate(output_classes):
j = classes.index(dx)
binary_outputs[:, j] = tmp_binary_outputs[:, k]
scalar_outputs[:, j] = tmp_scalar_outputs[:, k]
return classes, labels, binary_outputs, scalar_outputs
# Load a table with row and column names.
def load_table(table_file):
# The table should have the following form:
#
# , a, b, c
# a, 1.2, 2.3, 3.4
# b, 4.5, 5.6, 6.7
# c, 7.8, 8.9, 9.0
#
table = list()
with open(table_file, 'r') as f:
for i, l in enumerate(f):
arrs = [arr.strip() for arr in l.split(',')]
table.append(arrs)
# Define the numbers of rows and columns and check for errors.
num_rows = len(table)-1
if num_rows<1:
raise Exception('The table {} is empty.'.format(table_file))
num_cols = set(len(table[i])-1 for i in range(num_rows))
if len(num_cols)!=1:
raise Exception('The table {} has rows with different lengths.'.format(table_file))
num_cols = min(num_cols)
if num_cols<1:
raise Exception('The table {} is empty.'.format(table_file))
# Find the row and column labels.
rows = [table[0][j+1] for j in range(num_rows)]
cols = [table[i+1][0] for i in range(num_cols)]
# Find the entries of the table.
values = np.zeros((num_rows, num_cols))
for i in range(num_rows):
for j in range(num_cols):
value = table[i+1][j+1]
if is_number(value):
values[i, j] = float(value)
else:
values[i, j] = float('nan')
return rows, cols, values
# Load weights.
def load_weights(weight_file, classes):
# Load the weight matrix.
rows, cols, values = load_table(weight_file)
assert(rows == cols)
num_rows = len(rows)
# Assign the entries of the weight matrix with rows and columns corresponding to the classes.
num_classes = len(classes)
weights = np.zeros((num_classes, num_classes), dtype=np.float64)
for i, a in enumerate(rows):
if a in classes:
k = classes.index(a)
for j, b in enumerate(rows):
if b in classes:
l = classes.index(b)
weights[k, l] = values[i, j]
return weights
# Compute recording-wise accuracy.
def compute_accuracy(labels, outputs):
num_recordings, num_classes = np.shape(labels)
num_correct_recordings = 0
for i in range(num_recordings):
if np.all(labels[i, :]==outputs[i, :]):
num_correct_recordings += 1
return float(num_correct_recordings) / float(num_recordings)
# Compute confusion matrices.
def compute_confusion_matrices(labels, outputs, normalize=False):
# Compute a binary confusion matrix for each class k:
#
# [TN_k FN_k]
# [FP_k TP_k]
#
# If the normalize variable is set to true, then normalize the contributions
# to the confusion matrix by the number of labels per recording.
num_recordings, num_classes = np.shape(labels)
if not normalize:
A = np.zeros((num_classes, 2, 2))
for i in range(num_recordings):
for j in range(num_classes):
if labels[i, j]==1 and outputs[i, j]==1: # TP
A[j, 1, 1] += 1
elif labels[i, j]==0 and outputs[i, j]==1: # FP
A[j, 1, 0] += 1
elif labels[i, j]==1 and outputs[i, j]==0: # FN
A[j, 0, 1] += 1
elif labels[i, j]==0 and outputs[i, j]==0: # TN
A[j, 0, 0] += 1
else: # This condition should not happen.
raise ValueError('Error in computing the confusion matrix.')
else:
A = np.zeros((num_classes, 2, 2))
for i in range(num_recordings):
normalization = float(max(np.sum(labels[i, :]), 1))
for j in range(num_classes):
if labels[i, j]==1 and outputs[i, j]==1: # TP
A[j, 1, 1] += 1.0/normalization if normalization != 0 else float('nan')
elif labels[i, j]==0 and outputs[i, j]==1: # FP
A[j, 1, 0] += 1.0/normalization if normalization != 0 else float('nan')
elif labels[i, j]==1 and outputs[i, j]==0: # FN
A[j, 0, 1] += 1.0/normalization if normalization != 0 else float('nan')
elif labels[i, j]==0 and outputs[i, j]==0: # TN
A[j, 0, 0] += 1.0/normalization if normalization != 0 else float('nan')
else: # This condition should not happen.
raise ValueError('Error in computing the confusion matrix.')
return A
# Compute macro F-measure.
def compute_f_measure(labels, outputs):
num_recordings, num_classes = np.shape(labels)
A = compute_confusion_matrices(labels, outputs)
f_measure = np.zeros(num_classes)
for k in range(num_classes):
tp, fp, fn, tn = A[k, 1, 1], A[k, 1, 0], A[k, 0, 1], A[k, 0, 0]
if 2 * tp + fp + fn:
f_measure[k] = float(2 * tp) / float(2 * tp + fp + fn)
else:
f_measure[k] = float('nan')
macro_f_measure = np.nanmean(f_measure)
return macro_f_measure
# Compute F-beta and G-beta measures from the unofficial phase of the Challenge.
def compute_beta_measures(labels, outputs, beta):
num_recordings, num_classes = np.shape(labels)
A = compute_confusion_matrices(labels, outputs, normalize=True)
f_beta_measure = np.zeros(num_classes)
g_beta_measure = np.zeros(num_classes)
for k in range(num_classes):
tp, fp, fn, tn = A[k, 1, 1], A[k, 1, 0], A[k, 0, 1], A[k, 0, 0]
if (1+beta**2)*tp + fp + beta**2*fn:
f_beta_measure[k] = float((1+beta**2)*tp) / float((1+beta**2)*tp + fp + beta**2*fn)
else:
f_beta_measure[k] = float('nan')
if tp + fp + beta*fn:
g_beta_measure[k] = float(tp) / float(tp + fp + beta*fn)
else:
g_beta_measure[k] = float('nan')
macro_f_beta_measure = np.nanmean(f_beta_measure)
macro_g_beta_measure = np.nanmean(g_beta_measure)
return macro_f_beta_measure, macro_g_beta_measure
# Compute macro AUROC and macro AUPRC.
def compute_auc(labels, outputs):
num_recordings, num_classes = np.shape(labels)
# Compute and summarize the confusion matrices for each class across at distinct output values.
auroc = np.zeros(num_classes)
auprc = np.zeros(num_classes)
for k in range(num_classes):
# We only need to compute TPs, FPs, FNs, and TNs at distinct output values.
thresholds = np.unique(outputs[:, k])
thresholds = np.append(thresholds, thresholds[-1]+1)
thresholds = thresholds[::-1]
num_thresholds = len(thresholds)
# Initialize the TPs, FPs, FNs, and TNs.
tp = np.zeros(num_thresholds)
fp = np.zeros(num_thresholds)
fn = np.zeros(num_thresholds)
tn = np.zeros(num_thresholds)
fn[0] = np.sum(labels[:, k]==1)
tn[0] = np.sum(labels[:, k]==0)
# Find the indices that result in sorted output values.
idx = np.argsort(outputs[:, k])[::-1]
# Compute the TPs, FPs, FNs, and TNs for class k across thresholds.
i = 0
for j in range(1, num_thresholds):
# Initialize TPs, FPs, FNs, and TNs using values at previous threshold.
tp[j] = tp[j-1]
fp[j] = fp[j-1]
fn[j] = fn[j-1]
tn[j] = tn[j-1]
# Update the TPs, FPs, FNs, and TNs at i-th output value.
while i < num_recordings and outputs[idx[i], k] >= thresholds[j]:
if labels[idx[i], k]:
tp[j] += 1
fn[j] -= 1
else:
fp[j] += 1
tn[j] -= 1
i += 1
# Summarize the TPs, FPs, FNs, and TNs for class k.
tpr = np.zeros(num_thresholds)
tnr = np.zeros(num_thresholds)
ppv = np.zeros(num_thresholds)
npv = np.zeros(num_thresholds)
for j in range(num_thresholds):
if tp[j] + fn[j]:
tpr[j] = float(tp[j]) / float(tp[j] + fn[j])
else:
tpr[j] = float('nan')
if fp[j] + tn[j]:
tnr[j] = float(tn[j]) / float(fp[j] + tn[j])
else:
tnr[j] = float('nan')
if tp[j] + fp[j]:
ppv[j] = float(tp[j]) / float(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) for class k.
for j in range(num_thresholds-1):
auroc[k] += 0.5 * (tpr[j+1] - tpr[j]) * (tnr[j+1] + tnr[j])
auprc[k] += (tpr[j+1] - tpr[j]) * ppv[j+1]
# Compute macro AUROC and macro AUPRC across classes.
macro_auroc = np.nanmean(auroc)
macro_auprc = np.nanmean(auprc)
return macro_auroc, macro_auprc
# Compute modified confusion matrix for multi-class, multi-label tasks.
def compute_modified_confusion_matrix(labels, outputs):
# Compute a binary multi-class, multi-label confusion matrix, where the rows
# are the labels and the columns are the outputs.
num_recordings, num_classes = np.shape(labels)
A = np.zeros((num_classes, num_classes))
# Iterate over all of the recordings.
for i in range(num_recordings):
# Calculate the number of positive labels and/or outputs.
normalization = float(max(np.sum(np.any((labels[i, :], outputs[i, :]), axis=0)), 1))
# Iterate over all of the classes.
for j in range(num_classes):
# Assign full and/or partial credit for each positive class.
if labels[i, j]:
for k in range(num_classes):
if outputs[i, k]:
A[j, k] += 1.0/normalization
return A
# Compute the evaluation metric for the Challenge.
def compute_challenge_metric(weights, labels, outputs, classes, normal_class):
num_recordings, num_classes = np.shape(labels)
normal_index = classes.index(normal_class)
# Compute the observed score.
A = compute_modified_confusion_matrix(labels, outputs)
observed_score = np.nansum(weights * A)
# Compute the score for the model that always chooses the correct label(s).
correct_outputs = labels
A = compute_modified_confusion_matrix(labels, correct_outputs)
correct_score = np.nansum(weights * A)
if normal_index is None:
warn("No normalization when computing challenge_metric!!")
return observed_score
# Compute the score for the model that always chooses the normal class.
inactive_outputs = np.zeros((num_recordings, num_classes), dtype=np.bool)
inactive_outputs[:, normal_index] = 1
A = compute_modified_confusion_matrix(labels, inactive_outputs)
inactive_score = np.nansum(weights * A)
if correct_score != inactive_score:
normalized_score = float(observed_score - inactive_score) / float(correct_score - inactive_score)
else:
normalized_score = float('nan')
return normalized_score
if __name__ == '__main__':
auroc, auprc, accuracy, f_measure, f_beta_measure, g_beta_measure, challenge_metric = evaluate_12ECG_score(sys.argv[1], sys.argv[2])
output_string = 'AUROC,AUPRC,Accuracy,F-measure,Fbeta-measure,Gbeta-measure,Challenge metric\n{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f},{:.3f}'.format(auroc, auprc, accuracy, f_measure, f_beta_measure, g_beta_measure, challenge_metric)
if len(sys.argv) > 3:
with open(sys.argv[3], 'w') as f:
f.write(output_string)
else:
print(output_string)