-
Notifications
You must be signed in to change notification settings - Fork 38
/
prepare_ptbxl_data.py
172 lines (140 loc) · 6.9 KB
/
prepare_ptbxl_data.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
#!/usr/bin/env python
# Load libraries.
import argparse
import ast
import numpy as np
import os
import os.path
import pandas as pd
import shutil
import sys
from helper_code import *
# Parse arguments.
def get_parser():
description = 'Prepare the PTB-XL database for use in the Challenge.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-i', '--input_folder', type=str, required=True)
parser.add_argument('-pd', '--ptbxl_database_file', type=str, required=True) # ptbxl_database.csv
parser.add_argument('-pm', '--ptbxl_mapping_file', type=str, required=True) # scp_statements.csv
parser.add_argument('-sd', '--sl_database_file', type=str, required=True) # 12sl_statements.csv
parser.add_argument('-sm', '--sl_mapping_file', type=str, required=True) # 12slv23ToSNOMED.csv
parser.add_argument('-o', '--output_folder', type=str, required=True)
return parser
# Run script.
def run(args):
# Assign each class to a superclass; these commands were adapted from the PhysioNet project documentation.
df_ptbxl_mapping = pd.read_csv(args.ptbxl_mapping_file, index_col=0)
subclass_to_superclass = dict()
for i, row in df_ptbxl_mapping.iterrows():
if row['diagnostic'] == 1:
subclass_to_superclass[i] = row['diagnostic_class']
def assign_superclass(subclasses):
superclasses = list()
for subclass in subclasses:
if subclass in subclass_to_superclass:
superclass = subclass_to_superclass[subclass]
if superclass not in superclasses:
superclasses.append(superclass)
return superclasses
# Load the PTB-XL labels.
df_ptbxl_database = pd.read_csv(args.ptbxl_database_file, index_col='ecg_id')
df_ptbxl_database.scp_codes = df_ptbxl_database.scp_codes.apply(lambda x: ast.literal_eval(x))
# Map the PTB-XL classes to superclasses.
df_ptbxl_database['diagnostic_superclass'] = df_ptbxl_database.scp_codes.apply(assign_superclass)
# Load the 12SL labels.
df_sl_database = pd.read_csv(args.sl_database_file, index_col='ecg_id')
# Map the 12SL classes to the PTB-XL classes for the following acute myocardial infarction (MI) classes; PTB-XL does not include
# a separate acute MI class.
df_sl_mapping = pd.read_csv(args.sl_mapping_file, index_col='StatementNumber')
acute_mi_statements = set([821, 822, 823, 827, 829, 902, 903, 904, 963, 964, 965, 966, 967, 968])
acute_mi_classes = set()
for statement in acute_mi_statements:
if statement in df_sl_mapping.index:
acute_mi_classes.add(df_sl_mapping.loc[statement]['Acronym'])
# Identify the header files.
records = find_records(args.input_folder)
# Update the header files to include demographics data and labels and copy the signal files unchanged.
for record in records:
# Extract the demographics data.
record_path, record_basename = os.path.split(record)
ecg_id = int(record_basename.split('_')[0])
row = df_ptbxl_database.loc[ecg_id]
recording_date_string = row['recording_date']
date_string, time_string = recording_date_string.split(' ')
yyyy, mm, dd = date_string.split('-')
date_string = f'{dd}/{mm}/{yyyy}'
age = row['age']
age = cast_int_float_unknown(age)
sex = row['sex']
if sex == 0:
sex = 'Male'
elif sex == 1:
sex = 'Female'
else:
sex = 'Unknown'
height = row['height']
height = cast_int_float_unknown(height)
weight = row['weight']
weight = cast_int_float_unknown(weight)
scp_code_dict = row['scp_codes']
scp_codes = [scp_code for scp_code in scp_code_dict if scp_code_dict[scp_code] >= 0]
superclasses = row['diagnostic_superclass']
if ecg_id in df_sl_database.index:
sl_codes = df_sl_database.loc[ecg_id]['statements']
else:
sl_codes = list()
labels = list()
if 'NORM' in superclasses:
labels.append('NORM')
if any(c in sl_codes for c in acute_mi_classes):
labels.append('Acute MI')
if 'MI' in superclasses and not any(c in sl_codes for c in acute_mi_classes):
labels.append('Old MI')
if 'STTC' in superclasses:
labels.append('STTC')
if 'CD' in superclasses:
labels.append('CD')
if 'HYP' in superclasses:
labels.append('HYP')
if 'PAC' in scp_codes:
labels.append('PAC')
if 'PVC' in scp_codes:
labels.append('PVC')
if 'AFIB' in scp_codes or 'AFLT' in scp_codes:
labels.append('AFIB/AFL')
if 'STACH' in scp_codes or 'SVTAC' in scp_codes or 'PSVT' in scp_codes:
labels.append('TACHY')
if 'SBRAD' in scp_codes:
labels.append('BRADY')
labels = ', '.join(labels)
# Update the header file.
input_header_file = os.path.join(args.input_folder, record + '.hea')
output_header_file = os.path.join(args.output_folder, record + '.hea')
input_path = os.path.join(args.input_folder, record_path)
output_path = os.path.join(args.output_folder, record_path)
os.makedirs(output_path, exist_ok=True)
with open(input_header_file, 'r') as f:
input_header = f.read()
lines = input_header.split('\n')
record_line = ' '.join(lines[0].strip().split(' ')[:4]) + '\n'
signal_lines = '\n'.join(l.strip() for l in lines[1:] \
if l.strip() and not l.startswith('#')) + '\n'
comment_lines = '\n'.join(l.strip() for l in lines[1:] \
if l.startswith('#') and not any((l.startswith(x) for x in ('# Age:', '# Sex:', '# Height:', '# Weight:', f'{substring_labels}')))) + '\n'
record_line = record_line.strip() + f' {time_string} {date_string} ' + '\n'
signal_lines = signal_lines.strip() + '\n'
comment_lines = comment_lines.strip() + f'# Age: {age}\n# Sex: {sex}\n# Height: {height}\n# Weight: {weight}\n{substring_labels} {labels}\n'
output_header = record_line + signal_lines + comment_lines
with open(output_header_file, 'w') as f:
f.write(output_header)
# Copy the signal files if the input and output folders are different.
if os.path.normpath(args.input_folder) != os.path.normpath(args.output_folder):
relative_path = os.path.split(record)[0]
signal_files = get_signal_files(input_header_file)
for signal_file in signal_files:
input_signal_file = os.path.join(args.input_folder, relative_path, signal_file)
output_signal_file = os.path.join(args.output_folder, relative_path, signal_file)
if os.path.isfile(input_signal_file):
shutil.copy2(input_signal_file, output_signal_file)
if __name__=='__main__':
run(get_parser().parse_args(sys.argv[1:]))