intakeinformer.data_processing
Module Contents
Functions
|
Calculates the calorie content per 100g for user-selected food items and returns a dictionary with individual and total calories. |
|
Extracts macronutrient and other nutrient information from the foodNutrients data structure, |
- intakeinformer.data_processing.calculate_calories_from_selection(selected_items)
Calculates the calorie content per 100g for user-selected food items and returns a dictionary with individual and total calories. It first tries to use the ‘Energy’ nutrient value (Energy_id = 1008) and falls back to calculating calories from protein, fat, and carbohydrates if ‘Energy’ is not located.
- Parameters:
selected_items (list) – A list of DataFrame rows, each representing a user-selected food item. Each row should contain the food nutrients data.
- Returns:
A dictionary where each key is the food item’s name, and the corresponding value is its calorie content per 100g. The dictionary also includes a ‘total’ key representing the sum of calories from all selected items.
- Return type:
dict
Examples
# Assuming selected_banana, selected_apple, and selected_orange are DataFrame rows obtained from user selections >>> selected_items = [selected_banana, selected_apple, selected_orange] >>> calories_content = calculate_calories_from_selection(selected_items) >>> print(calories_content) {‘Banana’: 89.0, ‘Apple’: 52.0, ‘Orange’: 47.0, ‘total’: 188.0}
- intakeinformer.data_processing.extract_nutrients(food_nutrients)
Extracts macronutrient and other nutrient information from the foodNutrients data structure, obtained from the USDA FoodData Central API via get_user_food_selection() function.
This function processes a list of nutrient data for a given food item, identifying and extracting key nutrients like carbohydrates, proteins, fats, and selected vitamins and minerals. It utilizes regular expressions to match nutrient names from the data with predefined patterns, ensuring accurate identification despite potential variations in nutrient naming.
- Parameters:
food_nutrients (list) – A list of dictionaries, each representing a nutrient data point for a food item. Each dictionary typically includes nutrient names, values, units, and other metadata.
- Returns:
A dictionary mapping nutrient names (e.g., ‘Carbohydrate(g/d)’, ‘Proteinb(g/d)’) to their corresponding values extracted from the food_nutrients data. If a nutrient is not found, its value in the dictionary is set to None.
- Return type:
dict
Examples
>>> food_nutrients = [ {'nutrientName': 'Protein', 'value': 2.5}, {'nutrientName': 'Total lipid (fat)', 'value': 0.5}, {'nutrientName': 'Carbohydrate, by difference', 'value': 30}, {'nutrientName': 'Fiber, total dietary', 'value': 2}, {'nutrientName': 'Calcium, Ca', 'value': 50}, # ... more nutrients ... ] >>> extracted_nutrients = extract_nutrients(food_nutrients) >>> print(extracted_nutrients) {'Carbohydrate(g/d)': 30, 'Total Fiber(g/d)': 2, 'Fat(g/d)': 0.5, 'Proteinb(g/d)': 2.5, 'Calcium (mg/d)': 50, ...}