Example #1
0
def create_model(current_game_name):

    models = 'db_models.py'

    model_found = False
    ldb = LotteryDatabase(True)

    curr_game = ldb.fetchone(LottoGame, {'game_name': current_game_name})

    if ldb.check_model_exist_by_table_name(curr_game.input_model):
        model_class = ldb.set_model_by_table_name(curr_game.input_model)
        model_class.__table__.drop(ldb.engine)

    with open(models, 'r') as models_file:
        models_code = models_file.readlines()

    try:
        with open(models, 'w') as models_file:
            for code_line in models_code:
                if curr_game.input_model in code_line:
                    model_found = True
                if model_found:
                    if code_line.strip() == '':
                        model_found = False
                if not model_found:
                    models_file.write(code_line)

            model_list = [
                'class {}(BASE):'.format(curr_game.input_model) + '\n',
                "    __tablename__ = '{}'".format('INPUT_' +
                                                  curr_game.game_table) + '\n',
                "    id = Column('id', Integer, primary_key=True)" + '\n'
            ]

            model_list += [
                "    {} = Column('{}', Integer)".format(
                    'NR_'.lower() + str(n), 'NR_' + str(n)) + '\n'
                for n in range(1, curr_game.game_len + 1)
            ]

            models_file.write('\n\n')
            models_file.writelines(model_list)

        BASE.metadata.create_all(bind=ldb.engine)

    except Exception:
        with open(models, 'w') as models_file:
            models_file.writelines(models_code)
        raise
Example #2
0
    def run(self):

        process_name = self.window.select_thread

        if process_name == "process_input":

            if self.window.input_line.text() == "":
                self.signal_infobox.emit(
                    'Missing input',
                    'No input numbers to proceed. Please try again.')
            else:
                self.table_name = 'PREDICT_' + self.window.combo_predict_model.currentText(
                )

                try:
                    convert = ConvertModel(self)
                    ml = MachineLearning(self)

                    convert.create_prediction_model(
                        self.window.input_line.text())
                    ml.random_forest_predict()
                    self.signal_progress_bar.emit(0)
                    self.signal_infobox.emit('Completed',
                                             'Prediction model created!')
                except Exception as exc:
                    self.signal_infobox.emit(
                        'Error', 'Something went wrong!! ' + str(exc))
                    self.signal_progress_bar.emit(0)

        elif process_name == 'update_la_jolla':

            try:
                ldb = LotteryDatabase()
                imported, rejected = ldb.import_la_jolla()
                self.signal_infobox.emit(
                    'Completed',
                    str.format(
                        'Lottery data imported! \n Imported: {} \n Rejected: {}',
                        imported, rejected))

            except Exception as exc:

                self.signal_infobox.emit('Error',
                                         'Something went wrong!! ' + str(exc))
                self.signal_progress_bar.emit(0)

        elif process_name == "create_model":

            if self.window.combo_db.currentText() != '':

                self.table_name = 'MODEL_' + self.window.combo_db.currentText()
                self.signal_qbox.emit('Create',
                                      'Do you want create new model?')

                while self.window.response is None:
                    pass

                if self.window.response == QMessageBox.Yes:
                    self.window.response = None

                    try:
                        convert = ConvertModel(self)

                        if self.window.check_win_loss.isChecked():

                            win, loss = convert.create_training_model()
                            self.signal_progress_bar.emit(0)
                            self.signal_infobox.emit(
                                'Completed', 'Training model created! \n' +
                                'Win Classification: ' + str(win) + '\n' +
                                'Loss Classification: ' + str(loss))

                        else:

                            zero, one, two, three, four = convert.create_training_model(
                            )
                            self.signal_progress_bar.emit(0)
                            self.signal_infobox.emit(
                                'Completed', 'Training model created! \n' +
                                'First Classification: ' + str(zero) + '\n' +
                                'Second Classification: ' + str(one) + '\n' +
                                'Third Classification: ' + str(two) + '\n' +
                                'Fourth Classification: ' + str(three) + '\n' +
                                'Fifth Classification: ' + str(four))

                    except Exception as exc:
                        self.signal_infobox.emit(
                            'Error', 'Something went wrong!! ' + str(exc))
                        self.signal_progress_bar.emit(0)

            else:
                self.signal_infobox.emit('Missing', 'Select model first!')

        elif process_name == "process_embedded":

            if self.window.input_line.text(
            ) == "" and not self.window.check_latest.isChecked():
                self.signal_infobox.emit(
                    'Missing input',
                    'No input numbers to proceed. Please try again.')
            elif self.window.check_latest.isChecked():

                ldb = LotteryDatabase()
                ldb_original = 'INPUT_' + CONFIG['games']['mini_lotto'][
                    'database']
                original_len = ldb.get_table_length(ldb_original)

                for i in range(1, 32):

                    try:

                        fetch_one = list(
                            ldb.fetchone(ldb_original, original_len - i + 1))

                        for j in range(self.window.combo_db.count()):

                            self.window.combo_db.setCurrentIndex(j)

                            self.table_name = 'MODEL_' + self.window.combo_db.currentText(
                            )
                            self.currentThread().__name__ = "MainThread"
                            ml = MachineLearning(self)
                            _ = ml.embedded_learning(
                                " ".join(map(str, fetch_one[1:6])), i,
                                fetch_one[0])

                    except Exception as exc:
                        self.signal_infobox.emit(
                            'Error', 'Something went wrong!! \n' + str(exc))

                self.signal_infobox.emit('Done', 'Finished!!')

            else:

                self.table_name = 'MODEL_' + self.window.combo_db.currentText()
                self.currentThread().__name__ = "MainThread"
                ml = MachineLearning(self)

                try:
                    output = ml.embedded_learning(
                        self.window.input_line.text())
                    self.signal_infobox.emit(
                        'Completed', 'Embedded Training finished! \n' + output)
                except Exception as exc:
                    self.signal_infobox.emit(
                        'Error', 'Something went wrong!! \n' + str(exc))

        elif process_name == "sklearn_ml":

            if len(self.window.list_ml.selectedItems()) > 0:
                self.table_name = 'MODEL_' + self.window.combo_db.currentText()
                self.currentThread().__name__ = "MainThread"
                ml = MachineLearning(self)

                try:
                    ml.sklearn_model_train()
                    self.signal_infobox.emit('Completed',
                                             'Training model finished!')
                except Exception as exc:
                    self.signal_infobox.emit(
                        'Error', 'Something went wrong!! \n' + str(exc))
            else:
                self.signal_infobox.emit('Missing',
                                         'Algorithm has not been selected!')

        elif process_name == "keras_ml":

            self.table_name = 'MODEL_' + self.window.combo_db.currentText()
            self.currentThread().__name__ = "MainThread"
            ml = MachineLearning(self)

            try:
                ml.keras_model_train()
                self.signal_infobox.emit('Completed',
                                         'Training model finished!')
            except Exception as exc:
                self.signal_infobox.emit(
                    'Error', 'Something went wrong!! \n' + str(exc))
                print(traceback.format_exc())

        elif process_name == "export_to_csv":

            export_to = ConvertModel(self, False)

            try:
                export_to.convert_to_original()
                self.signal_progress_bar.emit(0)
                self.signal_infobox.emit('Completed', 'Export complete!')
            except Exception as exc:
                self.signal_infobox.emit(
                    'Error', 'Something went wrong!! \n' + str(exc))
                self.signal_progress_bar.emit(0)

        elif process_name == "import_data":

            options = QFileDialog.Options()
            options |= QFileDialog.DontUseNativeDialog
            file_name, _ = QFileDialog.getOpenFileName(
                self.window,
                "Import file",
                "",
                "All Files (*);;Text Files (*.txt)",
                options=options)
            if file_name:

                ldb = LotteryDatabase()
                curr_game = ldb.fetchone(
                    LottoGame, {'name': self.window.line_current_game.text()})

                if not ldb.check_model_exist_by_table_name(
                        'INPUT_' + curr_game.input_table):

                    input_params = {
                        '__tablename__': 'INPUT_' + curr_game.input_table,
                        'id': Column('id', Integer, primary_key=True)
                    }

                    for i in range(1, curr_game.length + 1):
                        input_params['NR_' + str(i)] = Column(
                            'NR_' + str(i), Integer)
                    ldb.create_class_model(curr_game.input_model, input_params)
                    ldb.meta_create_all()

                imported, rejected = ldb.import_file(
                    'INPUT_' + curr_game.input_table, file_name,
                    curr_game.length + 1)

                self.signal_infobox.emit(
                    'Completed',
                    str.format(
                        'Lottery data imported! \n '
                        'Imported: {} \n '
                        'Rejected: {}', imported, rejected))