-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove submodule: plugins/FoodData_parser * Add files from submodule. * Replace FOODON_ by empty in FoodData_parser. * 💅
- Loading branch information
1 parent
0b18e7e
commit 5d6ee44
Showing
5 changed files
with
440 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule FoodData_parser
deleted from
0789d8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import ujson | ||
import os | ||
|
||
|
||
def load_data(data_folder: str): | ||
with open(os.path.join(data_folder, 'FoodData_Central_foundation_food_json_2022-04-28.json')) as f: | ||
data = ujson.load(f)['FoundationFoods'] | ||
for food in data: | ||
base = { | ||
'subject': { | ||
'description': food['description'], | ||
'ndbNumber': food['ndbNumber'], | ||
'fdcId': food['fdcId'], | ||
'foodCategory': food['foodCategory']['description'] | ||
} | ||
} | ||
for n in food['foodNutrients']: | ||
doc = base.copy() | ||
doc['object'] = { | ||
'nutrientName': n['nutrient']['name'], | ||
'nutrientId': n['nutrient']['id'], | ||
'nutrientRank': n['nutrient']['rank'] | ||
} | ||
doc['relation'] = {} | ||
|
||
if 'amount' in n: | ||
doc['object']['nutrientAmount'] = n['amount'] | ||
doc['object']['nutrientAmountUnits'] = n['nutrient']['unitName'] | ||
|
||
if 'code' in n['foodNutrientDerivation']: | ||
doc['relation']['code'] = n['foodNutrientDerivation']['code'] | ||
if 'code' in n['foodNutrientDerivation']['foodNutrientSource']: | ||
doc['relation']['sourceCode'] = n['foodNutrientDerivation']['foodNutrientSource']['code'] | ||
if 'description' in n['foodNutrientDerivation']: | ||
doc['relation']['description'] = n['foodNutrientDerivation']['description'] | ||
if 'description' in n['foodNutrientDerivation']['foodNutrientSource']: | ||
doc['relation']['sourceDescription'] = n['foodNutrientDerivation']['foodNutrientSource']['description'] | ||
|
||
if 'min' in n: | ||
doc['relation']['nutrientMinAmount'] = n['min'] | ||
if 'max' in n: | ||
doc['relation']['nutrientMaxAmount'] = n['max'] | ||
if 'median' in n: | ||
doc['relation']['nutrientMedianAmount'] = n['median'] | ||
|
||
doc['_id'] = f"{doc['subject']['fdcId']}-{doc['object']['nutrientId']}" | ||
yield doc | ||
|
||
|
||
def main(): | ||
obj = {} | ||
nutrients = {"total": 0} | ||
for docs in load_data('./'): | ||
if docs['_id'] in obj: | ||
print(docs['_id']) | ||
obj[docs['_id']] = docs | ||
|
||
if docs['object']['nutrientName'] in nutrients: | ||
nutrients[docs['object']['nutrientName']] += 1 | ||
else: | ||
nutrients[docs['object']['nutrientName']] = 1 | ||
|
||
print('done') | ||
nutrients["total"] = len(nutrients.keys()) | ||
with open('./output.tsv', 'w') as f: | ||
f.write("nutrient\tcount\n") | ||
for i in nutrients: | ||
f.write(f"{i}\t{nutrients[i]}\n") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"version": "0.2", | ||
"__metadata__": { | ||
"url": "https://fdc.nal.usda.gov/index.html" | ||
}, | ||
"author" : { | ||
"name" : "Rohan Juneja", | ||
"url" : "https://github.com/rjawesome" | ||
}, | ||
"dumper" : { | ||
"data_url" : ["https://fdc.nal.usda.gov/fdc-datasets/FoodData_Central_foundation_food_json_2022-04-28.zip", "https://fdc.nal.usda.gov/fdc-datasets/FoodData_Central_foundation_food_csv_2022-04-28.zip"], | ||
"uncompress" : true | ||
}, | ||
"uploader" : { | ||
"parser" : "parser:load_data", | ||
"on_duplicates": "error" | ||
} | ||
} |
Oops, something went wrong.