-
Notifications
You must be signed in to change notification settings - Fork 1
/
qcew_to_json.py
executable file
·192 lines (142 loc) · 5.35 KB
/
qcew_to_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
import colorsys
import csv
import json
import random
import sys
reader = csv.DictReader(open('qcew/qcew_smith.csv'))
data = {}
def _int(s):
try:
return int(s)
except ValueError:
return 0
def color_generator(n):
for h in range(0, 360, 360 / n):
h = float(h) / 360
s = (90 + 10 * random.random()) / 200
l = (30 + 70 * random.random()) / 200
rgb = colorsys.hls_to_rgb(h, l, s)
rgb = map(lambda n: n * 255, rgb)
yield '#%02x%02x%02x' % tuple(rgb)
def splitthousands(s, sep=','):
if len(s) <= 3: return s
return splitthousands(s[:-3], sep) + sep + s[-3:]
def add_commas(n):
return splitthousands(str(n), ',')
def create_obj_for_row(row, root_obj):
obj = {}
ownership_code = row['ownership_code']
industry_code = row['industry_code']
obj['id'] = '%s-%s' % (ownership_code, industry_code)
if industry_code == '10':
if ownership_code == '0':
obj['name'] = 'All Smith County Employers'
elif ownership_code == '1':
obj['name'] = 'Federal Government'
elif ownership_code == '2':
obj['name'] = 'State Government'
elif ownership_code == '3':
obj['name'] = 'Local Government'
elif ownership_code == '5':
obj['name'] = 'Private Industry'
else:
obj['name'] = '%s' % row['industry_title']
obj['data'] = {
'status': row['annual_status_code'],
'establishments': _int(row['annual_average_number_of_establishments']),
'paid_employees': _int(row['annual_average_employment']),
'annual_payroll': _int(row['annual_total_wages']),
'$area': _int(row['annual_total_wages']),
'str_establishments': add_commas(_int(row['annual_average_number_of_establishments'])),
'str_paid_employees': add_commas(_int(row['annual_average_employment'])),
'str_annual_payroll': '$%s' % add_commas(_int(row['annual_total_wages'])),
}
if root_obj:
obj['data']['str_establishments_pct'] = '%.2f%%' % (float(obj['data']['establishments']) / root['data']['establishments'] * 100)
obj['data']['str_paid_employees_pct'] = '%.2f%%' % (float(obj['data']['paid_employees']) / root['data']['paid_employees'] * 100)
obj['data']['str_annual_payroll_pct'] = '%.2f%%' % (float(obj['data']['annual_payroll']) / root['data']['annual_payroll'] * 100)
obj['children'] = []
return obj
# First row is totals
row = reader.next()
root = create_obj_for_row(row, None)
root = create_obj_for_row(row, root) # quick hack to get 100%s
ownership = None
sector = None
subsector = None
industry_group = None
for row in reader:
ownership_code = row['ownership_code']
industry_code = row['industry_code']
# Skip combined government
if ownership_code == 4:
continue
obj = create_obj_for_row(row, root)
# Total row
if industry_code == '10':
if ownership:
if sector:
if subsector:
if industry_group:
subsector['children'].append(industry_group)
industry_group = None
sector['children'].append(subsector)
subsector = None
ownership['children'].append(sector)
sector = None
root['children'].append(ownership)
ownership = obj
# TODO: invalid NAICS codes?
elif industry_code[:2] == '10':
continue
elif len(industry_code) == 2 or '-' in industry_code:
if sector:
if subsector:
if industry_group:
subsector['children'].append(industry_group)
industry_group = None
sector['children'].append(subsector)
subsector = None
ownership['children'].append(sector)
sector = obj
elif len(industry_code) == 3:
if subsector:
if industry_group:
subsector['children'].append(industry_group)
industry_group = None
sector['children'].append(subsector)
subsector = obj
elif len(industry_code) == 4:
if industry_group:
subsector['children'].append(industry_group)
industry_group = obj
if industry_group:
subsector['children'].append(industry_group)
if subsector:
sector['children'].append(subsector)
if sector:
ownership['children'].append(sector)
if ownership:
root['children'].append(ownership)
# Recursively purge incomplete data
def recurse_purge_incomplete(node):
if len(node['children']) == 0:
return
if any([child['data']['status'] == 'N' for child in node['children']]):
print 'Dropping children of %s' % node['id']
node['children'] = []
for child in node['children']:
recurse_purge_incomplete(child)
recurse_purge_incomplete(root)
# Recursively assign colors
def recurse_assign_colors(node):
for child in node['children']:
if len(child['children']) > 0:
recurse_assign_colors(child)
colors = color_generator(len(node['children']))
for child in node['children']:
child['data']['$color'] = colors.next()
recurse_assign_colors(root)
with open('assets/data.js', 'w') as f:
f.write('DATA = %s' % json.dumps(root, indent=4))