-
Notifications
You must be signed in to change notification settings - Fork 6
/
classifier_train.py
executable file
·138 lines (97 loc) · 5.17 KB
/
classifier_train.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
#!/usr/bin/env python3
"""Script to get the classification performance."""
import argparse
from pathlib import Path
import random as rn
import numpy as np
import pandas as pd
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import RobustScaler
from sklearn_rvm.em_rvm import EMRVC
from tqdm import tqdm
from joblib import dump
from utils import COLUMNS_NAME, load_dataset
PROJECT_ROOT = Path.cwd()
def main(dataset_name, disease_label):
"""Calculate the performance of the classifier in each iteration of the bootstrap method."""
# ----------------------------------------------------------------------------
n_bootstrap = 1000
participants_path = PROJECT_ROOT / 'data' / dataset_name / 'participants.tsv'
freesurfer_path = PROJECT_ROOT / 'data' / dataset_name / 'freesurferData.csv'
hc_label = 1
# ----------------------------------------------------------------------------
# Set random seed
random_seed = 42
np.random.seed(random_seed)
rn.seed(random_seed)
classifier_dir = PROJECT_ROOT / 'outputs' / 'classifier_analysis'
classifier_dataset_dir = classifier_dir / dataset_name
classifier_dataset_analysis_dir = classifier_dataset_dir / '{:02d}_vs_{:02d}'.format(hc_label, disease_label)
ids_dir = classifier_dataset_analysis_dir / 'ids'
classifier_storage_dir = classifier_dataset_analysis_dir / 'models'
classifier_storage_dir.mkdir(exist_ok=True)
predictions_dir = classifier_dataset_analysis_dir / 'predictions'
predictions_dir.mkdir(exist_ok=True)
auc_bootstrap_train = []
auc_bootstrap_test = []
# ----------------------------------------------------------------------------
for i_bootstrap in tqdm(range(n_bootstrap)):
ids_filename_train = 'homogeneous_bootstrap_{:03d}_train.csv'.format(i_bootstrap)
ids_path_train = ids_dir / ids_filename_train
dataset_df = load_dataset(participants_path, ids_path_train, freesurfer_path)
x_data = dataset_df[COLUMNS_NAME].values
tiv = dataset_df['EstimatedTotalIntraCranialVol'].values
tiv = tiv[:, np.newaxis]
x_data = (np.true_divide(x_data, tiv)).astype('float32')
x_data = np.concatenate((x_data[dataset_df['Diagn'] == hc_label],
x_data[dataset_df['Diagn'] == disease_label]), axis=0)
y_data = np.concatenate((np.zeros(sum(dataset_df['Diagn'] == hc_label)),
np.ones(sum(dataset_df['Diagn'] == disease_label))))
# Scaling using inter-quartile
scaler = RobustScaler()
x_data = scaler.fit_transform(x_data)
rvm = EMRVC(kernel='linear')
rvm.fit(x_data, y_data)
pred = rvm.predict(x_data)
predictions_proba = rvm.predict_proba(x_data)
auc = roc_auc_score(y_data, predictions_proba[:, 1])
auc_bootstrap_train.append(auc)
predictions_df = dataset_df[['Image_ID']].copy()
predictions_df['predictions'] = pred
dump(rvm, classifier_storage_dir / '{:03d}_rvr.joblib'.format(i_bootstrap))
dump(scaler, classifier_storage_dir / '{:03d}_scaler.joblib'.format(i_bootstrap))
# -----------------------------------------------------------------
ids_filename_test = 'homogeneous_bootstrap_{:03d}_test.csv'.format(i_bootstrap)
ids_path_test = ids_dir / ids_filename_test
dataset_df = load_dataset(participants_path, ids_path_test, freesurfer_path)
x_test = dataset_df[COLUMNS_NAME].values
tiv = dataset_df['EstimatedTotalIntraCranialVol'].values
tiv = tiv[:, np.newaxis]
x_test = (np.true_divide(x_test, tiv)).astype('float32')
x_test = np.concatenate((x_test[dataset_df['Diagn'] == hc_label],
x_test[dataset_df['Diagn'] == disease_label]), axis=0)
y_test = np.concatenate((np.zeros(sum(dataset_df['Diagn'] == hc_label)),
np.ones(sum(dataset_df['Diagn'] == disease_label))))
x_test = scaler.transform(x_test)
pred = rvm.predict(x_test)
predictions_proba = rvm.predict_proba(x_test)
auc = roc_auc_score(y_test, predictions_proba[:, 1])
auc_bootstrap_test.append(auc)
temp_df = dataset_df[['Image_ID']].copy()
temp_df['predictions'] = pred
predictions_df = pd.concat([predictions_df, temp_df], axis=0)
predictions_df.to_csv(predictions_dir / 'homogeneous_bootstrap_{:03d}_prediction.csv'.format(i_bootstrap),
index=False)
np.save(classifier_dataset_analysis_dir / 'aucs_train.npy', np.array(auc_bootstrap_train))
np.save(classifier_dataset_analysis_dir / 'aucs_test.npy', np.array(auc_bootstrap_test))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-D', '--dataset_name',
dest='dataset_name',
help='Dataset name to train the classifiers.')
parser.add_argument('-L', '--disease_label',
dest='disease_label',
help='Disease label to train the classifiers.',
type=int)
args = parser.parse_args()
main(args.dataset_name, args.disease_label)