# Measures "the money backing / opposing I-638" several ways from PDC open data (data.wa.gov). # Usage: python -I scripts/scope.py (reads ../raw from fetch.sh; writes ../out/results.json and prints tables) import json, os, re, sys from collections import defaultdict sys.path.insert(0, os.path.dirname(__file__)) from common import load, measures, i638_share OUT = os.path.join(os.path.dirname(__file__), '..', 'out'); os.makedirs(OUT, exist_ok=True) SMALL = re.compile(r'^(SMALL CONTRIBUTIONS|MISCELLANEOUS RECEIPTS)$', re.I) # Union names exactly as in L3's ORG map (reproduces the draft's 59.77%) L3_UNIONS = {'SEIU 775 Ballot Fund', 'Washington Education Association', 'National Education Association', 'SEIU Initiative Fund', 'UFCW Local 3000', 'WFSE', 'SEIU 925 Ballot Fund', 'Teamsters Local Union 117', 'SEIU Washington State Council', 'Spokane Regional Labor Council, AFL-CIO'} # Broader name rule (variant): any organization whose name marks it as a union or labor council UNION_RE = re.compile(r'\b(SEIU|UFCW|Teamsters|AFL-CIO|AFSCME|WFSE|OPEIU|UAW|IBEW|PTE Local|Uniserv|Labor Council|Education Association|' r'Federation of Teachers|Local \d+|Union)\b', re.I) def report_dates(f): return {r['report_number']: r for r in load('hist_' + f)} def receipts(f): hist = report_dates(f) if os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'raw', 'hist_' + f + '.json')) else {} rows = [] for r in load('con_' + f): d = (r.get('receipt_date') or '')[:10] if not d or d < '2025': # undated small-gift lines and 9 rows dated 2024 in election year 2026: use the report's end date h = hist.get(r['report_number']); d = h['report_through'][:10] if h else d name = r['contributor_name']; a = float(r['amount']) # Groups follow the PDC contributor category (as the draft's 50.58% does): 'MISCELLANEOUS RECEIPTS' lines filed as Organization stay organizations if r.get('contributor_category') == 'Individual': g = 'unitemized' if SMALL.match(name) else ('heywood' if re.match(r'^HEYWOOD BRIAN$', name, re.I) else 'individual') else: g = 'org' rows.append(dict(date=d, ey=r['election_year'], amount=a, group=g, name=name, inatai=name == 'Inatai Foundation', union_l3=name in L3_UNIONS, union_re=g == 'org' and bool(UNION_RE.search(name)))) return rows def shares(rows, weight=None): """weight: function(row) -> multiplier (pro-rata). Returns totals and shares.""" t = defaultdict(float) for r in rows: w = weight(r) if weight else 1.0 a = r['amount'] * w t['total'] += a; t[r['group']] += a if r['inatai']: t['inatai'] += a if r['union_l3']: t['union_l3'] += a if r['union_re']: t['union_re'] += a T = t['total'] o = {k: round(v, 2) for k, v in t.items()} o['other_ind_incl_unitemized'] = round(t['individual'] + t['unitemized'], 2) sh = lambda v: round(100 * v / T, 2) if T else None o['pct'] = dict(heywood=sh(t['heywood']), other_individuals=sh(t['individual'] + t['unitemized']), named_other_individuals=sh(t['individual']), unitemized=sh(t['unitemized']), organizations=sh(t['org']), all_individuals=sh(t['heywood'] + t['individual'] + t['unitemized']), inatai=sh(t['inatai']), unions_l3=sh(t['union_l3']), unions_re=sh(t['union_re']), inatai_unions_l3=sh(t['inatai'] + t['union_l3']), inatai_unions_re=sh(t['inatai'] + t['union_re'])) o['other_ind_margin_over_half'] = round(t['individual'] + t['unitemized'] - T / 2, 2) return o def monthly_spend(f): m = defaultdict(lambda: dict(total=0.0, i638=0.0, unalloc=0.0)) for x in load('exp_' + f): d = (x.get('expenditure_date') or '')[:7]; a = float(x['amount']); s, alloc = i638_share(x) m[d]['total'] += a; m[d]['i638'] += s if not alloc: m[d]['unalloc'] += a for v in m.values(): allocated = v['total'] - v['unalloc'] v['i638_prorated'] = v['i638'] + (v['unalloc'] * v['i638'] / allocated if allocated else 0) return m def prorata(rows, spend, key, by): """Allocate each period's I-638 spending to donor groups by that period's receipt mix. by: 'month' or 'ey'.""" rec = defaultdict(float) per = (lambda r: r['date'][:7]) if by == 'month' else (lambda r: r['ey']) for r in rows: rec[per(r)] += r['amount'] if by == 'ey': sp = defaultdict(float) for mth, v in spend.items(): sp[mth[:4]] += v[key] else: sp = {k: v[key] for k, v in spend.items()} unmatched = sum(v for k, v in sp.items() if rec.get(k, 0) <= 0) w = lambda r: (sp.get(per(r), 0) / rec[per(r)]) if rec[per(r)] > 0 else 0 o = shares(rows, w); o['i638_spending_allocated'] = round(sum(sp.values()) - unmatched, 2); o['unmatched_spending'] = round(unmatched, 2) return o def window(rows, *spans): return [r for r in rows if any(a <= r['date'] <= b for a, b in spans)] res = {} for f, spans in [('HEYWL--535', dict( b1_first_to_last_receipt=[('2025-09-15', '2026-11-04')], b1x_first_to_last_i638_item=[('2025-09-15', '2026-10-26')], b2_drive_plus_fall=[('2025-09-15', '2026-01-31'), ('2026-07-01', '2026-11-04')], b2x_drive_plus_fall_to_oct26=[('2025-09-15', '2026-01-31'), ('2026-07-01', '2026-10-26')], b3_signature_drive=[('2025-09-15', '2026-01-31')], b4_fall_campaign=[('2026-07-01', '2026-11-04')], cal2025=[('2025-01-01', '2025-12-31')], cal2026=[('2026-01-01', '2026-12-31')])), ('FREEW--138', dict( b1_first_to_last_receipt=[('2025-09-02', '2026-10-30')], b1x_first_to_last_i638_item=[('2025-09-02', '2026-10-26')], b3_before_2026=[('2025-09-02', '2025-12-31')], b4_2026=[('2026-01-01', '2026-10-30')], cal2025=[('2025-01-01', '2025-12-31')], cal2026=[('2026-01-01', '2026-12-31')]))]: rows = receipts(f); sp = monthly_spend(f) r = res[f] = {} r['a_all_2025_26'] = shares(rows) r['ey2025'] = shares([x for x in rows if x['ey'] == '2025']); r['ey2026'] = shares([x for x in rows if x['ey'] == '2026']) for k, s in spans.items(): r[k] = shares(window(rows, *s)); r[k]['spans'] = s r['c_month_equal_split'] = prorata(rows, sp, 'i638', 'month') r['c_month_prorated_overhead'] = prorata(rows, sp, 'i638_prorated', 'month') r['c_year_equal_split'] = prorata(rows, sp, 'i638', 'ey') r['c_year_prorated_overhead'] = prorata(rows, sp, 'i638_prorated', 'ey') r['spend_by_month'] = {k: {kk: round(vv, 2) for kk, vv in v.items()} for k, v in sorted(sp.items())} r['latest_receipt'] = max(x['date'] for x in rows) # (d) every committee registered for / against I-638 in the 2026 summary, receipts 2025-26 combined summ = load('summ_638_2026') for side in ('for', 'against'): allrows = [] for c in summ: if c['for_or_against'] != side: continue n = c['filer_id'].replace(' ', '_') try: rr = receipts(n) if n in ('HEYWL--535', 'FREEW--138') else None except FileNotFoundError: rr = None if rr is None: rr = [] for x in load('con_' + n): name = x['contributor_name']; g = (('unitemized' if SMALL.match(name) else ('heywood' if re.match(r'^HEYWOOD BRIAN$', name, re.I) else 'individual')) if x.get('contributor_category') == 'Individual' else 'org') rr.append(dict(date=(x.get('receipt_date') or '')[:10], ey=x['election_year'], amount=float(x['amount']), group=g, name=name, inatai=name == 'Inatai Foundation', union_l3=name in L3_UNIONS, union_re=g == 'org' and bool(UNION_RE.search(name)))) allrows += rr res['d_all_committees_' + side] = shares(allrows) res['d_committees_' + side] = [(c['filer_id'], c['filer_name'], c['ballot_number']) for c in summ if c['for_or_against'] == side] json.dump(res, open(os.path.join(OUT, 'results.json'), 'w'), indent=1) for f in ('HEYWL--535', 'FREEW--138'): print('==', f, 'latest receipt', res[f]['latest_receipt']) for k, v in res[f].items(): if isinstance(v, dict) and 'pct' in v: p = v['pct'] print(f"{k:32s} total {v['total']:>13,.2f} Hey {p['heywood']:6} oth {p['other_individuals']:6} (named {p['named_other_individuals']}, unit {p['unitemized']}) org {p['organizations']:6} | Inatai {p['inatai']} un {p['unions_l3']}/{p['unions_re']} I+U {p['inatai_unions_l3']}/{p['inatai_unions_re']} margin {v['other_ind_margin_over_half']:,.0f}" + (f" alloc {v.get('i638_spending_allocated')}" if 'i638_spending_allocated' in v else '')) for side in ('for', 'against'): v = res['d_all_committees_' + side]; print('d', side, v['total'], v['pct'])