""" Selected batches of the San Diego 1% draw, matched to the batch canvass. Contest counts and small cells. """ import csv, collections, re, sys D = sys.argv[1] # ---- the drawn batches, by draw draws = [] draw = '' for line in open(f"{D}/SD_2026_random_draw_batches.txt"): line = line.strip() if line.startswith('VC Batch'): draw = 'Nov 4 VC' elif line.startswith('mail batches Nov 4'): draw = 'Nov 4 mail' elif line.startswith('Nov 9'): draw = 'Nov 9 mail' m = re.match(r'^(\d)(\d)(\d\d)-(\d+)$', line) if m: kind = {'4': 'Mail', '6': 'Vote Centers'}[m.group(1)] make = {'0': 'HiPro', '1': 'Canon'}[m.group(2)] draws.append((draw, line, f"{kind} {make} {m.group(3)}", int(m.group(4)))) # ---- the canvass, by batch and contest cells = collections.defaultdict(dict) # (tab, batch, contest) -> choice -> votes for n in (1, 2): for r in csv.DictReader(open(f"{D}/sov_{n}.csv", newline='')): if r['tabulator'] == 'Contest Total': continue cells[(r['tabulator'], int(r['batch']), r['contest'])][r['choice']] = int(r['votes'] or 0) contests = sorted({k[2] for k in cells}) # ---- vote for N: the largest N that divides every batch row of the contest sums = collections.defaultdict(list) for (t, b, c), ch in cells.items(): sums[c].append(sum(ch.values())) vote_for = {c: max(n for n in range(1, 8) if all(s % n == 0 for s in sums[c])) for c in contests} def cell_stats(ch, n): ballots = sum(ch.values()) // n voted = {k: v for k, v in ch.items() if k not in ('Over Votes', 'Under Votes') and v} unanimous = ballots > 0 and len(voted) == 1 and not ch.get('Under Votes') and not ch.get('Over Votes') return ballots, unanimous # ---- 1. totals of each contest in each selected batch batch_by_key = collections.defaultdict(list) for k in cells: batch_by_key[(k[0], k[1])].append(k[2]) out1 = csv.writer(open(f"{D}/selected_batch_contest_totals.csv", 'w', newline='')) out1.writerow(['draw', 'batch_id', 'tabulator', 'batch', 'contest', 'vote_for', 'ballots', 'choice', 'votes']) found, missing = [], [] for draw, bid, tab, b in draws: cs = batch_by_key.get((tab, b)) if not cs: missing.append((draw, bid, tab, b)); continue found.append((draw, bid, tab, b)) for c in contests: if c not in cs: continue ch = cells[(tab, b, c)] ballots, _ = cell_stats(ch, vote_for[c]) if ballots == 0: continue for choice, v in ch.items(): out1.writerow([draw, bid, tab, b, c[2:], vote_for[c], ballots, choice, v]) # ---- 2. ballots sampled for each contest, and 3. small cells per_contest = collections.defaultdict(lambda: [0, 0]) small = [] for draw, bid, tab, b in found: for c in batch_by_key[(tab, b)]: ballots, unan = cell_stats(cells[(tab, b, c)], vote_for[c]) if ballots == 0: continue per_contest[c][0] += 1; per_contest[c][1] += ballots if ballots < 10 or unan: small.append((bid, c[2:], ballots, unan, cells[(tab, b, c)])) total_in_county = collections.Counter() for (t, b, c), ch in cells.items(): total_in_county[c] += sum(ch.values()) // vote_for[c] out2 = csv.writer(open(f"{D}/contest_sample_counts.csv", 'w', newline='')) out2.writerow(['contest', 'vote_for', 'batches_sampled', 'ballots_sampled', 'ballots_in_canvass', 'share']) for c in contests: bs, bl = per_contest.get(c, [0, 0]) out2.writerow([c[2:], vote_for[c], bs, bl, total_in_county[c], f"{bl / total_in_county[c]:.4f}" if total_in_county[c] else '']) out3 = csv.writer(open(f"{D}/selected_small_cells.csv", 'w', newline='')) out3.writerow(['batch_id', 'contest', 'ballots', 'unanimous', 'votes']) for s in small: out3.writerow([s[0], s[1], s[2], int(s[3]), '; '.join(f"{k} {v}" for k, v in s[4].items() if v)]) # ---- the same small-cell counts over every batch in the published canvass all_cells = all_small = all_unan_small = all_unan = 0 for (t, b, c), ch in cells.items(): ballots, unan = cell_stats(ch, vote_for[c]) if ballots == 0: continue all_cells += 1 all_small += ballots < 10 all_unan += unan all_unan_small += unan and ballots < 10 # ---- summary print('drawn', len(draws), 'found', len(found), 'missing', len(missing)) for m in missing: print(' missing', m) print('vote_for > 1:', {c[2:60]: n for c, n in vote_for.items() if n > 1}) sel_cells = sum(v[0] for v in per_contest.values()) print('selected: contests touched', len(per_contest), 'of', len(contests), 'contest-batch cells', sel_cells) print('selected small cells (<10 ballots)', sum(1 for s in small if s[2] < 10), 'unanimous small', sum(1 for s in small if s[2] < 10 and s[3]), 'unanimous any size', sum(1 for s in small if s[3])) print('whole canvass: cells', all_cells, 'small', all_small, 'unanimous', all_unan, 'unanimous small', all_unan_small) bsz = [] for draw, bid, tab, b in found: ch = cells[(tab, b, '1 GOVERNOR')] bsz.append((bid, sum(ch.values()))) print('governor ballots per selected batch', sorted(v for _, v in bsz)) print('sum of governor ballots by draw', {d: sum(v for (dd, bid, t, b), (_, v) in zip(found, bsz) if dd == d) for d in ('Nov 4 VC', 'Nov 4 mail')})