{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "research-question",
      "metadata": {},
      "source": "# King County crash research\n\nReproduce the published comparison for Seattle, Bellevue and Tukwila:\n**City Street severe crashes, 2021–2023**. Keep the question, code and\noutput together, with the source table available for inspection.\n\nThis reproducibility example was assembled for the homepage from the\nexisting published study tables. It is not a capture of a hosted\nTrendSights research run or a new acquisition of crash records.\n"
    },
    {
      "cell_type": "code",
      "id": "load-published-table",
      "metadata": {},
      "source": "import csv\nimport hashlib\nimport io\nfrom pathlib import Path\nfrom urllib.request import urlopen\n\nsource_url = \"https://www.trendsights.io/research/king-county-2026-10-20/data/city-road-year.csv\"\nexpected_sha256 = \"746195e73810e199a09cb47568044ef4e6cc21b952760addd3c5c9880adb4f89\"\nrelative_csv = Path(\"public/research/king-county-2026-10-20/data/city-road-year.csv\")\ncandidates = [Path(\"city-road-year.csv\")]\ncandidates += [folder / relative_csv for folder in [Path.cwd(), *Path.cwd().parents]]\nlocal_csv = next((path for path in candidates if path.is_file()), None)\n\nif local_csv is not None:\n    source_bytes = local_csv.read_bytes()\nelse:\n    with urlopen(source_url, timeout=20) as response:\n        source_bytes = response.read()\n\nassert hashlib.sha256(source_bytes).hexdigest() == expected_sha256, \"Study edition changed\"\nrows = list(csv.DictReader(io.StringIO(source_bytes.decode(\"utf-8\"))))\nprint(f\"Verified published table: {len(rows)} city / year / road-category rows\")\n",
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": "Verified published table: 35 city / year / road-category rows\n"
        }
      ]
    },
    {
      "cell_type": "code",
      "id": "compare-city-street-crashes",
      "metadata": {},
      "source": "cities = (\"Seattle\", \"Bellevue\", \"Tukwila\")\nselected = [\n    row for row in rows\n    if row[\"city\"] in cities and row[\"road_category\"] == \"City Street\"\n    and 2021 <= int(row[\"year\"]) <= 2023\n]\nannual = {(row[\"city\"], int(row[\"year\"])): int(row[\"severe_events\"]) for row in selected}\nassert len(selected) == len(annual) == 9, \"Expected one row per city and year\"\ntotals = {city: sum(annual[city, year] for year in range(2021, 2024)) for city in cities}\nassert totals == {\"Seattle\": 644, \"Bellevue\": 67, \"Tukwila\": 40}\nassert sum(totals.values()) == 751\n\nsummary = [\n    {\"city\": city, \"start\": annual[city, 2021], \"end\": annual[city, 2023],\n     \"change\": annual[city, 2023] - annual[city, 2021], \"total\": totals[city]}\n    for city in cities\n]\nprint(\"City       2021  2023  Change  2021–2023 total\")\nfor row in summary:\n    print(f'{row[\"city\"]:<10} {row[\"start\"]:>4}  {row[\"end\"]:>4}  {row[\"change\"]:>+6}  {row[\"total\"]:>15}')\nprint(\"Check: all 751 mapped severe City Street crashes retained.\")\n",
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": "City       2021  2023  Change  2021–2023 total\nSeattle     179   259     +80              644\nBellevue     20    21      +1               67\nTukwila      12    14      +2               40\nCheck: all 751 mapped severe City Street crashes retained.\n"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "id": "interpretation-and-source",
      "metadata": {},
      "source": "## Read the result carefully\n\nSeattle's severe City Street crash count rose from **179 in 2021 to 259\nin 2023**, a change of **+80**. The three-city map contains **751** severe\nCity Street crashes across 2021–2023: Seattle 644, Bellevue 67 and Tukwila 40.\n\nSevere crashes have a fatal or suspected serious maximum injury. These are reported crash counts, not injured people or travel-adjusted risk. The comparison is descriptive; it does not establish the\ncause of the change. Resident and workplace data are separate context.\n\n[Published source table](https://www.trendsights.io/research/king-county-2026-10-20/data/city-road-year.csv) ·\n[King County study](https://www.trendsights.io/case-studies/king-county-traffic-crashes)\n"
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.12.1"
    },
    "trendsights": {
      "provenance": "Reproducibility example assembled from published study tables",
      "source_url": "https://www.trendsights.io/research/king-county-2026-10-20/data/city-road-year.csv"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
