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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/integration/insights/test_category_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TestCategoryImporter:

We only test the scenarios that are of actual use
"""

def fake_product_store(self):
return {barcode1: Product({"categories_tags": ["en:Fish"]})}

Expand Down Expand Up @@ -131,7 +132,6 @@ def test_import_one(self, predictions):
assert not inserted.automatic_processing

def test_import_auto(self):
import pdb; pdb.set_trace()
imported = self._run_import(
[neural_prediction("en:smoked-salmons", confidence=0.91, auto=True)]
)
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/insights/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,6 @@ def test_generate_candidates(self):


class TestLabelInsightImporter:
import pdb;pdb.set_trace();

def test_get_type(self):
assert LabelInsightImporter.get_type() == InsightType.label

Expand Down
93 changes: 74 additions & 19 deletions tests/unit/workers/tasks/test_import_image.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,82 @@
import requests
from PIL import Image
from tests import data
from robotoff.insights.extraction import get_predictions_from_image
from robotoff.insights.importer import import_insights
from unittest.mock import Mock

from robotoff import settings
from tests.integration.models_utils import PredictionFactory

def test_import_insights_from_image(mock):

def test_import_insights_from_image(mocker):
barcode = "3302740030949"
source_image = '/330/274/003/0949/6.jpg '
ocr_url = 'https://world.openfoodfacts.org/images/products/802/509/314/0251/4.json'
server_domain = 'api.openfoodfacts.org'

expected_predictions_all = [
PredictionFactory(type='label', barcode=3302740030949, data={"model": "nutriscore", "confidence": 0.871872, "bounding_box": [1441.3848, 1036.8024, 1685.6928, 1430.2488]}, value_tag="en:nutriscore-c", value="", automatic_processing=False),
PredictionFactory(type='label', barcode=3302740030949, data={"model": "nutriscore", "confidence": 0.816114128, "bounding_box": [0.591395915, 0.477426708, 0.688566923, 0.624632716]}, value_tag="en:nutriscore-a", value="", automatic_processing=False),
PredictionFactory(type='label', barcode=3302740030949, data={"model": "nutriscore", "confidence": 0.913557172, "bounding_box": [0.596912444, 0.429353863, 0.70557, 0.592592061]}, value_tag="en:nutriscore-c", value="", automatic_processing=False),
PredictionFactory(type='label', barcode=3302740030949, data={"model": "nutriscore", "confidence": 0.99035, "bounding_box": [0.575399518, 0.472130537, 0.691237211, 0.625134587]}, value_tag="en:nutriscore-e", value="", automatic_processing=False)
]
source_image = "https://world.openfoodfacts.org/images/products/330/274/003/0949/front_fr.110.400.jpg"
ocr_url = "https://world.openfoodfacts.org/images/products/802/509/314/0251/4.json"
server_domain = settings.OFF_SERVER_DOMAIN

get_predictions_from_image = mock.Mock(return_value=expected_predictions_all)
image = Image.open(requests.get(source_image, stream=True).raw)

get_product = {"code":"3302740030949","product":{"labels":"Sans colorants, Nutriscore, Nutriscore C, Fumé au bois de hêtre","product_name":"Pommes de terre gratinées aux lardons et champignons"},"status":1,"status_verbose":"product found"}
mock_import_insights = mocker.patch(
"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

)

predictions_list = [
PredictionFactory(
type="label",
barcode=3302740030949,
data={
"model": "nutriscore",
"confidence": 0.871872,
"bounding_box": [1441.3848, 1036.8024, 1685.6928, 1430.2488],
},
value_tag="en:nutriscore-c",
value="",
automatic_processing=False,
),
PredictionFactory(
type="label",
barcode=3302740030949,
data={
"model": "nutriscore",
"confidence": 0.816114128,
"bounding_box": [0.591395915, 0.477426708, 0.688566923, 0.624632716],
},
value_tag="en:nutriscore-a",
value="",
automatic_processing=False,
),
PredictionFactory(
type="label",
barcode=3302740030949,
data={
"model": "nutriscore",
"confidence": 0.913557172,
"bounding_box": [0.596912444, 0.429353863, 0.70557, 0.592592061],
},
value_tag="en:nutriscore-c",
value="",
automatic_processing=False,
),
PredictionFactory(
type="label",
barcode=3302740030949,
data={
"model": "nutriscore",
"confidence": 0.99035,
"bounding_box": [0.575399518, 0.472130537, 0.691237211, 0.625134587],
},
value_tag="en:nutriscore-e",
value="",
automatic_processing=False,
),
]

# import_insights = mock.Mock(return_value=)

mock_import_insights.assert_called_once_with(
predictions=predictions_list,
server_domain=server_domain,
automatic=True,
)

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.

barcode=barcode, image=image, source_image=source_image, ocr_url=ocr_url
)