-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_splits.py
237 lines (221 loc) · 8.98 KB
/
data_splits.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
import argparse
import os
import shutil
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, StratifiedKFold, KFold
from tqdm import tqdm
from DataProcessing.find_and_load_patient_files import (
find_patient_files,
load_patient_data,
)
from DataProcessing.label_extraction import get_murmur, get_outcome
from DataProcessing.XGBoost_features.metadata import get_metadata
def stratified_test_vali_split(
stratified_features: list,
data_directory: str,
stratified_directory: str,
test_size: float,
vali_size: float,
random_states: list = [42],
cv: bool = False,
n_splits: int = 10,
stratified_cv: bool = False,
):
# Check if stratified_directory directory exists, otherwise create it.
if not os.path.exists(stratified_directory):
os.makedirs(stratified_directory)
# Get metadata
patient_files = find_patient_files(data_directory)
num_patient_files = len(patient_files)
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 tqdm(range(num_patient_files)):
# Load the current patient data and recordings.
current_patient_data = load_patient_data(patient_files[i])
# Extract features.
current_features = get_metadata(current_patient_data)
current_features = np.insert(
current_features, 0, current_patient_data.split(" ")[0]
)
current_features = np.insert(
current_features, 1, current_patient_data.split(" ")[2][:-3]
)
features.append(current_features)
# Extract labels and use one-hot encoding.
# Murmur
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)
# Outcome
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)
# Combine dataframes
features_pd = pd.DataFrame(
features,
columns=[
"id",
"hz",
"age",
"female",
"male",
"height",
"weight",
"is_pregnant",
],
)
murmurs_pd = pd.DataFrame(murmurs, columns=murmur_classes)
outcomes_pd = pd.DataFrame(outcomes, columns=outcome_classes)
complete_pd = pd.concat([features_pd, murmurs_pd, outcomes_pd], axis=1)
complete_pd["id"] = complete_pd["id"].astype(int).astype(str)
complete_pd["stratify_column"] = (
complete_pd[stratified_features].astype(str).agg("-".join, axis=1)
)
# Split data
complete_pd_train_list = list()
complete_pd_val_list = list()
complete_pd_test_list = list()
cnums = list()
if cv:
if stratified_cv:
print("Performing stratified cross-validation")
skf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=42)
for i, (train_index, test_index) in enumerate(
skf.split(complete_pd, complete_pd["stratify_column"])
):
cnums.append(f"split_{i}")
complete_pd_train, complete_pd_test = complete_pd.iloc[train_index], complete_pd.iloc[test_index]
vali_split = vali_size / (1 - test_size)
complete_pd_train, complete_pd_val = train_test_split(
complete_pd_train,
test_size=vali_split,
random_state=42,
stratify=complete_pd_train["stratify_column"],
)
complete_pd_train_list.append(complete_pd_train)
complete_pd_val_list.append(complete_pd_val)
complete_pd_test_list.append(complete_pd_test)
else:
print("Performing random cross-validation")
kf = KFold(n_splits=n_splits, shuffle=True, random_state=42)
for i, (train_index, test_index) in enumerate(
kf.split(complete_pd)
):
cnums.append(f"split_{i}")
complete_pd_train, complete_pd_test = complete_pd.iloc[train_index], complete_pd.iloc[test_index]
vali_split = vali_size / (1 - test_size)
complete_pd_train, complete_pd_val = train_test_split(
complete_pd_train,
test_size=vali_split,
random_state=42,
)
complete_pd_train_list.append(complete_pd_train)
complete_pd_val_list.append(complete_pd_val)
complete_pd_test_list.append(complete_pd_test)
else:
print("Performing statified split")
for random_state in random_states:
cnums.append(f"seed_{random_state}")
complete_pd_train, complete_pd_test = train_test_split(
complete_pd,
test_size=test_size,
random_state=random_state,
stratify=complete_pd["stratify_column"],
)
vali_split = vali_size / (1 - test_size)
complete_pd_train, complete_pd_val = train_test_split(
complete_pd_train,
test_size=vali_split,
random_state=random_state + 1,
stratify=complete_pd_train["stratify_column"],
)
complete_pd_train_list.append(complete_pd_train)
complete_pd_val_list.append(complete_pd_val)
complete_pd_test_list.append(complete_pd_test)
# Save the files.
for cnum, complete_pd_train, complete_pd_val, complete_pd_test in zip(
cnums, complete_pd_train_list, complete_pd_val_list, complete_pd_test_list
):
print(f"Saving split {cnum} with cv {cv} from {len(cnums)} splits...")
if cv:
save_folder = os.path.join(stratified_directory, f"cv_{cv}_stratified_{stratified_cv}", cnum)
else:
save_folder = os.path.join(stratified_directory, f"cv_{cv}", cnum)
os.makedirs(os.path.join(save_folder, "train_data"))
os.makedirs(os.path.join(save_folder, "vali_data"))
os.makedirs(os.path.join(save_folder, "test_data"))
with open(os.path.join(save_folder, "split_details.txt"), "w") as text_file:
text_file.write("This data split is stratified over the following features: \n")
for feature in stratified_features:
text_file.write(feature + ", ")
for f in complete_pd_train["id"]:
copy_files(
data_directory,
f,
os.path.join(save_folder, "train_data/"),
)
for f in complete_pd_val["id"]:
copy_files(
data_directory,
f,
os.path.join(save_folder, "vali_data/"),
)
for f in complete_pd_test["id"]:
copy_files(
data_directory,
f,
os.path.join(save_folder, "test_data/"),
)
def copy_files(data_directory: str, ident: str, stratified_directory: str) -> None:
# Get the list of files in the data folder.
files = os.listdir(data_directory)
# Copy all files in data_directory that start with f to stratified_directory
for f in files:
if f.startswith(ident):
_ = shutil.copy(os.path.join(data_directory, f), stratified_directory)
if __name__ == "__main__":
print("---------------- Starting data_splits.py to split the data ----------------")
parser = argparse.ArgumentParser(prog="StratifiedDataSplit")
parser.add_argument(
"--data_directory",
type=str,
help="The directory containing the data you wish to split.",
default="physionet.org/files/circor-heart-sound/1.0.3/training_data",
)
parser.add_argument(
"--stratified_directory",
type=str,
help="The directory to store the split data.",
default="data/a_splits",
)
parser.add_argument(
"--vali_size", type=float, default=0.16, help="The size of the test split."
)
parser.add_argument(
"--test_size", type=float, default=0.2, help="The size of the test split."
)
parser.add_argument(
"--cv", type=bool, default=False, help="Whether to run cv."
)
parser.add_argument(
"--stratified_cv", type=bool, default=False, help="Whether to run cv."
)
args = parser.parse_args()
stratified_features = ["Normal", "Abnormal", "Absent", "Present", "Unknown"]
# Create the test split.
stratified_test_vali_split(stratified_features, **vars(args))