def get_remove_existing_foods_list(x, y, theta): dt_product = regression.dot_product(x, theta) # required = y[0] # computed_total = dt_product[0] ratio = (y / dt_product).tolist()[0] difference = (y - dt_product).tolist()[0] remove_existing = [] for i in range(len(ratio)): if ratio[i] < 0.5 or difference[i] < (-50): remove_existing.append(i) return [constants.REQUIRED_NUTRIENT_LIST[x] for x in remove_existing]
def getRemoveExistingFoodsList(x, y, theta): dtProduct = regression.dot_product(x, theta) required = y[0] computedTotal = dtProduct[0] ratio = (y / dtProduct).tolist()[0] difference = (y - dtProduct).tolist()[0] removeExisting = [] for i in range(len(ratio)): if (ratio[i] < 0.5 or difference[i] < (-50)): removeExisting.append(i) return [reqMineralList[x] for x in removeExisting]
def get_add_more_list(x, y, theta): dt_product = regression.dot_product(x, theta) # required = y[0] # computed_total = dt_product[0] ratio = (y / dt_product).tolist()[0] difference = (y - dt_product).tolist()[0] add_more_list = [] for i in range(len(ratio)): if ratio[i] > 2 or difference[i] > 50: add_more_list.append(i) return [constants.REQUIRED_NUTRIENT_LIST[x] for x in add_more_list]
def getAddMoreList(x, y, theta): dtProduct = regression.dot_product(x, theta) required = y[0] computedTotal = dtProduct[0] ratio = (y / dtProduct).tolist()[0] difference = (y - dtProduct).tolist()[0] addMoreList = [] for i in range(len(ratio)): if (ratio[i] > 2 or difference[i] > 50): addMoreList.append(i) return [reqMineralList[x] for x in addMoreList]
def display_output(x, y, theta, final_foods, req_mineral_list): with codecs.open(constants.NUTRIENT_DETAILS_FILE, 'r', encoding='utf-8', errors='ignore') as nutrient_detail_file: food_items = nutrient_detail_file.read().split('\n')[1:] food_items = [food_item.split("^") for food_item in food_items] food_dict = {} [ food_dict.update({str(int(food_item[0])): food_item}) for food_item in food_items ] required_mineral_names = [ constants.NUTRIENT_LIST[i] for i in req_mineral_list ] output_double_array = list([]) output_double_array.append(['Food Name', 'ID', 'weight'] + required_mineral_names) output_double_array.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(required_mineral_names)) for food in range(len(final_foods)): food_name = food_dict[final_foods[food]][3] weight = theta[:, 1][food] * 100 food_id = final_foods[food] req_nutrient_values = [ float(food_dict[final_foods[food]][y1]) * (float(weight / 100)) for y1 in [x + constants.NUTRIENT_START_INDEX for x in req_mineral_list] ] output_double_array.append([food_name, food_id, weight] + req_nutrient_values) output_double_array.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(required_mineral_names)) output_double_array.append(['Total', '', ''] + regression.dot_product(x, theta).tolist()[0]) output_double_array.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(required_mineral_names)) output_double_array.append(['Required', '', ''] + y.tolist()[0]) output_double_array.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(required_mineral_names)) output_double_array.append(['Difference', '', ''] + ( (y - regression.dot_product(x, theta)).tolist()[0])) output_double_array.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(required_mineral_names)) output_string = '' for row in output_double_array[:2]: out_row = '' out_row += "{0:<27}".format(row[0][:25]) \ + "{0:<12}".format(row[1][:10]) \ + "{0:<12}".format(str(row[2])[:10]) for index in range(3, len(row)): out_row += "{0:<12}".format(str(row[index])[:10]) # print(outRow) output_string += out_row output_string += '\n' for row in output_double_array[2:-7]: out_row = '' out_row += "{0:<27}".format(row[0][:25]) \ + "{0:<12}".format(row[1][:10]) \ + "{0:<12}".format(str(row[2])[:10]) for index in range(3, len(row)): out_row += "{0:<12}".format( str('{:f}'.format(float(row[index])))[:10]) output_string += out_row output_string += '\n' for row in output_double_array[len(output_double_array) - 7:]: out_row = '' out_row += "{0:<27}".format(row[0][:25]) \ + "{0:<12}".format(row[1][:10]) \ + "{0:<12}".format(str(row[2])[:10]) for index in range(3, len(row)): out_row += "{0:<12}".format(str(row[index])[:10]) output_string += out_row output_string += '\n' open(constants.OUTPUT_FILE, 'w').write(output_string)
def display_output(x, y, theta, finalFoods, GRAMS, daily_nutrients_limit_y, reqMineralList): food_items = list([]) with codecs.open(foodsWithNutrientDetalsFinalFile, 'r', encoding='utf-8', errors='ignore') as fdata: food_items = fdata.read().split('\n')[1:] food_items = [food_item.split("^") for food_item in food_items] food_dict = {} [ food_dict.update({str(int(food_item[0])): food_item}) for food_item in food_items ] requiredMineralNames = [NUTRIENT_LIST[i] for i in reqMineralList] #print(requiredMineralNames) outputDoubleArray = list([]) outputDoubleArray.append(['Food Name', 'ID', 'weight'] + requiredMineralNames) outputDoubleArray.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(requiredMineralNames)) for looper3 in range(len(finalFoods)): foodName = food_dict[finalFoods[looper3]][3] weight = theta[:, 1][looper3] * 100 id = finalFoods[looper3] reqNutrientValues = [ float(food_dict[finalFoods[looper3]][y1]) * (float(weight / 100)) for y1 in [x + NUTRIENT_START_INDEX for x in reqMineralList] ] outputDoubleArray.append([foodName, id, weight] + reqNutrientValues) outputDoubleArray.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(requiredMineralNames)) outputDoubleArray.append(['Total', '', ''] + regression.dot_product(x, theta).tolist()[0]) outputDoubleArray.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(requiredMineralNames)) outputDoubleArray.append(['Required', '', ''] + y.tolist()[0]) outputDoubleArray.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(requiredMineralNames)) outputDoubleArray.append(['Difference', '', ''] + ( (y - regression.dot_product(x, theta)).tolist()[0])) outputDoubleArray.append(['-' * 27, '-' * 12, '-' * 12] + ['-' * 12] * len(requiredMineralNames)) outputString = '' for row in outputDoubleArray[:2]: outRow = '' outRow += "{0:<27}".format(row[0][:25]) + "{0:<12}".format( row[1][:10]) + "{0:<12}".format(str(row[2])[:10]) for index in range(3, len(row)): outRow += "{0:<12}".format(str(row[index])[:10]) #print(outRow) outputString += outRow outputString += '\n' for row in outputDoubleArray[2:-7]: outRow = '' outRow += "{0:<27}".format(row[0][:25]) + "{0:<12}".format( row[1][:10]) + "{0:<12}".format(str(row[2])[:10]) for index in range(3, len(row)): outRow += "{0:<12}".format( str('{:f}'.format(float(row[index])))[:10]) #print(outRow) outputString += outRow outputString += '\n' for row in outputDoubleArray[len(outputDoubleArray) - 7:]: outRow = '' outRow += "{0:<27}".format(row[0][:25]) + "{0:<12}".format( row[1][:10]) + "{0:<12}".format(str(row[2])[:10]) for index in range(3, len(row)): outRow += "{0:<12}".format(str(row[index])[:10]) #print(outRow) outputString += outRow outputString += '\n' open(OUTPUT_FILE, 'w').write(outputString)