-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_dbs.py
296 lines (221 loc) · 8.73 KB
/
prepare_dbs.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from collections import defaultdict
from csv import DictReader
from json import load
from pathlib import Path
from shutil import copy2
from string import punctuation
from sys import stdout
from zipfile import ZipFile
from shapely.geometry import MultiPolygon, shape
from sqlite_utils import Database
from sqlite_utils.db import NotFoundError
from unidecode import unidecode
from xlrd import open_workbook
translation_table = str.maketrans("", "", punctuation)
def clean_name(name):
return unidecode(name).translate(translation_table)
def find_matching_candidate(candidates, name):
# if we have an exact match, return it
for candidate in candidates:
if clean_name(candidate["name"]) == name:
return candidate
cleaned_name = clean_name(name)
# otherwise, let's hunt for a match
# modified_name = name.replace(" JR", "").strip()
last_name = cleaned_name.split()[-1]
# if we have a match on last name, return it
possible_matches = [c for c in candidates if last_name in clean_name(c["name"])]
# if we have a single match, return it
if len(possible_matches) == 1:
return possible_matches[0]
# another try, let's try to match on first name
first_name = cleaned_name.split()[0]
# if we have a match on first name, return it
possible_matches = [c for c in candidates if first_name in clean_name(c["name"])]
# if we have a single match, return it
if len(possible_matches) == 1:
return possible_matches[0]
# ugh - let's try to remove the prefix
last_name_without_prefix = cleaned_name.replace("JR", "").strip().split()[-1]
# if we have a match on last name, return it
possible_matches = [
c for c in candidates if last_name_without_prefix in clean_name(c["name"])
]
# if we have a single match, return it
if len(possible_matches) == 1:
return possible_matches[0]
raise ValueError(f"Could not find a match for {name}")
def determine_number_vote_for(s):
if s == "":
return 1
if s.endswith("than two"):
return 2
if s.endswith("than three"):
return 3
if s.endswith("than four"):
return 4
if s.endswith("than five"):
return 5
raise ValueError(f'Could not determine number of votes: "{s}"')
def convert_type(s):
if s == "POLLING PLACE":
return "polling_place"
if s == "VBM PORTION":
return "vote_by_mail"
if s == "TOTAL":
return "total"
raise ValueError(f'Could not determine type: "{s}"')
def prepare_vote_lookup():
lookup = {}
count = 0
with open("inputs/counter_data.json") as infile:
data = load(infile)
for row in data["Data"]:
if row["ReferenceType"] == "CAND":
count += 1
lookup[row["ReferenceID"]] = row["Value"]
return lookup
def run():
# Create a ZipFile object from the response and extract
with ZipFile("inputs/statement_of_votes_cast.zip") as zipfile:
zipfile.extractall("tmp/results/")
# Create a database
db = Database("tmp/results_spatial.db", recreate=True)
# Bootstrap the precincts table
with open("inputs/precincts.csv") as infile:
reader = DictReader(infile)
for row in reader:
# Load each of our precincts
db["precincts"].insert(
{
"precinct": row["Precinct"],
"ballot_group": row["BallotGroup"],
"serial_number": row["SerialNumber"],
"location": "",
},
pk="precinct",
columns={"ballot_group": int, "serial_number": int},
)
# Prepare the lookup for getting non-scubbed vote totals
vote_lookup = prepare_vote_lookup()
# Bootstrap the contests table
with open("inputs/election_data.json") as infile:
data = load(infile)
contest_groups = data["Data"]["ContestGroups"]
for contest_group in contest_groups:
group = contest_group["Name"]
contests = contest_group["Contests"]
for contest in contests:
contest_id = contest["ID"]
db["contests"].insert(
{
"id": contest_id,
"group": group,
"name": contest["Title"],
"type": contest["Type"],
"non_partisan": contest["NonPartisan"],
"voter_nominated": contest["VoterNominated"],
"vote_for": determine_number_vote_for(contest["VoteFor"]),
},
pk="id",
columns={
"voter_nominated": bool,
"non_partisan": bool,
"vote_for": int,
},
)
candidates = contest["Candidates"]
for candidate in candidates:
db["candidates"].insert(
{
"id": candidate["ID"],
"name": candidate["Name"],
"party": candidate["Party"],
"contest_id": contest_id,
"total_votes": vote_lookup[candidate["ID"]],
},
pk="id",
foreign_keys=[("contest_id", "contests", "id")],
)
# Find all the Excel files in the results directory
files = Path("tmp/results/").glob("*.xls")
# Loop through each file
stdout.write("Loading results\n")
for file in files:
stdout.write(".")
stdout.flush()
# Grab the contest ID from the filename
contest_id = int(file.stem.split("-")[-1])
# Open the Excel file
workbook = open_workbook(file)
# Grab the first (and only) sheet
sheet = workbook.sheet_by_index(0)
# Grab the column names from the header row (row 2)
headers = sheet.row_values(2)
candidate_names = [s for s in headers[8:] if s]
results = defaultdict(dict)
# Loop through the remaining rows
for index in range(3, sheet.nrows):
# Grab the values for this row
values = sheet.row_values(index)
# Create a dictionary of {column_name: value}
row_data = dict(zip(headers, values))
precinct_id = row_data["PRECINCT"]
type = convert_type(row_data["TYPE"])
candidates = list(
db["candidates"].rows_where("contest_id = ?", [contest_id])
)
if type == "total":
db["precincts"].update(
precinct_id,
{
"location": row_data["LOCATION"],
},
)
for name in candidate_names:
candidate = find_matching_candidate(candidates, name)
results[(precinct_id, candidate["id"])][type] = int(row_data[name])
db["results"].insert_all(
[
{
"candidate_id": candidate_id,
"contest_id": contest_id,
"precinct_id": precinct_id,
**values,
}
for (precinct_id, candidate_id), values in results.items()
],
columns={"polling_place": int, "vote_by_mail": int, "total": int},
foreign_keys=[
("candidate_id", "candidates", "id"),
("contest_id", "contests", "id"),
("precinct_id", "precincts", "precinct"),
],
batch_size=1_000,
)
# Create indexes for all the foreign keys
db.index_foreign_keys()
# Create a copy without spatialite support
copy2("tmp/results_spatial.db", "tmp/results.db")
# Initialize SpatiaLite
db.init_spatialite()
# Add a SpatiaLite 'geometry' column
db["precincts"].add_geometry_column("geometry", "MULTIPOLYGON")
with open("inputs/precincts.json") as infile:
collection = load(infile)
stdout.write("\nLoading precinct geographies\n")
for feature in collection["features"]:
stdout.write(".")
stdout.flush()
precinct_id = feature["properties"]["Precinct"]
geometry = shape(feature["geometry"])
# We need to convert the geometry to a MultiPolygon
if geometry.type == "Polygon":
geometry = MultiPolygon([geometry])
db["precincts"].update(
precinct_id,
{"geometry": geometry.wkt},
conversions={"geometry": "GeomFromText(?, 4326)"},
)
if __name__ == "__main__":
run()