Skip to content

analyze_recipe seems to multiply nutrients incorrectly #3

@kevinsu628

Description

@kevinsu628

Hi, thanks for this amazing open source project!

I think I've discovered a bug in the analyze_recipe method.

ef analyze_recipe(
    recipe: Recipe,
    dri_type: DriType = DriType.RDA,
    gender: Gender = Gender.MALE,
    age: int = 30
) -> RecipeAnalysis:
    """
    Analyze the nutrient content of a recipe.
    
    Args:
        recipe: The recipe to analyze.
        dri_type: The type of DRI to use for comparison.
        gender: The gender to use for DRI values.
        age: The age to use for DRI values.
        
    Returns:
        A RecipeAnalysis object.
    """
    ...
    
    # Combine nutrients from all ingredients
    nutrient_map: Dict[int, Nutrient] = {}
    
    for analysis in ingredient_analyses:
        for nutrient in analysis.food.nutrients:
            if nutrient.id in nutrient_map:
                # Add to existing nutrient
                existing = nutrient_map[nutrient.id]
                existing.amount += nutrient.amount * (analysis.serving_size / 100.0)
            else:
                # Create new nutrient
                new_nutrient = Nutrient(
                    id=nutrient.id,
                    name=nutrient.name,
                    amount=nutrient.amount * (analysis.serving_size / 100.0),
                    unit_name=nutrient.unit_name,
                    nutrient_nbr=nutrient.nutrient_nbr,
                    rank=nutrient.rank
                )
                nutrient_map[nutrient.id] = new_nutrient
                combined_food.nutrients.append(new_nutrient)
    
    # Analyze the combined food per serving
    per_serving_analysis = analyze_food(
        combined_food,
        serving_size=recipe.get_weight_per_serving(),
        dri_type=dri_type,
        gender=gender,
        age=age
    )
    
    return RecipeAnalysis(
        recipe=recipe,
        per_serving_analysis=per_serving_analysis,
        ingredient_analyses=ingredient_analyses
    )

while calculating each nutrient, we already multiply the nutrient amount by the serving size factor: amount=nutrient.amount * (analysis.serving_size / 100.0),

but while calculating per_serving_analysis, we are multiply by the recipe.get_weight_per_serving() again by the analyze_food function.

For example, if I have this recipe with only 1 ingredient:

'1 cup oatmeal'

the converted gram is 240g, the food entry taken is the 1st match , then the factor is 240g/100g = 2.4.
This 1995469 oatmeal has 1580 kcal per 100g, so 1580 x 2.4 = 3,792 kcal for 1 serving.
But 3,792 kcal is multiplied by recipe.get_weight_per_serving() again, which gives me calories_per_serving = 9089.279999999999

This doesn't seem correct to me.

Hope to get any response. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions