Analyze, Visualize, and Compare "Superfoods"
Superfoods are foods that are very high in one or more nutrients. This example defines a set of superfoods, then contrasts them with more "typical" foods using food data from the Wolfram Knowledgebase. Finally, it constructs an insignia appropriate for foods worthy of the "super" epithet.
First, define a list of superfoods containing the nutrients for which they are known.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_297.png)
superfoodsRawData = {{"greek yogurt", {"TotalProtein"}}, {"quinoa", \
{"TotalProtein"}}, {"blueberries", {"TotalFiber",
"VitaminC"}}, {"kale", {"TotalFiber", "Calcium",
"Iron"}}, {"chia", {"Magnesium", "Iron", "Calcium",
"Potassium"}}, {"oatmeal", {"TotalFiber"}}, {"broccoli", \
{"TotalFiber", "VitaminC",
"TotalFolate"}}, {"strawberries", {"VitaminC"}}, {"salmon", \
{"TotalProtein"}}, {"watermelon", {"TotalSugar", "VitaminA",
"VitaminC", "TotalCalories"}}, {"lima beans", {"TotalFiber"}},
{"edamame", {"TotalFiber"}}, {"spinach", {"Calcium",
"VitaminK"}}, {"pistachios", {"TotalProtein", "TotalFiber",
"Potassium"}}, {"eggs", {"TotalProtein"}}, {"almonds", \
{"TotalFiber", "Potassium", "Calcium", "VitaminE", "Magnesium",
"Iron"}}, {"pumpkin", {"BetaCarotene"}}, {"apples", \
{"TotalFiber", "TotalCalories"}}, {"lentils", {"TotalProtein",
"Iron"}}};
Convert this to a Dataset for easier computation.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_298.png)
superfoodsDatset =
Dataset[<|"Food" -> #1, "Nutrients" -> #2|> & @@@ superfoodsRawData]
![](assets.en/analyze-visualize-and-compare-superfoods/O_199.png)
Use Interpreter to get Wolfram Language entities for the superfoods.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_299.png)
superfoodsDatset1 =
superfoodsDatset[All,
KeySort[Prepend[#, <|
"FoodEntity" -> Interpreter["Food"][#Food]|>]] &];
List the nutrients.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_300.png)
Union @@ Normal[superfoodsDatset1[[All, "Nutrients"]]]
![](assets.en/analyze-visualize-and-compare-superfoods/O_200.png)
Create rules to convert the given nutrients to their appropriate properties.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_301.png)
superfoodsDatset2 =
superfoodsDatset1 /.
AssociationMap[
EntityProperty["Food", "Relative" <> # <> "Content"] &,
Union @@ Normal[superfoodsDatset1[[All, "Nutrients"]]]]
![](assets.en/analyze-visualize-and-compare-superfoods/O_201.png)
Find the most common nutrients in the Dataset.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_302.png)
mostCommonProperties =
superfoodsDatset2[Counts[Flatten[#]] &, "Nutrients"][TakeLargest[5]]
![](assets.en/analyze-visualize-and-compare-superfoods/O_202.png)
Find superfoods known for being high in fiber and compare them to nearly 9000 other foods. Visualize as a smoothed histogram, where the gray curve shows the probability density function of fiber content for most foods.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_303.png)
fiberDataset =
superfoodsDatset2[
Select[ContainsAny[#Nutrients, {EntityProperty["Food",
"RelativeTotalFiberContent"]}] &], KeyDrop["Nutrients"]];
![](assets.en/analyze-visualize-and-compare-superfoods/O_203.png)
Do the same process for protein-rich superfoods.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_310.png)
proteinDataset =
superfoodsDatset2[
Select[ContainsAny[#Nutrients, {EntityProperty["Food",
"RelativeTotalProteinContent"]}] &], KeyDrop["Nutrients"]];
![](assets.en/analyze-visualize-and-compare-superfoods/O_204.png)
Create a superfoods insignia, beginning with a set of superfood images.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_318.png)
GraphicsGrid[
Partition[
ims = EntityValue[
Cases[superfoodsDatset2[All, "FoodEntity"] // Normal,
Entity["FoodType", _], Infinity], "Image"], UpTo[5]]]
![](assets.en/analyze-visualize-and-compare-superfoods/O_205.png)
As the background, use a shape resembling that of a well-known super.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_319.png)
Entity["Lamina", "SupermanInsigniaLamina"]["Image"] // ImageCrop
![](assets.en/analyze-visualize-and-compare-superfoods/O_206.png)
Use some simple image processing to convert it to a background.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_320.png)
background =
ColorNegate[
Binarize[Entity["Lamina", "SupermanInsigniaLamina"]["Image"], .99]]
![](assets.en/analyze-visualize-and-compare-superfoods/O_207.png)
Create a "word cloud" consisting of superfoods.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_321.png)
wc = WordCloud[(RandomReal[{0.6, 1.2}] -> #) & /@ ims, background]
![](assets.en/analyze-visualize-and-compare-superfoods/O_208.png)
Combine the word cloud and background.
![Click for copyable input](assets.en/analyze-visualize-and-compare-superfoods/In_322.png)
ImageMultiply[{ColorReplace[background, Black -> LightGray], wc}]
![](assets.en/analyze-visualize-and-compare-superfoods/O_209.png)