Showing posts with label NHTS. Show all posts
Showing posts with label NHTS. Show all posts

Tuesday, April 7, 2009

Loading data from the NHTS

The data in the NHTS is available in a couple of forms. My preference is to work with the CSV format. In Python, it is easy to load a CSV table into an object in memory. For the CSV format, the NHTS data is organized into 4 files. Each file contains the data for one table. There are common columns in each file that allow information to be correlated between the tables. A CSV file typically is organized into a header row followed by data rows. The NHTS follows this format.
HOUSEID,VHCASEID,VEHID, ... ,DRVRCNT,MSAPOP 010000018,01000001801,01, ... ,2,7608070 . . . 915637259,91563725904,04, ... ,2,-1
One way to get this data into Python is using the file object. This allows a file to be opened for read access, then for each line in the file to be loaded individually. To organize the data, there are several options. For this data I used a Python dictionary which associated each header with a column from the file. The simplest implementation of this function is only a few lines:
def loadTable(filename,maxRows=1e9,keepList=[],ignoreList=[]): f = file(filename,'r') table = {} for line in f: if count == 0: headers = line.split(',') for header in headers: table[header]=[] else: line = line.strip() line = line.strip('\n') row = line.split(',') for idx in range(0,len(row)): table[headers[idx]].append(row[idx]) return table
While this code will work, it is not robust to a host of problems. The wrong file name can be supplied, the system may not have enought memory, or a row may be ill formed. Additionally, no real documentation is provided so the dir() can provide help on the function. Also, you might not want all of the data in the file to be loaded. Certain columns can safely be omitted. To correct these issues, the following function will be used to load the data tables:
def loadTable(filename,maxRows=1e9,keepList=[],ignoreList=[]): ''' This function will load up to maxRows from the CSV file with the first row specifying the names of the columns. into a dictionary where each key specified a column. To minimize memory use, there are two optional lists of strings which limit the columns loaded from the file. If keepList==[] and ignoreList==[] then all columns from the file will be loaded. If keepList!=[], then only those colums in keepList and not in the ignoreList are loaded. If keepList==[] then all columns which match names in ignoreList are omitted from the returned table. ''' try: f = file(filename,'r') count = 0 table = {} for line in f: if count == 0: headers = line.split(',') for header in headers: table[header]=[] else: line = line.strip() line = line.strip('\n') row = line.split(',') for idx in range(0,len(row)): if ((headers[idx] in keepList or len(keepList)==0) and (len(ignoreList)==0 or not (headers[idx] in ignoreList))): table[headers[idx]].append(row[idx]) count += 1 if count==maxRows: print 'Terminated load - maximum number of rows exceeded' return table return except IOError: print 'IOError: File can not be opened...' return {} except MemoryError: print 'MemoryError: ran out of working memory...' return table except: print 'Unknown error when loading table ....' return {}

Monday, April 6, 2009

Build the working directory

To replicate the work I'm going to share, you will need to build a directory that will share the source code and the data. Follow these steps to build that directory.
  1. Create a new directory for testing.
  2. Download the 2001 NHTS ASCII.CSV data set.
  3. Unzip the contents of that file into the testing directory. You should have four CSV files.

If you do not have Python already available on your machine, I'd recommend starting with Portable Python, I'm using Portable Python 1.1 which implement Python 2.5.4 as a portable program (try saying that 3 time fast!). Everything initially will work in this implementation.

Using Python to Visualize the 2001 NHTS - Project Scope

This project will work through using Python for visualizing a complex data set to gain insight and understanding. One of my favorite data sets is the US National Household Travel Survey. This survey summarizes demographics and a days worth of driving for 70,000 people over a one year period. The data set is so complex that after about two years of looking at it, I've only just started to find the interesting information contained in it. My initial goals are as follows:
  1. Write pure Python libraries for inputting the data sets and preprocessing the results. I'm going to write everything in pure Python from scratch, rather than use existing tools for two reasons. First I need the exercises to learn Python better. Second, I want my tools for importing the data to reusable under Jython and for use with Blender out of the box.
  2. Use the following tools to visualize information in the NHTS:
  3. I'll be using the following Python distributions:
  4. Create both static and animated visualizations in each tool.
  5. Gain some insight into travel patterns that I did not have before starting this project.
  6. Build some tools for exploring the 2008 release of the NHTS.