示例#1
0
class ModelAddDialog(QtBaseAddClass, Ui_AddDialog):

    signal_add_to_list = QtCore.pyqtSignal(str)
    signal_add_to_model = QtCore.pyqtSignal(str)

    def __init__(self, window, parent=None):
        super(ModelAddDialog, self).__init__(parent)

        # class initialize
        self.setupUi(self)
        self.window = window
        self.ldb = LotteryDatabase()
        self.list_add_available_init()

        #  signals
        self.signal_add_to_list.connect(self.list_add_selected)

        #  buttons
        self.push_add_ok.clicked.connect(self.add_feature)
        self.push_add_cancel.clicked.connect(self.close_dialog)
        self.feature_add.clicked.connect(self.list_add_selected)
        self.feature_remove.clicked.connect(self.list_remove_selected)

        self.feature_sortUp.clicked.connect(self.move_item_up)
        self.feature_sortDown.clicked.connect(self.move_item_down)

    def list_add_selected(self):
        for item in self.list_add_available.selectedItems():
            if not self.list_feature_order.findItems(item.text(),
                                                     QtCore.Qt.MatchExactly):
                self.list_feature_order.addItem(item.text())

    def list_remove_selected(self):
        self.list_feature_order.takeItem(self.list_feature_order.currentRow())

    def move_item_up(self):
        if self.list_feature_order.currentRow() > 0:
            current_row = self.list_feature_order.currentRow()
            current_item = self.list_feature_order.takeItem(current_row)
            self.list_feature_order.insertItem(current_row - 1, current_item)
            self.list_feature_order.setCurrentRow(current_row - 1)
            self.list_feature_order.item(current_row - 1).setSelected(True)

    def move_item_down(self):
        if self.list_feature_order.currentRow(
        ) < self.list_feature_order.count() - 1:
            current_row = self.list_feature_order.currentRow()
            current_item = self.list_feature_order.takeItem(current_row)
            self.list_feature_order.insertItem(current_row + 1, current_item)
            self.list_feature_order.setCurrentRow(current_row + 1)
            self.list_feature_order.item(current_row + 1).setSelected(True)

    def list_add_available_init(self):
        features = self.ldb.fetchall(
            ModelFeatures, {'game': self.window.line_current_game.text()})
        for feature in features:
            self.list_add_available.addItem(feature.name)
        for i in range(self.window.list_model.count()):
            self.list_feature_order.addItem(
                self.window.list_model.item(i).text())

    def add_feature(self):

        self.window.list_model.clear()

        feature_count = self.list_feature_order.count()

        list_model = '|'.join([
            str(self.list_feature_order.item(i).text())
            for i in range(feature_count)
        ])

        user_config = {'list_model': list_model}

        self.ldb.update_record(
            UserSettings, {
                'user_parent': 'default',
                'line_current_game': self.window.line_current_game.text()
            }, user_config)

        for model in list_model.split('|'):
            self.signal_add_to_model.emit(model)

        self.close_dialog()

    def close_dialog(self):
        self.close()
示例#2
0
class Window(QtBaseClass, Ui_MainWindow):
    def __init__(self, parent=None):

        super(QtBaseClass, self).__init__(parent)

        #  class initialize
        self.setupUi(self)
        self.worker = ThreadClass(self)
        self.ldb = LotteryDatabase(True)
        self.update_algorithms()
        self.update_combobox_ml()
        self.get_user_settings()

        # sys.stdout = OutLog(self.stdout_text, sys.stdout, QtGui.QColor(255, 255, 255))
        # sys.stderr = OutLog(self.stdout_text, sys.stderr, QtGui.QColor(255, 255, 255))

        #  variables
        self.select_thread = None
        self.response = None

        # signals
        # self.ldb.signal_db_error.connect(self.info_box)

        self.worker.signal_progress_bar.connect(self.update_progress_bar)
        self.worker.signal_infobox.connect(self.info_box)
        self.worker.signal_status.connect(self.update_status_bar)
        self.worker.signal_qbox.connect(self.question_box)

        # buttons
        self.push_delete.clicked.connect(self.delete_feature)
        self.push_create.clicked.connect(self.create_model)
        self.push_predict.clicked.connect(self.process_input)
        self.push_embedded.clicked.connect(self.process_embedded)
        self.push_add.clicked.connect(self.load_add_ui)
        self.push_ml.clicked.connect(self.sklearn_ml)
        self.push_knn.clicked.connect(self.keras_ml)

        # menu bar actions
        self.actionDatabase_Manager.triggered.connect(self.load_db_manager)
        self.actionExit_Program.triggered.connect(self.close_app)
        self.actionImport_from_file.triggered.connect(self.import_data)
        self.actionVersion.triggered.connect(self.program_version)
        self.actionExport_to.triggered.connect(self.export_to_csv)
        self.actionImport_La_Jolla.triggered.connect(self.update_la_jolla)

        # tooltips
        self.check_add_random.setToolTip('Add random numbers to each sample.')
        self.combo_test_size.setToolTip(
            'Determine testing size for each sample.')

    def save_user_settings(self):

        list_model = '|'.join([
            str(self.list_model.item(i).text())
            for i in range(self.list_model.count())
        ])

        user_config = {
            'check_win_loss': self.check_win_loss.isChecked(),
            'check_add_random': self.check_add_random.isChecked(),
            'check_latest': self.check_latest.isChecked(),
            'check_sampling': self.check_sampling.isChecked(),
            'check_keras': self.check_keras.isChecked(),
            'combo_predict_model': self.combo_predict_model.currentText(),
            'combo_predict_ml': self.combo_predict_ml.currentText(),
            'combo_db': self.combo_db.currentText(),
            'combo_test_size': self.combo_test_size.currentText(),
            'combo_scoring': self.combo_scoring.currentText(),
            'list_model': list_model
        }

        self.ldb.update_record(
            UserSettings, {
                'user_parent': 'default',
                'line_current_game': self.line_current_game.text()
            }, user_config)

    def get_user_settings(self):

        user_config = self.ldb.fetchone(
            UserSettings, {
                'user_parent': 'default',
                'line_current_game': self.line_current_game.text()
            })

        self.check_win_loss.setChecked(user_config.check_win_loss)
        self.check_add_random.setChecked(user_config.check_add_random)
        self.check_latest.setChecked(user_config.check_latest)
        self.check_sampling.setChecked(user_config.check_sampling)
        self.check_keras.setChecked(user_config.check_keras)

        self.combo_predict_model.setCurrentText(
            user_config.combo_predict_model)
        self.combo_predict_ml.setCurrentText(user_config.combo_predict_ml)
        self.combo_db.setCurrentText(user_config.combo_db)
        self.combo_test_size.setCurrentText(user_config.combo_test_size)
        self.combo_scoring.setCurrentText(user_config.combo_scoring)

        if user_config.list_model != '':
            for each_key in user_config.list_model.split("|"):
                self.list_model.addItem(each_key)

    def load_add_ui(self):
        feature_dialog = ModelAddDialog(self)
        feature_dialog.signal_add_to_model.connect(
            self.update_add_feature_list)

        for i in range(self.list_model.count()):

            feature_dialog.signal_add_to_list.emit(
                self.list_model.item(i).text())

        feature_dialog.exec_()

    def load_db_manager(self):
        db_manager = DatabaseManagerDialog(self)
        db_manager.exec_()

    def delete_feature(self):
        self.list_model.takeItem(self.list_model.currentRow())

        list_model = '|'.join([
            str(self.list_model.item(i).text())
            for i in range(self.list_model.count())
        ])

        user_config = {'list_model': list_model}

        self.ldb.update_record(
            UserSettings, {
                'user_parent': 'default',
                'line_current_game': self.line_current_game.text()
            }, user_config)

    def update_add_feature_list(self, item):
        self.list_model.addItem(item)

    def update_algorithms(self):
        self.list_ml.addItem('RandomForestClassifier')
        self.list_ml.addItem('RandomForestRegressor')
        self.list_ml.addItem('LogisticRegression')
        self.list_ml.addItem('SGDClassifier')

    def update_combobox_ml(self):

        tables = self.ldb.fetchall(DatabaseModels, {})

        self.combo_predict_model.clear()
        self.combo_db.clear()
        self.combo_test_size.clear()
        self.combo_predict_ml.clear()

        for alg in range(self.list_ml.count()):
            self.combo_predict_ml.addItem(self.list_ml.item(alg).text())

        for table in tables:
            if table.database_name.startswith('MODEL_'):
                self.combo_db.addItem(table.database_name.replace(
                    'MODEL_', ''))
            elif table.database_name.startswith('PREDICT_'):
                self.combo_predict_model.addItem(
                    table.database_name.replace('PREDICT_', ''))

        for n in range(6, 13):
            self.combo_test_size.addItem(str(n))

    def update_status_bar(self, status):
        self.statusBar().showMessage(status)

    def update_progress_bar(self, progress_val):

        if self.select_thread == "process_input":

            self.progress_predict.setValue(progress_val)

        elif self.select_thread == "create_model":

            self.progress_create.setValue(progress_val)

        else:

            self.progress_ml.setValue(progress_val)

    def sklearn_ml(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def keras_ml(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def create_model(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def import_data(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def process_input(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def export_to_csv(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def process_embedded(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def update_la_jolla(self):
        self.select_thread = inspect.stack()[0][3]
        self.worker.start()

    def info_box(self, info_head, info_text):
        QMessageBox.information(self, info_head, info_text)

    def question_box(self, question_head, question_text):

        self.response = QMessageBox.question(self, question_head,
                                             question_text,
                                             QMessageBox.Yes | QMessageBox.No,
                                             QMessageBox.No)

    def closeEvent(self, event):

        odp = QMessageBox.question(self, 'Exit', "Are you sure you want exit?",
                                   QMessageBox.Yes | QMessageBox.No,
                                   QMessageBox.No)

        if odp == QMessageBox.Yes:
            self.save_user_settings()
            event.accept()
            sys.exit()
        else:
            event.ignore()

    def keyPressEvent(self, event):

        if event.key() == QtCore.Qt.Key_Escape:

            odp = QMessageBox.question(self, 'Exit',
                                       "Are you sure you want exit?",
                                       QMessageBox.Yes | QMessageBox.No,
                                       QMessageBox.No)

            if odp == QMessageBox.Yes:
                self.save_user_settings()
                event.accept()
                sys.exit()
            else:
                event.ignore()

    def close_app(self):
        odp = QMessageBox.question(self, 'Exit', "Are you sure you want exit?",
                                   QMessageBox.Yes | QMessageBox.No,
                                   QMessageBox.No)

        if odp == QMessageBox.Yes:
            self.save_user_settings()
            sys.exit()

    def program_version(self):
        self.info_box('Program Version', VERSION)