Skip to content
Snippets Groups Projects
DisplayWindow.py 7.41 KiB
Newer Older
import sys
from PyQt6.QtCore import Qt
from data_processing import DataPreprocessor as dp
from modeling import Model
import RegressionModel
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget,
                             QVBoxLayout, QHBoxLayout, QGridLayout,
                             QLabel, QPushButton, QSlider, QDateTimeEdit,
                             QLineEdit, QComboBox, QDateEdit, QTabWidget, QCheckBox)
from PyQt6.QtGui import QPalette, QColor, QIcon
from sklearn.model_selection import train_test_split
import joblib
class MyTabs(QMainWindow):
    def __init__(self):
        super().__init__()

        # Create your custom pages (e.g., TradeView and OrderView)
        trade_view = MainWindow()
        order_view = Advanced()

        # Create a tab widget
        tab_widget = QTabWidget(self)
        tab_widget.addTab(trade_view, "Main")
        tab_widget.addTab(order_view, "Advanced")

        # Set the central widget
        self.setCentralWidget(tab_widget)


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowIcon(QIcon("icon.ico"))
        self.setMinimumSize(720, 640)

        # Create a central widget
        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)

        # Create a grid layout and set it to the central widget
        grid_layout = QGridLayout(central_widget)

        # Create labels and input fields
        self.labels_text = ['Cut', 'Colour', 'Clarity', 'Carat', 'Depth', 'Table', 'X', 'Y', 'Z']
        self.input_fields = [QComboBox(self), QComboBox(self), QComboBox(self), QLineEdit(self),
                             QLineEdit(self), QLineEdit(self), QLineEdit(self), QLineEdit(self),
                             QLineEdit(self)]
        self.input_fields[0].addItems(cut)
        self.input_fields[1].addItems(colors)
        self.input_fields[2].addItems(clarity)
        # add a button to capture all the data
        self.calculate_button = QPushButton('Calculate Price', self)
        self.calculate_button.clicked.connect(self.update_values)

        # Add labels and input fields to the grid layout
        for i in range(len(self.labels_text)):
            label = QLabel(self.labels_text[i], self)
            grid_layout.addWidget(label, 0, i)
            grid_layout.addWidget(self.input_fields[i], 1, i)

        # Create a plot widget
        graphWidget = pg.PlotWidget(self)
        graphWidget.setBackground('w')
        graphWidget.plot(diamonds['carat'], diamonds['price'])

        # Add the plot widget to the grid layout
        grid_layout.addWidget(self.calculate_button, 2, 0, 1, 2)
        grid_layout.addWidget(graphWidget, 3, 0, 1, 9)
        grid_layout.addWidget(QLabel("Predicted Price: ", self), 4, 0, 1, 1)
        self.display_price = QLabel(str(RegressionModel.price))
        grid_layout.addWidget(self.display_price, 4, 2, 1, 1)
        # grid_layout.addWidget(QLabel(str(RegressionModel.price), self), 4, 2, 1, 1)
        # Remember to add the predicted price

        self.setWindowTitle('Assistance Systems')
        self.show()

    def update_values(self):
        menu_items = {3: 'carat',
                        0: 'cut',
                        1: 'color',
                        2: 'clarity',
                       4: 'depth', 5: 'table',
                      6: 'x', 7: 'y', 8: 'z'}
        for key, value in menu_items.items():
            if key < 3:
                RegressionModel.GUI_selections[value] = self.input_fields[key].currentText()
            else:
                RegressionModel.GUI_selections[value] = self.input_fields[key].text()
            # print(RegressionModel.GUI_selections)
        
        # convert GUI_selections to dataframe
        X_test = pd.DataFrame(RegressionModel.GUI_selections, index=[0])
        
        X_test['cut'] = X_test['cut'].map({'Ideal': 0, 'Premium': 1, 'Very Good': 2, 'Good': 3, 'Fair': 4})
        X_test['color'] = X_test['color'].map({'E': 0, 'I': 1, 'J': 2, 'H': 3, 'F': 4, 'G': 5, 'D': 6})
        X_test['clarity'] = X_test['clarity'].map({'SI2': 0, 'SI1': 1, 'VS1': 2, 'VS2': 3, 'VVS2': 4, 'VVS1': 5, 'IF': 7, 'I1': 6})

        price = model.predict_price(X_test)
        # price = model.predict(X_test)[0]
        self.display_price.setText(str(price))

        



class Advanced(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowIcon(QIcon("icon.ico"))
        self.setMinimumSize(720, 640)

        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)
        # Create a grid layout and set it to the central widget
        grid_layout = QGridLayout(central_widget)

        self.graph_selector_X = QComboBox(self)
        self.graph_selector_Y = QComboBox(self)
        self.regression_model = QComboBox(self)
        self.check_labels = ['Cut', 'Colour', 'Clarity', 'Carat', 'Depth', 'Table', 'X', 'Y', 'Z']
        self.graph_selector_X.addItems((self.check_labels + ['price']))
        self.graph_selector_Y.addItems((self.check_labels + ['price']))
Michael Mutote's avatar
Michael Mutote committed
        self.regression_model.addItems(["Linear Regression", "XG Boost", "XGBRegressor", "Neural Network"])
        self.checkboxes = []


        grid_layout.addWidget(QLabel("select features to include in Modelling"), 0, 0, 1, 0)
        # grid_layout.addWidget(self.graph_selector_X, 1, 2)
        # grid_layout.addWidget(self.graph_selector_Y, 1, 3)
        grid_layout.addWidget(self.regression_model, 2, 1)
        # grid_layout.addWidget(QLabel("X-PLOT", self), 0, 2)
        # grid_layout.addWidget(QLabel("Y-PLOT", self), 0, 3)
        grid_layout.addWidget(QLabel("Regression Model : ", self), 2, 0,1,1)


        for i, label in enumerate(self.check_labels):
            checkbox = QCheckBox(label, self)
            checkbox.setChecked(True)
            checkbox.stateChanged.connect(self.handle_checkbox_state)  # Connect signal
            grid_layout.addWidget(checkbox, 1, i)
            self.checkboxes.append(checkbox)  # Store checkboxes in the list

    def handle_checkbox_state(self):
Michael Mutote's avatar
Michael Mutote committed
        RegressionModel.Advanced_selections = []
        for i, checkbox in enumerate(self.checkboxes):
            state = checkbox.checkState()
Michael Mutote's avatar
Michael Mutote committed
            if state == Qt.CheckState.Unchecked:
                RegressionModel.Advanced_selections.append(checkbox.text())




diamonds = pd.read_csv('diamonds.csv')
diamonds.dropna(inplace=True)
cut = list(diamonds["cut"].unique())
colors = list(diamonds["color"].unique())
clarity = list(diamonds["clarity"].unique())
# diamonds_numerical = diamonds.copy()
# cut_mapping = {cut: i for i, cut in enumerate(diamonds['cut'].unique())}
# color_mapping = {color: i for i, color in enumerate(diamonds['color'].unique())}
# clarity_mapping = {clarity: i for i, clarity in enumerate(diamonds['clarity'].unique())}
# diamonds_numerical['cut'] = diamonds['cut'].map(cut_mapping)
# diamonds_numerical['color'] = diamonds['color'].map(color_mapping)
# diamonds_numerical['clarity'] = diamonds['clarity'].map(clarity_mapping)
# X = diamonds_numerical.drop('price', axis = 1)
# y = diamonds_numerical['price']
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# model = Model(X_train, X_test, y_train, y_test,'RF')
# model.train()
# joblib.dump(model, 'RF.joblib')
model = joblib.load('RF.joblib')
print(type(model))
price = 0





app = QApplication(sys.argv)

window = MyTabs()
window.show()

app.exec()