-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI_modified.py
475 lines (396 loc) · 15.9 KB
/
GUI_modified.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Version 2.2.2
import copy
import os
import sys
from os.path import relpath
from multiprocessing import Pool, Value
# PyQt5 GUI
from PyQt5 import uic
from PyQt5.QtWidgets import (
QDialog,
QApplication,
QLabel,
QPushButton,
QLCDNumber,
QFileDialog,
QListWidget,
QProgressBar,
QCheckBox,
)
from PyQt5.QtCore import Qt, pyqtSignal, QThread
# Data Processing
import pandas as pd
import numpy as np
# Modelling
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
accuracy_score,
confusion_matrix,
precision_score,
recall_score,
ConfusionMatrixDisplay,
)
from sklearn.model_selection import RandomizedSearchCV, train_test_split
from scipy.stats import randint
# Tree Visualisation
from sklearn.tree import export_graphviz
from IPython.display import Image
import graphviz
import scikitplot as skplt
import matplotlib.pyplot as plt
import seaborn as sns
# Enable High-DPI scaling
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) # Use high-DPI icons
class Worker(QThread):
progress_updated = pyqtSignal(int) # Signal to update progress
finished = pyqtSignal() # Signal to indicate that the thread has finished
def run(self):
with Pool() as pool:
# Use imap to get results as they are ready
for i, result in enumerate(
pool.imap(call_accuracy_test, range(len(acc_check.X))), 1
):
self.progress_updated.emit(i) # Emit signal with progress
self.finished.emit() # Emit signal to indicate that the thread has finished
class UI(QDialog):
# Define a signal that takes an integer parameter
progress_updated = pyqtSignal(int)
def __init__(self):
super(UI, self).__init__()
self.initialize_ui()
def initialize_ui(self):
# Load the UI file and define widgets
uic.loadUi("loadui.ui", self)
self.setup_widgets()
self.connect_signals()
self.show()
def setup_widgets(self):
# Define the widgets used in the UI
self.identifying_status = self.findChild(QLabel, "label_identifying_status")
self.button_browse = self.findChild(QPushButton, "pushButton_browse")
self.button_samplebrowse = self.findChild(
QPushButton, "pushButton_browse_samplefile"
)
self.button_featureList = self.findChild(
QPushButton, "pushButton_browse_featureList"
)
self.button_run = self.findChild(QPushButton, "pushButton_accuracytest")
self.button_identify = self.findChild(QPushButton, "pushButton_identify")
self.lcdnumber_wrong = self.findChild(QLCDNumber, "lcdNumber_wrong_detection")
self.lcdnumber_step = self.findChild(QLCDNumber, "lcdNumber_checks")
self.lcdnumber_accuracy = self.findChild(QLCDNumber, "lcdNumber_accuracy")
self.flist = self.findChild(QListWidget, "listWidget_files")
self.sfilelist = self.findChild(QListWidget, "listWidget_samplefile")
self.ffilelist = self.findChild(QListWidget, "listWidget_featureList")
self.identified_result = self.findChild(
QListWidget, "listWidget_identifiedlist"
)
self.suspicious_data_lst = self.findChild(
QListWidget, "listWidget_suspiciousdata"
)
self.summary = self.findChild(QListWidget, "listWidget_summary")
self.progressbar = self.findChild(QProgressBar, "loading")
self.do_save_to_file = self.findChild(QCheckBox, "checkBox_SaveToFile")
def connect_signals(self):
# Connect widget signals to corresponding slots
self.button_browse.clicked.connect(self.browse_trainingfiles)
self.button_samplebrowse.clicked.connect(self.browse_samplefile)
self.button_featureList.clicked.connect(self.browse_featureList)
self.button_run.clicked.connect(self.accuracytest_serial)
self.button_identify.clicked.connect(self.sample_identify)
def browse_trainingfiles(self):
self.flist.clear()
acc_check.fnames = []
fopath = os.path.join(os.path.dirname(__file__), "data")
filepaths, _ = QFileDialog.getOpenFileNames(
self, "Open CSV", fopath, "CSV Files (*.csv)"
)
for path in filepaths:
fname = os.path.basename(path)
acc_check.fpaths[(fname.replace(".csv", ""))] = path
self.flist.addItem(fname)
acc_check.fnames.append(fname.replace(".csv", ""))
acc_check.number_of_files = self.flist.count()
def browse_samplefile(self):
self.sfilelist.clear()
fopath = os.path.join(os.path.dirname(__file__), "data")
filepath, _ = QFileDialog.getOpenFileName(
self, "Open CSV", fopath, "CSV Files (*.csv)"
)
acc_check.sfpath = filepath
fname = os.path.basename(filepath)
if fname != "":
self.sfilelist.addItem(fname)
acc_check.sfname = fname.replace(".csv", "")
else:
acc_check.error = True
UIWindow.identifying_status.setText("No sample file is chosen.")
def browse_featureList(self):
self.ffilelist.clear()
fopath = os.path.join(os.path.dirname(__file__), "data")
filepath, _ = QFileDialog.getOpenFileName(
self, "Open CSV", fopath, "CSV Files (*.csv)"
)
acc_check.ffpath = filepath
fname = os.path.basename(filepath)
if fname != "":
self.ffilelist.addItem(fname)
acc_check.ffname = fname.replace(".csv", "")
else:
acc_check.error = True
UIWindow.identifying_status.setText("No sample file is chosen.")
def accuracytest_parallel(self):
self.suspicious_data_lst.clear()
acc_check.reset()
acc_check.load_training_files()
if not acc_check.error:
self.worker = Worker()
self.worker.progress_updated.connect(self.update_progress)
self.worker.finished.connect(
self.on_worker_finished
) # Connect to the finished signal
self.worker.start()
def accuracytest_serial(self):
self.suspicious_data_lst.clear()
acc_check.reset()
acc_check.load_training_files()
if not acc_check.error:
self.summary.clear()
self.summary_update()
self.identifying_status.setText("")
self.progressbar.setMaximum(len(acc_check.X))
self.lcdnumber_accuracy.display(round(0.0, 2))
self.progressbar.setEnabled(True)
while acc_check.checks <= len(acc_check.X):
call_accuracy_test(1)
QApplication.processEvents() # Prevents GUI freezes in loops
self.lcdnumber_wrong.display(acc_check.wrong_detection)
self.lcdnumber_step.display(acc_check.checks)
self.progressbar.setValue(acc_check.checks)
acc_check.checks += 1
self.update_lcd()
self.progressbar.setEnabled(False)
self.progressbar.setValue(0)
acc_check.error = False
def on_worker_finished(self):
# Code to execute after the worker thread has finished
self.update_lcd()
self.progressbar.setEnabled(False)
self.progressbar.setValue(0)
acc_check.error = False
# ...
def update_progress(self, value):
self.progressbar.setValue(value)
def sample_identify(self):
self.identifying_status.setText("Identifying process started ...")
self.suspicious_data_lst.clear()
self.identified_result.clear()
self.summary.clear()
QApplication.processEvents() # Prevents GUI freezes in loops
acc_check.reset()
acc_check.load_training_files()
if not acc_check.error:
result = sample_identifer()
if not acc_check.error:
self.summary_update()
# Writing to file
if self.do_save_to_file.isChecked():
with open("result.txt", "w") as file1:
for i, item in enumerate(result):
self.identified_result.addItem(f"Sample #{i + 1} -> {item} ")
# Writing data to a file
items = [
self.flist.item(x).text().replace(".csv", "")
for x in range(self.flist.count())
]
file1.write(f"{items.index(item)}\n")
# file1.write(f'sample #{i + 1} -> {item}\n')
else:
for i, item in enumerate(result):
self.identified_result.addItem(f"Sample #{i + 1} -> {item} ")
self.identifying_status.setText("Done!")
acc_check.error = False
def update_lcd(self):
acc_check.accuracy_calc()
self.lcdnumber_accuracy.display(round(acc_check.calculation_accuracy, 2))
def summary_update(self):
self.summary.addItem(f"Total samples: {len(acc_check.X)}")
self.summary.addItem(
f"Detected features: {acc_check.cols_length[acc_check.fnames[0]]}"
)
class AccuracyCheck:
def __init__(self):
# Training-related attributes
self.fnames = []
self.fpaths = {}
self.sfpath = ""
self.ffpath = ""
self.relative_paths = {}
self.number_of_samples = []
self.number_of_files = 0
self.files_data = {}
self.row_length = {}
self.cols_length = {}
self.X = []
self.Y = []
# Prediction-related attributes
self.sfname = ""
# Accuracy calculation attributes
self.error = False
self.wrong_detection = Value("i", 0) # 'i' indicates an integer type
self.checks = 1
self.calculation_accuracy = 0
self.suspecious_data = []
def reset(self):
"""Reset the accuracy calculation attributes."""
self.wrong_detection = 0
self.checks = 1
self.calculation_accuracy = 0
self.files_data = {}
self.row_length = {}
self.cols_length = {}
self.X = []
self.Y = []
self.suspecious_data = []
def accuracy_calc(self):
"""Calculate the accuracy percentage."""
self.calculation_accuracy = 100 - self.wrong_detection / self.checks * 100
def load_training_files(self):
"""Load training files and preprocess the data."""
if len(self.fnames) <= 1:
self.error = True
UIWindow.identifying_status.setText(
"At least two training files are required."
)
return
for fname in self.fnames:
this_dir = os.path.dirname(__file__)
self.relative_paths[fname] = relpath(self.fpaths[fname], this_dir)
train_data = pd.read_csv(self.fpaths[fname], header=None)
self.cols_length[fname], self.row_length[fname] = train_data.shape
self.number_of_samples.append(self.row_length[fname] - 2)
self.files_data[fname] = train_data
self.load_samples(fname)
def load_samples(self, fname):
"""Load samples from the given file name."""
num_features = self.cols_length[fname]
num_samples = self.row_length[fname]
for i in range(1, num_samples):
x = [self.files_data[fname].iloc[j, i] for j in range(num_features)]
self.X.append(x)
self.Y.append(fname)
def suspicious_data_tracker(self):
"""Track and manage suspicious data."""
group = self.Y[self.checks - 1]
idx = (
self.checks
if self.row_length[group] >= self.checks
else self.checks - self.row_length[group]
)
self.suspecious_data.append([group, idx])
UIWindow.suspicious_data_lst.addItem(f"{group}\t-> {idx}")
def accuracy_sequential_checker(fnames):
X = copy.deepcopy(acc_check.X)
Y = copy.deepcopy(acc_check.Y)
# Pick the test sample
picked_for_test_X = [X.pop(acc_check.checks - 1)]
picked_for_test_Y = [Y.pop(acc_check.checks - 1)]
return X, Y, picked_for_test_X, picked_for_test_Y
def read_unknown_samples():
unknown_sample = pd.read_csv(acc_check.sfpath, header=None)
unX = unknown_sample.T.values.tolist()
num_features = unknown_sample.shape[0]
return unX, num_features
def handle_feature_importance(clf):
feature_importance = clf.feature_importances_
feature_name = pd.read_csv(acc_check.ffpath, header=None).iloc[:, 0].tolist()
df = pd.DataFrame({"Spectrum": feature_name, "Gini Value": feature_importance})
df.to_csv("data/gini_values.csv", index=False)
plt.barh(df["Spectrum"], df["Gini Value"], height=1)
plt.show()
def split_data():
return train_test_split(acc_check.X, acc_check.Y, test_size=0.33, random_state=4)
def train_classifier(X_train, y_train):
clf = RandomForestClassifier()
clf.fit(X_train, y_train)
return clf
def predict_and_evaluate(clf, X_test, y_test):
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
return y_pred, accuracy
def train_and_predict():
X_train, X_test, y_train, y_test = split_data()
clf = train_classifier(X_train, y_train)
y_pred, accuracy = predict_and_evaluate(clf, X_test, y_test)
return clf, y_pred, y_test
def plot_confusion_matrix(y_test, y_pred):
cm = confusion_matrix(y_test, y_pred)
ConfusionMatrixDisplay(confusion_matrix=cm).plot()
plt.show()
skplt.metrics.plot_confusion_matrix(
y_test, y_pred, normalize=False, title="Confusion Matrix"
)
plt.savefig("data/raw/results/confusion_matrix.png")
print(cm)
def train_model():
clf = RandomForestClassifier()
clf.fit(acc_check.X, acc_check.Y)
return clf
def validate_sample(unX, num_features):
if num_features != acc_check.cols_length[acc_check.fnames[0]]:
UIWindow.identifying_status.setText(
"Sample and training set doesn't match! ..."
)
acc_check.error = True
return False
return True
def sample_identifer():
plt.clf()
result = []
if acc_check.sfname == "":
acc_check.error = True
UIWindow.identifying_status.setText("No sample file is chosen.")
return result
unX, num_features = read_unknown_samples()
if not validate_sample(unX, num_features):
return result
clf = train_model()
handle_feature_importance(clf)
clf, y_pred, y_test = train_and_predict()
plot_confusion_matrix(y_test, y_pred)
result = clf.predict(unX)
return result
def compare_parallel(lst1, lst2):
print(lst1[0], " = ", lst2[0])
if lst1[0] != lst2[0]:
with acc_check.wrong_detection.get_lock(): # Acquire the lock before updating
acc_check.wrong_detection.value += 1
acc_check.suspicious_data_tracker()
def compare_serial(lst1, lst2):
if lst1[0] != lst2[0]:
acc_check.wrong_detection += 1
acc_check.suspicious_data_tracker()
def call_accuracy_test(check):
preprocessed_output = accuracy_sequential_checker(acc_check.fnames)
# Define what needs to be parallelized here
return machine_learning_accuracytest_serial(preprocessed_output) # Adjust as needed
def machine_learning_accuracytest_parallel(preprocessed_output):
features, categories, samples, samples_cats = preprocessed_output
clf.fit(features, categories)
result = clf.predict(samples)
compare_parallel(result, samples_cats)
def machine_learning_accuracytest_serial(preprocessed_output):
features, categories, samples, samples_cats = preprocessed_output
clf.fit(features, categories)
result = clf.predict(samples)
compare_serial(result, samples_cats)
# Initialize the GUI App
acc_check = AccuracyCheck()
# Preprocess the accuracy_sequential_checker if possible
# Initialize the classifier outside the function if it's being reused
clf = RandomForestClassifier()
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()