Ejemplo n.º 1
0
    def __init__(self, worker):

        self.worker = worker
        self.ldb = LotteryDatabase()

        # variables
        self.x = None
        self.y = None

        self.x_train = None
        self.x_validation = None
        self.y_train = None
        self.y_validation = None

        self.N_FOLDS = 5
        self.MAX_EVALS = 20
        self.RANDOM_STATE = 42
        self.training_size = 15
        self.n_increment = 10
        self.curr_game = CONFIG['games']['mini_lotto']

        # features
        self.table_headers = []
        self.table_name = self.worker.table_name
        self.features = self.curr_game['features']

        for i in range(self.worker.window.list_model.count()):
            feature_len = self.features[self.worker.window.list_model.item(
                i).text()]['length'] + 1
            feature_header = self.features[self.worker.window.list_model.item(
                i).text()]['header']
            self.table_headers += [
                feature_header + str(n) + ' INTEGER'
                for n in range(1, feature_len)
            ]
Ejemplo n.º 2
0
    def convert_to_original(self):

        self.ldb = LotteryDatabase()
        combo_predict = self.worker.window.combo_predict_model
        self.table_name = 'PREDICT_' + combo_predict.currentText()

        now = datetime.datetime.now()
        file_name = str.format('{} {}', combo_predict.currentText(), now.strftime("%Y-%m-%d %H %M %S"))
        export_columns = ['FIRST', 'SECOND', 'THIRD', 'FOURTH', 'FIFTH', 'SIXTH', 'LABEL', 'OUTPUT']

        with open('archived/' + file_name + '.csv', 'a', newline='') as csv_file:

            writer = csv.writer(csv_file)
            writer.writerow(export_columns)

            for o in range(1, self.input_record_count):
                fetch_one = list(self.ldb.fetchone(self.table_name, o))
                fetch_output = list(self.ldb.fetchone('OUTPUT_prediction', o))

                originals = fetch_one[1:50]
                label_column = [fetch_one[-1]]
                output_column = [fetch_output[-1]]

                output_list = [n + 1 for n in range(0, len(originals)) if originals[n] == 1]
                output_list = output_list + label_column + output_column

                writer.writerow(output_list)

                self.worker.signal_status.emit('Export in progress: {} of {}.'.format(o, self.input_record_count - 1))

        self.worker.signal_status.emit('')
Ejemplo n.º 3
0
    def __init__(self):

        self.ldb = LotteryDatabase(CONFIG['database'])

        self.x = None
        self.y = None

        self.x_train = None
        self.x_validation = None
        self.y_train = None
        self.y_validation = None

        self.curr_game = CONFIG['games']['mini_lotto']

        self.table_headers = []
        self.features = self.curr_game['features']

        feat = ['number_map', 'number_cycles', 'cool numbers']

        for i in feat:
            feature_len = self.features[i]['length'] + 1
            feature_header = self.features[i]['header']
            self.table_headers += [
                feature_header + str(n) + ' INTEGER'
                for n in range(1, feature_len)
            ]
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    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.')
Ejemplo n.º 6
0
def declarative_change():

    ldb = LotteryDatabase(True)

    class1 = type('Class1', (BASE, ), {
        '__tablename__': 'test',
        'id': Column('id', Integer, primary_key=True)
    })

    # unregister_class(class1)

    class1 = type('Class1', (BASE, ), {
        '__tablename__': 'test1',
        'id': Column('id', Integer, primary_key=True)
    })

    print(class1)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    def __init__(self, window, parent=None):
        super(DatabaseManagerDialog, self).__init__(parent)

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

        # variables
        self.deleted = {}
        self.created = {}

        # buttons
        self.btn_add_model.clicked.connect(self.add_model)
        self.btn_delete_model.clicked.connect(self.delete_model)

        self.btn_add_predict.clicked.connect(self.add_predict)
        self.btn_delete_predict.clicked.connect(self.delete_predict)

        self.btn_save.clicked.connect(self.save_database)
        self.btn_cancel.clicked.connect(self.close_db_manager)
Ejemplo n.º 9
0
    def __create_rash_group(self):

        self.ldb = LotteryDatabase()
        fetch_a = self.ldb.execute(self.input_table)

        first_data_group = defaultdict(int)
        second_data_group = defaultdict(int)
        third_data_group = defaultdict(int)
        new_set = []

        for line in fetch_a:
            line = line[1:7]
            for num in line:
                if num in self.curr_game['groups']['first_group']:
                    new_set.append(1)
                elif num in self.curr_game['groups']['second_group']:
                    new_set.append(2)
                elif num in self.curr_game['groups']['third_group']:
                    new_set.append(3)

            count = Counter(new_set)

            if count[1] == 4:
                self.rash_one += 1
                for x, y in zip(line, new_set):
                    if y == 1:
                        first_data_group[x] += 1

            elif count[2] == 4:
                self.rash_two += 1
                for x, y in zip(line, new_set):
                    if y == 2:
                        second_data_group[x] += 1

            elif count[3] == 4:
                self.rash_three += 1
                for x, y in zip(line, new_set):
                    if y == 3:
                        third_data_group[x] += 1

            rash_group = []

            for num in line:
                if num in self.curr_game['groups']['first_group']:
                    d = first_data_group.get(num)
                    if d == "":
                        per = round(float(1 / self.rash_one) * 10, 2)
                    else:
                        per = round(float(d / self.rash_one) * 10, 2)

                    rash_group.append(per)
                elif num in self.curr_game['groups']['second_group']:
                    d = second_data_group.get(num)
                    if d == "":
                        per = round(float(1 / self.rash_two) * 10, 2)
                    else:
                        per = round(float(d / self.rash_two) * 10, 2)
                    rash_group.append(per)
                elif num in self.curr_game['groups']['third_group']:
                    d = third_data_group.get(num)
                    if d == "":
                        per = round(float(1 / self.rash_three) * 10, 2)
                    else:
                        per = round(float(d / self.rash_three) * 10, 2)

                    rash_group.append(per)

        self.ldb.__del__()
Ejemplo n.º 10
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))