-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmdline_check.py
227 lines (197 loc) · 7.13 KB
/
cmdline_check.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
import json
import math
import glob
import os
import sys
import argparse
import sqlite3
import time
import csv
import itertools
from tqdm import tqdm
import multiprocessing
from ballot_image import ballot_image, contest_options, utils
# Lots of this is taken from https://eli.thegreenplace.net/2012/01/16/python-parallelizing-cpu-bound-tasks-with-multiprocessing
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print('[%s]' % self.name, end=' ')
print('Elapsed: %s' % (time.time() - self.tstart))
styleLookup = {
"0,1,6,15,16,17,19,27,66": "style7.json",
"00000000280000459009000011": "style7.json",
"0,1,6,15,16,18,19,27,66": "style31.json",
"00000002820002031873000011": "style31.json",
"Ballot Style 31": "style31.json",
"Ballot Style 7": "style7.json",
}
def basic_worker(targets, out_q):
for t in targets:
try:
ballot = ballot_image.BallotImage(t.path)
ballot.find_boxes()
except Exception as e:
#print(e)
#print(t.path)
b = utils.BallotInfo(t.cvr_id, style_string="Unknown", express_vote=False, failed=True)
out_q.put(b)
continue
if ballot.express_vote:
b = utils.BallotInfo(t.cvr_id, ballot.detected_style[:-6], True, ballot.detected_style, ",".join([barcode for barcode in ballot.barcodes.keys()]))
else:
b = utils.BallotInfo(t.cvr_id, ballot.detected_style, False, boxcount=ballot.boxcount)
out_q.put(b)
def basic_styles(targets, nprocs, output_file = None):
ballots = []
out_q = multiprocessing.Queue()
procs = []
chunksize = int(math.ceil(len(targets) / float(nprocs)))
print("Items are %d" % (len(targets)))
for i in range(nprocs):
print("Chunk %d: starts at %d ends at %d" % (i, chunksize * i, chunksize * (i + 1)))
for i in range(nprocs):
p = multiprocessing.Process(
target=basic_worker,
args=(targets[chunksize * i:chunksize * (i + 1)], out_q))
procs.append(p)
p.start()
with tqdm(total=len(targets)) as pbar:
for i in range(len(targets)):
ballots.append(out_q.get())
pbar.update(1)
for p in procs:
p.join()
#print(len(ballots))
#for b in ballots[:25]:
# print(b)
#for b in ballots:
# if b.failed:
# print("Failed: %s" % (b))
if output_file:
with open(output_file, 'w', newline='') as csvfile:
fieldnames = ['cvr_id', 'failed', 'style_string', 'express_vote', 'long_style_string', 'barcodes_string', "boxcount"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for b in ballots:
writer.writerow({'cvr_id': b.cvr_id, 'failed': b.failed, 'style_string': b.style_string, 'express_vote': b.express_vote, 'long_style_string': b.long_style_string, 'barcodes_string': b.barcodes_string, 'boxcount': b.boxcount})
else:
for b in ballots:
print(b)
def basic_styles_old(targets):
ballots = []
for t in tqdm(targets):
ballot = ballot_image.BallotImage(t.path)
ballot.find_boxes()
#print("%s: %s" % (t.cvr_id, ballot.detected_style))
if ballot.express_vote:
ballots.append(BallotInfo(t.cvr_id, ballot.detected_style[:-6], True, ballot.detected_style, ",".join([barcode for barcode in ballot.barcodes.keys()])))
else:
ballots.append(BallotInfo(t.cvr_id, ballot.detected_style, False))
for b in ballots:
print(b)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--basic", action="store_true")
parser.add_argument("--basic_output_file", default=None, help="Output CVS file for basic style classification")
#group.add_argument("--score", action="store_true")
parser.add_argument("--inputdir", help="Directory to scan",action="append", nargs="+")
parser.add_argument("--cvrids", help="Cast Vote Records to process individually. Requires dbfile as well", type=int, action="append", nargs="+")
parser.add_argument("--dbfile", default="test_data/records.db", help="sqlite3 database with CVRs")
parser.add_argument("--pngdir", help="Top level directory for PNGs when using only CVRs")
#parser.add_argument("--mismatches", help="File to store ballots that do not match ES&S 100 percent")
#parser.add_argument("--expressvotes", help="Output file for list of ballots that are expressvote images")
#parser.add_argument("--overall", help="File for overall scores")
parser.add_argument("--nprocs", type=int, default=1, help="Number of workers to use")
args = parser.parse_args()
#overall_file= args.overall
#mismatches_file = args.mismatches
inputdirs = args.inputdir
#expressvotes_file = args.expressvotes
dbfile = args.dbfile
cvrids = args.cvrids
pngdir = args.pngdir
nprocs = args.nprocs
if cvrids:
if dbfile is None:
print("Error: specifying CVR IDs requires a SQLite database to be defined as well")
exit(-1)
if inputdirs:
inputdirs = list(itertools.chain.from_iterable(inputdirs))
if cvrids:
cvrids = list(itertools.chain.from_iterable(cvrids))
#print(inputdirs)
if not inputdirs and not cvrids:
print("Must have at least some files to process!")
exit(-1)
conn = sqlite3.connect(dbfile)
conn.row_factory = sqlite3.Row
c = conn.cursor()
files = []
if inputdirs:
for d in inputdirs:
files.extend(utils.generate_files(d))
if cvrids:
for cvr in cvrids:
files.extend(utils.generate_file_from_cvrid(cvr, pngdir, c))
if args.basic:
with Timer("Basic"):
basic_styles(files, nprocs, args.basic_output_file)
'''
overall = []
failed = []
expressvote = []
for imgFile in tqdm(files):
#for imgFile in files[0:10]:
c.execute('select * from results where "Cast Vote Record" = {castrecord}'.format(castrecord=imgFile.cvr_id))
all_rows = c.fetchall()
row = all_rows[0]
try:
ballot = ballot_image.BallotImage(imgFile.path, style=row["Ballot Style"], styleDir="/Users/epaulson/development/DaneCountyVotes/Fall2018General/ballot_styles")
styleFile = styleLookup[row["Ballot Style"]]
cvr = contest_options.create_cvr("test_data/ballot_styles/" + styleFile, conn, imgFile.cvr_id)
ballot.find_boxes()
results = ballot.score(imgFile.cvr_id, cvr)
except Exception as e:
res = "%d: FAIL\n" % (imgFile.cvr_id)
failed.append(imgFile.cvr_id)
overall.append(res)
print(e)
continue
if results["expressVote"]==True:
expressvote.append(imgFile.cvr_id)
success = 0.0
fail = 0.0
for race in results["votes"]:
details = race['details']
if details['ESS'] != details['scored']:
fail += 1.0
else:
success += 1.0
if fail == 0.0:
res = "%s: Success %f (%f match, %f misses)\n" % (imgFile.cvr_id, (success / (success+fail)), success, fail)
overall.append(res)
if fail > 0.0:
res = "%s: Partial %f (%f match, %f misses)\n" % (imgFile.cvr_id, (success / (success+fail)), success, fail)
overall.append(res)
failed.append(imgFile.cvr_id)
print("Mismatches: (%d)" % (len(failed)))
for mismatch in failed:
print(mismatch)
if overall_file:
with open(overall_file, "w") as f:
for x in overall:
f.write(x)
if mismatches_file:
with open(mismatches_file, "w") as f:
for x in failed:
f.write("%s\n" % str(x))
if expressvotes_file:
with open(expressvotes_file, "w") as f:
for x in expressvote:
f.write("%s\n" % str(x))
'''