-
Notifications
You must be signed in to change notification settings - Fork 1
/
DICOMetricsPythonClassifier.py
204 lines (158 loc) · 6.88 KB
/
DICOMetricsPythonClassifier.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
# coding: utf-8
# In[1]:
import sys
import scipy
import numpy as np
import matplotlib
import pandas as pd;
import sklearn
pd.options.display.precision = 3;
pd.set_option('display.height', 2000)
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', 100)
pd.set_option('display.width', 2000)
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
import os
scriptDir = os.path.dirname(os.path.realpath(__file__))
# In[2]:
print('Python: {}'.format(sys.version))
print('scipy: {}'.format(scipy.__version__))
print('numpy:{}'.format(np.__version__))
print('matplotlib: {}'.format(matplotlib.__version__))
print('pandas: {}'.format(pd.__version__))
print('sklearn: {}'.format(sklearn.__version__))
# In[3]:
from pandas.tools.plotting import scatter_matrix
import matplotlib.pyplot as plt
plt.figure(figsize=(40,40))
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
pd.options.display.width = 180
# In[4]:
import requests
from io import BytesIO
import scipy.io as sio
urlData = 'https://github.com/dyt811/QCMetrics/raw/master/Results/BDP/Metrics/LabeledMatrix.csv'
urlLabel = 'https://raw.githubusercontent.com/CNBP/DICOMetrics/master/Results/BDPLabel1.csv'
Data = requests.get(urlData)
Label = requests.get(urlLabel)
data = pd.read_csv(BytesIO(Data.content))
label = pd.read_csv(BytesIO(Label.content), header=None, names=['Class'])
label['Class']=label['Class'].astype('category')
label['Class'].value_counts()
#1 is bad image (target)
#0 is good image (non-target)
pattern = r'^.SS'
#Epic tips from the internet: https://stackoverflow.com/questions/31551412/how-to-select-dataframe-columns-based-on-partial-matching
dataFocus = data[data.columns[data.columns.to_series().str.contains('Focus')]]
dataSNR = data[data.columns[data.columns.to_series().str.contains('SNR')]]
dataTexture = data[data.columns[data.columns.to_series().str.contains('Texture')]]
dataNSS = data[data.columns[data.columns.to_series().str.contains(pattern)]]
dataDICOM = data[data.columns[data.columns.to_series().str.contains('Dicom')]]
dataArray = [dataFocus, dataSNR, dataTexture, dataNSS, dataDICOM]
if label.size == dataFocus.shape[0]:
dataFocusLabelled = pd.concat([dataFocus,label], axis=1)
if label.size == dataSNR.shape[0]:
dataSNRLabelled = pd.concat([dataSNR,label], axis=1)
if label.size == dataTexture.shape[0]:
dataTextureLabelled = pd.concat([dataTexture,label], axis=1)
if label.size == dataNSS.shape[0]:
dataNSSLabelled = pd.concat([dataNSS,label], axis=1)
if label.size == dataDICOM.shape[0]:
dataDICOMLabelled = pd.concat([dataDICOM,label], axis=1)
# In this step we are going to take a look at the data a few different ways:
#
# Dimensions of the dataset.
# Peek at the data itself.
# Statistical summary of all attributes.
# Breakdown of the data by the class variable.
# ### Basic Data Description Section
dataFocusLabelled
dataSNRLabelled
dataTextureLabelled
dataNSSLabelled
dataDICOMLabelled
dataLabelledArray = [dataFocusLabelled, dataSNRLabelled,dataTextureLabelled, dataNSSLabelled, dataDICOMLabelled]
dataLabelledArray
dataFocusLabelled.columns
plt.clf()
#Generate today' date:
from datetime import date, time, datetime
Current = datetime.now().isoformat(timespec='seconds');
directoryName = Current.replace(':','')
print(directoryName)
os.makedirs(directoryName)
os.chdir(directoryName)
for currentDataType in dataLabelledArray:
currentDataType
#ncolumns, nrows = (6, 6)
#fig = plt.figure(ncolumns, nrows)
#gs = gridspec.gridspec()
plt.clf()
plt.subplot(10,10,10)
# Loop through ALl columns within the currenet
for currentMetric in range(0,(len(currentDataType.columns)-1)):
#plt.subplot(ncolumns,nrows,currentMetric+1)
#Get data.
C1=currentDataType[currentDataType.columns[currentMetric]]
#Append the CLASS information.
C2=pd.concat([C1,label], axis=1)
#C3 = np.vstack([C2.loc[C2.Class==0],C2.loc[C2.Class==1]]).T
#C2.boxplot(by='Class',figsize=(6,6))
C2.hist(figsize=(6,6), label=['QC Fail', 'QC Pass'], histtype='barstacked',stacked=True)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
#plt.xticks([1,2],['Pass','Fail'])
#C2.plot(kind='box',by='Class',figsize=(6,6))
plt.savefig(str('Histogram_'+currentDataType.columns[currentMetric])+'.png')
plt.show()
os.getcwd()
#DataType Loop
for currentDataType in dataLabelledArray:
currentDataType
#ncolumns, nrows = (6, 6)
#fig = plt.figure(ncolumns, nrows)
#gs = gridspec.gridspec()
plt.clf()
plt.subplot(10,10,10)
# Loop through ALl columns within the currenet
for currentMetric in range(0,(len(currentDataType.columns)-1)):
#plt.subplot(ncolumns,nrows,currentMetric+1)
#Get data.
C1=currentDataType[currentDataType.columns[currentMetric]]
#Append the CLASS information.
C2=pd.concat([C1,label], axis=1)
C2.boxplot(by='Class',figsize=(6,6))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xticks([1,2],['Pass','Fail'])
#C2.plot(kind='box',by='Class',figsize=(6,6))
plt.savefig(str('Boxplot'+currentDataType.columns[currentMetric])+'.png')
plt.show()
#Development block:
# dataTextureLabelled
# grouped = dataTextureLabelled.groupby('Class')
#
# data = [grouped.get_group(0).columns[0],grouped.get_group(1).columns[0]]
# plt.figure()
# plt.boxplot(data)
#
# grouped = dataTextureLabelled.groupby('Class')
# rowlength = grouped.ngroups//2
# fig, axs = plt.subplots(figsize=(9,4),
# nrows=2, ncols=rowlength, # fix as above
# gridspec_kw=dict(hspace=0.4))
# targets = zip(grouped.groups.keys(), axs.flatten())
# for i, (key, ax) in enumerate(targets):
# grouped.get_group(key).plot(kind='box')
#
#
# ax.legend()
# plt.show()