# Builds dataset v5 tables (receipts by day and donor group; I-638 spending by month) and the new analyses. # Reuses funding-scope/scripts (raw PDC data downloaded 2026-11-27). Usage: python -I build_v5.py import json, os, sys, csv from collections import defaultdict HERE = os.path.dirname(os.path.abspath(__file__)) SCOPE = os.path.join(HERE, '..', '..', 'funding-scope', 'scripts') sys.path.insert(0, SCOPE) import io, contextlib with contextlib.redirect_stdout(io.StringIO()): import scope from scope import receipts, monthly_spend OUT = os.path.join(HERE, 'out'); os.makedirs(OUT, exist_ok=True) NAMES = {'HEYWL--535': "Let's Go Washington (Sponsored by Brian Heywood)", 'FREEW--138': 'No Hate in WA State'} SIDE = {'HEYWL--535': 'Yes', 'FREEW--138': 'No'} def grp(r): if r['group'] == 'heywood': return 'Brian Heywood' if r['group'] == 'individual': return 'Other individuals (named)' if r['group'] == 'unitemized': return 'Small unitemized gifts' if r['inatai']: return 'Inatai Foundation' if r['union_re']: return 'Labor unions' return 'Other organizations' R = {f: receipts(f) for f in NAMES} S = {f: monthly_spend(f) for f in NAMES} print('LGW union-regex names:', sorted({r['name'] for r in R['HEYWL--535'] if r['union_re']})) print('NoHate union-regex names not in L3:', sorted({r['name'] for r in R['FREEW--138'] if r['union_re'] and not r['union_l3']})) day = defaultdict(lambda: [0.0, 0]) for f, rows in R.items(): for r in rows: k = (f, r['date'], r['ey'], grp(r)); day[k][0] += r['amount']; day[k][1] += 1 with open(os.path.join(OUT, 'i638_receipts_by_day.csv'), 'w', newline='', encoding='utf-8') as fh: w = csv.writer(fh, lineterminator='\n') w.writerow(['filer_id', 'side', 'receipt_date', 'election_year', 'donor_group', 'amount', 'receipts']) for k in sorted(day): w.writerow([k[0], SIDE[k[0]], k[1], k[2], k[3], f'{round(day[k][0], 2):.2f}', day[k][1]]) print('day rows', len(day)) months = sorted({r['date'][:7] for f in R for r in R[f]} | {m for f in S for m in S[f] if m}) mrows = [] for f in NAMES: for m in months: rec = defaultdict(float) for r in R[f]: if r['date'][:7] == m: rec[grp(r)] += r['amount'] sp = S[f].get(m, dict(total=0.0, i638=0.0, unalloc=0.0, i638_prorated=0.0)) tot = sum(rec.values()) if tot == 0 and sp['total'] == 0: continue mrows.append(dict(filer_id=f, side=SIDE[f], month=m, receipts_total=round(tot, 2), heywood=round(rec['Brian Heywood'], 2), other_individuals_named=round(rec['Other individuals (named)'], 2), small_unitemized=round(rec['Small unitemized gifts'], 2), inatai=round(rec['Inatai Foundation'], 2), labor_unions=round(rec['Labor unions'], 2), other_organizations=round(rec['Other organizations'], 2), spending_total=round(sp['total'], 2), i638_equal_split=round(sp['i638'], 2), not_allocated=round(sp['unalloc'], 2), i638_with_prorated_overhead=round(sp['i638_prorated'], 2))) cols = list(mrows[0].keys()) with open(os.path.join(OUT, 'i638_money_by_month.csv'), 'w', newline='', encoding='utf-8') as fh: w = csv.DictWriter(fh, cols, lineterminator='\n'); w.writeheader() for r in mrows: w.writerow({k: (f'{v:.2f}' if isinstance(v, float) else v) for k, v in r.items()}) print('month rows', len(mrows)) for r in mrows: if r['receipts_total'] == 0 and r['i638_equal_split']: print('UNMATCHED spending month', r) def shares(pairs): t = defaultdict(float) for g, a in pairs: t[g] += a T = sum(t.values()) sh = lambda v: round(v / T, 6) oth = t['Other individuals (named)'] + t['Small unitemized gifts'] orgs = t['Inatai Foundation'] + t['Labor unions'] + t['Other organizations'] return dict(total=round(T, 2), heywood=round(t['Brian Heywood'], 2), heywood_share=sh(t['Brian Heywood']), other_individuals=round(oth, 2), other_individuals_share=sh(oth), named_other_individuals_share=sh(t['Other individuals (named)']), all_individuals_share=sh(t['Brian Heywood'] + oth), organizations=round(orgs, 2), organizations_share=sh(orgs), inatai_share=sh(t['Inatai Foundation']), labor_unions_share=sh(t['Labor unions']), inatai_and_unions_share=sh(t['Inatai Foundation'] + t['Labor unions']), other_individuals_minus_half=round(oth - T / 2, 2)) def win(f, spans): return [(grp(r), r['amount']) for r in R[f] if any(a <= r['date'] <= b for a, b in spans)] def alloc(f, key, by): rec = defaultdict(lambda: defaultdict(float)); per = (lambda r: r['date'][:7]) if by == 'month' else (lambda r: r['ey']) for r in R[f]: rec[per(r)][grp(r)] += r['amount'] sp = defaultdict(float) for m, v in S[f].items(): sp[m if by == 'month' else m[:4]] += v[key] pairs = [] for p, amt in sp.items(): T = sum(rec[p].values()) if T <= 0: if amt: print('unmatched', f, p, amt) continue for g, a in rec[p].items(): pairs.append((g, amt * a / T)) return pairs res = {} A = [] for f in NAMES: for lab, key, by in [('By month, equal split', 'i638', 'month'), ('By month, overhead prorated', 'i638_prorated', 'month'), ('By election year, equal split', 'i638', 'ey'), ('By election year, overhead prorated', 'i638_prorated', 'ey')]: s = shares(alloc(f, key, by)); s.update(committee=NAMES[f], side=SIDE[f], method=lab); A.append(s) res['A'] = A B = [] for f, lab, spans in [('HEYWL--535', 'Signature drive and fall campaign: Sept 15, 2025 to Jan 31, 2026, and July 1 to Nov 4, 2026', [('2025-09-15', '2026-01-31'), ('2026-07-01', '2026-11-04')]), ('HEYWL--535', 'Signature drive and fall campaign, to the last itemized I-638 spending: Sept 15, 2025 to Jan 31, 2026, and July 1 to Oct 26, 2026', [('2025-09-15', '2026-01-31'), ('2026-07-01', '2026-10-26')]), ('HEYWL--535', 'Signature drive only: Sept 15, 2025 to Jan 31, 2026', [('2025-09-15', '2026-01-31')]), ('HEYWL--535', 'Fall campaign only: July 1 to Nov 4, 2026', [('2026-07-01', '2026-11-04')]), ('FREEW--138', 'First I-638 spending to last receipt: Sept 2, 2025 to Oct 30, 2026', [('2025-09-02', '2026-10-30')])]: s = shares(win(f, spans)); s.update(committee=NAMES[f], side=SIDE[f], period=lab); B.append(s) res['B'] = B C = [] for f, lab, spans, eys in [('HEYWL--535', 'All receipts, 2025-26 election years', None, ('2025', '2026')), ('HEYWL--535', '2025 election year', None, ('2025',)), ('HEYWL--535', '2026 election year', None, ('2026',)), ('HEYWL--535', 'Receipts dated through July 31, 2026', [('2000-01-01', '2026-07-31')], ('2025', '2026')), ('HEYWL--535', 'Receipts dated through October 23, 2026', [('2000-01-01', '2026-10-23')], ('2025', '2026')), ('FREEW--138', 'All receipts, 2025-26 election years', None, ('2025', '2026'))]: rows = [r for r in R[f] if r['ey'] in eys and (spans is None or any(a <= r['date'] <= b for a, b in spans))] s = shares([(grp(r), r['amount']) for r in rows]); s.update(committee=NAMES[f], side=SIDE[f], period=lab); C.append(s) res['C'] = C json.dump(res, open(os.path.join(OUT, 'analyses.json'), 'w', encoding='utf-8'), indent=1) for k in res: print('==', k) for s in res[k]: print(' ', s['side'], s.get('method') or s.get('period'), s['total'], 'Hey', s['heywood_share'], 'oth', s['other_individuals_share'], 'named', s['named_other_individuals_share'], 'allind', s['all_individuals_share'], 'org', s['organizations_share'], 'I', s['inatai_share'], 'U', s['labor_unions_share'], 'I+U', s['inatai_and_unions_share'], 'margin', s['other_individuals_minus_half'])