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: bypass data quality error for citrus #8444

Merged

Conversation

benbenben2
Copy link
Collaborator

Make sure you've done all the following (You can delete the checklist before submitting)

  • PR title is prefixed by one of the following: feat, fix, docs, style, refactor, test, build, ci, chore, revert, l10n, taxonomy
  • Code is well documented
  • Include unit tests for new functionality
  • Code passes GitHub workflow checks in your branch
  • If you have multiple commits please combine them into one commit by squashing them.
  • Read and understood the contribution guidelines

What

Added feature to bypass "Energy value in kJ does not correspond to the value calculated from the other nutrients"

Screenshot

Not sure what to screenshot because there is no error anymore :-D

Related issue(s) and discussion

@benbenben2 benbenben2 self-assigned this May 22, 2023
@benbenben2 benbenben2 requested a review from a team as a code owner May 22, 2023 18:34
@github-actions github-actions bot added categories 🧽 Data quality https://wiki.openfoodfacts.org/Quality 🧬 Taxonomies https://wiki.openfoodfacts.org/Global_taxonomies 🧪 tests labels May 22, 2023
Comment on lines 594 to 627
# following error/warning should be ignored for some categories
# for example, lemon juices containing organic acid, it is forbidden to display organic acid in nutrition tables but
# organic acid contributes to the total energy calculation
my $skip_energy_error = 0;
if (defined $product_ref->{categories_tags}) {
# start from end, categories_tags has the most specific categories at the end
my $i = @{$product_ref->{categories_tags}} - 1;
while (
($i >= 0)
and (
not(
# category - or a parent - with expected tag
defined get_inherited_property(
"categories", $product_ref->{categories_tags}[$i],
"ignore_energy_calculated_error:en"
)
and (
# we expect single letter a, b, c, d, e for nutriscore grade in the taxonomy. Case insensitive (/i).
get_inherited_property(
"categories", $product_ref->{categories_tags}[$i],
"ignore_energy_calculated_error:en"
) =~ /yes$/i
)
)
)
)
{
$i--;
}

if ($i >= 0) {
$skip_energy_error = 1;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we make it a separate method ? (easier to read code)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is a new function/subroutine in Tags.pm (because it is based on get_inherited_property subroutine).
That new subroutine is called from DataQualityFood.pm

The idea would be to reuse this function in the other PR (#8360)
(eventually) It could be improved, because we expect a "yes" and we could maybe add what is expected as input parameter in the function. Here we expect a "yes", in the other PR we expect a nutriscore. What do you think?

Comment on lines 599 to 602
# start from end, categories_tags has the most specific categories at the end
my $i = @{$product_ref->{categories_tags}} - 1;
while (
($i >= 0)
Copy link
Member

Choose a reason for hiding this comment

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

It seems to me you could rewrite this with a foreach and the reverse function + a break if you match condition to skip energy error. It would be more consistent with the rest of the codebase.

Copy link
Member

@alexgarel alexgarel May 23, 2023

Choose a reason for hiding this comment

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

Also, you make two calls to get_inherited_property while it's a costly method, you'd better store the result in a local variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, thank you for the review and comments. Sorry, I am still learning. I will try to improve it.

I was inspired by the following snippet in ProducersFood.pm


	my $i = @{$product_ref->{categories_tags}} - 1;

	while (
		($i >= 0)
		and not((defined $categories_nutriments_ref->{$product_ref->{categories_tags}[$i]})
			and (defined $categories_nutriments_ref->{$product_ref->{categories_tags}[$i]}{nutriments}))
		)
	{
		$i--;
	}
	# categories_tags has the most specific categories at the end

	if ($i >= 0) {

Now I reworked it based on compute_ecoscore_agribalyse found in Ecoscore.pm

Can you review this PR as well: #8360?
I did code something similar there too.

@github-actions github-actions bot added the Tags label May 23, 2023
@codecov-commenter
Copy link

codecov-commenter commented May 24, 2023

Codecov Report

Merging #8444 (6b9436d) into main (6068ff9) will increase coverage by 0.02%.
The diff coverage is 94.11%.

@@            Coverage Diff             @@
##             main    #8444      +/-   ##
==========================================
+ Coverage   48.54%   48.57%   +0.02%     
==========================================
  Files         114      114              
  Lines       21314    21327      +13     
  Branches     4776     4780       +4     
==========================================
+ Hits        10347    10359      +12     
  Misses       9679     9679              
- Partials     1288     1289       +1     
Impacted Files Coverage Δ
tests/unit/dataqualityfood.t 84.84% <75.00%> (-0.21%) ⬇️
lib/ProductOpener/DataQualityFood.pm 60.16% <100.00%> (+0.21%) ⬆️
lib/ProductOpener/Tags.pm 40.94% <100.00%> (+0.23%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more


=cut

sub get_inherited_property_from_categories_tags ($product_ref, $property) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to create this function!

$category_match = lc(get_inherited_property("categories", $category, $property));
last if $category_match;
}
if ($category_match) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could remove the lines 411 to 420 and just have a single "return $category_match" at the end (which would return undef if there was no match).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the suggestion!
Done! Please, check again before to merge

Copy link
Contributor

@stephanegigandet stephanegigandet left a comment

Choose a reason for hiding this comment

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

Thank you very much!

@sonarcloud
Copy link

sonarcloud bot commented May 24, 2023

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

@benbenben2 benbenben2 merged commit e3a7394 into main May 25, 2023
14 checks passed
@benbenben2 benbenben2 deleted the feat_exclude_categories_from_energy_error_from_other_nutrients branch May 25, 2023 20:05
MonalikaPatnaik pushed a commit to MonalikaPatnaik/openfoodfacts-server that referenced this pull request May 31, 2023
* added feature to ignore energy does not match value computed from other nut

* rework get_inherited_property

* rework get_inherited_property

* apply suggestions for return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
categories 🧽 Data quality https://wiki.openfoodfacts.org/Quality Tags 🧬 Taxonomies https://wiki.openfoodfacts.org/Global_taxonomies 🧪 tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate acides in energy computation or bypass data quality error for citrus
4 participants