"""Reproduce the 2024 motorcycle brief from six transcribed ADOT aggregate rows. Python 3.10+, standard library for calculations; ReportLab for SVG figures. Run: python reproduce.py No network requests, private records or statistical risk models. """ from pathlib import Path from decimal import Decimal, ROUND_HALF_UP import csv import hashlib import json import base64 ROOT = Path(__file__).resolve().parent CSV = ROOT / 'adot-motorcycle-geography-2024.csv' rows = list(csv.DictReader(CSV.open(encoding='utf-8', newline=''))) assert len(rows) == 6 assert len({r['measure'] for r in rows}) == 6 data = {r['measure']: {k: int(r[k]) for k in ('statewide','urban','rural')} for r in rows} assert {r['data_year'] for r in rows} == {'2024'} assert all(v['urban'] + v['rural'] == v['statewide'] for v in data.values()) for g in ('statewide', 'urban', 'rural'): assert sum(data[m][g] for m in ('fatal_crashes','injury_crashes','property_damage_only_crashes')) == data['total_crashes'][g] assert data['total_crashes']['statewide'] == 3036 assert data['fatal_crashes']['statewide'] == 218 def pct(numerator, denominator): return Decimal(numerator) / Decimal(denominator) * 100 def one(n): return str(n.quantize(Decimal('.1'), rounding=ROUND_HALF_UP)) results = { 'population': 'Motorcycle crashes in ADOT 2024 Crash Facts Table 8-4', 'data_year': 2024, 'report_published': '2025-07-09', 'accessed': '2026-11-14', 'csv_sha256': hashlib.sha256(CSV.read_bytes()).hexdigest(), 'checks': {'all_six_geographic_row_sums_match': True, 'severity_sums_match_all_three_geographies': True}, 'derived': [] } for measure, label in [('total_crashes','Share of all recorded motorcycle crashes'),('fatal_crashes','Share of fatal motorcycle crashes')]: for g in ('urban','rural'): n, d = data[measure][g], data[measure]['statewide'] results['derived'].append({'measure':measure+'_geographic_share','geography':g,'numerator':n,'denominator':d,'percent':str(pct(n,d)),'display_percent':one(pct(n,d)),'label':label}) for g in ('urban','rural','statewide'): n, d = data['fatal_crashes'][g], data['total_crashes'][g] results['derived'].append({'measure':'fatal_share_of_recorded_crashes','geography':g,'numerator':n,'denominator':d,'percent':str(pct(n,d)),'display_percent':one(pct(n,d)),'label':'Fatal crashes / all motorcycle crashes in this geography'}) results['rural_minus_urban_fatal_share_pp'] = str(pct(55,593)-pct(163,2443)) results['rural_share_fatal_minus_all_pp'] = str(pct(55,218)-pct(593,3036)) results['excluded_data'] = 'None of the six Table 8-4 rows excluded. No raw incident records used.' results['geography_missingness'] = 'No unknown geography row is published; urban+rural reconciles to statewide. This does not independently validate geocoding.' results['not_measured'] = ['miles or trips ridden','individual risk','causation','legal fault','claim value','current dashboard totals'] (ROOT / 'calculation-results.json').write_text(json.dumps(results, indent=2)+'\n', encoding='utf-8') # Publication figures use zero-based scales, direct labels, and exact count denominators. from reportlab.graphics.shapes import Drawing, Rect, String, Line from reportlab.graphics import renderSVG from reportlab.lib.colors import HexColor NAVY = '#203B4A' CYAN = '#48A8C6' PALE = '#E7F3F7' GRAY = '#657B88' WHITE = '#FFFFFF' ORANGE = '#EF793E' def text(d,x,y,value,size=20,color=NAVY,font='Helvetica'): d.add(String(x,y,value,fontName=font,fontSize=size,fillColor=HexColor(color))) def rect(d,x,y,w,h,color): d.add(Rect(x,y,w,h,fillColor=HexColor(color),strokeColor=None)) def save(d,name,title,description): path=ROOT / (name+'.svg') renderSVG.drawToFile(d,str(path)) svg=path.read_text(encoding='utf-8') svg=svg.replace('...','').replace('...','') svg=svg.replace('font-family: Helvetica-Bold;', 'font-family: Lato, Arial, sans-serif; font-weight:700;') svg=svg.replace('font-family: Helvetica;', 'font-family: Lato, Arial, sans-serif;') # Embed approved Lato fonts when running in this resource repository; # portable runs without them retain the documented Arial fallback. fontroot=ROOT.parents[1] / 'polished-v5' / 'library' / 'assets' fonts='' for weight in (400,700): f=fontroot / f'lato-{weight}.woff2' if f.exists(): encoded=base64.b64encode(f.read_bytes()).decode('ascii') fonts+=f"@font-face{{font-family:Lato;font-weight:{weight};src:url(data:font/woff2;base64,{encoded}) format('woff2')}}" start=svg.index('',start) svg=svg[:end]+ ' role="img" aria-labelledby="chart-title chart-desc"'+svg[end:] end=svg.index('>',svg.index('{title}{description}'+svg[end+1:] path.write_text(svg,encoding='utf-8') d=Drawing(1080,450) rect(d,0,0,1080,450,WHITE) text(d,12,414,'WHERE THE RECORDED CRASHES HAPPENED',20,NAVY,'Helvetica-Bold') text(d,12,380,'Arizona motorcycle crashes | 2024 published report',18,GRAY) for y,measure,label in [(252,'total_crashes','All crashes'),(118,'fatal_crashes','Fatal crashes')]: total=data[measure]['statewide']; u=data[measure]['urban']; r=data[measure]['rural'] text(d,12,y+62,f'{label}: {total:,}',22,NAVY,'Helvetica-Bold') bx,bw=12,1040 uw=bw*u/total rect(d,bx,y,uw,44,CYAN);rect(d,bx+uw,y,bw-uw,44,NAVY) text(d,bx+14,y+13,f'Urban {one(pct(u,total))}%',21,NAVY,'Helvetica-Bold') text(d,bx+uw+12,y+13,f'Rural {one(pct(r,total))}%',21,WHITE,'Helvetica-Bold') text(d,bx,y-27,f'{u:,} urban + {r:,} rural = {total:,} crashes',18,GRAY) text(d,12,24,'Source: ADOT 2024 Crash Facts, Table 8-4. Percentages calculated by Impact Legal.',16,GRAY) save(d,'motorcycle-geographic-shares-2024','Geographic shares of Arizona motorcycle crashes in the 2024 report','All crashes: urban 2,443 of 3,036 (80.5%); rural 593 of 3,036 (19.5%). Fatal crashes: urban 163 of 218 (74.8%); rural 55 of 218 (25.2%). Each bar represents 100% of its own crash group. These are crash counts, not deaths or riding exposure.') d=Drawing(1080,470) rect(d,0,0,1080,470,WHITE) text(d,12,431,'FATAL SHARE WITHIN EACH SETTING',20,NAVY,'Helvetica-Bold') text(d,12,395,'Fatal crashes divided by all recorded motorcycle crashes in that setting',18,GRAY) left,plotw=195,690 for val in (0,2,4,6,8,10): x=left+plotw*val/10 d.add(Line(x,125,x,328,strokeColor=HexColor(PALE),strokeWidth=2)) text(d,x-10,90,f'{val}%',18,GRAY) for y,g,color in [(263,'urban',CYAN),(150,'rural',NAVY)]: n=data['fatal_crashes'][g];den=data['total_crashes'][g];v=pct(n,den) text(d,12,y+30,g.title(),24,NAVY,'Helvetica-Bold') text(d,12,y+3,f'{n:,} / {den:,}',18,GRAY) w=plotw*float(v)/10 rect(d,left,y,w,54,color) text(d,left+w+12,y+17,one(v)+'%',25,NAVY,'Helvetica-Bold') text(d,12,44,'Not a per-mile or per-trip risk estimate.',20,NAVY,'Helvetica-Bold') text(d,12,15,'Source: ADOT 2024 Crash Facts, Table 8-4. Calculations by Impact Legal.',16,GRAY) save(d,'motorcycle-fatal-share-2024','Fatal share of recorded Arizona motorcycle crashes by setting, 2024 report','Urban: 163 fatal crashes divided by 2,443 motorcycle crashes equals 6.7%. Rural: 55 divided by 593 equals 9.3%. Scale starts at zero and ends at ten percent. A fatal crash is an event, not a count of people killed. These shares cannot estimate a rider\'s risk per mile or trip.') print(json.dumps({'checks':'PASS','csv_rows':len(rows),'fatal_share_urban':one(pct(163,2443)),'fatal_share_rural':one(pct(55,593)),'charts':2},indent=2))