def test_read_invalid_sudoku_from_cmd_line(self): sudoku_size = -1 input_line = "000000680000073009309000045490000000803050902000000036" import_data = ImportData() sudoku = import_data.read_sudoku_data_from_line(input_line) self.assertEquals(input_line, sudoku[0][0]) self.assertEquals(sudoku_size, sudoku[0][1])
def CleanData(): met_df = ImportData() print('met_df_1: {0}'.format(met_df.shape)) # Drop rows with NaN in reclat and/or recling met_df = met_df.dropna(axis=0, how="any", subset=['reclat', 'reclong']) print('met_df_2: {0}'.format(met_df.shape)) # Determine no. of rows where latitude and longitude = 0.000000 zero_df = met_df.loc[(met_df['reclat'] == '0.000000') & (met_df['reclong'] == '0.000000')] print('zero_df: {0}'.format(zero_df.shape)) # Drop rows where latitude and longitude is 0.000000 met_df = met_df.drop(met_df[(met_df['reclat'] == '0.000000') & (met_df['reclong'] == '0.000000')].index) print('met_df_3: {0}'.format(met_df.shape)) # Convert year column from pandas object to pandas datetime64 datatype met_df['year'] = pd.to_datetime(met_df['year'], format='%Y-%m-%dT%H:%M:%S.%f', errors='coerce') # Extract year from column (datatype:datetime64) and update column value (new datatype:float64) met_df['year'] = met_df['year'].dt.year # Convert float64 to int64 to remove decimal | Fill any NA/NaN for year with 0 met_df['year'] = met_df['year'].fillna(0).astype('int64') print(met_df.dtypes) print(met_df.head()) return met_df
def import_relations(self): key_names, value_names = self.list_dicts() import_data = ImportData() for i in range(len(key_names)): with open(self.store_dir + value_names[i]) as f2: file_manager = FileManager(key_names[i]) f1 = file_manager.fetch() import_data.batch_fill_user_and_relations3(f1, f2)
def load_data(read=1): # 加载数据,如果之前已经生成预处理数据并且read=1,则直接读取数据,否则重新生成数据 if os.path.exists(r'pre_data/train.csv') == False or read == 0: data = ImportData() train, train_label, test = data.preprocess_data() else: train = pd.read_csv(r'pre_data/train.csv').values train_label = pd.read_csv(r'pre_data/train_label.csv').values test = pd.read_csv(r'pre_data/test.csv').values return train, train_label, test
def test_read_sudoku_from_valid_csv(self): sudoku_size = 9 import_data = ImportData() exp_val = "316578492\n" + \ "529134768\n" + \ "487629531\n" + \ "263415987\n" + \ "974863125\n" + \ "851792643\n" + \ "138947256\n" + \ "692351874\n" + \ "745286319\n" sudoku = import_data.read_sudoku_data_from_file('testvalidcsv.csv') self.assertEquals(exp_val, sudoku[0][0]) self.assertEquals(sudoku_size, sudoku[0][1])
def test_read_valid_sudoku_from_cmd_line(self): sudoku_size = 9 input_line = "000000680000073009309000045490000000803050" + \ "902000000036960000308700680000028000000" import_data = ImportData() exp_result = "000000680\n" + \ "000073009\n" + \ "309000045\n" + \ "490000000\n" + \ "803050902\n" + \ "000000036\n" + \ "960000308\n" + \ "700680000\n" + \ "028000000\n" sudoku = import_data.read_sudoku_data_from_line(input_line) self.assertEquals(exp_result, sudoku[0][0]) self.assertEquals(sudoku_size, sudoku[0][1])
def xgboost_optuna(self): """ """ # load the pima indian diabetes dataset pima = ImportData(file_type=self.file_type).import_data() # prepare the pima dataset train, val, test = PrepareDataset( data=pima).prepare_data() # fit a xgboost model to the training dataset also apply hyperparmeter tuning DataModelling(train, val, test).data_modelling()
def __init__(self): self.configuration = XMLConfig() self.import_data = ImportData() self.empty_spot_char = self.configuration.get_empty_spot_char() self.export_sudoku = ExportSudoku() self.display_main_menu()
class Main(): def __init__(self): self.configuration = XMLConfig() self.import_data = ImportData() self.empty_spot_char = self.configuration.get_empty_spot_char() self.export_sudoku = ExportSudoku() self.display_main_menu() def display_main_menu(self): """ Displays the Main Menu and validates the entered values for the required options """ print self.logo() self.print_dictionary_list(self.main_menu_options()) while True: op_main_menu = raw_input("\nPlease enter a number: ") if op_main_menu == "1" or op_main_menu == "2" or op_main_menu == "3"\ or op_main_menu == "4" or op_main_menu == "5": self.main_menu_options()[op_main_menu] break else: print "Oops! That was not a valid option number. Try again...\n" self.execute_main_option(op_main_menu) def logo(self): """ Displays the logo on the menu""" logo_str = " _____ _ _ ___\n" + \ "/ ___| | | | | / _ \\\n" + \ "\ `--. _ _ __| | ___ | | ___ _ / /_\ \\\n" + \ " `--. \ | | |/ _` |/ _ \| |/ / | | | | _ |\n" + \ "/\__/ / |_| | (_| | (_) | <| |_| | | | | |\n" + \ "\____/ \__,_|\__,_|\___/|_|\_\\\\__,_| \_| |_/\n" return logo_str def main_menu_options(self): """ Defines the 3 available options for the Sudoku Main Menu """ main_menu_opts = {'1': 'Configure', '2': 'Solve Sudoku', '3': 'Generate Sudoku',\ '4': 'Play online!', '5': 'Exit'} return main_menu_opts def execute_change_solve_option(self, op_solve_menu): """ Executes the Solve Menu for the selected option Keyword arguments: op_solve_menu -- takes one of the three options to select on the Main Menu """ if op_solve_menu == "1": self.solve_sudoku_from_cmd("txt") if op_solve_menu == "2": self.solve_sudoku_from_file("txt") if op_solve_menu == "3": self.solve_sudoku_from_file("txt") if op_solve_menu == "4": self.display_main_menu() def solve_sudoku_from_cmd(self, type_file): """ Method used for solving the sudoku given the input by command line Keyword arguments: type_file -- stores the selected cmd option """ cmd_sudoku = raw_input( '\nEnter the sudoku to solve in the same line without spaces or' 'commas, it should have 81 numbers as the following example:\n0' '03020600900305001001806400008102900700000008006708200002609500' '800203009005010300\n') print "\nSolution:\n" sudoku_matrix = self.import_data.read_sudoku_data_from_line(cmd_sudoku) self.export_sudoku_to(sudoku_matrix, type_file) self.display_main_menu() def export_sudoku_to(self, sudoku_matrix, type_file): """ Method used for exporting the solved sudoku to the default "results" path Keyword arguments: sudoku_matrix -- stores the sudoku on the valid sudoku matrix to solve type_file -- stores the selected file type; file or cmd """ path = "../results/" algoritms = self.create_algorith_to_solve_sudoku( sudoku_matrix, self.empty_spot_char) for algoritm_to_solve in algoritms: sudoku_solved = algoritm_to_solve.solve_sudoku() if sudoku_solved != []: export_to = self.configuration.get_output_type() if export_to == 'file': self.export_file(algoritm_to_solve, type_file, sudoku_solved, path) else: self.export_sudoku.export_sudoku(sudoku_solved, "cmd line", "", "") else: print "\nInvalid sudoku, it cannot be solved!\n" def file_exists(self): """ Method used for verifying whether the entered file exists or not """ result = "" path = "../inputs/" while True: file_name = raw_input( '\nEnter the name of the file to solve or enter quit to exit: ' ) file_txt = path + "\\" + file_name if os.path.exists(file_txt): result = file_txt break elif file_name == "quit": break else: print "\nThe file provided does not exists, please try again\n" return result def solve_sudoku_from_file(self, type_file): """ Method used for solving the sudoku given a file Keyword arguments: type_file -- stores the selected file type, it can be a txt or csv file """ file_ext = self.file_exists() if file_ext != "": sudoku_matrix = self.import_data.read_sudoku_data_from_file( file_ext) self.export_sudoku_to(sudoku_matrix, type_file) #self.display_main_menu() #else: self.display_solve_menu() def export_file(self, algoritm_to_solve, type_file, sudoku_solved, path): """ Method used for create the solved sudoku in a file on the default "results" path Keyword arguments: algoritm_to_solve -- the selected algorithm to solve the sudoku type_file -- stores the selected file type; file or cmd sudoku_solved -- sores the solved sudoku path -- stores the default "results" path for the solutions """ file_name = 'sudoku_solved_' + algoritm_to_solve.__class__.__name__ + "_" + \ str(algoritm_to_solve.get_time()) + "_" + time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime(time.time())) + "." + type_file if self.export_sudoku.export_sudoku(sudoku_solved, type_file, path, file_name): print "\nThe file was created successfully\n" else: print "\nThere was a error, the file couldn't be created\n" def create_algorith_to_solve_sudoku(self, sudoku_matrix, empty_character): """ Method used for create the algorithm to solve given the expected sudoku matrix and empty character Keyword arguments: sudoku_matrix -- stores the sudoku in the valid matrix ready to be solved empty_character -- stores the selected character for the empty spots """ algorithm_selected = self.configuration.get_algorithm() algoritms = [] for i in range(0, len(sudoku_matrix)): algoritm_to_solve = Algorithm(sudoku_matrix[i][0], empty_character) if algorithm_selected == 'norvig': algoritm_to_solve = NorvigAlgorithm(sudoku_matrix[i][0], empty_character) elif algorithm_selected == 'backtracking': algoritm_to_solve = BacktrakingAlgorithm( sudoku_matrix[i][0], empty_character) elif algorithm_selected == 'brute force': algoritm_to_solve = BruteForceAlgorithm( sudoku_matrix[i][0], empty_character) algoritms.append(algoritm_to_solve) return algoritms def display_solve_menu(self): """ Displays/validates the selected options for solving the sudoku using one of the three valid input types """ self.print_dictionary_list(self.change_solve_menu_options(), "--- Solve Sudoku Menu ---") while True: solve_opts = raw_input("\nPlease enter a number: ") if solve_opts == "1" or solve_opts == "2" or solve_opts == "3" or solve_opts == "4": self.change_solve_menu_options()[solve_opts] break else: print "\nOops! That was not a valid option number. Try again...\n" self.execute_change_solve_option(solve_opts) def change_solve_menu_options(self): """ Defines the 3 available options for the inputs type for solving the sudoku """ solve_opts = { '1': 'Solve sudoku entered by command line', '2': 'Solve sudoku from a txt file', '3': 'Solve sudoku from a csv file', '4': 'Back' } return solve_opts def conf_menu_options(self): """ Defines the 3 available options for the: "1 . Configure" option """ conf_opts = { '1': 'Change Output format', '2': 'Change Level', '3': 'Change Algorithm to use', '4': 'Change empty spot character', '5': 'Back' } return conf_opts def execute_main_option(self, op_main_menu): """ Executes the Main Menu for the selected option Keyword arguments: op_main_menu -- takes one of the four options to select on the Main Menu """ if op_main_menu == "1": self.display_configure_menu() if op_main_menu == "2": self.display_solve_menu() if op_main_menu == "3": self.display_generate_sudoku_menu() if op_main_menu == "4": SudokuUI() if op_main_menu == "5": print "Good bye!" def execute_conf_option(self, conf_opts): """ Executes the selected option on the "1 . Configure" option Keyword arguments: conf_opts -- takes one of the four options available in the option: "1 . Configure" """ if conf_opts == "1": self.display_output_type_menu() if conf_opts == "2": self.display_level_algorithm_menu() if conf_opts == "3": self.display_algorithm_menu() if conf_opts == "4": self.execute_change_empty_spot() if conf_opts == "5": self.display_main_menu() def execute_change_empty_spot(self): """ Method that changes the character used as empty spot """ empty_spot_char = str( raw_input('Enter a new character used as empty spot or' ' enter <quit> to go back menu: ')) if empty_spot_char >= '1' and empty_spot_char <= '9': print "\nInvalid empty spot character, please don't use numbers from 1 to 9" elif empty_spot_char == ',': print "\nInvalid empty spot character, please don't use the comma (,) character" elif len(empty_spot_char) == 1: print "\n", self.configuration.modify_empty_spot_char( empty_spot_char) elif empty_spot_char.lower() == 'quit': print "\nDefault empty spot char will be used: "\ + self.configuration.get_empty_spot_char() else: print "\nInvalid empty spot character, please use only one character" self.display_configure_menu() def execute_change_level_option(self, level_opts): """ Changes the complexity level for create or solve the sudoku Keyword arguments: level_opts -- takes one of the three options for the complexity level """ if level_opts == "1": # save Easy level print "\n", self.configuration.modify_complexity('Easy') if level_opts == "2": # save Medium level print "\n", self.configuration.modify_complexity('Medium') if level_opts == "3": # save Hard level print "\n", self.configuration.modify_complexity('Hard') self.execute_change_algorithm_option('4') def execute_change_algorithm_option(self, chng_algorit): """ Changes the Algorithm used for create or solve the sudoku Keyword arguments: chng_algorit -- takes one of the three options for changing the Algorithm to use """ if chng_algorit == "1": # save Norvig algorithm print "\n", self.configuration.modify_algorithm('Norvig') if chng_algorit == "2": # save Backtraking algorithm print "\n", self.configuration.modify_algorithm('Backtracking') if chng_algorit == "3": # save Brute Force algorithm print "\n", self.configuration.modify_algorithm("brute force") self.display_configure_menu() def execute_change_output_option(self, output_opts): """ Changes the output format for the solved the sudoku Keyword arguments: output_opts -- takes one of the two options, solved sudoku could be displayed in console, or in a file """ if output_opts == "1": # display solved sudoku in console print "\n", self.configuration.modify_output_type('console') if output_opts == "2": # save solved sudoku in a file print "\n", self.configuration.modify_output_type('file') self.display_configure_menu() def execute_generate_sudoku_option(self, generate_opts): """Executes the action selected by the user save sudoku on TXT file or diplay in console Keyword arguments: generate_opts -- takes one of the two options, generated sudoku could be displayed in console, or in a file """ if generate_opts in ['1', '2']: complexity = self.configuration.get_complexity() min_holes = self.configuration.get_min_holes_by_complexity( complexity) max_holes = self.configuration.get_max_holes_by_complexity( complexity) empty_spot_char = self.configuration.get_empty_spot_char() sudoku_generator = SudokuGenerator() sudoku_generator.generate_sudoku_pattern_by_complexity( min_holes, max_holes, empty_spot_char) if generate_opts == "1": # save sudoku generated in file name_sudoku = "sudoku_generated_" + time.strftime("%Y-%m-%dT%H-%M-%S",\ time.localtime(time.time())) + ".txt" if self.export_sudoku.export_sudoku( sudoku_generator.sudoku_pattern, 'txt', "../results/", name_sudoku) is True: print "\nSudoku was saved to: '", name_sudoku, "'" self.display_main_menu() else: print "\nFailed to save in TXT file. Try again...!" self.display_generate_sudoku_menu() if generate_opts == "2": # display generate sudoku in console print "\nGenerated Sudoku:\n" self.export_sudoku.export_sudoku(sudoku_generator.sudoku_pattern, 'cmd line', '', '') self.display_main_menu() if generate_opts == "3": self.display_main_menu() def display_algorithm_menu(self): """ Displays/validates the selected options for the option: 3 . Change Algorithm to solve sudokus """ self.print_dictionary_list(self.change_algorithm_menu_options(), "--- Change Algorithm Menu ---") while True: algorit_opts = raw_input("\nPlease enter a number: ") if algorit_opts == "1" or algorit_opts == "2" or algorit_opts == "3"\ or algorit_opts == "4": self.change_algorithm_menu_options()[algorit_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_change_algorithm_option(algorit_opts) def display_level_algorithm_menu(self): """ Displays and validates the selected options for the option "2 . Change level" """ self.print_dictionary_list(self.change_level_menu_options(), "--- Change Complexity Level Menu ---") while True: level_opts = raw_input("\nPlease enter a number: ") if level_opts == "1" or level_opts == "2" or level_opts == "3" or level_opts == "4": self.change_level_menu_options()[level_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_change_level_option(level_opts) def display_output_type_menu(self): """ Displays/validates the selected options for the option: 1 . Change output format """ self.print_dictionary_list(self.change_output_type_options(), "--- Change Output Type Menu ---") while True: output_opts = raw_input("\nPlease enter a number: ") if output_opts == "1" or output_opts == "2" or output_opts == "3": self.change_output_type_options()[output_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_change_output_option(output_opts) def display_generate_sudoku_menu(self): """ Displays and validates the selected options for the option "3. Generate Sudoku" """ self.print_dictionary_list(self.change_generate_sudoku_options(), "--- Generate Sudoku Menu ---") while True: generate_opts = raw_input("\nPlease enter a number: ") if generate_opts == "1" or generate_opts == "2" or generate_opts == "3": self.change_generate_sudoku_options()[generate_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_generate_sudoku_option(generate_opts) def display_configure_menu(self): """ Displays and validates the selected options for the option "1 . Configure" """ self.print_dictionary_list(self.conf_menu_options(), "--- Configuration Menu ---") while True: config_opts = raw_input("\nPlease enter a number: ") if config_opts == "1" or config_opts == "2" or config_opts == "3"\ or config_opts == "4" or config_opts == "5": self.conf_menu_options()[config_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_conf_option(config_opts) def change_generate_sudoku_options(self): """ Defines the 3 available options for the option "3. Generate Sudoku" """ generate_opts = {'1': 'Save Sudoku to TXT file', '2': 'Display Sudoku on console',\ '3': 'Back to Main menu'} return generate_opts def change_output_type_options(self): """ Defines the 3 available options for the option "1 . Change output format" """ output_opts = { '1': 'Display result in console', '2': 'Save result in a file', '3': 'Back' } return output_opts def change_algorithm_menu_options(self): """ Defines the available Algorithm methods for the sudoku resolution """ algorit_opts = { '1': 'Norvig', '2': 'Backtracking', '3': 'Brute Force', '4': 'Back' } return algorit_opts def change_level_menu_options(self): """ Defines the available levels for the option "2 . Change level" """ level_opts = {'1': 'Easy', '2': 'Medium', '3': 'Hard', '4': 'Back'} return level_opts def print_dictionary_list(self, dictionary, title_menu=""): """ Prints the dictionary list for each value """ if title_menu != "": print "\n" + title_menu + "\n" for n in range(1, len(dictionary) + 1): for key, value in dictionary.iteritems(): if str(n) == key: print key, value
def test_read_sudoku_from_empty_csv(self): import_data = ImportData() sudoku_is_read = import_data.read_sudoku_data_from_file('empty.csv') self.assertFalse(sudoku_is_read)
continue return weight def lift(cred, sup): try: return cred / sup except ZeroDivisionError as error: return -1 def fix(numObj, digits=0): return f'{numObj:.{digits}f}' if __name__ == '__main__': file = ImportData('resources/transaktions.txt') f = 2 c_level = 50 file.read_file() trans = file.transactions.copy() print_trans(trans) sets = get_sets_freq(f, trans, 3) frequent_Fs = assosiation_rules(sets, trans, c_level) print( f'\n<----------Ассоциативные правила с достоверностью {c_level}%---------->\n' ) for key in frequent_Fs: print(f'{key} => {frequent_Fs[key]}')
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent=parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.showMaximized() self.ui.txtFromDate.setDateTime(QtCore.QDateTime.currentDateTime()) self.ui.txtFromDate.setCalendarPopup(True) self.ui.txtToDate.setDateTime(QtCore.QDateTime.currentDateTime()) self.ui.txtToDate.setCalendarPopup(True) # init connection self.connection = create_open_database(host='localhost',port=3307,user='******',password='******',db_name='ksoft') # source_item_columns = [] # target_item_columns = [] # path = 'C:\\Users\\Kimsong\\Desktop\\Online Retail.xlsx' # result = import_from_excel(path,'Online Retail',source_item_columns,target_item_columns,connection) # print(result) header = self.ui.itemsetTableWidget.horizontalHeader() header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents) header.setSectionResizeMode(2, QtWidgets.QHeaderView.Interactive) header.setSectionResizeMode(3, QtWidgets.QHeaderView.Stretch) header1 = self.ui.rulesTableWidget.horizontalHeader() header1.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) header1.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents) header1.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch) header1.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents) header1.setSectionResizeMode(4, QtWidgets.QHeaderView.Stretch) # end test connection # create event handler self.ui.actionImport_Items.triggered.connect(self.import_option_click) self.ui.actionItem_List.triggered.connect(self.item_list_click) self.ui.btnRunApriori.clicked.connect(self.run_apriori_click) self.ui.btnFilterItemset.clicked.connect(self.filter_itemset_click) # self.ui.chkShow.clicked.connect(self.show_checked) self.ui.btnFilterAnt.clicked.connect(self.filter_ant_click) self.ui.btnFilterConseq.clicked.connect(self.filter_conseq_click) # end create event handler self.getLastFilter() self.getItemsets() self.getRules() def getLastFilter(self): record = get_last_apriori_filter(self.connection) if record == None: self.ui.lblFomDateFilter.setText("") self.ui.lblToDateFilter.setText("") self.ui.lblMinSupportFilter.setText("") self.ui.lblMinConfFilter.setText("") else: self.ui.lblFomDateFilter.setText((record[0]).strftime("%m/%d/%Y")) self.ui.lblToDateFilter.setText((record[1]).strftime("%m/%d/%Y")) self.ui.lblMinSupportFilter.setText(format(record[2],'0.4f')) self.ui.lblMinConfFilter.setText(format(record[3],'0.2f')) def getItemsets(self): chk = False contain = '' if(self.ui.chkShow.isChecked()): chk = True if len(self.ui.txtFilterContains.text()) > 0: contain = self.ui.txtFilterContains.text() pre_records = get_itemset_list(self.connection,chk,str(self.ui.lblMinSupportFilter.text()),contain) if not pre_records == None: self.ui.itemsetTableWidget.setRowCount(0) for row, record in enumerate(pre_records): self.ui.itemsetTableWidget.insertRow(row) self.ui.itemsetTableWidget.setItem(row,0,QtWidgets.QTableWidgetItem(str(record[0]))) self.ui.itemsetTableWidget.setItem(row,1,QtWidgets.QTableWidgetItem(str(record[1]))) self.ui.itemsetTableWidget.setItem(row,2,QtWidgets.QTableWidgetItem(str(record[2]))) self.ui.itemsetTableWidget.setItem(row,3,QtWidgets.QTableWidgetItem(str(record[3]))) def getRules(self): # select to list records = get_rules_list(self.connection,self.ui.txtFilterContainAnt.text(),self.ui.txtFilterContainConsq.text()) self.ui.rulesTableWidget.setRowCount(0) for row, record in enumerate(records): self.ui.rulesTableWidget.insertRow(row) self.ui.rulesTableWidget.setItem(row,0,QtWidgets.QTableWidgetItem(str(record[0]))) self.ui.rulesTableWidget.setItem(row,1,QtWidgets.QTableWidgetItem('->')) self.ui.rulesTableWidget.setItem(row,2,QtWidgets.QTableWidgetItem(str(record[1]))) self.ui.rulesTableWidget.setItem(row,3,QtWidgets.QTableWidgetItem(format(record[2],'0.4f'))) self.ui.rulesTableWidget.setItem(row,4,QtWidgets.QTableWidgetItem(format(record[3],'0.2f'))) @pyqtSlot() def import_option_click(self): self.importForm = ImportData(self.connection) self.importForm.show() @pyqtSlot() def item_list_click(self): self.items = ItemList(self.connection) self.items.show() @pyqtSlot() def run_apriori_click(self): try: from_date = self.ui.txtFromDate.date() from_date = from_date.toPyDate() to_date = self.ui.txtToDate.date() to_date = to_date.toPyDate() min_support = self.ui.minSupportSpinBox.value() min_confidence = self.ui.minConfSpinBox.value() # preprocessing start_time = datetime.now() total_tran = generate_preprocessing(self.connection,from_date,to_date) # print("Trans : " + str(total_tran)) filter_id = insert_apriori_filter(self.connection,from_date,to_date,min_support,min_confidence,total_tran) end_time = datetime.now() insert_activity_log(self.connection,filter_id,'matching',start_time,end_time) self.connection.commit() # start generate itemsets start_time = datetime.now() dataset = load_dataset(self.connection) # generating frequent_itemsets,support_data = apriori(dataset,min_support) end_time = datetime.now() insert_activity_log(self.connection,filter_id,'generate_itemset',start_time,end_time) insert_itemset(self.connection,support_data,total_tran) # self.connection.commit() # Generating rules start_time = datetime.now() rule_list = generate_rule(frequent_itemsets,support_data,min_confidence) # print(rule_list) end_time = datetime.now() insert_activity_log(self.connection,filter_id,'generate_rules',start_time,end_time) insert_rule(self.connection,rule_list) self.connection.commit() # connection.close() self.getLastFilter() self.getItemsets() self.getRules() except Exception as e: print(str(e)) QMessageBox.warning(None,'Recommendation System',str(e)) @pyqtSlot() def filter_itemset_click(self): self.getItemsets() @pyqtSlot() def filter_ant_click(self): self.getRules() @pyqtSlot() def filter_conseq_click(self): self.getRules()
def test_read_sudoku_from_non_exist_csv(self): import_data = ImportData() sudoku_is_read = import_data.read_sudoku_data_from_file('non_exist.csv') self.assertFalse(sudoku_is_read)
from import_data import ImportData import numpy as np from neural_network import NeuralNetwork from src.shared.math_functions import MathFunctions as mf if __name__ == '__main__': importer = ImportData() X1: np.ndarray = importer.cut_columns_from_data(['Mitoses']) y1: np.ndarray = importer.import_data(np.array(['Class'])) y1 = mf.transform_into_discrete_values(y1) length = X1.shape[0] temp_X = np.array([X1[0]]) temp_y = np.array([y1[0]]) print(temp_X.shape, temp_X) nn = NeuralNetwork(temp_X, temp_y) nn.train_network(X1, y1) good_values = 0 values = 0 for i in range(length): temp_X = np.array([X1[i]]) temp_y = np.array([y1[i]]) predicted_value, true_value = nn.predict_value(temp_X, temp_y) a = round(predicted_value) b = round(true_value) if a == b:
def test_get_extension_double_extension(self): import_data = ImportData() ext = import_data.get_extension_file('file.tar.gz') self.assertEquals(".tar.gz", ext)
def test_get_extension_non_exist_file(self): import_data = ImportData() ext = import_data.get_extension_file('non_exist') self.assertEquals("", ext)
def test_get_extension_different_csv_double_extention(self): import_data = ImportData() ext = import_data.get_extension_file('testinvalidcsvext.csv.mp5') self.assertEquals(".mp5", ext)
def test_get_extension_csv(self): import_data = ImportData() ext = import_data.get_extension_file('testvalidcsv.csv') self.assertEquals(".csv", ext)
def import_option_click(self): self.importForm = ImportData(self.connection) self.importForm.show()
class Main(): def __init__(self): self.configuration = XMLConfig() self.import_data = ImportData() self.empty_spot_char = self.configuration.get_empty_spot_char() self.export_sudoku = ExportSudoku() self.display_main_menu() def display_main_menu(self): """ Displays the Main Menu and validates the entered values for the required options """ print self.logo() self.print_dictionary_list(self.main_menu_options()) while True: op_main_menu = raw_input("\nPlease enter a number: ") if op_main_menu == "1" or op_main_menu == "2" or op_main_menu == "3"\ or op_main_menu == "4" or op_main_menu == "5": self.main_menu_options()[op_main_menu] break else: print "Oops! That was not a valid option number. Try again...\n" self.execute_main_option(op_main_menu) def logo(self): """ Displays the logo on the menu""" logo_str = " _____ _ _ ___\n" + \ "/ ___| | | | | / _ \\\n" + \ "\ `--. _ _ __| | ___ | | ___ _ / /_\ \\\n" + \ " `--. \ | | |/ _` |/ _ \| |/ / | | | | _ |\n" + \ "/\__/ / |_| | (_| | (_) | <| |_| | | | | |\n" + \ "\____/ \__,_|\__,_|\___/|_|\_\\\\__,_| \_| |_/\n" return logo_str def main_menu_options(self): """ Defines the 3 available options for the Sudoku Main Menu """ main_menu_opts = {'1': 'Configure', '2': 'Solve Sudoku', '3': 'Generate Sudoku',\ '4': 'Play online!', '5': 'Exit'} return main_menu_opts def execute_change_solve_option(self, op_solve_menu): """ Executes the Solve Menu for the selected option Keyword arguments: op_solve_menu -- takes one of the three options to select on the Main Menu """ if op_solve_menu == "1": self.solve_sudoku_from_cmd("txt") if op_solve_menu == "2": self.solve_sudoku_from_file("txt") if op_solve_menu == "3": self.solve_sudoku_from_file("txt") if op_solve_menu == "4": self.display_main_menu() def solve_sudoku_from_cmd(self, type_file): """ Method used for solving the sudoku given the input by command line Keyword arguments: type_file -- stores the selected cmd option """ cmd_sudoku = raw_input ('\nEnter the sudoku to solve in the same line without spaces or' 'commas, it should have 81 numbers as the following example:\n0' '03020600900305001001806400008102900700000008006708200002609500' '800203009005010300\n') print "\nSolution:\n" sudoku_matrix = self.import_data.read_sudoku_data_from_line(cmd_sudoku) self.export_sudoku_to(sudoku_matrix, type_file) self.display_main_menu() def export_sudoku_to(self, sudoku_matrix, type_file): """ Method used for exporting the solved sudoku to the default "results" path Keyword arguments: sudoku_matrix -- stores the sudoku on the valid sudoku matrix to solve type_file -- stores the selected file type; file or cmd """ path = "../results/" algoritms = self.create_algorith_to_solve_sudoku(sudoku_matrix, self.empty_spot_char) for algoritm_to_solve in algoritms: sudoku_solved = algoritm_to_solve.solve_sudoku() if sudoku_solved != []: export_to = self.configuration.get_output_type() if export_to == 'file': self.export_file(algoritm_to_solve, type_file, sudoku_solved, path) else: self.export_sudoku.export_sudoku(sudoku_solved, "cmd line", "", "") else: print "\nInvalid sudoku, it cannot be solved!\n" def file_exists(self): """ Method used for verifying whether the entered file exists or not """ result = "" path = "../inputs/" while True : file_name = raw_input('\nEnter the name of the file to solve or enter quit to exit: ') file_txt = path + "\\" + file_name if os.path.exists(file_txt): result = file_txt break elif file_name == "quit": break else: print "\nThe file provided does not exists, please try again\n" return result def solve_sudoku_from_file(self, type_file): """ Method used for solving the sudoku given a file Keyword arguments: type_file -- stores the selected file type, it can be a txt or csv file """ file_ext = self.file_exists() if file_ext != "": sudoku_matrix = self.import_data.read_sudoku_data_from_file(file_ext) self.export_sudoku_to(sudoku_matrix, type_file) #self.display_main_menu() #else: self.display_solve_menu() def export_file(self, algoritm_to_solve, type_file, sudoku_solved, path): """ Method used for create the solved sudoku in a file on the default "results" path Keyword arguments: algoritm_to_solve -- the selected algorithm to solve the sudoku type_file -- stores the selected file type; file or cmd sudoku_solved -- sores the solved sudoku path -- stores the default "results" path for the solutions """ file_name = 'sudoku_solved_' + algoritm_to_solve.__class__.__name__ + "_" + \ str(algoritm_to_solve.get_time()) + "_" + time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime(time.time())) + "." + type_file if self.export_sudoku.export_sudoku(sudoku_solved, type_file, path, file_name) : print "\nThe file was created successfully\n" else: print "\nThere was a error, the file couldn't be created\n" def create_algorith_to_solve_sudoku(self, sudoku_matrix, empty_character): """ Method used for create the algorithm to solve given the expected sudoku matrix and empty character Keyword arguments: sudoku_matrix -- stores the sudoku in the valid matrix ready to be solved empty_character -- stores the selected character for the empty spots """ algorithm_selected = self.configuration.get_algorithm() algoritms = [] for i in range(0, len(sudoku_matrix)): algoritm_to_solve = Algorithm(sudoku_matrix[i][0], empty_character) if algorithm_selected == 'norvig': algoritm_to_solve = NorvigAlgorithm(sudoku_matrix[i][0], empty_character) elif algorithm_selected == 'backtracking': algoritm_to_solve = BacktrakingAlgorithm(sudoku_matrix[i][0], empty_character) elif algorithm_selected == 'brute force': algoritm_to_solve = BruteForceAlgorithm(sudoku_matrix[i][0], empty_character) algoritms.append(algoritm_to_solve) return algoritms def display_solve_menu(self): """ Displays/validates the selected options for solving the sudoku using one of the three valid input types """ self.print_dictionary_list(self.change_solve_menu_options(), "--- Solve Sudoku Menu ---") while True: solve_opts = raw_input("\nPlease enter a number: ") if solve_opts == "1" or solve_opts == "2" or solve_opts == "3" or solve_opts == "4": self.change_solve_menu_options()[solve_opts] break else: print "\nOops! That was not a valid option number. Try again...\n" self.execute_change_solve_option(solve_opts) def change_solve_menu_options(self): """ Defines the 3 available options for the inputs type for solving the sudoku """ solve_opts = {'1': 'Solve sudoku entered by command line', '2': 'Solve sudoku from a txt file', '3': 'Solve sudoku from a csv file', '4': 'Back'} return solve_opts def conf_menu_options(self): """ Defines the 3 available options for the: "1 . Configure" option """ conf_opts = {'1': 'Change Output format', '2': 'Change Level', '3': 'Change Algorithm to use', '4': 'Change empty spot character', '5': 'Back'} return conf_opts def execute_main_option(self, op_main_menu): """ Executes the Main Menu for the selected option Keyword arguments: op_main_menu -- takes one of the four options to select on the Main Menu """ if op_main_menu == "1": self.display_configure_menu() if op_main_menu == "2": self.display_solve_menu() if op_main_menu == "3": self.display_generate_sudoku_menu() if op_main_menu == "4": SudokuUI() if op_main_menu == "5": print "Good bye!" def execute_conf_option(self, conf_opts): """ Executes the selected option on the "1 . Configure" option Keyword arguments: conf_opts -- takes one of the four options available in the option: "1 . Configure" """ if conf_opts == "1": self.display_output_type_menu() if conf_opts == "2": self.display_level_algorithm_menu() if conf_opts == "3": self.display_algorithm_menu() if conf_opts == "4": self.execute_change_empty_spot() if conf_opts == "5": self.display_main_menu() def execute_change_empty_spot(self): """ Method that changes the character used as empty spot """ empty_spot_char = str(raw_input('Enter a new character used as empty spot or' ' enter <quit> to go back menu: ')) if empty_spot_char >= '1' and empty_spot_char <= '9': print "\nInvalid empty spot character, please don't use numbers from 1 to 9" elif empty_spot_char == ',': print "\nInvalid empty spot character, please don't use the comma (,) character" elif len(empty_spot_char) == 1: print "\n", self.configuration.modify_empty_spot_char(empty_spot_char) elif empty_spot_char.lower() == 'quit': print "\nDefault empty spot char will be used: "\ + self.configuration.get_empty_spot_char() else: print "\nInvalid empty spot character, please use only one character" self.display_configure_menu() def execute_change_level_option(self, level_opts): """ Changes the complexity level for create or solve the sudoku Keyword arguments: level_opts -- takes one of the three options for the complexity level """ if level_opts == "1": # save Easy level print "\n", self.configuration.modify_complexity('Easy') if level_opts == "2": # save Medium level print "\n", self.configuration.modify_complexity('Medium') if level_opts == "3": # save Hard level print "\n", self.configuration.modify_complexity('Hard') self.execute_change_algorithm_option('4') def execute_change_algorithm_option(self, chng_algorit): """ Changes the Algorithm used for create or solve the sudoku Keyword arguments: chng_algorit -- takes one of the three options for changing the Algorithm to use """ if chng_algorit == "1": # save Norvig algorithm print "\n", self.configuration.modify_algorithm('Norvig') if chng_algorit == "2": # save Backtraking algorithm print "\n", self.configuration.modify_algorithm('Backtracking') if chng_algorit == "3": # save Brute Force algorithm print "\n", self.configuration.modify_algorithm("brute force") self.display_configure_menu() def execute_change_output_option(self, output_opts): """ Changes the output format for the solved the sudoku Keyword arguments: output_opts -- takes one of the two options, solved sudoku could be displayed in console, or in a file """ if output_opts == "1": # display solved sudoku in console print "\n", self.configuration.modify_output_type('console') if output_opts == "2": # save solved sudoku in a file print "\n", self.configuration.modify_output_type('file') self.display_configure_menu() def execute_generate_sudoku_option(self, generate_opts): """Executes the action selected by the user save sudoku on TXT file or diplay in console Keyword arguments: generate_opts -- takes one of the two options, generated sudoku could be displayed in console, or in a file """ if generate_opts in ['1', '2']: complexity = self.configuration.get_complexity() min_holes = self.configuration.get_min_holes_by_complexity(complexity) max_holes = self.configuration.get_max_holes_by_complexity(complexity) empty_spot_char = self.configuration.get_empty_spot_char() sudoku_generator = SudokuGenerator() sudoku_generator.generate_sudoku_pattern_by_complexity(min_holes, max_holes, empty_spot_char) if generate_opts == "1": # save sudoku generated in file name_sudoku = "sudoku_generated_" + time.strftime("%Y-%m-%dT%H-%M-%S",\ time.localtime(time.time())) + ".txt" if self.export_sudoku.export_sudoku(sudoku_generator.sudoku_pattern, 'txt', "../results/", name_sudoku) is True: print "\nSudoku was saved to: '",name_sudoku,"'" self.display_main_menu() else: print "\nFailed to save in TXT file. Try again...!" self.display_generate_sudoku_menu() if generate_opts == "2": # display generate sudoku in console print "\nGenerated Sudoku:\n" self.export_sudoku.export_sudoku(sudoku_generator.sudoku_pattern, 'cmd line', '', '') self.display_main_menu() if generate_opts == "3": self.display_main_menu() def display_algorithm_menu(self): """ Displays/validates the selected options for the option: 3 . Change Algorithm to solve sudokus """ self.print_dictionary_list(self.change_algorithm_menu_options(), "--- Change Algorithm Menu ---") while True: algorit_opts = raw_input("\nPlease enter a number: ") if algorit_opts == "1" or algorit_opts == "2" or algorit_opts == "3"\ or algorit_opts == "4": self.change_algorithm_menu_options()[algorit_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_change_algorithm_option(algorit_opts) def display_level_algorithm_menu(self): """ Displays and validates the selected options for the option "2 . Change level" """ self.print_dictionary_list(self.change_level_menu_options(), "--- Change Complexity Level Menu ---") while True: level_opts = raw_input("\nPlease enter a number: ") if level_opts == "1" or level_opts == "2" or level_opts == "3" or level_opts == "4": self.change_level_menu_options()[level_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_change_level_option(level_opts) def display_output_type_menu(self): """ Displays/validates the selected options for the option: 1 . Change output format """ self.print_dictionary_list(self.change_output_type_options(), "--- Change Output Type Menu ---") while True: output_opts = raw_input("\nPlease enter a number: ") if output_opts == "1" or output_opts == "2" or output_opts == "3": self.change_output_type_options()[output_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_change_output_option(output_opts) def display_generate_sudoku_menu(self): """ Displays and validates the selected options for the option "3. Generate Sudoku" """ self.print_dictionary_list(self.change_generate_sudoku_options(), "--- Generate Sudoku Menu ---") while True: generate_opts = raw_input("\nPlease enter a number: ") if generate_opts == "1" or generate_opts == "2" or generate_opts == "3": self.change_generate_sudoku_options()[generate_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_generate_sudoku_option(generate_opts) def display_configure_menu(self): """ Displays and validates the selected options for the option "1 . Configure" """ self.print_dictionary_list(self.conf_menu_options(), "--- Configuration Menu ---") while True: config_opts = raw_input("\nPlease enter a number: ") if config_opts == "1" or config_opts == "2" or config_opts == "3"\ or config_opts == "4" or config_opts == "5": self.conf_menu_options()[config_opts] break else: print "Oops! That was not a valid option number. Try again..." self.execute_conf_option(config_opts) def change_generate_sudoku_options(self): """ Defines the 3 available options for the option "3. Generate Sudoku" """ generate_opts = {'1': 'Save Sudoku to TXT file', '2': 'Display Sudoku on console',\ '3': 'Back to Main menu'} return generate_opts def change_output_type_options(self): """ Defines the 3 available options for the option "1 . Change output format" """ output_opts = {'1': 'Display result in console', '2': 'Save result in a file', '3': 'Back'} return output_opts def change_algorithm_menu_options(self): """ Defines the available Algorithm methods for the sudoku resolution """ algorit_opts = {'1': 'Norvig', '2': 'Backtracking', '3': 'Brute Force', '4': 'Back'} return algorit_opts def change_level_menu_options(self): """ Defines the available levels for the option "2 . Change level" """ level_opts = {'1': 'Easy', '2': 'Medium', '3': 'Hard', '4': 'Back'} return level_opts def print_dictionary_list(self, dictionary, title_menu = ""): """ Prints the dictionary list for each value """ if title_menu != "": print "\n" + title_menu + "\n" for n in range (1, len (dictionary) + 1): for key, value in dictionary.iteritems() : if str(n) == key: print key, value
def test_read_sudoku_from_invalid_csv(self): expected_size = -1 import_data = ImportData() sudoku = import_data.read_sudoku_data_from_file('testinvalidcsv.csv') self.assertEquals(sudoku[0][1], expected_size)