def test_write_shopping_list(sefl): kitchen1 = Kitchen() assert len(kitchen1.pantry) == 0 # add food to pantry food1 = Food('food1', 'category1', 'unit1', quantity_needed_in_stock=2) food2 = Food('food2', 'category2', 'unit2', quantity_needed_in_stock=4) food3 = Food('food3', 'category3', 'unit3', quantity_needed_in_stock=10) kitchen1.add_to_pantry(food1, quantity=1) # in list kitchen1.add_to_pantry(food2, quantity=7) # not in list kitchen1.add_to_pantry(food3, quantity=4) # in list assert len(kitchen1.pantry) == 3 kitchen1.make_shopping_list() assert len(kitchen1.shopping_list) == 2 data = Data() data.write_shopping_list(kitchen1.shopping_list, 'test/test_write_shopping_list.json') with open('test/test_write_shopping_list.json', 'r') as list_reader: written_list = json.loads(list_reader.read()) assert written_list['food1'] == 1 assert written_list['food3'] == 6 list_reader.close()
def __init__(self): # kitchen and data objects self.data = Data() self.kitchen = Kitchen() self.kitchen.pantry = self.data.read_pantry('src/demo/pantry.json') self.kitchen.recipies = self.data.read_recipies( 'src/demo/recipies.json') # main screen self.stdscr = curses.initscr() self.height, self.width = self.stdscr.getmaxyx() # windows self.menubar = curses.newwin(2, self.width, 0, 0) self.menubar.addstr("Kitman") self.menubar.refresh() self.data_window = curses.newwin(self.height - 5, (self.width // 2), 3, 0) self.preview_window = curses.newwin(self.height - 5, self.width // 2, 3, (self.width // 2)) self.option_window = curses.newwin(2, self.width, self.height - 2, 0) # colors curses.start_color() curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
def test_read_pantry(self): data = Data() foods = data.read_pantry('test/test_read_food.json') assert len(foods) == 2 # check that Food objects are the value in the pair for food in foods: assert isinstance(foods[food], Food) # check object attributes assert foods['food4'].name == 'food4' assert foods['food4'].unit == 'unit4' assert foods['food4'].category == 'category4' assert foods['food4'].quantity_needed_in_stock == 4 assert foods['food5'].name == 'food5' assert foods['food5'].unit == 'unit5' assert foods['food5'].category == 'category5' assert foods['food5'].quantity_needed_in_stock == 5
def test_write_pantry(self): # initialize kitchen with food and recipies kitchen = Kitchen() food1 = Food(name='food1', unit='unit1', category='category1', quantity_needed_in_stock=10) food2 = Food('food2', 'category2', 'unit2', quantity_needed_in_stock=20) kitchen.add_to_pantry(food1, 10) kitchen.add_to_pantry(food2, 20) data = Data() data.write_pantry(kitchen.pantry, 'test/test_write_pantry.json') # check file for valid json objects with open('test/test_write_pantry.json') as file: json_data = json.loads(file.read()) assert type(json_data[0]) == dict assert json_data[0]['_name'] == 'food1' assert json_data[0]['_unit'] == 'unit1' assert json_data[0]['_category'] == 'category1' assert json_data[0]['_quantity_needed_in_stock'] == 10 assert json_data[0]['_quantity_in_stock'] == 10 assert type(json_data[1]) == dict assert json_data[1]['_name'] == 'food2' assert json_data[1]['_unit'] == 'unit2' assert json_data[1]['_category'] == 'category2' assert json_data[1]['_quantity_needed_in_stock'] == 20 assert json_data[1]['_quantity_in_stock'] == 20 file.close()
def test_read_recipies(self): data = Data() recipies = data.read_recipies('test/test_read_recipies.json') assert len(recipies) == 2 for recipie in recipies: assert isinstance(recipie, Recipie) test_recipie1 = recipies[0] assert test_recipie1.name == 'test_recipie1' assert test_recipie1.serving_number == 1 assert isinstance(test_recipie1.ingredients, dict) assert test_recipie1.ingredients['ingredient11'] == 11 assert test_recipie1.ingredients['ingredient12'] == 12 test_recipie2 = recipies[1] assert test_recipie2.name == 'test_recipie2' assert test_recipie2.serving_number == 2 assert isinstance(test_recipie2.ingredients, dict) assert test_recipie2.ingredients['ingredient21'] == 21 assert test_recipie2.ingredients['ingredient22'] == 22 assert isinstance(test_recipie1.instructions, list) assert test_recipie1.instructions[0] == "instruction_11" assert test_recipie1.instructions[1] == "instruction_12" assert test_recipie1.instructions[2] == "instruction_13" assert test_recipie1.instructions[3] == "instruction_14" assert isinstance(test_recipie2.instructions, list) assert test_recipie2.instructions[0] == "instruction_21" assert test_recipie2.instructions[1] == "instruction_22" assert test_recipie2.instructions[2] == "instruction_23" assert test_recipie2.instructions[3] == "instruction_24"
def test_write_recipies(self): data = Data() instruction_1 = "instruction_11" instruction_2 = "instruction_12" instruction_3 = "instruction_13" instruction_4 = "instruction_14" instruction_list1 = [ instruction_1, instruction_2, instruction_3, instruction_4 ] recipie1 = Recipie('recipie01', 10, dict(), instruction_list1) instruction_1 = "instruction_21" instruction_2 = "instruction_22" instruction_3 = "instruction_23" instruction_4 = "instruction_24" instruction_list2 = [ instruction_1, instruction_2, instruction_3, instruction_4 ] recipie2 = Recipie('recipie02', 20, { 'ingredient21': 21, 'ingredient22': 22 }, instruction_list2) recipie3 = Recipie('recipie03', 30, { 'ingredient31': 31, 'ingredient32': 32, 'ingredient33': 33 }) recipies = [recipie1, recipie2, recipie3] data.write_recipies(recipies, 'test/test_write_recipies.json') with open('test/test_write_recipies.json', 'r') as file: json_data = json.loads(file.read()) assert type(json_data[0]) == dict assert type(json_data[1]) == dict assert type(json_data[2]) == dict assert json_data[0]['_name'] == 'recipie01' assert json_data[1]['_name'] == 'recipie02' assert json_data[2]['_name'] == 'recipie03' assert json_data[0]['_serving_number'] == 10 assert json_data[1]['_serving_number'] == 20 assert json_data[2]['_serving_number'] == 30 assert type(json_data[0]['ingredients']) == dict assert type(json_data[1]['ingredients']) == dict assert type(json_data[2]['ingredients']) == dict assert len(json_data[0]['ingredients']) == 0 assert len(json_data[1]['ingredients']) == 2 assert len(json_data[2]['ingredients']) == 3 assert json_data[1]['ingredients']['ingredient21'] == 21 assert json_data[1]['ingredients']['ingredient22'] == 22 assert json_data[2]['ingredients']['ingredient31'] == 31 assert json_data[2]['ingredients']['ingredient32'] == 32 assert json_data[2]['ingredients']['ingredient33'] == 33 assert json_data[0]["instructions"][0] == "instruction_11" assert json_data[0]["instructions"][1] == "instruction_12" assert json_data[0]["instructions"][2] == "instruction_13" assert json_data[0]["instructions"][3] == "instruction_14" assert json_data[1]["instructions"][0] == "instruction_21" assert json_data[1]["instructions"][1] == "instruction_22" assert json_data[1]["instructions"][2] == "instruction_23" assert json_data[1]["instructions"][3] == "instruction_24" assert len(json_data[2]["instructions"]) == 0 file.close()
class Menu: def __init__(self): # kitchen and data objects self.data = Data() self.kitchen = Kitchen() self.kitchen.pantry = self.data.read_pantry('src/demo/pantry.json') self.kitchen.recipies = self.data.read_recipies( 'src/demo/recipies.json') # main screen self.stdscr = curses.initscr() self.height, self.width = self.stdscr.getmaxyx() # windows self.menubar = curses.newwin(2, self.width, 0, 0) self.menubar.addstr("Kitman") self.menubar.refresh() self.data_window = curses.newwin(self.height - 5, (self.width // 2), 3, 0) self.preview_window = curses.newwin(self.height - 5, self.width // 2, 3, (self.width // 2)) self.option_window = curses.newwin(2, self.width, self.height - 2, 0) # colors curses.start_color() curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) def menu(self, stdscr, current_row, menu_mode): """Main menu bar""" menu_bar = ["1: Pantry ", "2: Recipies ", "3: Shopping "] y = 0 x = 1 for idx, element in enumerate(menu_bar): if idx == menu_mode - 1: self.menubar.attron(curses.color_pair(1)) self.menubar.addstr(y, x, element) self.menubar.attroff(curses.color_pair(1)) else: self.menubar.addstr(y, x, element) # word spacing x += len(element) + len(menu_bar) // (len(menu_bar) - 1) self.menubar.refresh() def pantry(self, stdscr, current_row): """Show pantry in the data window and food details in the preview window""" self.data_window.clear() self.preview_window.clear() self.data_window.addstr(0, 0, "Food\tQuantity in stock") y = 2 x = 0 for idx, food in enumerate(self.kitchen.pantry): food_obj = self.kitchen.pantry[food] # highlight current row if idx == current_row: self.data_window.attron(curses.color_pair(1)) self.data_window.addstr( y, x, food.capitalize() + '\t' + str(food_obj.quantity_in_stock) + ' ' + food_obj.unit) self.data_window.attroff(curses.color_pair(1)) self.preview_window.addstr(str(food_obj)) else: self.data_window.addstr( y, x, food.capitalize() + '\t' + str(food_obj.quantity_in_stock) + ' ' + food_obj.unit) y += 2 self.data_window.refresh() self.preview_window.refresh() def recipies(self, stdscr, current_row): """show recipies in the data window and details in preview window""" self.data_window.clear() self.preview_window.clear() self.data_window.addstr(0, 0, "Recipies") self.preview_window.addstr(0, 0, "Ingredient\tQuantity\n\n") y = 2 x = 1 for idx, recipie in enumerate(self.kitchen.recipies): if idx == current_row: self.data_window.attron(curses.color_pair(1)) self.data_window.addstr(y, x, recipie.name.capitalize()) self.data_window.attroff(curses.color_pair(1)) for ingredient in recipie.ingredients: self.preview_window.addstr( ingredient.capitalize() + '\t' + str(recipie.ingredients[ingredient]) + ' ' + self.kitchen.pantry[ingredient].unit + '\n\n') for instruction in recipie.instructions: self.preview_window.addstr(instruction.capitalize() + '\n\n') else: self.data_window.addstr(y, x, recipie.name.capitalize()) y += 2 self.data_window.refresh() self.preview_window.refresh() def options(self, stdscr, current_row, menu_mode): """Options for additions, removals, modifications etc.""" self.option_window.clear() pantry_options = [ "a: Add to stock", "r: Remove from stock", "c: Change stock", "m: Modify food" ] recipie_options = ["a: Add", "r: Remove", "m: Modify"] if menu_mode == 1: for element in pantry_options: self.option_window.addstr(element + "\t") elif menu_mode == 2: for element in recipie_options: self.option_window.addstr(element + "\t") self.option_window.refresh() def refresh_data_window(self, stdscr, current_row, menu_mode): """Check what menu item method was selected with scroll event and call that method""" if menu_mode == 1: self.pantry(stdscr, current_row) elif menu_mode == 2: self.recipies(stdscr, current_row) self.menu(stdscr, current_row, menu_mode) self.options(stdscr, current_row, menu_mode) def main(self, stdscr): """Main event loop and scroll control""" curses.curs_set(0) menu_mode = 1 current_row = 0 self.menu(self.stdscr, current_row, 1) self.options(stdscr, current_row, 1) self.pantry(stdscr, current_row) while True: key = self.stdscr.getch() # main menu selection if key == ord('1') or key == ord('h') and menu_mode == 2: current_row = 0 menu_mode = 1 self.refresh_data_window(stdscr, current_row, menu_mode) elif key == ord('2') \ or key == ord('l') and menu_mode == 1 \ or key == ord('h') and menu_mode == 3: menu_mode = 2 current_row = 0 self.refresh_data_window(stdscr, current_row, menu_mode) elif key == ord("q"): break # scrolling elif (key == curses.KEY_UP or key == ord("k")) \ and current_row > 0: current_row -= 1 self.refresh_data_window(stdscr, current_row, menu_mode) elif (key == curses.KEY_DOWN or key == ord("j")): if menu_mode == 1 \ and current_row < len(self.kitchen.pantry) - 1: current_row += 1 elif menu_mode == 2 \ and current_row < len(self.kitchen.recipies) - 1: current_row += 1 self.refresh_data_window(stdscr, current_row, menu_mode) self.stdscr.refresh()
"""This script takes in a preprocessed dataset and trains a classifier, it finds the accuracy score and bias factor """ import pandas as pd from data_operations import Data file_name = '../data/processed_adult.csv' data_input = Data(file_name) X_train, X_test, y_train, y_test, Z_train, Z_test = data_input.data_split() # initialise NeuralNet Classifier clf_model = data_input.nn_classifier(X_train.shape[1]) # predict on test set y_pred, roc, accuracy = data_input.predict(clf_model, X_train, X_test, y_train.values, y_test) print(f"ROC AUC: {roc:.2f}") print(f"Accuracy: {accuracy:.1f}%") p_val = data_input.p_rule(y_pred.round(), Z_test) print("The classifier satisfies the following p-rule: " + str(p_val))