forked from data-science-bowl-2017/lung-cancer-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLung_Cancer_Analysis.py
229 lines (146 loc) · 4.72 KB
/
Lung_Cancer_Analysis.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
# coding: utf-8
# In[30]:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
import os
import glob
get_ipython().magic(u'matplotlib inline')
p = sns.color_palette()
os.listdir('./')
seed = 42
# In[6]:
for d in os.listdir('./sample_images'):
if '.DS_Store' not in d:
print("Patient '{}' has {} scans".format(d, len(os.listdir('./sample_images/' + d))))
print('----')
print('Total patients {} Total DCM files {}'.format(len(os.listdir('./sample_images')),
len(glob.glob('./sample_images/*/*.dcm'))))
# In[7]:
patient_sizes = [len(os.listdir('./sample_images/' + d)) for d in os.listdir('./sample_images') if '.DS_Store' not in d]
plt.hist(patient_sizes, color=p[2])
plt.ylabel('Number of patients')
plt.xlabel('DICOM files')
plt.title('Histogram of DICOM count per patient')
# # Training Set
# In[8]:
df_train = pd.read_csv('./stage1_labels.csv')
df_train.head()
# In[9]:
print('Number of training patients: {}'.format(len(df_train)))
print('Cancer rate: {:.4}%'.format(df_train.cancer.mean()*100))
# # Naive Submission
# Since the evaluation metric used in this competition is LogLoss and not something like AUC, this means that we can often gain an improvement just by aligning the probabilities of our sample submission to that of the training set.
# Before I try making a naive submission, I will calculate what the score of this submission would be on the training set to get a comparison.
# In[10]:
from sklearn.metrics import log_loss
logloss = log_loss(df_train.cancer, np.zeros_like(df_train.cancer) + df_train.cancer.mean())
print('Training logloss is {}'.format(logloss))
# In[11]:
# sample = pd.read_csv('./stage1_sample_submission.csv')
# sample['cancer'] = df_train.cancer.mean()
# sample.to_csv('naive_submission.csv', index=False)
# # Looking at images
# In[12]:
import dicom
# In[13]:
dcm = './0a67f9edb4915467ac16a565955898d3.dcm'
print('Filename: {}'.format(dcm))
dcm = dicom.read_file(dcm)
# In[14]:
print dcm
# In[15]:
img = dcm.pixel_array
print dcm.SliceLocation
img[img == -2000] = 0
print img
plt.axis('off')
plt.imshow(img)
plt.show()
plt.axis('off')
plt.imshow(-img) # Invert colors with -
plt.show()
# In[16]:
labels = pd.read_csv('stage1_labels.csv')
print labels.head()
# In[17]:
# import csv
# features = open('features.csv', 'wb')
# wr = csv.writer(features)
# In[39]:
a = []
b = []
ids = []
imgs = []
for i in os.listdir('./sample_images/'):
if '.DS_Store' not in i and '0b20184e0cd497028bdd155d9fb42dc9' not in i:
print "starting with new patient", i
label = int(labels[labels['id'] == i]['cancer'])
# print label
for j in os.listdir('./sample_images/'+i):
ids.append(i)
imgs.append(j)
b.append(label)
dcm = dicom.read_file('./sample_images/'+i+'/'+j)
img = dcm.pixel_array
img[img == -2000] = 0
img = np.sum(img, axis=1) ## sum of each row
# print img.shape
img = list(img.flatten())
loca = float(dcm.SliceLocation)
img.extend([loca])
a.append(img)
# break
# break
df = pd.DataFrame()
df['id'] = ids
df['image'] = imgs
df['label'] = b
df['feat'] = a
X = a
Y = b
# In[19]:
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
# In[41]:
# df = pd.read_csv('./features.csv')
# X = np.arrray(df)
print len(df)
# In[89]:
# X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.50, random_state=seed)
patients = [i for i in os.listdir('./sample_images/') if '.DS_Store' not in i]
print len(patients)
patients_train = np.random.choice(patients, 15)
X_train = []
y_train = []
X_test = []
y_test = []
for i in range(len(df)):
if df['id'][i] in patients_train:
X_train.append(np.array(df['feat'][i]))
y_train.append(df['label'][i])
else:
X_test.append(np.array(df['feat'][i]))
y_test.append(df['label'][i])
# In[90]:
model = RandomForestClassifier(random_state=seed)
model.fit(X_train, y_train)
predicted = model.predict(X_test)
predicted_prob = model.predict_proba(X_test)
# print 'log loss', log_loss(y_test, predicted)
print 'accuracy score', accuracy_score(y_test, predicted)
print predicted, y_test, X[0]
# In[72]:
# sample = pd.read_csv('./stage1_sample_submission.csv')
# test_ids = sample['id']
# print test_ids
# print np.sum(predicted_prob)
# predicted = model.predict(X_test)
# sample['cancer'] =
# sample.to_csv('submission2.csv', index=False)
# In[40]:
df.head(10)
# In[ ]:
# In[ ]: