forked from physionetchallenges/python-example-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam_code.py
264 lines (210 loc) · 11 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
#!/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.
################################################################################
#
# Optional libraries and functions. You can change or remove them.
#
################################################################################
from helper_code import *
import numpy as np, os, sys
import mne
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
import joblib
from fooof import FOOOF
################################################################################
#
# Required functions. Edit these functions to add your code, but do not change the arguments of the functions.
#
################################################################################
# Train your model.
def train_challenge_model(data_folder, model_folder, verbose):
# Find data files.
if verbose >= 1:
print('Finding the Challenge data...')
patient_ids = find_data_folders(data_folder)
num_patients = len(patient_ids)
if num_patients==0:
raise FileNotFoundError('No data was provided.')
# Create a folder for the model if it does not already exist.
os.makedirs(model_folder, exist_ok=True)
# Create a folder for the features if it does not already exist.
os.makedirs('features', exist_ok=True)
# Extract the features and labels.
if verbose >= 1:
print('Extracting features and labels from the Challenge data...')
features = list()
outcomes = list()
cpcs = list()
for i in range(num_patients):
if verbose >= 2:
print(' {}/{}...'.format(i+1, num_patients))
# Load data.
patient_id = patient_ids[i]
patient_metadata, recording_metadata, recording_data = load_challenge_data(data_folder, patient_id)
# Extract features.
current_features = get_features(patient_metadata, recording_metadata, recording_data)
features.append(current_features)
# Extract labels.
current_outcome = get_outcome(patient_metadata)
outcomes.append(current_outcome)
current_cpc = get_cpc(patient_metadata)
cpcs.append(current_cpc)
features = np.vstack(features)
outcomes = np.vstack(outcomes)
cpcs = np.vstack(cpcs)
np.savetxt('features/features.txt', features)
np.savetxt('features/outcomes.txt', outcomes)
np.savetxt('features/cpcs.txt', cpcs)
# Train the models.
if verbose >= 1:
print('Training the Challenge models on the Challenge data...')
# Define parameters for random forest classifier and regressor.
n_estimators = 123 # Number of trees in the forest.
max_leaf_nodes = 456 # Maximum number of leaf nodes in each tree.
random_state = 789 # Random state; set for reproducibility.
# Impute any missing features; use the mean value by default.
imputer = SimpleImputer().fit(features)
# Train the models.
features = imputer.transform(features)
outcome_model = RandomForestClassifier(
n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, outcomes.ravel())
cpc_model = RandomForestRegressor(
n_estimators=n_estimators, max_leaf_nodes=max_leaf_nodes, random_state=random_state).fit(features, cpcs.ravel())
# Save the models.
save_challenge_model(model_folder, imputer, outcome_model, cpc_model)
if verbose >= 1:
print('Done.')
# Load your trained models. 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_models(model_folder, verbose):
filename = os.path.join(model_folder, 'models.sav')
return joblib.load(filename)
# Run your trained models. 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_models(models, data_folder, patient_id, verbose):
imputer = models['imputer']
outcome_model = models['outcome_model']
cpc_model = models['cpc_model']
# Load data.
patient_metadata, recording_metadata, recording_data = load_challenge_data(data_folder, patient_id)
# Extract features.
features = get_features(patient_metadata, recording_metadata, recording_data)
features = features.reshape(1, -1)
# Impute missing data.
features = imputer.transform(features)
# Apply models to features.
outcome = outcome_model.predict(features)[0]
outcome_probability = outcome_model.predict_proba(features)[0, 1]
cpc = cpc_model.predict(features)[0]
# Ensure that the CPC score is between (or equal to) 1 and 5.
cpc = np.clip(cpc, 1, 5)
return outcome, outcome_probability, cpc
################################################################################
#
# 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, outcome_model, cpc_model):
d = {'imputer': imputer, 'outcome_model': outcome_model, 'cpc_model': cpc_model}
filename = os.path.join(model_folder, 'models.sav')
joblib.dump(d, filename, protocol=0)
# Extract features from the data.
def get_features(patient_metadata, recording_metadata, recording_data):
# Extract features from the patient metadata.
age = get_age(patient_metadata)
sex = get_sex(patient_metadata)
rosc = get_rosc(patient_metadata)
ohca = get_ohca(patient_metadata)
vfib = get_vfib(patient_metadata)
ttm = get_ttm(patient_metadata)
# Use one-hot encoding for sex; add more variables
sex_features = np.zeros(2, dtype=int)
if sex == 'Female':
female = 1
male = 0
other = 0
elif sex == 'Male':
female = 0
male = 1
other = 0
else:
female = 0
male = 0
other = 1
# Combine the patient features.
patient_features = np.array([age, female, male, other, rosc, ohca, vfib, ttm])
# Extract features from the recording data and metadata.
channels = ['Fp1-F7', 'F7-T3', 'T3-T5', 'T5-O1', 'Fp2-F8', 'F8-T4', 'T4-T6', 'T6-O2', 'Fp1-F3',
'F3-C3', 'C3-P3', 'P3-O1', 'Fp2-F4', 'F4-C4', 'C4-P4', 'P4-O2', 'Fz-Cz', 'Cz-Pz']
num_channels = len(channels)
num_recordings = len(recording_data)
# Compute mean and standard deviation for each channel for each recording.
available_signal_data = list()
for i in range(num_recordings):
signal_data, sampling_frequency, signal_channels = recording_data[i]
if signal_data is not None:
signal_data = reorder_recording_channels(signal_data, signal_channels, channels) # Reorder the channels in the signal data, as needed, for consistency across different recordings.
available_signal_data.append(signal_data)
if len(available_signal_data) > 0:
available_signal_data = np.hstack(available_signal_data)
signal_mean = np.nanmean(available_signal_data, axis=1)
signal_std = np.nanstd(available_signal_data, axis=1)
else:
signal_mean = float('nan') * np.ones(num_channels)
signal_std = float('nan') * np.ones(num_channels)
# Compute the power spectral density for the delta, theta, alpha, and beta frequency bands for each channel of the most
# recent recording.
index = None
for i in reversed(range(num_recordings)):
signal_data, sampling_frequency, signal_channels = recording_data[i]
if signal_data is not None:
index = i
break
if index is not None:
signal_data, sampling_frequency, signal_channels = recording_data[index]
signal_data = reorder_recording_channels(signal_data, signal_channels, channels) # Reorder the channels in the signal data, as needed, for consistency across different recordings.
delta_psd, _ = mne.time_frequency.psd_array_welch(signal_data, sfreq=sampling_frequency, fmin=0.5, fmax=4.0, verbose=False)
theta_psd, _ = mne.time_frequency.psd_array_welch(signal_data, sfreq=sampling_frequency, fmin=4.0, fmax=8.0, verbose=False)
alpha_psd, _ = mne.time_frequency.psd_array_welch(signal_data, sfreq=sampling_frequency, fmin=8.0, fmax=12.0, verbose=False)
beta_psd, _ = mne.time_frequency.psd_array_welch(signal_data, sfreq=sampling_frequency, fmin=12.0, fmax=30.0, verbose=False)
gamma_psd, _ = mne.time_frequency.psd_array_welch(signal_data, sfreq=sampling_frequency, fmin=30.0, fmax=50.0, verbose=False) # SF at 100 Hz, therefore 50 Hz max for gamma
fooof_psd, frequ = mne.time_frequency.psd_array_welch(signal_data, sfreq=sampling_frequency, fmin=1, fmax=45, verbose=False)
# AVERAGE PSD OVER channels
delta_psd_mean = np.nanmean(delta_psd, axis=1)
theta_psd_mean = np.nanmean(theta_psd, axis=1)
alpha_psd_mean = np.nanmean(alpha_psd, axis=1)
beta_psd_mean = np.nanmean(beta_psd, axis=1)
gamma_psd_mean = np.nanmean(gamma_psd, axis=1)
quality_score = get_quality_scores(recording_metadata)[index]
# Get the fooof
# Initialize a FOOOF object, with desired settings to get aperiodic component
# https://fooof-tools.github.io/fooof/generated/fooof.FOOOF.html#fooof.FOOOF.fit
oof_1_45 = []
oof_30_45 = []
for ch in range(num_channels):
if ~np.isfinite(np.log10(fooof_psd[ch])).any():
oof_30_45.append(float('nan'))
oof_1_45.append(float('nan'))
else:
# Initialize a FOOOF object, with desired settings
fm = FOOOF(peak_width_limits=(1, 12.0)) ## ! TODO: increase frequency resolution and decrease min peak to 0.5
fm.fit(frequ, fooof_psd[ch], [1, 45])
oof_1_45.append(fm.aperiodic_params_[1])
# Initialize a FOOOF object, with desired settings
fm = FOOOF(peak_width_limits=(1, 12.0)) ## ! TODO: increase frequency resolution and decrease min peak to 0.5
# Do the same for 30-45 Hz range
fm.fit(frequ, fooof_psd[ch], [30, 45])
oof_30_45.append(fm.aperiodic_params_[1])
else:
delta_psd_mean = theta_psd_mean = alpha_psd_mean = beta_psd_mean = gamma_psd_mean = oof_30_45= oof_1_45 = float('nan') * np.ones(num_channels)
quality_score = float('nan')
recording_features = np.hstack((signal_mean, signal_std, delta_psd_mean,
theta_psd_mean, alpha_psd_mean, beta_psd_mean,
gamma_psd_mean, quality_score,
oof_1_45, oof_30_45))
# Combine the features from the patient metadata and the recording data and metadata.
features = np.hstack((patient_features, recording_features))
return features