from a_datasetCreator import controlledDatasetRandomization
from b_trainingProgram import trainModel
from c_testModelAccuracy import getThePrediction
from PyQt5 import QtCore, QtGui, QtWidgets
import os

import ctypes
myappid = u"mycompany.myproduct.subproduct.version"
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

class Ui_MainWindow(QtWidgets.QMainWindow):

    os.system("cls")

    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(970, 550)
        MainWindow.setMinimumSize(QtCore.QSize(970, 550))
        MainWindow.setMaximumSize(QtCore.QSize(970, 550))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("data/icons/simulation.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 50, 301, 361))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.groupBox.setFont(font)
        self.groupBox.setObjectName("groupBox")
        self.label_2 = QtWidgets.QLabel(self.groupBox)
        self.label_2.setGeometry(QtCore.QRect(20, 30, 121, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(40, 60, 71, 21))
        self.label_3.setObjectName("label_3")
        self.tempLow = QtWidgets.QLineEdit(self.groupBox)
        self.tempLow.setGeometry(QtCore.QRect(110, 61, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.tempLow.setFont(font)
        self.tempLow.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.tempLow.setObjectName("tempLow")
        self.label_4 = QtWidgets.QLabel(self.groupBox)
        self.label_4.setGeometry(QtCore.QRect(170, 60, 16, 21))
        self.label_4.setObjectName("label_4")
        self.tempHigh = QtWidgets.QLineEdit(self.groupBox)
        self.tempHigh.setGeometry(QtCore.QRect(190, 61, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.tempHigh.setFont(font)
        self.tempHigh.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.tempHigh.setObjectName("tempHigh")
        self.label_5 = QtWidgets.QLabel(self.groupBox)
        self.label_5.setGeometry(QtCore.QRect(250, 60, 16, 21))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(self.groupBox)
        self.label_6.setGeometry(QtCore.QRect(40, 90, 81, 21))
        self.label_6.setObjectName("label_6")
        self.shortestCode = QtWidgets.QLineEdit(self.groupBox)
        self.shortestCode.setGeometry(QtCore.QRect(120, 90, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.shortestCode.setFont(font)
        self.shortestCode.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.shortestCode.setObjectName("shortestCode")
        self.longestCode = QtWidgets.QLineEdit(self.groupBox)
        self.longestCode.setGeometry(QtCore.QRect(200, 90, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.longestCode.setFont(font)
        self.longestCode.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.longestCode.setObjectName("longestCode")
        self.label_7 = QtWidgets.QLabel(self.groupBox)
        self.label_7.setGeometry(QtCore.QRect(180, 90, 16, 21))
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(self.groupBox)
        self.label_8.setGeometry(QtCore.QRect(260, 90, 16, 21))
        self.label_8.setObjectName("label_8")
        self.label_9 = QtWidgets.QLabel(self.groupBox)
        self.label_9.setGeometry(QtCore.QRect(40, 120, 81, 21))
        self.label_9.setObjectName("label_9")
        self.minimumUsedTime = QtWidgets.QLineEdit(self.groupBox)
        self.minimumUsedTime.setGeometry(QtCore.QRect(120, 120, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.minimumUsedTime.setFont(font)
        self.minimumUsedTime.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.minimumUsedTime.setObjectName("minimumUsedTime")
        self.label_10 = QtWidgets.QLabel(self.groupBox)
        self.label_10.setGeometry(QtCore.QRect(180, 120, 16, 21))
        self.label_10.setObjectName("label_10")
        self.maximumUsedTime = QtWidgets.QLineEdit(self.groupBox)
        self.maximumUsedTime.setGeometry(QtCore.QRect(200, 120, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.maximumUsedTime.setFont(font)
        self.maximumUsedTime.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.maximumUsedTime.setObjectName("maximumUsedTime")
        self.label_11 = QtWidgets.QLabel(self.groupBox)
        self.label_11.setGeometry(QtCore.QRect(260, 120, 16, 21))
        self.label_11.setObjectName("label_11")
        self.run = QtWidgets.QPushButton(self.groupBox)
        self.run.setGeometry(QtCore.QRect(20, 320, 71, 23))
        self.run.setObjectName("run")
        self.label_12 = QtWidgets.QLabel(self.groupBox)
        self.label_12.setGeometry(QtCore.QRect(20, 160, 121, 16))
        self.label_12.setObjectName("label_12")
        self.shortestCode_error = QtWidgets.QLineEdit(self.groupBox)
        self.shortestCode_error.setGeometry(QtCore.QRect(120, 219, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.shortestCode_error.setFont(font)
        self.shortestCode_error.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.shortestCode_error.setObjectName("shortestCode_error")
        self.label_13 = QtWidgets.QLabel(self.groupBox)
        self.label_13.setGeometry(QtCore.QRect(180, 219, 16, 21))
        self.label_13.setObjectName("label_13")
        self.minimumUsedTime_error = QtWidgets.QLineEdit(self.groupBox)
        self.minimumUsedTime_error.setGeometry(QtCore.QRect(120, 249, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.minimumUsedTime_error.setFont(font)
        self.minimumUsedTime_error.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.minimumUsedTime_error.setObjectName("minimumUsedTime_error")
        self.maximumUsedTime_error = QtWidgets.QLineEdit(self.groupBox)
        self.maximumUsedTime_error.setGeometry(QtCore.QRect(200, 249, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.maximumUsedTime_error.setFont(font)
        self.maximumUsedTime_error.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.maximumUsedTime_error.setObjectName("maximumUsedTime_error")
        self.label_14 = QtWidgets.QLabel(self.groupBox)
        self.label_14.setGeometry(QtCore.QRect(180, 249, 16, 21))
        self.label_14.setObjectName("label_14")
        self.label_15 = QtWidgets.QLabel(self.groupBox)
        self.label_15.setGeometry(QtCore.QRect(40, 219, 81, 21))
        self.label_15.setObjectName("label_15")
        self.longestCode_error = QtWidgets.QLineEdit(self.groupBox)
        self.longestCode_error.setGeometry(QtCore.QRect(200, 219, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.longestCode_error.setFont(font)
        self.longestCode_error.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.longestCode_error.setObjectName("longestCode_error")
        self.label_16 = QtWidgets.QLabel(self.groupBox)
        self.label_16.setGeometry(QtCore.QRect(40, 189, 71, 21))
        self.label_16.setObjectName("label_16")
        self.label_17 = QtWidgets.QLabel(self.groupBox)
        self.label_17.setGeometry(QtCore.QRect(260, 219, 16, 21))
        self.label_17.setObjectName("label_17")
        self.label_18 = QtWidgets.QLabel(self.groupBox)
        self.label_18.setGeometry(QtCore.QRect(170, 189, 16, 21))
        self.label_18.setObjectName("label_18")
        self.label_19 = QtWidgets.QLabel(self.groupBox)
        self.label_19.setGeometry(QtCore.QRect(40, 249, 81, 21))
        self.label_19.setObjectName("label_19")
        self.tempLow_error = QtWidgets.QLineEdit(self.groupBox)
        self.tempLow_error.setGeometry(QtCore.QRect(110, 190, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.tempLow_error.setFont(font)
        self.tempLow_error.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.tempLow_error.setObjectName("tempLow_error")
        self.label_20 = QtWidgets.QLabel(self.groupBox)
        self.label_20.setGeometry(QtCore.QRect(260, 249, 16, 21))
        self.label_20.setObjectName("label_20")
        self.label_21 = QtWidgets.QLabel(self.groupBox)
        self.label_21.setGeometry(QtCore.QRect(250, 189, 16, 21))
        self.label_21.setObjectName("label_21")
        self.tempHigh_error = QtWidgets.QLineEdit(self.groupBox)
        self.tempHigh_error.setGeometry(QtCore.QRect(190, 190, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.tempHigh_error.setFont(font)
        self.tempHigh_error.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.tempHigh_error.setObjectName("tempHigh_error")
        self.label_22 = QtWidgets.QLabel(self.groupBox)
        self.label_22.setGeometry(QtCore.QRect(40, 280, 61, 21))
        self.label_22.setObjectName("label_22")
        self.acceptableRange = QtWidgets.QLineEdit(self.groupBox)
        self.acceptableRange.setGeometry(QtCore.QRect(100, 280, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.acceptableRange.setFont(font)
        self.acceptableRange.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.acceptableRange.setObjectName("acceptableRange")
        self.label_23 = QtWidgets.QLabel(self.groupBox)
        self.label_23.setGeometry(QtCore.QRect(160, 280, 71, 21))
        self.label_23.setObjectName("label_23")
        self.train = QtWidgets.QPushButton(self.groupBox)
        self.train.setGeometry(QtCore.QRect(100, 320, 101, 23))
        self.train.setObjectName("train")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 10, 451, 21))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 420, 301, 121))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.groupBox_2.setFont(font)
        self.groupBox_2.setObjectName("groupBox_2")
        self.test = QtWidgets.QPushButton(self.groupBox_2)
        self.test.setGeometry(QtCore.QRect(200, 50, 75, 23))
        self.test.setObjectName("test")
        self.codeIn = QtWidgets.QLineEdit(self.groupBox_2)
        self.codeIn.setGeometry(QtCore.QRect(100, 60, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.codeIn.setFont(font)
        self.codeIn.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.codeIn.setObjectName("codeIn")
        self.UsedTime = QtWidgets.QLineEdit(self.groupBox_2)
        self.UsedTime.setGeometry(QtCore.QRect(100, 90, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.UsedTime.setFont(font)
        self.UsedTime.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.UsedTime.setObjectName("UsedTime")
        self.label_64 = QtWidgets.QLabel(self.groupBox_2)
        self.label_64.setGeometry(QtCore.QRect(20, 60, 81, 21))
        self.label_64.setObjectName("label_64")
        self.label_65 = QtWidgets.QLabel(self.groupBox_2)
        self.label_65.setGeometry(QtCore.QRect(20, 29, 71, 21))
        self.label_65.setObjectName("label_65")
        self.label_66 = QtWidgets.QLabel(self.groupBox_2)
        self.label_66.setGeometry(QtCore.QRect(160, 60, 16, 21))
        self.label_66.setObjectName("label_66")
        self.label_68 = QtWidgets.QLabel(self.groupBox_2)
        self.label_68.setGeometry(QtCore.QRect(20, 90, 81, 21))
        self.label_68.setObjectName("label_68")
        self.tempIn = QtWidgets.QLineEdit(self.groupBox_2)
        self.tempIn.setGeometry(QtCore.QRect(90, 30, 51, 20))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.tempIn.setFont(font)
        self.tempIn.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.tempIn.setObjectName("tempIn")
        self.label_69 = QtWidgets.QLabel(self.groupBox_2)
        self.label_69.setGeometry(QtCore.QRect(160, 90, 16, 21))
        self.label_69.setObjectName("label_69")
        self.label_70 = QtWidgets.QLabel(self.groupBox_2)
        self.label_70.setGeometry(QtCore.QRect(150, 29, 16, 21))
        self.label_70.setObjectName("label_70")
        self.label_24 = QtWidgets.QLabel(self.centralwidget)
        self.label_24.setGeometry(QtCore.QRect(320, 60, 640, 480))
        self.label_24.setText("")
        self.label_24.setPixmap(QtGui.QPixmap("data/initial.png"))
        self.label_24.setObjectName("label_24")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "模擬データで生産した製品の誤差を予想する"))
        self.groupBox.setTitle(_translate("MainWindow", "ステップ1：データセットの作成＋人工知能のトレーニング"))
        self.label_2.setText(_translate("MainWindow", "作成するデータの範囲"))
        self.label_3.setText(_translate("MainWindow", "回りの温度："))
        self.tempLow.setText(_translate("MainWindow", "-20.00"))
        self.label_4.setText(_translate("MainWindow", "~"))
        self.tempHigh.setText(_translate("MainWindow", "40.00"))
        self.label_5.setText(_translate("MainWindow", "°C"))
        self.label_6.setText(_translate("MainWindow", "コマンドの長さ："))
        self.shortestCode.setText(_translate("MainWindow", "10"))
        self.longestCode.setText(_translate("MainWindow", "1000"))
        self.label_7.setText(_translate("MainWindow", "~"))
        self.label_8.setText(_translate("MainWindow", "行"))
        self.label_9.setText(_translate("MainWindow", "使われた回数："))
        self.minimumUsedTime.setText(_translate("MainWindow", "0"))
        self.label_10.setText(_translate("MainWindow", "~"))
        self.maximumUsedTime.setText(_translate("MainWindow", "100"))
        self.label_11.setText(_translate("MainWindow", "回"))
        self.run.setText(_translate("MainWindow", "１．作成"))
        self.label_12.setText(_translate("MainWindow", "誤差が発生する範囲"))
        self.shortestCode_error.setText(_translate("MainWindow", "900"))
        self.label_13.setText(_translate("MainWindow", "~"))
        self.minimumUsedTime_error.setText(_translate("MainWindow", "75"))
        self.maximumUsedTime_error.setText(_translate("MainWindow", "100"))
        self.label_14.setText(_translate("MainWindow", "~"))
        self.label_15.setText(_translate("MainWindow", "コマンドの長さ："))
        self.longestCode_error.setText(_translate("MainWindow", "1000"))
        self.label_16.setText(_translate("MainWindow", "回りの温度："))
        self.label_17.setText(_translate("MainWindow", "行"))
        self.label_18.setText(_translate("MainWindow", "~"))
        self.label_19.setText(_translate("MainWindow", "使われた回数："))
        self.tempLow_error.setText(_translate("MainWindow", "35.00"))
        self.label_20.setText(_translate("MainWindow", "回"))
        self.label_21.setText(_translate("MainWindow", "°C"))
        self.tempHigh_error.setText(_translate("MainWindow", "40.00"))
        self.label_22.setText(_translate("MainWindow", "許容範囲："))
        self.acceptableRange.setText(_translate("MainWindow", "5.00"))
        self.label_23.setText(_translate("MainWindow", "％以下"))
        self.train.setText(_translate("MainWindow", "2．トレーニング"))
        self.label.setText(_translate("MainWindow", "生産した製品の誤差を予想するシミュレーションプログラム"))
        self.groupBox_2.setTitle(_translate("MainWindow", "ステップ2：人工知能の入力"))
        self.test.setText(_translate("MainWindow", "3．予想"))
        self.codeIn.setText(_translate("MainWindow", "10"))
        self.UsedTime.setText(_translate("MainWindow", "0"))
        self.label_64.setText(_translate("MainWindow", "コマンドの長さ："))
        self.label_65.setText(_translate("MainWindow", "回りの温度："))
        self.label_66.setText(_translate("MainWindow", "行"))
        self.label_68.setText(_translate("MainWindow", "使われた回数："))
        self.tempIn.setText(_translate("MainWindow", "-20.00"))
        self.label_69.setText(_translate("MainWindow", "回"))
        self.label_70.setText(_translate("MainWindow", "°C"))

        self.run.clicked.connect(self.takeInputAndGenerateDataset)
        self.train.clicked.connect(self.trainTensorFlowModel)
        self.test.clicked.connect(self.predictInputError)

    def dialogGood(self, s):

        dlg = QtWidgets.QMessageBox(self)
        dlg.setWindowIcon(QtGui.QIcon("data/icons/message.png"))
        dlg.setWindowTitle("予想")
        dlg.setText("誤差が発生する確率：" + str(s) + "%．誤差は許容範囲以内に予想する．")
        dlg.setIcon(QtWidgets.QMessageBox.Information)
        dlg.show()

    def dialogBad(self, s):

        dlg = QtWidgets.QMessageBox(self)
        dlg.setWindowIcon(QtGui.QIcon("data/icons/message.png"))
        dlg.setWindowTitle("予想")
        dlg.setText("誤差が発生する確率：" + str(s) + "%．差は許容範囲以外に予想する．")
        dlg.setIcon(QtWidgets.QMessageBox.Warning)
        dlg.show()

    def takeInputAndGenerateDataset(self):

        in_tempLow = float(self.tempLow.text())
        in_tempHigh = float(self.tempHigh.text())
        in_shortestCode = float(self.shortestCode.text())
        in_longestCode = float(self.longestCode.text())
        in_minimumUsedTime = float(self.minimumUsedTime.text())
        in_maximumUsedTime = float(self.maximumUsedTime.text())

        in_tempLow_error = float(self.tempLow_error.text()) if self.tempLow_error.text() != "" else None
        in_tempHigh_error = float(self.tempHigh_error.text()) if self.tempHigh_error.text() != "" else None
        in_shortestCode_error = float(self.shortestCode_error.text()) if self.shortestCode_error.text() != "" else None
        in_longestCode_error = float(self.longestCode_error.text()) if self.longestCode_error.text() != "" else None
        in_minimumUsedTime_error = float(self.minimumUsedTime_error.text()) if self.minimumUsedTime_error.text() != "" else None
        in_maximumUsedTime_error = float(self.maximumUsedTime_error.text()) if self.maximumUsedTime_error.text() != "" else None
        in_acceptableRange = float(self.acceptableRange.text()) if self.acceptableRange.text() != "" else None

        controlledDatasetRandomization(
            in_tempLow,
            in_tempHigh,
            in_shortestCode,
            in_longestCode,
            in_minimumUsedTime,
            in_maximumUsedTime,
            in_tempLow_error,
            in_tempHigh_error,
            in_shortestCode_error,
            in_longestCode_error,
            in_minimumUsedTime_error,
            in_maximumUsedTime_error,
            in_acceptableRange
        )

        self.label_24.setPixmap(QtGui.QPixmap("./data/output.png"))

    def trainTensorFlowModel(self):

        trainModel()

    def predictInputError(self):

        in_tempIn = float(self.tempIn.text())
        in_codeIn = float(self.codeIn.text())
        in_usedTime = float(self.UsedTime.text())

        result, yesNo = getThePrediction(in_tempIn, in_codeIn, in_usedTime)

        if (yesNo is False):

            self.dialogGood(result)
    
        else:

            self.dialogBad(result)

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_())

