-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_BETA_TOOL.py
341 lines (270 loc) · 13.3 KB
/
GUI_BETA_TOOL.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import tkinter
import tkinter.constants as Tkconstants
from tkinter import ttk,Frame
from tkinter import StringVar,IntVar,Checkbutton,CHECKBUTTON
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
from scipy import stats
from sklearn.preprocessing import MinMaxScaler
# Feature Selection method
import Main2
FilterOp = {'Mutual-Information', 'Entropy'}
WrapperOp = {'Sequntial-Forward-Selection', 'Particale-Swarm-Optimization'}
EmbeddedOp = {'Lasso', 'ElasticNet'}
class Adder(ttk.Frame):
XPath = ''
yPath = ''
dimRedValue=''
featSelecValue=''
featSelMethValue=''
ClassFiersTest=[]
histGram=0
scatPlot=0
impFeat=0
AccMod=0
missClasInst=0
histN=0
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()
def on_quit(self):
"""Exits program."""
quit()
def init_gui(self):
"""Builds GUI."""
self.root.title('AMIL - Feature Selection Tool')
self.root.option_add('*tearOff', 'FALSE')
self.root.geometry('900x1000+600+600')
self.grid(column=0, row=0, sticky='nsew')
self.menubar = tkinter.Menu(self.root)
self.menu_file = tkinter.Menu(self.menubar)
self.menu_file.add_command(label='Exit', command=self.on_quit)
self.menu_file.add_command(label='Open', command=self.OpenFile)
self.menu_edit = tkinter.Menu(self.menubar)
self.menubar.add_cascade(menu=self.menu_file, label='File')
self.menubar.add_cascade(menu=self.menu_edit, label='Edit')
self.menubar.add_cascade(menu=self.menu_edit, label='Exit',command=self.on_quit)
self.root.config(menu=self.menubar)
ttk.Label(self, text='Data Input:').grid(column=0, row=0,columnspan=4)
ttk.Separator(self, orient='horizontal').grid(column=0,row=1, columnspan=4, sticky='ew')
ttk.Label(self, text='Dependent Variables(X)').grid(column=0, row=2,sticky='w')
self.file_DiaY = ttk.Button(self, text="Choose X File",command=lambda: self.OpenFile(1))
self.file_DiaY.grid(column=1,row=2)
ttk.Label(self, text='Independent Variables(y)').grid(column=0, row=3,sticky='w')
self.file_DiaX = ttk.Button(self, text="Choose Y File",command=lambda: self.OpenFile(2))
self.file_DiaX.grid(column=1,row=3)
ttk.Separator(self, orient='horizontal').grid(column=0,row=4, columnspan=4, sticky='ew')
ttk.Label(self, text='Dimension Reduction?').grid(column=0, row=5,sticky='w')
tkvarDR=StringVar()
dimenReducCh = {'No', 'Yes, PCA', 'Yes, ICA'}
tkvarDR.set("No")
self.dimenReduc=ttk.OptionMenu(self,tkvarDR,*dimenReducCh,command=self.dimRedVal)
self.dimenReduc.grid(column=1, row=5)
ttk.Separator(self, orient='horizontal').grid(column=0,row=6, columnspan=4, sticky='ew')
ttk.Label(self, text='Feature Selection Type?').grid(column=0, row=7,sticky='w')
tkvarFS=StringVar()
FeatSeleCh = {'Filter', 'Wrapper', 'Embedded'}
tkvarFS.set('Wrapper')
ttk.OptionMenu(self,StringVar(),*FeatSeleCh,command=self.featSelecVal).grid(column=1, row=7)
tkvarFS.get()
ttk.Label(self, text='Feature Selection Method?').grid(column=4, row=7, sticky='w')
ttk.Separator(self, orient='horizontal').grid(column=0,row=9, columnspan=4, sticky='ew')
ttk.Label(self, text='Output :').grid(column=0, row=10, sticky='w')
self.histGram = IntVar()
ttk.Checkbutton(self, text="Histogram of Top n Features", variable=self.histGram).grid(row=12, sticky='w')
self.histGram.trace_variable("w",self.HistGramFxn)
ttk.Label(self, text='| n = ').grid(column=3, row=12, sticky='w')
self.histN=ttk.Entry(self).grid(column=4,row=12,pady=2,sticky='w')
self.scatPlot = IntVar()
ttk.Checkbutton(self, text="Scatler Plot of Top n Features", variable=IntVar()).grid(row=13, sticky='w')
self.scatPlot.trace_variable("w",self.ScatPlotFxn)
ttk.Label(self, text='| Select Plot Type = ').grid(column=3, row=13, sticky='w')
tkvarPT=StringVar()
ttk.OptionMenu(self,tkvarPT,'Make 2D PLot', 'Make 3D PLot').grid(column=4,row=13,sticky='w')
self.impFeat=IntVar()
ttk.Checkbutton(self, text="Show Importance of Each Feature", variable=self.impFeat).grid(row=14, sticky='w')
self.impFeat.trace_variable("w",self.ImpFeatFxn)
self.AccMod=IntVar()
ttk.Checkbutton(self, text="Show Accuracy of Model", variable=self.AccMod).grid(row=15, sticky='w')
self.AccMod.trace_variable("w",self.AccModFxn)
self.MisInt=IntVar()
ttk.Checkbutton(self, text="Misclassfied Instances", variable=self.MisInt).grid(row=16, sticky='w')
self.MisInt.trace_variable("w",self.MisIntFxn)
ttk.Button(self, text="Submit",command=lambda: self.collectValues()).grid(row=17,column=0,columnspan=16)
ttk.Button(self, text="Quit",command=lambda: self.on_quit()).grid(row=17,column=5,columnspan=2)
for child in self.winfo_children():
child.grid_configure(padx=5, pady=5)
def HistGramFxn(self,*args):
self.histGram=self.histGram.get()
def ScatPlotFxn(self,*args):
self.scatPlot=self.scatPlot.get()
def MisIntFxn(self,*args):
self.MisInt=self.MisInt.get()
def AccModFxn(self,*args):
self.AccMod=self.AccMod.get()
def ImpFeatFxn(self,*args):
self.impFeat=self.impFeat.get()
def collectValues(self):
self.draw_acc_plot()
frameScatPlot = Frame(self)
frameScatPlot.grid(column=0, row=60, columnspan=100, sticky=Tkconstants.NSEW)
frameScatPlot.rowconfigure(50, weight=100)
frameScatPlot.columnconfigure(1, weight=1)
frameFeatHist = Frame(self)
frameFeatHist.grid(column=0, row=30, columnspan=100, sticky=Tkconstants.NSEW)
frameFeatHist.rowconfigure(50, weight=100)
frameFeatHist.columnconfigure(1, weight=1)
n=8
myG = Main2.FeatureSelect()
myG.read_Data_Method2()
idx = myG.F_SCORE()
featureList,featName=myG.get_Feature_List(idx,n)
self.draw_Feat_Hist(frameFeatHist,featureList,n,featName)
self.draw_Scatt_Plot(frameScatPlot,featureList,n,featName)
def draw_Scatt_Plot(self,frameScatPlot,featureList,n,featName):
fig, axes = plt.subplots(1, n-1, sharex=True, figsize=((n-1)*6, 3), squeeze=False)
for i in range(0,n-1):
F10=MinMaxScaler().fit_transform(featureList[i][0][0])
F11 =MinMaxScaler().fit_transform(featureList[i][0][1])
F20=MinMaxScaler().fit_transform(featureList[i+1][0][0])
F21 =MinMaxScaler().fit_transform(featureList[i+1][0][1])
print('F10-',F10,'F20-', F20)
axes[0][i].plot(F10, F20,'go',label='benign')
axes[0][i].plot(F11, F21,'r^',label='malignant')
axes[0][i].legend(loc='best')
axes[0][i].axis([0, 1, 0, 1])
#prn='Feature'+str(i+1)+' VS '+'Feature'+str(i+2)
prn = str(featName[i]) + ' VS ' + str(featName[i + 1])
axes[0][i].set_title(prn)
fig.tight_layout()
self.addScrollingFigure(fig, frameScatPlot)
self.changeSize(fig,0.8)
def draw_Feat_Hist(self,frame,featureList,n,featName):
fig, ax = plt.subplots(1, n, sharex=True, figsize=(n*6, 3), squeeze=False)
for i in range(0,n):
feat=featureList[i]
ax[0][i-1].violinplot(feat[0], showmeans=False, showmedians=True)
pri=str(featName[i])+' p-value - ' +str(feat[1])
ax[0][i-1].set_title(pri)
fig.tight_layout()
self.addScrollingFigure(fig, frame)
self.changeSize(fig,0.8)
def changeSize(self,figure, factor):
global canvas, mplCanvas, interior, interior_id, frame, cwid
oldSize = figure.get_size_inches()
figure.set_size_inches([factor * s for s in oldSize])
wi, hi = [i * figure.dpi for i in figure.get_size_inches()]
mplCanvas.config(width=wi, height=hi)
canvas.itemconfigure(cwid, width=wi, height=hi)
canvas.config(scrollregion=canvas.bbox(Tkconstants.ALL), width=200, height=200)
figure.canvas.draw()
def draw_acc_plot(self):
classifiers = []
classifiers.append("lda")
ttk.Separator(self, orient='horizontal').grid(column=0,row=19, columnspan=4, sticky='ew')
txt="Running Sequential Forward Selection for "+ascii(classifiers[0])
tit=ttk.Label(self, text=txt)
tit.grid(row=21,column=0,sticky='w')
myG = Main2.FeatureSelect()
myG.read_Data_Method2()
rfs = myG.SFS(classifiers)
fs=rfs["bestFeatureSummary"]
for i in range(0, len(classifiers)):
title = "Sequential Forward Selection using " + classifiers[i]
accuracyString = "Overall Accuracy: " + str(fs[i][0][0])
accLowClassString = "Accuracy (Low Class): " + str(fs[i][1][0])
accHighString = "Accuracy (High Class): " + str(fs[i][2][0])
bfsShape = fs[i].shape
for j in range(3, bfsShape[0]):
featureString = "Feature at indice " + str(fs[i][j][0]) + " contributed " + \
str(fs[i][j][1]) + " percentage to accuracy"
# title = "SummarybestFeatures_"
# accuracyString = "Accuracy: "
# accLowClassString = "Accuracy (Low Class): "
# accHighString = "Accuracy (High Class): "
tit.grid_forget()
ttk.Label(self, text=title).grid(row=21,column=0,sticky='w')
ttk.Label(self, text=accuracyString).grid(row=22,column=0,sticky='w')
ttk.Label(self, text=accLowClassString).grid(row=23,column=0,sticky='w')
ttk.Label(self, text=accHighString).grid(row=24,column=0,sticky='w')
def addScrollingFigure(self,figure, frame):
global canvas, mplCanvas, interior, interior_id, cwid
# set up a canvas with scrollbars
canvas = tkinter.Canvas(frame)
canvas.grid(row=1, column=1, sticky=Tkconstants.NSEW)
xScrollbar = tkinter.Scrollbar(frame, orient=Tkconstants.HORIZONTAL)
yScrollbar = tkinter.Scrollbar(frame)
xScrollbar.grid(row=2, column=1, sticky=Tkconstants.EW)
yScrollbar.grid(row=1, column=2, sticky=Tkconstants.NS)
canvas.config(xscrollcommand=xScrollbar.set)
xScrollbar.config(command=canvas.xview)
canvas.config(yscrollcommand=yScrollbar.set)
yScrollbar.config(command=canvas.yview)
figAgg = FigureCanvasTkAgg(figure, canvas)
mplCanvas = figAgg.get_tk_widget()
cwid = canvas.create_window(0, 0, window=mplCanvas, anchor=Tkconstants.NW)
#changeSize(figure, 1)
def histGramChk(self,value):
print(self.featSelecValue)
def dimRedVal(self,value):
self.dimRedValue=value
def featSelecVal(self,value):
self.featSelecValue=value
tkvar = StringVar()
tkvar.set('')
if(self.featSelecValue=='Filter'):
FeatSelMethCh=FilterOp
elif(self.featSelecValue=='Wrapper'):
FeatSelMethCh=WrapperOp
elif(self.featSelecValue=='Embedded'):
FeatSelMethCh=EmbeddedOp
self.FeatSelMethOp = ttk.OptionMenu(self, tkvar, *FeatSelMethCh, command=self.featSelMethVal)
self.FeatSelMethOp.grid(column=6, row=7)
if(self.featSelecValue in('Embedded','Wrapper')):
ttk.Label(self, text='Classifier to Test:').grid(column=7, row=7, sticky='w')
ttk.Checkbutton(self, text="Linear Discriment Analysis (LDA)", variable=IntVar).grid(row=8,column=7, sticky='w')
ttk.Checkbutton(self, text="Quadratic Discriment Analysis (QDA)", variable=IntVar).grid(row=9,column=7, sticky='w')
ttk.Checkbutton(self, text="Support Vector Machine (SVM)", variable=IntVar).grid(row=10,column=7, sticky='w')
def featSelMethVal(self,value):
self.featSelMethValue=value
print(self.featSelMethValue)
def OpenFile(self,x):
name = filedialog.askopenfilename(initialdir="/home/launch/Desktop/Share/",
filetypes =(("Csv File", "*.csv"),("All Files","*.*")),
title = "Choose a file."
)
print (name)
#Using try in case user types in unknown file or closes without choosing a file.
try:
with open(name,'r') as UseFile:
print(UseFile.read())
except:
print("No file exists")
if x==1:
self.XPath=name
else:
self.yPath=name
class MyCheckButton(ttk.Checkbutton):
def __init__(self,*args,**kwargs):
self.var=kwargs.get('variable',IntVar())
kwargs['variable']=self.var
Checkbutton.__init__(self,*args,**kwargs)
def is_checked(self):
return self.var.get()
if __name__ == '__main__':
root = tkinter.Tk()
root.rowconfigure(1, weight=1)
root.columnconfigure(1, weight=1)
Adder(root)
root.mainloop()
'''
fs is {'bestFeatureSummary': [array([[ 70.37037037, 0. ],
[ 72. , 0. ],
[ 68.96551724, 0. ],
[ 9. , 62.96296296],
[ 33. , 3.7037037 ],
[ 73. , 3.7037037 ]])]}
'''