Should I Eat That? Food Safety with Wolfram Language

September 30, 2022

Isabel Skidmore, Lead Food Developer, Wolfram|Alpha Scientific Content

Foodborne illness, or food poisoning, is something many of us have experienced. According to the World Health Organization, almost 1 in 10 people in the world fall ill each year after eating contaminated food. Luckily, by following recommended food safety practices, we can do our best to avoid getting sick.

September is Food Safety Education Month. To highlight the importance of food safety, we have introduced two new properties in Wolfram Language that can help users make smart choices about food storage:

EntityValue["Food","Properties"]//Select[StringContainsQ[#["Label"],"storage time",IgnoreCase->True]&]

“I Bought Ground Beef on Tuesday for a Family Cookout on Saturday. Can I Keep the Ground Beef in the Refrigerator until Then?”

Let’s use this new property in Wolfram Language to check:

EntityValue[Interpreter["Food"]["raw ground beef"],(EntityProperty["maximum refrigerated storage time", EntityProperty["Food","RecommendedRefrigeratedStorageTime"], "EntityProperty[\"Food\", \"RecommendedRefrigeratedStorageTime\"]"])]

Based on the data, sourced from the U.S. Department of Agriculture (USDA), you should keep raw ground beef a maximum of two days in the refrigerator before it is cooked. In this case, it’s best to go ahead and freeze the ground beef the day you bought it. You also can query the Wolfram Language for how long the ground beef can be kept in the freezer before use:

EntityValue[Interpreter["Food"]["raw ground beef"],(EntityProperty["maximum frozen storage time", EntityProperty["Food","RecommendedFrozenStorageTime"], "EntityProperty[\"Food\", \"RecommendedFrozenStorageTime\"]"])]

We can use TimelinePlot to check the maximum refrigerated and frozen storage times for a variety of raw and cooked meats:

refrigeratedStorageTimesAssoc=EntityValue[{,,,},(EntityProperty["maximum refrigerated storage time", EntityProperty["Food","RecommendedRefrigeratedStorageTime"], "EntityProperty[\"Food\", \"RecommendedRefrigeratedStorageTime\"]"]),"EntityAssociation"];

styleStorageTimeLabel[Rule[q_,food_]]:=QuantityMagnitude[q,"Days"]->Column[{Style[[food],Gray,16],Style[ToString[Round@q],Lighter@Gray,14]}];
processAssoc[assoc_]:=Map[{styleStorageTimeLabel[#]}&,Map[Reverse]@Normal@assoc];
TimelinePlot[processAssoc@refrigeratedStorageTimesAssoc,,PlotLabel->"Maximum Refrigerated Storage Times",LabelStyle->Bold]

frozenStorageTimesAssoc=EntityValue[{,,,},(EntityProperty["maximum frozen storage time", EntityProperty["Food","RecommendedFrozenStorageTime"], "EntityProperty[\"Food\", \"RecommendedFrozenStorageTime\"]"]),"EntityAssociation"];

styleStorageTimeLabel[Rule[q_,food_]]:=QuantityMagnitude[q,"Days"]->Column[{Style[[food],Gray,16],Style[ToString[Round@q],Lighter@Gray,14]}];
processAssoc[assoc_]:=Map[{styleStorageTimeLabel[#]}&,Map[Reverse]@Normal@assoc];
TimelinePlot[processAssoc@frozenStorageTimesAssoc,,PlotLabel->"Maximum Frozen Storage Times",LabelStyle->Bold]

“I Shop at My Favorite Fish Market Once a Month. How Long Can I Keep the Fish in the Freezer?”

You can check maximum frozen storage times for a wide range of raw fish and seafood, including these examples:

fishAndSeafood=Interpreter["Food"][{"raw salmon","raw mackerel","raw halibut","raw snapper","raw crab","raw shrimp"}];
niceGrid=ResourceFunction["NiceGrid"];
EntityValue[fishAndSeafood,{(EntityProperty["maximum frozen storage time", EntityProperty["Food","RecommendedFrozenStorageTime"], "EntityProperty[\"Food\", \"RecommendedFrozenStorageTime\"]"])},"EntityPropertyAssociation"]//SortBy[Last]//niceGrid

Did you notice the shorter frozen storage times for fish with higher levels of omega-3 fatty acids, such as salmon and mackerel? See the comparison with other fish like halibut and snapper:

rawFish=Interpreter["Food"][{"raw salmon","raw mackerel","raw halibut","raw snapper"}];
omega3VSStorageTime=DeleteMissing[EntityValue[rawFish,{(EntityProperty["relative omega three fatty acid content", EntityProperty["Food","RelativeTotalOmegaThreeFattyAcidContent"], "EntityProperty[\"Food\", \"RelativeTotalOmegaThreeFattyAcidContent\"]"]),(EntityProperty["maximum frozen storage time", EntityProperty["Food","RecommendedFrozenStorageTime"], "EntityProperty[\"Food\", \"RecommendedFrozenStorageTime\"]"])},"EntityAssociation"],1,2];
ListPlot[KeyMap[]@omega3VSStorageTime,AxesLabel->Automatic,PlotLabel->"Relative Omega-3 Content vs. Maximum Frozen Storage Time",LabelStyle->Bold, ImageSize->600]

“It’s Lunchtime on Monday. I Have Leftover Pizza in the Fridge from Friday Night. Should I Eat That?”

You can get answers about popular leftovers, including your Friday-night pizza delivery. Since it’s been only three days (and assuming the leftover pizza was stored in the refrigerator within two hours), you can go ahead and enjoy it for Monday’s lunch:

leftovers=Interpreter["Food"][{"kung pao chicken","lasagna","pizza","chicken alfredo","fried rice"}];
EntityValue[leftovers,{(EntityProperty["maximum refrigerated storage time", EntityProperty["Food","RecommendedRefrigeratedStorageTime"], "EntityProperty[\"Food\", \"RecommendedRefrigeratedStorageTime\"]"])},"EntityPropertyAssociation"]//niceGrid

As you can see, most cooked leftovers that are refrigerated promptly can be stored three to four days.

“Turkey Sandwiches, Turkey Tetrazzini, Turkey Pot Pie. No More Turkey, Please.”

You can plan for safe storage of those inevitable Thanksgiving Day leftovers. A side-by-side comparison can help you decide whether to refrigerate or freeze that extra food:

ThanksgivingLeftovers=Interpreter["Food"][{"cooked turkey","mashed potatoes","gravy","stuffing","pumpkin pie"}];
EntityValue[ThanksgivingLeftovers,{(EntityProperty["maximum refrigerated storage time", EntityProperty["Food","RecommendedRefrigeratedStorageTime"], "EntityProperty[\"Food\", \"RecommendedRefrigeratedStorageTime\"]"]),(EntityProperty["maximum frozen storage time", EntityProperty["Food","RecommendedFrozenStorageTime"], "EntityProperty[\"Food\", \"RecommendedFrozenStorageTime\"]"])},"EntityPropertyAssociation"]//niceGrid

Gone overboard with your baking? Use (EntityProperty["maximum frozen storage time", EntityProperty["Food","RecommendedFrozenStorageTime"], "EntityProperty[\"Food\", \"RecommendedFrozenStorageTime\"]"]) to check how long you can freeze "cookie dough", "brownies", "fudge" and "cheesecake" before they lose freshness and quality.

“I Love Bringing Home a Basket of Colorful Produce from the Farmers’ Market.”

ListPlot can chart the maximum refrigerated storage times that will maintain freshness of those vibrant veggies, as well as fresh fruits and herbs:

farmersMarketProduce=Interpreter["Food"][{"zucchini","butternut squash","peppers", "fresh basil","corn","fresh spinach"}];
produceStorageTimes=EntityValue[farmersMarketProduce,(EntityProperty["maximum refrigerated storage time", EntityProperty["Food","RecommendedRefrigeratedStorageTime"], "EntityProperty[\"Food\", \"RecommendedRefrigeratedStorageTime\"]"]),"EntityAssociation"];

Evaluate the following cell to run the necessary initialization code to retrieve images for the foods:

CloudGet[CloudObject["https://wolfr.am/17QpViK7R"]]

Plot the data! The y axis displays the number of days, and the vertical lines indicate the range of how many days that food can be refrigerated before it loses freshness. Hover over the images to see larger versions and enjoy the colorful harvest:

produceImagesAndStorageTimes=KeyMap[getFoodImage]@produceStorageTimes;
ListPlot[Sort[produceImagesAndStorageTimes],AxesLabel->Automatic,PlotLabel->"Maximum Refrigerated Storage Times",LabelStyle->Bold,LabelingSize->70,ImageSize->600]

A Look Ahead

We’re looking ahead to how we can give the Wolfram community even more ways to explore food safety. By utilizing the synergies of food data and biology data, we are working to bring you the tools to investigate food sources of foodborne-illness pathogens. Stay tuned!

foodborneBacteriaList={(Entity["Aeromonas", Entity["Species","Genus:Aeromonas"], "Entity[\"Species\", \"Genus:Aeromonas\"]", "species specification"]),(Entity["Bacillus cereus", Entity["Species","Species:BacillusCereus"], "Entity[\"Species\", \"Species:BacillusCereus\"]", "species specification"]),(Entity["Brucella", Entity["Species","Genus:Brucella"], "Entity[\"Species\", \"Genus:Brucella\"]", "species specification"]),(Entity["Campylobacter", Entity["Species","Genus:Campylobacter"], "Entity[\"Species\", \"Genus:Campylobacter\"]", "species specification"]),(Entity["Clostridium botulinum", Entity["Species","Species:ClostridiumBotulinum"], "Entity[\"Species\", \"Species:ClostridiumBotulinum\"]", "species specification"]),(Entity["Clostridium perfringens", Entity["Species","Species:ClostridiumPerfringens"], "Entity[\"Species\", \"Species:ClostridiumPerfringens\"]", "species specification"]),(Entity["Coxiella", Entity["Species","Genus:Coxiella"], "Entity[\"Species\", \"Genus:Coxiella\"]", "species specification"]),(Entity[(RowWithSeparator[ , "Cronobacter", (EntitySublabel["bacterium"])]), Entity["TaxonomicSpecies","Cronobacter::7923w"], "Entity[\"TaxonomicSpecies\", \"Cronobacter::7923w\"]", "taxonomic species"]),(Entity["Enterococcus", Entity["Species","Genus:Enterococcus"], "Entity[\"Species\", \"Genus:Enterococcus\"]", "species specification"]),(Entity["E. coli", Entity["Species","Species:EscherichiaColi"], "Entity[\"Species\", \"Species:EscherichiaColi\"]", "species specification"]),(Entity["Francisella", Entity["Species","Genus:Francisella"], "Entity[\"Species\", \"Genus:Francisella\"]", "species specification"]),(Entity["Klebsiella", Entity["Species","Genus:Klebsiella"], "Entity[\"Species\", \"Genus:Klebsiella\"]", "species specification"]),(Entity["Listeria monocytogenes", Entity["Species","Species:ListeriaMonocytogenes"], "Entity[\"Species\", \"Species:ListeriaMonocytogenes\"]", "species specification"]),(Entity["Mycobacterium", Entity["Species","Genus:Mycobacterium"], "Entity[\"Species\", \"Genus:Mycobacterium\"]", "species specification"]),(Entity["Plesiomonas shigelloides", Entity["Species","Species:PlesiomonasShigelloides"], "Entity[\"Species\", \"Species:PlesiomonasShigelloides\"]", "species specification"]),(Entity["Salmonella", Entity["Species","Genus:Salmonella"], "Entity[\"Species\", \"Genus:Salmonella\"]", "species specification"]),(Entity["Shigella", Entity["Species","Genus:Shigella"], "Entity[\"Species\", \"Genus:Shigella\"]", "species specification"]),(Entity["Staphylococcus", Entity["Species","Genus:Staphylococcus"], "Entity[\"Species\", \"Genus:Staphylococcus\"]", "species specification"]),(Entity["Streptococcus", Entity["Species","Genus:Streptococcus"], "Entity[\"Species\", \"Genus:Streptococcus\"]", "species specification"]),(Entity["Vibrio", Entity["Species","Genus:Vibrio"], "Entity[\"Species\", \"Genus:Vibrio\"]", "species specification"]),(Entity["Yersinia", Entity["Species","Genus:Yersinia"], "Entity[\"Species\", \"Genus:Yersinia\"]", "species specification"])};
TreePlot["Foodborne Bacteria"->#&/@foodborneBacteriaList,Center,"Foodborne Bacteria",VertexLabels->Automatic,PlotTheme->"Scientific"]

Want to Learn More?

For more information about Food Safety Education Month and foodborne illness, you can visit these sites for free downloads, teacher resources, children’s activity sheets and current food recalls:

FoodSafety.gov

Partnership for Food Safety Education

U.S. Food & Drug Administration (FDA): Food Safety Education Month

Bonus Viz: How to Build a Food Image Bar Chart

Here’s how you can build the food bar chart we featured in the introduction:

farmersMarketProduceStrings={"raw shrimp","pie","lasagna","squash","spinach","melon","cake","peppers","pepperoni","cheddar cheese"};
farmersMarketProduce=Interpreter["Food"][farmersMarketProduceStrings];
produceStorageTimes=EntityValue[farmersMarketProduce,(EntityProperty["maximum refrigerated storage time", EntityProperty["Food","RecommendedRefrigeratedStorageTime"], "EntityProperty[\"Food\", \"RecommendedRefrigeratedStorageTime\"]"]),"EntityAssociation"];

CloudGet[CloudObject["https://wolfr.am/17RmSRFIa"]]

BarChart[Map[Max]@produceStorageTimes,ChartElements->Map[convertWhiteBackgroundToGray@getFoodImage[#]&]@Keys[produceStorageTimes],ChartLabels->Placed[farmersMarketProduceStrings,Above],AxesLabel->Automatic,ImageSize->600]