Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set max percent of sugar and salt ingredients based on nutrition facts #9276

Merged
merged 9 commits into from
Nov 16, 2023

Conversation

stephanegigandet
Copy link
Contributor

@stephanegigandet stephanegigandet commented Nov 9, 2023

A small improvement to ingredients percent estimation, made to test the new https://github.com/openfoodfacts/recipe-estimator-metrics framework.

Corresponding changes: openfoodfacts/recipe-estimator-metrics@32cb058

e.g. test summary differences on a small test set of 62 products:

test-sets/results/product_opener/fr-62-products-10-specified-ingredients/results_summary.json

image

@stephanegigandet stephanegigandet requested a review from a team as a code owner November 9, 2023 12:52
@github-actions github-actions bot added 🥗 Ingredients 🧽 Data quality https://wiki.openfoodfacts.org/Quality 🧪 tests 🥗🔍 Ingredients analysis https://wiki.openfoodfacts.org/Ingredients_Extraction_and_Analysis Nutri-Score labels Nov 9, 2023
stephanegigandet added a commit to openfoodfacts/recipe-estimator-metrics that referenced this pull request Nov 9, 2023
@codecov-commenter
Copy link

codecov-commenter commented Nov 9, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (f36b591) 47.95% compared to head (770a1bc) 48.04%.
Report is 21 commits behind head on main.

Files Patch % Lines
lib/ProductOpener/Ingredients.pm 95.65% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9276      +/-   ##
==========================================
+ Coverage   47.95%   48.04%   +0.09%     
==========================================
  Files          65       65              
  Lines       20277    20304      +27     
  Branches     4914     4922       +8     
==========================================
+ Hits         9724     9756      +32     
+ Misses       9301     9298       -3     
+ Partials     1252     1250       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stephanegigandet
Copy link
Contributor Author

I added a new test set with 1000 products with some specified ingredients percent.

Before this PR:

Test set world-1000-some-specified
number of products: 1000
total difference:27906.457323507402
average difference: 27.906457323507404

With this PR:

Test set world-1000-some-specified
number of products: 1000
total difference:26289.049043323128
average difference: 26.28904904332313

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve for urgency, but I think there are things to fix !


=cut

sub add_percent_max_for_salt_and_sugar_ingredients_from_nutrition_facts ($product_ref) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add_percent_max_for_ingredients_from_nutrition_facts as we could add more in the future ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

lib/ProductOpener/Ingredients.pm Outdated Show resolved Hide resolved
lib/ProductOpener/Ingredients.pm Outdated Show resolved Hide resolved
lib/ProductOpener/Ingredients.pm Outdated Show resolved Hide resolved
unshift @ingredients, @{$ingredient_ref->{ingredients}};
}

foreach my $ingredient_max_value_ref (@ingredient_max_values) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already iterated above ! Why do we need to iterate twice ?
(I prefer if we keep this inner loop, that said).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah good point, initially I had it in the inner loop but I wanted to move it to the outer loop, and I forgot to remove the inner loop.. :(

we can keep the inner loop only

foreach my $ingredient_max_value_ref (@ingredient_max_values) {
my $value = $ingredient_max_value_ref->{value};
if (is_a("ingredients", $ingredient_ref->{id}, $ingredient_max_value_ref->{ingredientid})) {
if (not defined $ingredient_ref->{percent_max}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we do it if percent_max is above current max ?

Suggested change
if (not defined $ingredient_ref->{percent_max}) {
if ((not defined $ingredient_ref->{percent_max}) or ($value < $ingredient_ref->{percent_max})) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, it was just to be safe if the sugar or salt is not 100% sugar or salt.

}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shan't we do a pass after that to eventually re-order the list of ingredients (or sub-ingredients) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ingredients are listed by quantity order, we keep them in this order.

Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@teolemon
Copy link
Member

@stephanegigandet applied changes, @alexgarel can we merge ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧽 Data quality https://wiki.openfoodfacts.org/Quality 🥗🔍 Ingredients analysis https://wiki.openfoodfacts.org/Ingredients_Extraction_and_Analysis 🥗 Ingredients ingredients 🚦 Nutri-Score 🧪 tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants