-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
6,295 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,56 @@ | ||
import os | ||
import sys | ||
import argparse | ||
|
||
from synthpop.recipes.starter3 import Starter | ||
from synthpop.synthesizer import synthesize_all, enable_logging | ||
import pandas as pd | ||
import numpy as np | ||
|
||
|
||
def run(state='AL', county='all', year=2013): | ||
def synthesize_county(state, county, year): | ||
starter = Starter(os.environ["CENSUS"], state, county, acsyear=year) | ||
synthetic_population = synthesize_all(starter) | ||
hh = synthetic_population[0] | ||
p = synthetic_population[1] | ||
state_fips = hh['state'].unique()[0] | ||
county_fips = hh['county'].unique()[0] | ||
|
||
# verify if there is a folder with the name of the state where we can save the synthesized files | ||
if not(os.path.isdir(state_fips)): | ||
os.mkdir(state_fips) | ||
|
||
p.to_csv('./{}/p_{}_{}_{}.csv'.format(state_fips, state_fips, county_fips, year)) | ||
hh.to_csv('./{}/hh_{}_{}_{}.csv'.format(state_fips, state_fips, county_fips, year)) | ||
|
||
if year >= 2018: | ||
url_national_data = "https://storage.googleapis.com/synthpop-public/PUMS2018/pums_2018_acs5/" | ||
else: | ||
url_national_data = "https://s3-us-west-1.amazonaws.com/synthpop-data2/" | ||
national_data = pd.read_csv(url_national_data + 'national_county.txt', dtype='str') | ||
state_data = national_data[national_data['State'] == state] | ||
|
||
if county != 'all': | ||
state_data = state_data[state_data['County ANSI'].isin(county.split(','))] | ||
|
||
for index, row in state_data.iterrows(): | ||
county_name = row['County Name'] | ||
synthesize_county(state, county_name, year) | ||
|
||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) > 1: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-s", "--state", type=str, help="State to synthesize") | ||
parser.add_argument("-c", "--county", type=str, | ||
help="County or list of counties to synthesize in ANSI") | ||
parser.add_argument("-y", "--year", type=int, help="Year to synthesize") | ||
|
||
args = parser.parse_args() | ||
state = args.state if args.state else 'AL' | ||
county = args.county if args.county else 'all' | ||
year = args.year if args.year else 2013 | ||
run(state, county, year) | ||
else: | ||
run() |
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
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
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