-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recipe_estimator_test.py
301 lines (250 loc) · 8.85 KB
/
recipe_estimator_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import json
from recipe_estimator import estimate_recipe
def test_estimate_recipe_accounts_for_lost_water():
product = {
'ingredients': [{
'id':'en:tomato',
'nutrients': {
'carbohydrates': {'percent_min': 2.5,'percent_max': 2.5},
'water': {'percent_min': 90,'percent_max': 90},
}
}],
'nutriments': {
'carbohydrates_100g': 5,
}}
estimate_recipe(product)
# Print the resulting product structure
print(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
ingredient = product['ingredients'][0]
# Percent estimate is relative to total ingredient quantities
percent_estimate = ingredient.get('percent_estimate')
assert round(percent_estimate) == 100
# Quantity estimate gives original quantity of ingredient per 100g/ml of product
quantity_estimate = ingredient.get('quantity_estimate')
assert round(quantity_estimate) == 200
lost_water = ingredient.get('lost_water')
assert round(lost_water) == 100
def test_estimate_recipe_lost_water_is_constrained():
product = {
'ingredients': [{
'id':'en:tomato',
'nutrients': {
'carbohydrates': {'percent_min': 2.5,'percent_max': 2.5},
'water': {'percent_min': 10,'percent_max': 10},
}
}],
'nutriments': {
'carbohydrates_100g': 5,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
ingredient = product['ingredients'][0]
# If tomatoes have a maximum water content of 10% then an original quantity of N times 90% = 100%, so N = 100 / 90 = 111
quantity_estimate = ingredient.get('quantity_estimate')
assert round(quantity_estimate) == 111
lost_water = ingredient.get('lost_water')
assert round(lost_water) == 11
def test_estimate_recipe_simple_recipe():
# A x 15 + B x 3 = 10
# A + B = 1
# 15A + 3 - 3A = 10
# A = 7 / 12 = 58%
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 3,'percent_max': 3},
}
}
],
'nutriments': {
'carbohydrates_100g': 10,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) == 58
assert round(product['ingredients'][1]['percent_estimate']) == 42
def test_estimate_recipe_simple_recipe_with_one_unmatched_ingredient():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 0,'percent_max': 100},
}
}
],
'nutriments': {
'carbohydrates_100g': 10,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) >= 50
assert round(product['ingredients'][1]['percent_estimate']) <= 50
assert round(product['ingredients'][0]['percent_estimate'] + product['ingredients'][1]['percent_estimate']) == 100
def test_estimate_recipe_simple_recipe_with_no_matched_ingredients():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 0,'percent_max': 100},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 0,'percent_max': 100},
}
}
],
'nutriments': {
'carbohydrates_100g': 10,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) >= 50
assert round(product['ingredients'][1]['percent_estimate']) <= 50
assert round(product['ingredients'][0]['percent_estimate'] + product['ingredients'][1]['percent_estimate']) == 100
def test_estimate_recipe_simple_recipe_with_no_nutriments():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 3,'percent_max': 3},
}
}
]}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) >= 50
assert round(product['ingredients'][1]['percent_estimate']) <= 50
def test_estimate_recipe_subingredients():
product = {
'ingredients': [{
'id':'en:tomato',
'nutrients': {
'carbohydrates': {'percent_min': 2.5,'percent_max': 2.5},
'water': {'percent_min': 90,'percent_max': 90},
'sugars': {'percent_min': 0,'percent_max': 0},
'salt': {'percent_min': 0,'percent_max': 0},
}
},
{
'id':'en:sugar-and-salt',
'ingredients': [{
'id':'en:sugar',
'nutrients': {
'sugars': {'percent_min': 100,'percent_max': 100},
'carbohydrates': {'percent_min': 0,'percent_max': 0},
'salt': {'percent_min': 0,'percent_max': 0},
}
},
{
'id':'en:salt',
'nutrients': {
'salt': {'percent_min': 100,'percent_max': 100},
'carbohydrates': {'percent_min': 0,'percent_max': 0},
'sugars': {'percent_min': 0,'percent_max': 0},
}
}
]
}],
'nutriments': {
'carbohydrates_100g': 5,
'sugars_100g': 10,
'salt_100g': 5
}}
estimate_recipe(product)
# Print the resulting product structure
print(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
sugar = product['ingredients'][1]['ingredients'][0]
# Percent estimate is relative to total ingredient quantities
assert round(sugar.get('percent_estimate')) == 5
# Quantity estimate gives original quantity of ingredient per 100g/ml of product
assert round(sugar.get('quantity_estimate')) == 10
def test_estimate_recipe_minimize_maximum_distance_between_ingredients():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'three',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'four',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
}
],
'nutriments': {
'carbohydrates_100g': 60,
}}
estimate_recipe(product)
# Pretty print with indents the resulting product structure
print(json.dumps(product, indent=4))
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) == 40
assert round(product['ingredients'][1]['percent_estimate']) == 30
assert round(product['ingredients'][2]['percent_estimate']) == 20
assert round(product['ingredients'][3]['percent_estimate']) == 10