• Stars
    star
    115
  • Rank 296,416 (Top 6 %)
  • Language
    Python
  • Created over 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

USDA food files SR28 to SQLite

USDA database to SQLite

Converts the USDA food database to SQLite.

The USDA has a database of nutritional information for many foods available at:

http://www.ars.usda.gov/Services/docs.htm?docid=8964

By default it's contained in flat files. This project includes scripts to generate appropriate schema and import them into a SQLite database. I've also hand generated a subset of the nutrients that includes commonly-familiar nutrients vs. the very extensive list provided by the USDA.

You will need to first download the files and unzip them into the data directory. Files for SR28 are included.

Run create_db.sh with the name of output database:

$ ./create_db.sh usda.sql3

On my system it takes about 10 seconds to populate the db.

Some example scripts to query the db are in the example folder. Sample usage:

$ python example/nutrition.py 01001
$ python example/search.py salmon

SQLlite browser

I recommend using the open source sqlitebrowser to view the usda.sql3 database. sqllitebrowser_screenshot

You can also upload the created *.sql3 file to https://sqliteonline.com/, saving yourself from needing to download any applications.

Usage Examples

SQL

  • SELECT * FROM nutrient; - print out all nutrient data
  • SELECT Count(*) FROM food; - 8789 - all foods
  • SELECT Count(*) FROM food where food_group_id !=300 and food_group_id !=3500 - 8257 - Exclude all baby food and Native American food
  • SELECT id FROM food; - [ [food.id] ]
  • SELECT id,long_desc FROM food; - [ [food.id, food.long_desc] ]
  • SELECT id,name FROM nutrient; - [ [nutrient.id, nutrient.name] ]
  • SELECT count(*),food_group.name FROM food inner join food_group on food.food_group_id=food_group.id group by food_group_id; - the count of all the foods in each food group

Every food's serving size, if it has one

SELECT 
  food.id,
  food.long_desc,
  gm_weight
FROM food 
INNER JOIN weight ON weight.food_id=food.id
where weight.description=='serving';

Nutrient amounts per 100g of food

SELECT 
  food.id,
  nutrient.id,
  nutrition.amount
FROM nutrition 
INNER JOIN food ON nutrition.food_id=food.id 
INNER JOIN nutrient ON nutrition.nutrient_id=nutrient.id;

Post processing

You can export this data to JSON and do further processing, like indexing. Indexing speeds up access for client Applications and reduces file size (by removing redundant data). Indexing the final SQL example reduces JSON representation from ~10MB to 7.0MB.

import Amt from './UsdaAmounts'; //results of the final query: [ [food.id, nutrient.id, nutrition.amount] ] for every food
let b = Amt.reduce((total, curr) => {
  const foodId = curr[0];
  const nutrientId = curr[1];
  const amt = curr[2];
  const foodIsNew = !(foodId in total);
  if (foodIsNew) total[foodId] = {};
  total[foodId][nutrientId] = amt;
  return total;
}, {})
console.log(JSON.stringify(b)); //note this add " characters to the beginning and end when printing to the console

Attribution

Adapted from https://github.com/czarandy/usda for scripts to run on Mac OS X with SR28 release.

More Repositories

1

face_morpher

πŸ‘Ό Morph faces with Python, Numpy, Scipy
Python
865
star
2

3Dreconstruction

3D reconstruction, sfm with Python3
Python
399
star
3

opencv

Find squares in an image with OpenCV C++
C++
239
star
4

hough_transform

Hough Transform implementation in Python
Python
122
star
5

reconstruction

3D reconstruction with openCV and SFM
C++
77
star
6

react-redux-table-example

React-Redux Router Table Sample App
JavaScript
50
star
7

stats-analysis

Engineering Statistics and Data Analysis
JavaScript
32
star
8

prequest

Promisified HTTP requests with `bluebird` and `request` modules
JavaScript
27
star
9

flask-restful-api-appengine

A restful API with flask for Google App Engine
Python
23
star
10

timeseries

Time series analytics with Python
Python
17
star
11

stasm_build

Mac/unix build scripts for stasm 4.1 with OpenCV 2 & 3
CMake
14
star
12

face-find-fun

Browser head-tracking and draws a party hat! - uses webrtc getUserMedia API, headtrackr and canvas
HTML
11
star
13

celery-flask-demo

Simple Flask app using celery with broker and backend
Python
10
star
14

python-data-science-intro

Intro to Python for Data Science for GA class
Jupyter Notebook
8
star
15

stasm

python wrapper for stasm - face points detector
C++
8
star
16

heap

Heap implementation in C++
C++
8
star
17

stl-complexities

C++ STL container complexities
8
star
18

MOOCS

MOOCS I love
7
star
19

alyssaq.github.io

Personal blog
CSS
5
star
20

esm-browser-example

ES modules in the browser
JavaScript
5
star
21

node-mongodb-api

Nodejs, MongoDB, Mongoose API boilerplate
JavaScript
4
star
22

hawkersg

Map markers and directions to hawker centres in Singapore
JavaScript
4
star
23

face_verifier

Verify whether a face is present in an image
Python
3
star
24

timezones-buddy

Convert between different timezones and unix timestamps
JavaScript
3
star
25

robotics_nanodegree_udacity_2019

Udacity's robotics nanodegree (2019)
CMake
3
star
26

kaggle

python notebooks for kaggle
Jupyter Notebook
3
star
27

egg

Egg math equation drawn with canvas
JavaScript
3
star
28

bottle-heroku-skeleton

Bottle skeleton with scientific packages for deployment on Heroku
Python
2
star
29

face-trackr-canvas

Face detection and canvas shape
JavaScript
2
star
30

cs231n-2017

Assignment solutions for cs231n 2017
Jupyter Notebook
2
star
31

financial-report

A payment report for rental property
JavaScript
2
star
32

alyssa.asia

My online CV - alyssa.asia site
JavaScript
1
star
33

backbone-basics

Simple backbonejs apps
JavaScript
1
star
34

appcache_sample

Sample offline app with appcache manifest
JavaScript
1
star
35

face_morpher_slides

Face Morpher Slides for FOSS Asia 2015
CSS
1
star
36

meetup_events

Find meetup events in your country
JavaScript
1
star
37

proxy

Go Proxy Server Example
Go
1
star
38

tornado-examples

Tornado examples showing async, coroutines, yield, periodic calls, etc
Python
1
star
39

facebook_api_user

Get user info from Facebook Graph API
JavaScript
1
star
40

threejs-jam

Three.js playground - ball bouncing
1
star
41

rainbow

RGB to hex visualizer, slider
JavaScript
1
star