Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
171 lines (112 loc) · 4.54 KB
/
Copy pathmain.py
File metadata and controls
171 lines (112 loc) · 4.54 KB
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
import os
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from sklearn.svm import SVC
from sklearn.metrics import roc_curve, auc
from sklearn.utils import shuffle
from sklearn.preprocessing import MinMaxScaler
from util import loader, feature_extractor
def show_nodule(nodule):
plt.imshow(nodule.pixels, cmap=cm.Greys_r)
plt.show()
def load_dataset(dataset_path, dataset_type, use_dump=False):
basename = os.path.basename(os.path.normpath(dataset_path))
dump_path = 'nodules-' + basename + '.dump'
if use_dump and os.path.isfile(dump_path):
print 'Load nodules from file ' + dump_path
nodules = loader.restore_nodules(dump_path)
else:
print 'Load nodules from dataset ' + dataset_path
nodules = loader.load_nodules(dataset_path, dataset_type)
loader.dump_nodules(dump_path, nodules)
print 'Nodules loaded: ' + str(len(nodules))
return nodules
def get_features(nodules, use_dump=False, dump_name='default'):
dump_path = 'features-' + dump_name + '.dump'
if use_dump and os.path.isfile(dump_path):
print 'Load features from file ' + dump_path
features = feature_extractor.restore_features(dump_path)
else:
print 'Load features from nodules'
features = feature_extractor.get_features(nodules)
feature_extractor.dump_features(dump_path, features)
print 'Features loaded: ' + str(len(features))
return np.array(features)
def plot_roc_curves(vals):
colors = ['k', 'b', 'g', 'r', 'c', 'm', 'y', 'w']
plt.figure()
it = 0
for (fpv, tpv, av, name) in vals:
label = name + '(area = ' + str(av) + ')'
plt.plot(fpv, tpv, lw=1, label=label)
it += 1
plt.xlim([-0.01, 1.0])
plt.ylim([0.0, 1.01])
plt.xlabel('FP rate')
plt.ylabel('TP rate')
plt.legend(loc="lower right")
plt.show()
def add_noise(features, step):
for i in range(0, len(features), step):
print i
features[i].conclusion = not features[i].conclusion
def calculate_impact(a_train, a_ext, f_train, f_ext, f_test, ind):
print ind
f_train_t = np.append(f_train, f_ext[ind])
f_ext_t = np.delete(f_ext, ind)
_, _, a1 = run_svm(f_train_t, f_test)
_, _, a2 = run_svm(f_ext_t, f_test)
d0 = a_train - a_ext
dt = a1 - a2
return abs(d0 - dt)
def run_svm(features_train, features_test):
x_train, y_train = feature_extractor.transform_features(features_train)
x_test, y_test = feature_extractor.transform_features(features_test)
scaler = MinMaxScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
svm = SVC(kernel='poly', max_iter=-1, probability=True)
svm.fit(x_train, y_train)
y_score = svm.decision_function(x_test)
fpv, tpv, _ = roc_curve(y_test, y_score)
av = auc(fpv, tpv)
return fpv, tpv, av
def main():
lidc_nodules = load_dataset('E:/Datasets/LIDC', 'LIDC', use_dump=True)
#nsclc_nodules = load_dataset('nsclc-data', 'NSCLC', use_dump=True)
spie_nodules = load_dataset('E:/Datasets/SPIE', 'SPIE', use_dump=True)
lidc_features = get_features(lidc_nodules, use_dump=True, dump_name='lidc')[:4500]
# nsclc_features = get_features(nsclc_nodules, use_dump=True, dump_name='nsclc')
spie_features = get_features(spie_nodules, use_dump=True, dump_name='spie')
#lidc_features = shuffle(lidc_features[:1200], random_state=1)
lidc_features = shuffle(lidc_features, random_state=1)[:500]
part_size = len(lidc_features) // 2
f_train = lidc_features[:part_size]
f_test = lidc_features[part_size:]
#f_test = spie_features
f_extra = spie_features
fpo, tpo, ao = run_svm(f_train, f_test)
vals = [(fpo, tpo, ao, 'ROC train')]
fpe, tpe, ae = run_svm(f_extra, f_test)
vals.append((fpe, tpe, ae, 'ROC extra'))
impacts = []
for i in range(len(f_extra)):
imp = calculate_impact(ao, ae, f_train, f_extra, f_test, i)
impacts.append(imp)
impacts = impacts/np.linalg.norm(impacts)
bad_inds = []
for i, imp in enumerate(impacts):
if imp > 0.08:
bad_inds.append(i)
print bad_inds
print len(bad_inds)
f_filtered = np.delete(f_extra, bad_inds)
f_train_and_filtered = np.append(f_train, f_filtered)
fpf, tpf, af = run_svm(f_train_and_filtered, f_test)
vals.append((fpf, tpf, af, 'ROC train + filtered extra'))
plot_roc_curves(vals)
f_result = np.append(f_train_and_filtered, f_test)
feature_extractor.save_as_dataset(f_result, 'test')
if __name__ == '__main__':
main()