-
Notifications
You must be signed in to change notification settings - Fork 25
/
team_code.py
213 lines (171 loc) · 8.51 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
#!/usr/bin/env python
# Edit this script to add your team's code. Some functions are *required*, but you can edit most parts of the required functions,
# change or remove non-required functions, and add your own functions.
################################################################################
#
# Import libraries and functions. You can change or remove them.
#
################################################################################
from helper_code import *
import numpy as np, scipy as sp, scipy.stats, os, sys, joblib
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
################################################################################
#
# Required functions. Edit these functions to add your code, but do not change the arguments.
#
################################################################################
# Train your model.
def train_challenge_model(data_folder, model_folder, verbose):
# Find data files.
if verbose >= 1:
print('Finding data files...')
# Find the patient data files.
patient_files = find_patient_files(data_folder)
num_patient_files = len(patient_files)
if num_patient_files==0:
raise Exception('No data was provided.')
# Create a folder for the model if it does not already exist.
os.makedirs(model_folder, exist_ok=True)
# Extract the features and labels.
if verbose >= 1:
print('Extracting features and labels from the Challenge data...')
murmur_classes = ['Present', 'Unknown', 'Absent']
num_murmur_classes = len(murmur_classes)
outcome_classes = ['Abnormal', 'Normal']
num_outcome_classes = len(outcome_classes)
features = list()
murmurs = list()
outcomes = list()
for i in range(num_patient_files):
if verbose >= 2:
print(' {}/{}...'.format(i+1, num_patient_files))
# Load the current patient data and recordings.
current_patient_data = load_patient_data(patient_files[i])
current_recordings = load_recordings(data_folder, current_patient_data)
# Extract features.
current_features = get_features(current_patient_data, current_recordings)
features.append(current_features)
# Extract labels and use one-hot encoding.
current_murmur = np.zeros(num_murmur_classes, dtype=int)
murmur = get_murmur(current_patient_data)
if murmur in murmur_classes:
j = murmur_classes.index(murmur)
current_murmur[j] = 1
murmurs.append(current_murmur)
current_outcome = np.zeros(num_outcome_classes, dtype=int)
outcome = get_outcome(current_patient_data)
if outcome in outcome_classes:
j = outcome_classes.index(outcome)
current_outcome[j] = 1
outcomes.append(current_outcome)
features = np.vstack(features)
murmurs = np.vstack(murmurs)
outcomes = np.vstack(outcomes)
# Train the model.
if verbose >= 1:
print('Training model...')
# Define parameters for random forest classifier.
n_estimators = 123 # Number of trees in the forest.
max_leaf_nodes = 45 # Maximum number of leaf nodes in each tree.
random_state = 6789 # Random state; set for reproducibility.
imputer = SimpleImputer().fit(features)
features = imputer.transform(features)
murmur_classifier = RandomForestClassifier(n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, murmurs)
outcome_classifier = RandomForestClassifier(n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, outcomes)
# Save the model.
save_challenge_model(model_folder, imputer, murmur_classes, murmur_classifier, outcome_classes, outcome_classifier)
if verbose >= 1:
print('Done.')
# Load your trained model. This function is *required*. You should edit this function to add your code, but do *not* change the
# arguments of this function.
def load_challenge_model(model_folder, verbose):
filename = os.path.join(model_folder, 'model.sav')
return joblib.load(filename)
# Run your trained model. This function is *required*. You should edit this function to add your code, but do *not* change the
# arguments of this function.
def run_challenge_model(model, data, recordings, verbose):
imputer = model['imputer']
murmur_classes = model['murmur_classes']
murmur_classifier = model['murmur_classifier']
outcome_classes = model['outcome_classes']
outcome_classifier = model['outcome_classifier']
# Load features.
features = get_features(data, recordings)
# Impute missing data.
features = features.reshape(1, -1)
features = imputer.transform(features)
# Get classifier probabilities.
murmur_probabilities = murmur_classifier.predict_proba(features)
murmur_probabilities = np.asarray(murmur_probabilities, dtype=np.float32)[:, 0, 1]
outcome_probabilities = outcome_classifier.predict_proba(features)
outcome_probabilities = np.asarray(outcome_probabilities, dtype=np.float32)[:, 0, 1]
# Choose label with highest probability.
murmur_labels = np.zeros(len(murmur_classes), dtype=np.int_)
idx = np.argmax(murmur_probabilities)
murmur_labels[idx] = 1
outcome_labels = np.zeros(len(outcome_classes), dtype=np.int_)
idx = np.argmax(outcome_probabilities)
outcome_labels[idx] = 1
# Concatenate classes, labels, and probabilities.
classes = murmur_classes + outcome_classes
labels = np.concatenate((murmur_labels, outcome_labels))
probabilities = np.concatenate((murmur_probabilities, outcome_probabilities))
return classes, labels, probabilities
################################################################################
#
# Optional functions. You can change or remove these functions and/or add new functions.
#
################################################################################
# Save your trained model.
def save_challenge_model(model_folder, imputer, murmur_classes, murmur_classifier, outcome_classes, outcome_classifier):
d = {'imputer': imputer, 'murmur_classes': murmur_classes, 'murmur_classifier': murmur_classifier, 'outcome_classes': outcome_classes, 'outcome_classifier': outcome_classifier}
filename = os.path.join(model_folder, 'model.sav')
joblib.dump(d, filename, protocol=0)
# Extract features from the data.
def get_features(data, recordings):
# Extract the age group and replace with the (approximate) number of months for the middle of the age group.
age_group = get_age(data)
if compare_strings(age_group, 'Neonate'):
age = 0.5
elif compare_strings(age_group, 'Infant'):
age = 6
elif compare_strings(age_group, 'Child'):
age = 6 * 12
elif compare_strings(age_group, 'Adolescent'):
age = 15 * 12
elif compare_strings(age_group, 'Young Adult'):
age = 20 * 12
else:
age = float('nan')
# Extract sex. Use one-hot encoding.
sex = get_sex(data)
sex_features = np.zeros(2, dtype=int)
if compare_strings(sex, 'Female'):
sex_features[0] = 1
elif compare_strings(sex, 'Male'):
sex_features[1] = 1
# Extract height and weight.
height = get_height(data)
weight = get_weight(data)
# Extract pregnancy status.
is_pregnant = get_pregnancy_status(data)
# Extract recording locations and data. Identify when a location is present, and compute the mean, variance, and skewness of
# each recording. If there are multiple recordings for one location, then extract features from the last recording.
locations = get_locations(data)
recording_locations = ['AV', 'MV', 'PV', 'TV', 'PhC']
num_recording_locations = len(recording_locations)
recording_features = np.zeros((num_recording_locations, 4), dtype=float)
num_locations = len(locations)
num_recordings = len(recordings)
if num_locations==num_recordings:
for i in range(num_locations):
for j in range(num_recording_locations):
if compare_strings(locations[i], recording_locations[j]) and np.size(recordings[i])>0:
recording_features[j, 0] = 1
recording_features[j, 1] = np.mean(recordings[i])
recording_features[j, 2] = np.var(recordings[i])
recording_features[j, 3] = sp.stats.skew(recordings[i])
recording_features = recording_features.flatten()
features = np.hstack(([age], sex_features, [height], [weight], [is_pregnant], recording_features))
return np.asarray(features, dtype=np.float32)