intakeinformer.data_fetching

Module Contents

Functions

_fetch_food_data(FDC_Key, query)

Fetches food data from the USDA FoodData Central API for a specified query.

get_user_food_selection(food_df)

Prompts the user to select a specific food item from a list presented based on the queried food.

get_user_life_stage()

Interactively prompts users to identify their life stage category and age range

intakeinformer.data_fetching._fetch_food_data(FDC_Key, query)

Fetches food data from the USDA FoodData Central API for a specified query.

This function sends a GET request to the USDA FoodData Central API with a given food query and returns the results as a pandas DataFrame. It handles HTTP errors and returns an empty DataFrame if no data is found.

Parameters:
  • FDC_Key (str) – The USDA API Key that works as personal access token to the API.

  • query (str) – The food query string to search for in the USDA FoodData Central API.

Returns:

A DataFrame containing the fetched food data. Each row represents a different food item, with columns for various food attributes.

Return type:

pandas.DataFrame

Raises:
  • HTTPError – If the HTTP request to the USDA API fails.

  • Exception – For general exceptions that may occur during the API request.

Examples

>>> _fetch_food_data(FDC_Key, "apple")
DataFrame containing data for various apple products, with columns such as 'description', 'brandOwner', and nutritional information.

Notes

The function uses an internal API key defined as FDC_key. Ensure this key is properly set before using the function. The function is intended for internal use within the package and is not exposed to the end users.

intakeinformer.data_fetching.get_user_food_selection(food_df)

Prompts the user to select a specific food item from a list presented based on the queried food.

This function displays a list of food items retrieved from the USDA FoodData Central API, allowing the user to select one. The list includes various brands or types of the food item queried. The user’s selection is returned for further processing, such as nutrient analysis or calorie calculation.

Parameters:

food_df (pandas.DataFrame) – A DataFrame containing a list of food items, obtained from the USDA FoodData Central API. Each row represents a different variety or brand of the queried food.

Returns:

A single-row DataFrame representing the user’s selected food item.

Return type:

pandas.DataFrame

Examples

>>> food_df = _fetch_food_data("apple")
>>> selected_apple = get_user_food_selection(food_df)
>>> print(selected_apple)

Notes

  • The function works as an internal helper function for both main functions.

intakeinformer.data_fetching.get_user_life_stage()

Interactively prompts users to identify their life stage category and age range for Dietary Reference Intake (DRI) comparison.

This function asks users to choose from predefined life stage categories and age ranges that align with DRI standards. These categories include options for Infants, Children, Males, Females, Pregnancy, and Lactation, with specific age ranges under each. The user’s selections are returned as a tuple, which can be used to retrieve appropriate DRI benchmarks for nutrient comparison.

Returns:

A tuple containing two strings: the life stage category (e.g., ‘Children’) and the specific age range within that category (e.g., ‘1–3 y’).

Return type:

tuple

Examples

>>> life_stage_info = get_user_life_stage()
>>> print(life_stage_info)
('Children', '1–3 y')