forked from physionetchallenges/python-classifier-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam_code.py
267 lines (208 loc) · 10 KB
/
team_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
#!/usr/bin/env python
# Edit this script to add your team's training code.
# Some functions are *required*, but you can edit most parts of required functions, remove non-required functions, and add your own function.
from helper_code import *
import numpy as np, os, sys, joblib
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
twelve_lead_model_filename = '12_lead_model.sav'
six_lead_model_filename = '6_lead_model.sav'
three_lead_model_filename = '3_lead_model.sav'
two_lead_model_filename = '2_lead_model.sav'
################################################################################
#
# Training function
#
################################################################################
# Train your model. This function is *required*. Do *not* change the arguments of this function.
def training_code(data_directory, model_directory):
# Find header and recording files.
print('Finding header and recording files...')
header_files, recording_files = find_challenge_files(data_directory)
num_recordings = len(recording_files)
if not num_recordings:
raise Exception('No data was provided.')
# Create a folder for the model if it does not already exist.
if not os.path.isdir(model_directory):
os.mkdir(model_directory)
# Extract classes from dataset.
print('Extracting classes...')
classes = set()
for header_file in header_files:
header = load_header(header_file)
classes |= set(get_labels(header))
if all(is_integer(x) for x in classes):
classes = sorted(classes, key=lambda x: int(x)) # Sort classes numerically if numbers.
else:
classes = sorted(classes) # Sort classes alphanumerically otherwise.
num_classes = len(classes)
# Extract features and labels from dataset.
print('Extracting features and labels...')
data = np.zeros((num_recordings, 14), dtype=np.float32) # 14 features: one feature for each lead, one feature for age, and one feature for sex
labels = np.zeros((num_recordings, num_classes), dtype=np.bool) # One-hot encoding of classes
for i in range(num_recordings):
print(' {}/{}...'.format(i+1, num_recordings))
# Load header and recording.
header = load_header(header_files[i])
recording = load_recording(recording_files[i])
# Get age, sex and root mean square of the leads.
age, sex, rms = get_features(header, recording, twelve_leads)
data[i, 0:12] = rms
data[i, 12] = age
data[i, 13] = sex
current_labels = get_labels(header)
for label in current_labels:
if label in classes:
j = classes.index(label)
labels[i, j] = 1
# Train models.
# Define parameters for random forest classifier.
n_estimators = 3 # Number of trees in the forest.
max_leaf_nodes = 100 # Maximum number of leaf nodes in each tree.
random_state = 0 # Random state; set for reproducibility.
# Train 12-lead ECG model.
print('Training 12-lead ECG model...')
leads = twelve_leads
filename = os.path.join(model_directory, twelve_lead_model_filename)
feature_indices = [twelve_leads.index(lead) for lead in leads] + [12, 13]
features = data[:, feature_indices]
imputer = SimpleImputer().fit(features)
features = imputer.transform(features)
classifier = RandomForestClassifier(n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, labels)
save_model(filename, classes, leads, imputer, classifier)
# Train 6-lead ECG model.
print('Training 6-lead ECG model...')
leads = six_leads
filename = os.path.join(model_directory, six_lead_model_filename)
feature_indices = [twelve_leads.index(lead) for lead in leads] + [12, 13]
features = data[:, feature_indices]
imputer = SimpleImputer().fit(features)
features = imputer.transform(features)
classifier = RandomForestClassifier(n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, labels)
save_model(filename, classes, leads, imputer, classifier)
# Train 3-lead ECG model.
print('Training 3-lead ECG model...')
leads = three_leads
filename = os.path.join(model_directory, three_lead_model_filename)
feature_indices = [twelve_leads.index(lead) for lead in leads] + [12, 13]
features = data[:, feature_indices]
imputer = SimpleImputer().fit(features)
features = imputer.transform(features)
classifier = RandomForestClassifier(n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, labels)
save_model(filename, classes, leads, imputer, classifier)
# Train 2-lead ECG model.
print('Training 2-lead ECG model...')
leads = two_leads
filename = os.path.join(model_directory, two_lead_model_filename)
feature_indices = [twelve_leads.index(lead) for lead in leads] + [12, 13]
features = data[:, feature_indices]
imputer = SimpleImputer().fit(features)
features = imputer.transform(features)
classifier = RandomForestClassifier(n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, labels)
save_model(filename, classes, leads, imputer, classifier)
################################################################################
#
# File I/O functions
#
################################################################################
# Save your trained models.
def save_model(filename, classes, leads, imputer, classifier):
# Construct a data structure for the model and save it.
d = {'classes': classes, 'leads': leads, 'imputer': imputer, 'classifier': classifier}
joblib.dump(d, filename, protocol=0)
# Load your trained 12-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def load_twelve_lead_model(model_directory):
filename = os.path.join(model_directory, twelve_lead_model_filename)
return load_model(filename)
# Load your trained 6-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def load_six_lead_model(model_directory):
filename = os.path.join(model_directory, six_lead_model_filename)
return load_model(filename)
# Load your trained 3-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def load_three_lead_model(model_directory):
filename = os.path.join(model_directory, three_lead_model_filename)
return load_model(filename)
# Load your trained 2-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def load_two_lead_model(model_directory):
filename = os.path.join(model_directory, two_lead_model_filename)
return load_model(filename)
# Generic function for loading a model.
def load_model(filename):
return joblib.load(filename)
################################################################################
#
# Running trained model functions
#
################################################################################
# Run your trained 12-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def run_twelve_lead_model(model, header, recording):
return run_model(model, header, recording)
# Run your trained 6-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def run_six_lead_model(model, header, recording):
return run_model(model, header, recording)
# Run your trained 3-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def run_three_lead_model(model, header, recording):
return run_model(model, header, recording)
# Run your trained 2-lead ECG model. This function is *required*. Do *not* change the arguments of this function.
def run_two_lead_model(model, header, recording):
return run_model(model, header, recording)
# Generic function for running a trained model.
def run_model(model, header, recording):
classes = model['classes']
leads = model['leads']
imputer = model['imputer']
classifier = model['classifier']
# Load features.
num_leads = len(leads)
data = np.zeros(num_leads+2, dtype=np.float32)
age, sex, rms = get_features(header, recording, leads)
data[0:num_leads] = rms
data[num_leads] = age
data[num_leads+1] = sex
# Impute missing data.
features = data.reshape(1, -1)
features = imputer.transform(features)
# Predict labels and probabilities.
labels = classifier.predict(features)
labels = np.asarray(labels, dtype=np.int)[0]
probabilities = classifier.predict_proba(features)
probabilities = np.asarray(probabilities, dtype=np.float32)[:, 0, 1]
return classes, labels, probabilities
################################################################################
#
# Other functions
#
################################################################################
# Extract features from the header and recording.
def get_features(header, recording, leads):
# Extract age.
age = get_age(header)
if age is None:
age = float('nan')
# Extract sex. Encode as 0 for female, 1 for male, and NaN for other.
sex = get_sex(header)
if sex in ('Female', 'female', 'F', 'f'):
sex = 0
elif sex in ('Male', 'male', 'M', 'm'):
sex = 1
else:
sex = float('nan')
# Reorder/reselect leads in recordings.
available_leads = get_leads(header)
indices = list()
for lead in leads:
i = available_leads.index(lead)
indices.append(i)
recording = recording[indices, :]
# Pre-process recordings.
adc_gains = get_adcgains(header, leads)
baselines = get_baselines(header, leads)
num_leads = len(leads)
for i in range(num_leads):
recording[i, :] = (recording[i, :] - baselines[i]) / adc_gains[i]
# Compute the root mean square of each ECG lead signal.
rms = np.zeros(num_leads, dtype=np.float32)
for i in range(num_leads):
x = recording[i, :]
rms[i] = np.sqrt(np.sum(x**2) / np.size(x))
return age, sex, rms