-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
340 lines (322 loc) · 14 KB
/
main.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
import pandas as pd
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from About_Us import Ui_AboutUs
from About_Application import Ui_AboutApp
def is_number(s):
try:
float(s) or int(s)
return True
except ValueError:
return False
class Ui_MainWindow(object):
def OpenWindow(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_AboutApp()
self.ui.setupUi(self.window)
self.window.show()
def openWindow(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_AboutUs()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(350, 591)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("post.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout_5.addWidget(self.label)
self.Browse = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
self.Browse.setFont(font)
self.Browse.setObjectName("Browse")
self.verticalLayout_5.addWidget(self.Browse)
#########################################################
self.Browse.clicked.connect(self.clicker)
#########################################################
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.Silhouette = QtWidgets.QRadioButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(9)
self.Silhouette.setFont(font)
self.Silhouette.setObjectName("Silhouette")
self.horizontalLayout_2.addWidget(self.Silhouette)
########################################################
self.Silhouette.setEnabled(False)
self.Silhouette.toggled.connect(self.Metric_Choosing)
########################################################
self.Distortion = QtWidgets.QRadioButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(9)
self.Distortion.setFont(font)
self.Distortion.setObjectName("Distortion")
self.horizontalLayout_2.addWidget(self.Distortion)
########################################################
self.Distortion.setEnabled(False)
self.Distortion.toggled.connect(self.Metric_Choosing)
########################################################
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.verticalLayout_5.addLayout(self.verticalLayout_2)
self.Run = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
self.Run.setFont(font)
self.Run.setObjectName("Run")
self.verticalLayout_5.addWidget(self.Run)
#########################################################
self.Run.setEnabled(False)
self.Run.clicked.connect(self.Preprocess_Dataset)
#########################################################
self.Save = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
self.Save.setFont(font)
self.Save.setObjectName("Save")
self.verticalLayout_5.addWidget(self.Save)
#########################################################
self.Save.setEnabled(False)
self.Save.clicked.connect(self.Save_Result)
#########################################################
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.Tab = QtWidgets.QTabWidget(self.centralwidget)
self.Tab.setObjectName("Tab")
self.Log = QtWidgets.QWidget()
self.Log.setObjectName("Log")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.Log)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.Log1 = QtWidgets.QTextBrowser(self.Log)
self.Log1.setObjectName("Log1")
self.verticalLayout_4.addWidget(self.Log1)
self.Tab.addTab(self.Log, "")
self.Result = QtWidgets.QWidget()
self.Result.setObjectName("Result")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.Result)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.Result1 = QtWidgets.QTextBrowser(self.Result)
self.Result1.setObjectName("Result1")
self.verticalLayout_3.addWidget(self.Result1)
self.Tab.addTab(self.Result, "")
self.verticalLayout.addWidget(self.Tab)
self.verticalLayout_5.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.AboutUs = QtWidgets.QPushButton(self.centralwidget, clicked = lambda:self.openWindow())
font = QtGui.QFont()
font.setPointSize(10)
self.AboutUs.setFont(font)
self.AboutUs.setObjectName("AboutUs")
self.horizontalLayout.addWidget(self.AboutUs)
self.AboutSoftware = QtWidgets.QPushButton(self.centralwidget, clicked = lambda:self.OpenWindow())
font = QtGui.QFont()
font.setPointSize(10)
self.AboutSoftware.setFont(font)
self.AboutSoftware.setObjectName("AboutSoftware")
self.horizontalLayout.addWidget(self.AboutSoftware)
self.verticalLayout_5.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 459, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.Tab.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "All-Purpose Clusterer"))
self.label.setText(_translate("MainWindow", "Clustering Tool"))
self.Browse.setText(_translate("MainWindow", "Browse"))
self.label_2.setText(_translate("MainWindow", "Method for Finding optimal cluster number"))
self.Silhouette.setText(_translate("MainWindow", "Silhouette"))
self.Distortion.setText(_translate("MainWindow", "Distortion"))
self.Run.setText(_translate("MainWindow", "Run"))
self.Save.setText(_translate("MainWindow", "Save"))
self.Tab.setTabText(self.Tab.indexOf(self.Log), _translate("MainWindow", "Log"))
self.Tab.setTabText(self.Tab.indexOf(self.Result), _translate("MainWindow", "Result"))
self.AboutUs.setText(_translate("MainWindow", "About Us"))
self.AboutSoftware.setText(_translate("MainWindow", "About Application"))
def clicker(self):
app.processEvents()
self.Log1.clear()
self.Result1.clear()
path,_ = QtWidgets.QFileDialog.getOpenFileName(None, 'Open file', '', 'Excel (*.xls, *.xlsx *.xlsm)')
print(path)
if path:
df = pd.read_excel(path)
dataset = df.values
self.Log1.append("File has been loaded successfully.")
self.Log1.append("\nThe File location is: "+ str(path))
self.dataset = dataset
self.Distortion.setEnabled(True)
self.Silhouette.setEnabled(True)
return(dataset)
else:
self.Log1.append("\nNo File selected...")
def Preprocess_Dataset(self):
dataset = self.dataset
app.processEvents()
I = dataset.shape[0]
J = dataset.shape[1]
###### X is data without label and X_bolean is made for checking whter all data is number or not!
X = []
X_bolean = []
for i in range(I):
for j in range(2, J):
X.append(dataset[i][j])
X_bolean.append(is_number(str(dataset[i][j])))
##### Creating data with label
X1 = []
for i in range(I):
for j in range(1, J):
X1.append(dataset[i][j])
###### Cause is_number func didn't support nan, we used isna from pandas
Isnan = pd.isna(X)
Res_nan = False
Res = True
for i in range(len(Isnan)):
Res_nan = Res_nan or Isnan[i]
Res = Res and X_bolean[i]
if (Res==True and Res_nan==False):
# Checking whether number of samples is more than 3.
if I>3:
X = np.array(X)
X = np.reshape(X,(I,J-2))
X1 = np.array(X1)
X1 = np.reshape(X1,(I,J-1))
self.Log1.append("\nThe data format is ok.")
self.X = X
self.X1 = X1
self.Finding_Optimal()
else:
self.Log1.append("\nThe Number of samples should be at least 4.")
else:
self.Log1.append("\nThe File format is not supported. For more information, visit the 'About Application' section.")
def Metric_Choosing(self):
app.processEvents()
if self.Distortion.isChecked():
s ="distortion"
else:
s ="silhouette"
self.Run.setEnabled(True)
return(s)
def Metric_Changing(self,M):
if (M=="distortion"):
s="silhouette"
else:
s ="distortion"
return(s)
def Finding_Optimal(self):
app.processEvents()
s = self.Metric_Choosing()
self.Log1.append("\nThe metric is %s"%(str(s)))
X = self.X
X1 = self.X1
############## Finding optimal K ###################
from sklearn.cluster import KMeans
# Elbow Method for K means
# Import ElbowVisualizer
from yellowbrick.cluster import KElbowVisualizer
model0 = KMeans()
# k is range of number of clusters.
visualizer0 = KElbowVisualizer(model0, k=(2,X.shape[0]), metric=s, timings= True)
visualizer0.fit(X) # Fit data to visualizer
k0 = visualizer0.elbow_value_
itr = 0
while (k0 == None):
itr = itr + 1
if (itr==2):
self.Log1.append("\nSorry! We can't find any optimal cluster number for this data.")
self.Log1.append("\n%s metric didn't find any optimal cluster number!"%(str(s)))
self.Log1.append("\nMetric is changing...")
s = self.Metric_Changing(s)
self.Log1.append("\nThe new metric is %s"%(s))
visualizer0 = KElbowVisualizer(model0, k=(2,X.shape[0]), metric=s, timings= True)
visualizer0.fit(X) # Fit data to visualizer
k0 = visualizer0.elbow_value_
k0 = int(visualizer0.elbow_value_)
print(k0)
self.Log1.append("\nThe optimal number of clusters is: "+ str(k0))
self.Log1.append("\nYou can see the results in corresponding tab.")
self.X = X
self.X1 = X1
self.k0 = k0
self.Run_App()
def Run_App(self):
app.processEvents()
X = self.X
X1 = self.X1
k0 = self.k0
from sklearn.cluster import KMeans
#k0 = self.k0
########## k-means clustering ##########################
# define the model
model2 = KMeans(n_clusters=k0)
# fit the model
model2.fit(X)
# assign a cluster to each example
yhat = model2.predict(X)
################ clusters result ##########
cluster =[[] for i in range(int(k0))]
for i in range(len(yhat)):
cluster[yhat[i]].append(X1[i][0])
centers = model2.cluster_centers_
for i in range(k0):
self.Result1.append("\nCluster %d is %s:" %(i+1, str(cluster[i])))
self.Result1.append('\nThe center of cluster %d is %s.' %(int(i+1), str(list(centers[i]))))
self.cluster = cluster
self.k0 = k0
self.Save.setEnabled(True)
def Save_Result(self):
app.processEvents()
cluster = self.cluster
k0 = self.k0
dic = {}
for i in range(k0):
dic['Cluster'+str(i+1)] = cluster[i]
df1 = pd.DataFrame.from_dict(dic, orient='index')
path2,_ = QtWidgets.QFileDialog.getSaveFileName(None, 'Save file', '', 'Excel (*.xlsx)')
print(path2)
if path2:
df1.to_excel(path2)
self.Log1.append("\nFile has been saved successfully.")
self.Log1.append("The File location is: "+ str(path2))
else:
self.Log1.append("\nThe File has not been saved.")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())