/
tabwidget.py
192 lines (161 loc) · 7.43 KB
/
tabwidget.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
import pandas as pd, os
from selection_dialog import SelectionFeaturesDialog
from tableview import TableView
from algorithm_groupboxes import KNearestNeighborsGroupBox, DecisionTreeGroupBox, RandomForestGroupBox, LinearRegressionGroupBox, LogisticRegressionGroupBox, LinearSVMGroupBox, MLPGroupBox
from progress_dialog import ProgressDialog
from PyQt5.QtWidgets import QApplication, QTabWidget, QVBoxLayout, QGroupBox, QFileDialog, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QUrl
class MainTabWidget(QTabWidget):
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
self.setTabsClosable(True)
self.setCurrentIndex(-1)
self.tabBar().setMovable(True)
self.tabCloseRequested.connect(self.closeTab)
self.setStyleSheet("""
QTabWidget::pane {
border: 1px solid black;
background: white;
}
QTabBar::tab {
height: 50px;
width: 300px;
font-size: 14px;
font-weight: bold;
color: black;
}
QTabBar::tab:selected {
background-color: rgba(49,49,49, 0.5);
color:white;
}
""")
def newTab(self, currentIndex):
# File Dialog (uses csv directory)
directory = os.path.dirname(os.path.realpath(__file__)) + "\csv"
if not os.path.exists(directory):
os.makedirs(directory)
QApplication.beep()
# Path to Selected CSV File
path = QFileDialog.getOpenFileName(self, "Open CSV File", directory, "Comma-Separated Values File (*.csv)")[0]
if not path:
return
filename = QUrl.fromLocalFile(path).fileName()
# Dialog for user to select features and label
progressDialog = ProgressDialog(None, None, 0, 100, self)
progressDialog.setWindowTitle("Reading " + filename + "...")
progressDialog.setValue(0)
progressDialog.show()
QApplication.processEvents()
selectionFeaturesDialog = SelectionFeaturesDialog(path)
progressDialog.setWindowTitle("Done")
progressDialog.setValue(100)
progressDialog.close()
QApplication.processEvents()
QApplication.beep()
dialogCode = selectionFeaturesDialog.exec_()
#If Dialog was cancelled, then avoid opening a new tab
if dialogCode == 0:
return
features = selectionFeaturesDialog.getFeatures()
label = selectionFeaturesDialog.getLabel()
tabGroupBox = AlgorithmGroupBox(path, features, label)
pathToNewCSVIcon = os.path.dirname(os.path.realpath(__file__)) + "\\assets" + "\\csv_icon.png"
self.addTab(tabGroupBox, QIcon(pathToNewCSVIcon), filename)
self.setCurrentWidget(tabGroupBox)
def closeTab(self, currentIndex):
tabWidgetToClose = self.widget(currentIndex)
tabWidgetToClose.deleteLater()
self.removeTab(currentIndex)
# Used for creating a new tab, contains the table of the CSV data and the machine learning algorithms
class AlgorithmGroupBox(QGroupBox):
def __init__(self, path, features, label):
super().__init__()
self.path = path
self.features = features
self.label = label
self.df = pd.read_csv(path, sep=',')
self.checkCategoricalFeatures()
self.X = self.df[self.features]
self.y = self.df[self.label[0]]
self.columns = self.features + self.label
self.df = self.df[self.columns]
self.data = self.df.to_numpy().tolist()
self.initUI()
def initUI(self):
tabLayout = QVBoxLayout()
self.tabTableView = TableView(self.data, self.columns)
# Tab Widget
tabWidget = TabWidget(self.X, self.y)
tabLayout.addWidget(self.tabTableView)
tabLayout.addWidget(tabWidget)
self.setLayout(tabLayout)
# Checking for categorical features and one-hot encoding them
def checkCategoricalFeatures(self):
featureDataframe = self.df[self.features]
categorical_features = featureDataframe.select_dtypes( exclude=['int', 'float', 'int32', 'float32', 'int64', 'float64']).columns
# Apply One-hot encoding to the selected categorical features
if len(categorical_features) != 0:
categorical_df = featureDataframe[categorical_features]
new_numerical_dataframe = pd.get_dummies(categorical_df)
self.df.drop(categorical_features, axis=1, inplace=True)
self.df = pd.concat([self.df, new_numerical_dataframe], axis=1)
# Remove the categorical features in the features array. Then adding the new one-hot encoding features to the old list
for feature in categorical_features:
self.features.remove(feature)
self.features = self.features + list(new_numerical_dataframe.columns)
messageBox = QMessageBox()
messageBox.setWindowTitle("Detected Categorical Features!")
string = ", ".join(categorical_features)
messageBox.setText(string + " are categorical features. They have been one-hot encoded.");
messageBox.setStyleSheet("""
QMessageBox {
background-color: rgb(255, 229, 204);
color: #fffff8;
}
""")
messageBox.exec()
# An Inner QTabWidget that has different tabs for each machine learning algorithm
class TabWidget(QTabWidget):
def __init__(self, X, y):
super().__init__()
self.X = X
self.y = y
self.initUI()
def initUI(self):
self.setTabsClosable(False)
self.setCurrentIndex(-1)
self.setStyleSheet("""
QTabBar::tab {
height: 50px;
width: 175px;
font-size: 12px;
}
QTabWidget::tab-bar {
alignment: center;
}
""")
self.createTabs()
def createTabs(self):
self.algorithms = ["K-Nearest Neighbors", "Decision Tree", "Random Forest", "Linear Regression", "Logistic Regression", "Linear SVC", "Multilayer Perceptron"]
for algorithm in self.algorithms:
pathToIcon = os.path.dirname(os.path.realpath(__file__)) + "\\assets" + "\\" + algorithm + ".png"
groupBox = None
if algorithm == "K-Nearest Neighbors":
groupBox = KNearestNeighborsGroupBox(self.X, self.y)
elif algorithm == "Decision Tree":
groupBox = DecisionTreeGroupBox(self.X, self.y)
elif algorithm == "Random Forest":
groupBox = RandomForestGroupBox(self.X, self.y)
elif algorithm == "Linear Regression":
groupBox = LinearRegressionGroupBox(self.X, self.y)
elif algorithm == "Logistic Regression":
groupBox = LogisticRegressionGroupBox(self.X, self.y)
elif algorithm == "Linear SVC":
groupBox = LinearSVMGroupBox(self.X, self.y)
elif algorithm == "Multilayer Perceptron":
groupBox = MLPGroupBox(self.X, self.y)
# groupBox = ComputeGroupBox(algorithm, self.X, self.y)
self.addTab(groupBox, QIcon(pathToIcon), algorithm)