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

fix: Do not apply nutriscore prediction if it does not match the current nutriscore of the product #772

Closed
wants to merge 6 commits into from

Conversation

Jagrutiti
Copy link
Member

@Jagrutiti Jagrutiti commented May 19, 2022

What

  • This PR compares the predicted and current nutriscore.

Why

The Nutri-Score model is not very reliable

Fixes bug(s)

Part of

from PIL import Image
from robotoff.utils import get_image_from_url, get_logger, http_session

image = get_image_from_url('https://world.openfoodfacts.org/images/products/802/509/314/0251/4.jpg', error_raise=False)
Copy link
Member

Choose a reason for hiding this comment

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

good idea, but you should put it in the repository instead (in a tests/data directory for example), we wont to avoid tests depending on an external service.

Copy link
Member

Choose a reason for hiding this comment

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

ocr_url = 'https://world.openfoodfacts.org/images/products/802/509/314/0251/4.json'
sever_domain = 'http://openfoodfacts.org/'

expected_predictions_all = get_predictions_from_image(barcode, image, source_image, ocr_url, sever_domain)
Copy link
Member

Choose a reason for hiding this comment

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

It's strange that you call it, to mock it !
In test you won't have a model.
Instead you should create a list of Prediction using PredictionFactory.

You are a bit lost maybe because you don't know what to put inside it, but what you are testing should tell you what to put in it. I can also provides you some data from production.

ocr_url = 'https://world.openfoodfacts.org/images/products/802/509/314/0251/4.json'
server_domain = 'api.openfoodfacts.org'

expected_predictions_all = get_predictions_from_image(barcode, data.image, source_image, ocr_url)
Copy link
Member

Choose a reason for hiding this comment

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

We don't want to really call get_predictions_from_image because this would need to have the model, and we don't want to have it in dev / test environment.

That's why you will mock the function below.

Instead you should have something like:

expected_predictions_all = [
   PredictionFactory(type=label,  barcode=barcode, data={"model": "nutriscore", "confidence": 0.871872]}, value_tag="en:nutriscore-c", value="", automatic_processing="f")
   PredictionFactory(type=label,  barcode=barcode, data={"....

Where parameter of your instances of PredictionFactory are taken from #115 (comment) but I don't take unnecessary values (some values are automatically generated by PredictionFactory, you can look at its code).

tests/data.py Outdated
@@ -0,0 +1,5 @@
from robotoff.utils import get_image_from_url

image = get_image_from_url('https://world.openfoodfacts.org/images/products/330/274/003/0949/front_fr.110.400.jpg', error_raise=False)
Copy link
Member

Choose a reason for hiding this comment

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

You misinterpret my previous command about putting the image in test/data, it was to put the image file in a data/ folder :-) (that is the jpg file).

But I think you don't need that.

tests/data.py Outdated

image = get_image_from_url('https://world.openfoodfacts.org/images/products/330/274/003/0949/front_fr.110.400.jpg', error_raise=False)

# expected_predictions_all = [Prediction(type=<PredictionType.nutrient: 'nutrient'>, data={'nutrients': {'salt': [{'raw': 'sel: 1,7 g', 'nutrient': 'salt', 'value': '1.7', 'unit': 'g'}]}, 'version': '2'}, value_tag=None, value=None, automatic_processing=None, predictor=None, barcode='3302740030949', timestamp=None, source_image='/802/509/314/0251/4.jpg', server_domain=None, id=None), Prediction(type=<PredictionType.nutrient_mention: 'nutrient_mention'>, data={'mentions': {'saturated_fat': [{'raw': 'acides gras saturés', 'span': [101, 120], 'languages': ['fr']}], 'fat': [{'raw': 'lipides', 'span': [87, 94], 'languages': ['fr']}], 'sugar': [{'raw': 'sucres', 'span': [151, 157], 'languages': ['fr']}], 'protein': [{'raw': 'proteines', 'span': [158, 167], 'languages': ['fr']}], 'salt': [{'raw': 'sel', 'span': [168, 171], 'languages': ['fr']}], 'nutrition_values': [{'raw': 'valeur nutritionnelle', 'span': [0, 21], 'languages': ['fr']}]}, 'version': '2'}, value_tag=None, value=None, automatic_processing=None, predictor=None, barcode='3302740030949', timestamp=None, source_image='/802/509/314/0251/4.jpg', server_domain=None, id=None), Prediction(type=<PredictionType.image_lang: 'image_lang'>, data={'count': {'fr': 25, 'null': 5, 'words': 35, 'en': 2, 'it': 3}, 'percent': {'fr': 71.42857142857143, 'null': 14.285714285714286, 'en': 5.714285714285714, 'it': 8.571428571428571}}, value_tag=None, value=None, automatic_processing=None, predictor=None, barcode='3302740030949', timestamp=None, source_image='/802/509/314/0251/4.jpg', server_domain=None, id=None), Prediction(type=<PredictionType.image_orientation: 'image_orientation'>, data={'count': {'up': 35}, 'orientation': 'up', 'rotation': 0}, value_tag=None, value=None, automatic_processing=None, predictor=None, barcode='3302740030949', timestamp=None, source_image='/802/509/314/0251/4.jpg', server_domain=None, id=None)]
Copy link
Member

Choose a reason for hiding this comment

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

I think that's what you get when you run the prediction locally, but as you don't have models for nutriscore locally (they are big models), this give you no prediction with value_tag="en:nutriscore-c". You only get predictions computed by other kind of predictors.

But as told below, this is not a problem, we are not interested in testing get_image_from_url but the import process.


mock_get_predictions_from_image.assert_called_once_with(
Copy link
Member Author

Choose a reason for hiding this comment

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

Both assert_called_once_with throw the error saying assert_called_once_with shou d be called once but called 0 times

I was not able to resolve it. I did see the existing examples in the code. I read where to patch and tried to follow the steps in the document but no luck.

"robotoff.insights.importer.import_insights", return_value=4
)
mock_get_predictions_from_image = mocker.patch(
"robotoff.insights.extraction.get_predictions_from_image"
Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the code I was not able to figure out what will get_predictions_from_image return

@teolemon
Copy link
Member

=========================== short test summary info ============================
FAILED tests/unit/workers/tasks/test_import_image.py::test_import_insights_from_image
================= 1 failed, 315 passed, 10 warnings in 27.20s ==================
1
make: *** [Makefile:143: unit-tests] Error 1
Error: Process completed with exit code 2.

@alexgarel
Copy link
Member

Bug was closed.

@alexgarel alexgarel closed this Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants