{
"cells": [
{
"cell_type": "markdown",
"id": "b72504e7",
"metadata": {},
"source": [
"# DBbun Davis — Medium Demo Notebook\n",
"This notebook loads the synthetic **Davis Square** dataset and produces attractive, saved-to-disk figures and tables.\n",
"\n",
"## How to use\n",
"1. Generate the dataset first (recommended **medium** size): \n",
" ```bash\n",
" python make_davis_square.py --size medium --seed 42 --out ./out_dbbun_davis_medium\n",
" ```\n",
"2. Update `DATA_DIR` below to point to your output folder.\n",
"3. Run all cells. All figures will be saved to `FIGS_DIR`, and summary tables to `TABLES_DIR`.\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2ba10144",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All expected files found.\n"
]
}
],
"source": [
"# --- User config ---\n",
"#DATA_DIR = \"./out_dbbun_davis_medium\" # <-- change if needed\n",
"DATA_DIR = \"./\"\n",
"FIGS_DIR = \"./figs\"\n",
"TABLES_DIR = \"./tables\"\n",
"\n",
"# --- Imports & setup ---\n",
"import os\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import json\n",
"\n",
"os.makedirs(FIGS_DIR, exist_ok=True)\n",
"os.makedirs(TABLES_DIR, exist_ok=True)\n",
"\n",
"def savefig(name):\n",
" path = os.path.join(FIGS_DIR, name)\n",
" plt.savefig(path, bbox_inches=\"tight\", dpi=144)\n",
" plt.close()\n",
" print(f\"Saved figure: {path}\")\n",
"\n",
"def savetab(df, name):\n",
" path = os.path.join(TABLES_DIR, name)\n",
" df.to_csv(path, index=False)\n",
" print(f\"Saved table: {path}\")\n",
"\n",
"# Verify required files exist\n",
"required_files = [\n",
" \"geo_streets.csv\",\"geo_parks.csv\",\"poi_generic.csv\",\"households.csv\",\n",
" \"pets_registry.csv\",\"pet_incidents.csv\",\"mobility_trips.csv\",\"public_safety.csv\",\n",
" \"events_civic.csv\",\"observations.csv\",\"transit_lines.csv\",\"transit_stops.csv\",\n",
" \"transit_ridership_daily.csv\",\"bike_infra.csv\",\"traffic_counts.csv\",\n",
" \"prices_index.csv\",\"weather_daily.csv\",\"trees_inventory.csv\",\n",
" \"infrastructure_events.csv\",\"building_issues.csv\",\"DATA_DICTIONARY.json\"\n",
"]\n",
"missing = [f for f in required_files if not os.path.exists(os.path.join(DATA_DIR, f))]\n",
"if missing:\n",
" print(\"WARNING: Missing files (update DATA_DIR or regenerate dataset):\")\n",
" for m in missing:\n",
" print(\" -\", m)\n",
"else:\n",
" print(\"All expected files found.\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "39136d91-c63f-4e18-b8c5-fd77f95305bc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['.ipynb_checkpoints', 'bike_infra.csv', 'building_issues.csv', 'DATA_DICTIONARY.json', 'DBbun_Davis_medium_demo.ipynb', 'events_civic.csv', 'figs', 'geo_parks.csv', 'geo_streets.csv', 'households.csv']\n"
]
}
],
"source": [
"import os\n",
"print(os.listdir(DATA_DIR)[:10])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8bcd1b10",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" table | \n",
" rows | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" streets | \n",
" 15 | \n",
"
\n",
" \n",
" | 1 | \n",
" parks | \n",
" 4 | \n",
"
\n",
" \n",
" | 2 | \n",
" poi | \n",
" 19 | \n",
"
\n",
" \n",
" | 3 | \n",
" households | \n",
" 8000 | \n",
"
\n",
" \n",
" | 4 | \n",
" pets | \n",
" 3213 | \n",
"
\n",
" \n",
" | 5 | \n",
" pet_inc | \n",
" 338 | \n",
"
\n",
" \n",
" | 6 | \n",
" trips | \n",
" 120000 | \n",
"
\n",
" \n",
" | 7 | \n",
" safety | \n",
" 12000 | \n",
"
\n",
" \n",
" | 8 | \n",
" events | \n",
" 195 | \n",
"
\n",
" \n",
" | 9 | \n",
" obs | \n",
" 35000 | \n",
"
\n",
" \n",
" | 10 | \n",
" lines | \n",
" 4 | \n",
"
\n",
" \n",
" | 11 | \n",
" stops | \n",
" 4 | \n",
"
\n",
" \n",
" | 12 | \n",
" ridership | \n",
" 92 | \n",
"
\n",
" \n",
" | 13 | \n",
" bike | \n",
" 9 | \n",
"
\n",
" \n",
" | 14 | \n",
" traffic | \n",
" 315 | \n",
"
\n",
" \n",
" | 15 | \n",
" prices | \n",
" 301 | \n",
"
\n",
" \n",
" | 16 | \n",
" weather | \n",
" 101136 | \n",
"
\n",
" \n",
" | 17 | \n",
" trees | \n",
" 79 | \n",
"
\n",
" \n",
" | 18 | \n",
" infra | \n",
" 253 | \n",
"
\n",
" \n",
" | 19 | \n",
" bissues | \n",
" 1614 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" table rows\n",
"0 streets 15\n",
"1 parks 4\n",
"2 poi 19\n",
"3 households 8000\n",
"4 pets 3213\n",
"5 pet_inc 338\n",
"6 trips 120000\n",
"7 safety 12000\n",
"8 events 195\n",
"9 obs 35000\n",
"10 lines 4\n",
"11 stops 4\n",
"12 ridership 92\n",
"13 bike 9\n",
"14 traffic 315\n",
"15 prices 301\n",
"16 weather 101136\n",
"17 trees 79\n",
"18 infra 253\n",
"19 bissues 1614"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\dataset_shapes.csv\n"
]
}
],
"source": [
"# Load data\n",
"def load_csv(name):\n",
" return pd.read_csv(os.path.join(DATA_DIR, name))\n",
"\n",
"streets = load_csv(\"geo_streets.csv\")\n",
"parks = load_csv(\"geo_parks.csv\")\n",
"poi = load_csv(\"poi_generic.csv\")\n",
"households = load_csv(\"households.csv\")\n",
"pets = load_csv(\"pets_registry.csv\")\n",
"pet_inc = load_csv(\"pet_incidents.csv\")\n",
"trips = load_csv(\"mobility_trips.csv\")\n",
"safety = load_csv(\"public_safety.csv\")\n",
"events = load_csv(\"events_civic.csv\")\n",
"obs = load_csv(\"observations.csv\")\n",
"lines = load_csv(\"transit_lines.csv\")\n",
"stops = load_csv(\"transit_stops.csv\")\n",
"ridership = load_csv(\"transit_ridership_daily.csv\")\n",
"bike = load_csv(\"bike_infra.csv\")\n",
"traffic = load_csv(\"traffic_counts.csv\")\n",
"prices = load_csv(\"prices_index.csv\")\n",
"weather = load_csv(\"weather_daily.csv\")\n",
"trees = load_csv(\"trees_inventory.csv\")\n",
"infra = load_csv(\"infrastructure_events.csv\")\n",
"bissues = load_csv(\"building_issues.csv\")\n",
"\n",
"with open(os.path.join(DATA_DIR, \"DATA_DICTIONARY.json\"), \"r\", encoding=\"utf-8\") as f:\n",
" dd = json.load(f)\n",
"\n",
"# Peek sizes\n",
"summary_shapes = pd.DataFrame({\n",
" \"table\": [\"streets\",\"parks\",\"poi\",\"households\",\"pets\",\"pet_inc\",\"trips\",\"safety\",\"events\",\"obs\",\n",
" \"lines\",\"stops\",\"ridership\",\"bike\",\"traffic\",\"prices\",\"weather\",\"trees\",\"infra\",\"bissues\"],\n",
" \"rows\": [len(streets),len(parks),len(poi),len(households),len(pets),len(pet_inc),\n",
" len(trips),len(safety),len(events),len(obs),len(lines),len(stops),\n",
" len(ridership),len(bike),len(traffic),len(prices),len(weather),len(trees),len(infra),len(bissues)]\n",
"})\n",
"display(summary_shapes)\n",
"savetab(summary_shapes, \"dataset_shapes.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "54398070",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved figure: ./figs\\01_geo_scatter.png\n"
]
}
],
"source": [
"import re\n",
"\n",
"def wkt_point_to_lonlat(wkt):\n",
" # expects 'POINT(lon lat)'\n",
" m = re.match(r\"POINT\\(([-0-9\\.]+)\\s+([-0-9\\.]+)\\)\", str(wkt))\n",
" if not m:\n",
" return np.nan, np.nan\n",
" lon, lat = float(m.group(1)), float(m.group(2))\n",
" return lon, lat\n",
"\n",
"streets[[\"lon\",\"lat\"]] = streets[\"geometry_wkt\"].apply(lambda s: pd.Series(wkt_point_to_lonlat(s)))\n",
"parks[[\"lon\",\"lat\"]] = parks[\"geometry_wkt\"].apply(lambda s: pd.Series(wkt_point_to_lonlat(s)))\n",
"\n",
"# Save quick sample map\n",
"plt.figure(figsize=(6,6))\n",
"plt.scatter(streets[\"lon\"], streets[\"lat\"], s=12, alpha=0.7)\n",
"plt.scatter(parks[\"lon\"], parks[\"lat\"], s=40, marker=\"^\", alpha=0.9)\n",
"plt.title(\"Synthetic Geo Points — Streets & Parks (WKT)\")\n",
"plt.xlabel(\"Longitude\"); plt.ylabel(\"Latitude\")\n",
"savefig(\"01_geo_scatter.png\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c3230d2c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" category | \n",
" subcategory | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 5 | \n",
" community_center | \n",
" civic | \n",
" 3 | \n",
"
\n",
" \n",
" | 0 | \n",
" bakery | \n",
" retail | \n",
" 2 | \n",
"
\n",
" \n",
" | 3 | \n",
" cafe | \n",
" coffee_tea | \n",
" 2 | \n",
"
\n",
" \n",
" | 6 | \n",
" hardware | \n",
" retail | \n",
" 2 | \n",
"
\n",
" \n",
" | 9 | \n",
" post_office | \n",
" civic | \n",
" 2 | \n",
"
\n",
" \n",
" | 1 | \n",
" bank | \n",
" retail | \n",
" 1 | \n",
"
\n",
" \n",
" | 2 | \n",
" bar | \n",
" neighborhood | \n",
" 1 | \n",
"
\n",
" \n",
" | 4 | \n",
" clinic | \n",
" primary | \n",
" 1 | \n",
"
\n",
" \n",
" | 7 | \n",
" pharmacy | \n",
" retail | \n",
" 1 | \n",
"
\n",
" \n",
" | 8 | \n",
" playground | \n",
" standard | \n",
" 1 | \n",
"
\n",
" \n",
" | 10 | \n",
" school | \n",
" public | \n",
" 1 | \n",
"
\n",
" \n",
" | 11 | \n",
" shelter | \n",
" animal | \n",
" 1 | \n",
"
\n",
" \n",
" | 12 | \n",
" urgent_care | \n",
" clinic | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" category subcategory count\n",
"5 community_center civic 3\n",
"0 bakery retail 2\n",
"3 cafe coffee_tea 2\n",
"6 hardware retail 2\n",
"9 post_office civic 2\n",
"1 bank retail 1\n",
"2 bar neighborhood 1\n",
"4 clinic primary 1\n",
"7 pharmacy retail 1\n",
"8 playground standard 1\n",
"10 school public 1\n",
"11 shelter animal 1\n",
"12 urgent_care clinic 1"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\poi_mix.csv\n",
"Saved figure: ./figs\\02_poi_category_bar.png\n"
]
}
],
"source": [
"poi_mix = poi.groupby([\"category\",\"subcategory\"])[\"poi_id\"].count().reset_index(name=\"count\").sort_values(\"count\", ascending=False)\n",
"display(poi_mix.head(20))\n",
"savetab(poi_mix, \"poi_mix.csv\")\n",
"\n",
"plt.figure(figsize=(8,4))\n",
"topcats = poi_mix.groupby(\"category\")[\"count\"].sum().sort_values(ascending=False).head(10)\n",
"topcats.plot(kind=\"bar\")\n",
"plt.title(\"POI by Category (Top 10)\")\n",
"plt.xlabel(\"Category\"); plt.ylabel(\"Count\")\n",
"savefig(\"02_poi_category_bar.png\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ab0b40ad",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" era | \n",
" dwelling_type | \n",
" tenure | \n",
" n | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 1800s | \n",
" apartment | \n",
" own | \n",
" 185 | \n",
"
\n",
" \n",
" | 1 | \n",
" 1800s | \n",
" apartment | \n",
" rent | \n",
" 149 | \n",
"
\n",
" \n",
" | 2 | \n",
" 1800s | \n",
" condo | \n",
" own | \n",
" 174 | \n",
"
\n",
" \n",
" | 3 | \n",
" 1800s | \n",
" condo | \n",
" rent | \n",
" 165 | \n",
"
\n",
" \n",
" | 4 | \n",
" 1800s | \n",
" single_family | \n",
" own | \n",
" 156 | \n",
"
\n",
" \n",
" | 5 | \n",
" 1800s | \n",
" single_family | \n",
" rent | \n",
" 160 | \n",
"
\n",
" \n",
" | 6 | \n",
" 1800s | \n",
" triple_decker | \n",
" own | \n",
" 175 | \n",
"
\n",
" \n",
" | 7 | \n",
" 1800s | \n",
" triple_decker | \n",
" rent | \n",
" 139 | \n",
"
\n",
" \n",
" | 8 | \n",
" 1900-1945 | \n",
" apartment | \n",
" own | \n",
" 172 | \n",
"
\n",
" \n",
" | 9 | \n",
" 1900-1945 | \n",
" apartment | \n",
" rent | \n",
" 171 | \n",
"
\n",
" \n",
" | 10 | \n",
" 1900-1945 | \n",
" condo | \n",
" own | \n",
" 176 | \n",
"
\n",
" \n",
" | 11 | \n",
" 1900-1945 | \n",
" condo | \n",
" rent | \n",
" 179 | \n",
"
\n",
" \n",
" | 12 | \n",
" 1900-1945 | \n",
" single_family | \n",
" own | \n",
" 145 | \n",
"
\n",
" \n",
" | 13 | \n",
" 1900-1945 | \n",
" single_family | \n",
" rent | \n",
" 163 | \n",
"
\n",
" \n",
" | 14 | \n",
" 1900-1945 | \n",
" triple_decker | \n",
" own | \n",
" 172 | \n",
"
\n",
" \n",
" | 15 | \n",
" 1900-1945 | \n",
" triple_decker | \n",
" rent | \n",
" 151 | \n",
"
\n",
" \n",
" | 16 | \n",
" 1946-1999 | \n",
" apartment | \n",
" own | \n",
" 163 | \n",
"
\n",
" \n",
" | 17 | \n",
" 1946-1999 | \n",
" apartment | \n",
" rent | \n",
" 146 | \n",
"
\n",
" \n",
" | 18 | \n",
" 1946-1999 | \n",
" condo | \n",
" own | \n",
" 167 | \n",
"
\n",
" \n",
" | 19 | \n",
" 1946-1999 | \n",
" condo | \n",
" rent | \n",
" 159 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" era dwelling_type tenure n\n",
"0 1800s apartment own 185\n",
"1 1800s apartment rent 149\n",
"2 1800s condo own 174\n",
"3 1800s condo rent 165\n",
"4 1800s single_family own 156\n",
"5 1800s single_family rent 160\n",
"6 1800s triple_decker own 175\n",
"7 1800s triple_decker rent 139\n",
"8 1900-1945 apartment own 172\n",
"9 1900-1945 apartment rent 171\n",
"10 1900-1945 condo own 176\n",
"11 1900-1945 condo rent 179\n",
"12 1900-1945 single_family own 145\n",
"13 1900-1945 single_family rent 163\n",
"14 1900-1945 triple_decker own 172\n",
"15 1900-1945 triple_decker rent 151\n",
"16 1946-1999 apartment own 163\n",
"17 1946-1999 apartment rent 146\n",
"18 1946-1999 condo own 167\n",
"19 1946-1999 condo rent 159"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\households_profile.csv\n",
"Saved figure: ./figs\\03_households_occupants_hist.png\n"
]
}
],
"source": [
"hh_profile = households.groupby([\"era\",\"dwelling_type\",\"tenure\"]).size().reset_index(name=\"n\")\n",
"display(hh_profile.head(20))\n",
"savetab(hh_profile, \"households_profile.csv\")\n",
"\n",
"plt.figure(figsize=(8,4))\n",
"households[\"occupants\"].plot(kind=\"hist\", bins=20)\n",
"plt.title(\"Household Occupants — Distribution\")\n",
"plt.xlabel(\"Occupants\"); plt.ylabel(\"Frequency\")\n",
"savefig(\"03_households_occupants_hist.png\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ecabf3f1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" species | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" dog | \n",
" 2067 | \n",
"
\n",
" \n",
" | 1 | \n",
" cat | \n",
" 960 | \n",
"
\n",
" \n",
" | 2 | \n",
" other | \n",
" 186 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" species count\n",
"0 dog 2067\n",
"1 cat 960\n",
"2 other 186"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\pets_species_counts.csv\n",
"Saved figure: ./figs\\04_pets_species_bar.png\n"
]
}
],
"source": [
"pet_species = pets[\"species\"].value_counts().reset_index()\n",
"pet_species.columns = [\"species\",\"count\"]\n",
"display(pet_species)\n",
"savetab(pet_species, \"pets_species_counts.csv\")\n",
"\n",
"plt.figure(figsize=(6,4))\n",
"pet_species.set_index(\"species\")[\"count\"].plot(kind=\"bar\")\n",
"plt.title(\"Pets by Species\")\n",
"plt.xlabel(\"Species\"); plt.ylabel(\"Count\")\n",
"savefig(\"04_pets_species_bar.png\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "1478d234",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" mode | \n",
" share | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" walk | \n",
" 0.329950 | \n",
"
\n",
" \n",
" | 1 | \n",
" car | \n",
" 0.205342 | \n",
"
\n",
" \n",
" | 2 | \n",
" subway | \n",
" 0.115433 | \n",
"
\n",
" \n",
" | 3 | \n",
" bus | \n",
" 0.096825 | \n",
"
\n",
" \n",
" | 4 | \n",
" autonomous | \n",
" 0.084275 | \n",
"
\n",
" \n",
" | 5 | \n",
" bike | \n",
" 0.068183 | \n",
"
\n",
" \n",
" | 6 | \n",
" streetcar | \n",
" 0.046658 | \n",
"
\n",
" \n",
" | 7 | \n",
" micromobility | \n",
" 0.029967 | \n",
"
\n",
" \n",
" | 8 | \n",
" horse | \n",
" 0.023367 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" mode share\n",
"0 walk 0.329950\n",
"1 car 0.205342\n",
"2 subway 0.115433\n",
"3 bus 0.096825\n",
"4 autonomous 0.084275\n",
"5 bike 0.068183\n",
"6 streetcar 0.046658\n",
"7 micromobility 0.029967\n",
"8 horse 0.023367"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\mobility_mode_share_overall.csv\n",
"Saved figure: ./figs\\05_trips_modes_overall_bar.png\n",
"Saved table: ./tables\\mobility_mode_counts_by_era.csv\n",
"Saved table: ./tables\\mobility_mode_share_by_era.csv\n",
"Saved figure: ./figs\\06_trips_by_mode_over_eras_line.png\n"
]
}
],
"source": [
"mode_share = trips[\"mode\"].value_counts(normalize=True).reset_index()\n",
"mode_share.columns = [\"mode\",\"share\"]\n",
"display(mode_share)\n",
"savetab(mode_share, \"mobility_mode_share_overall.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"trips[\"mode\"].value_counts().plot(kind=\"bar\")\n",
"plt.title(\"Trip Modes — Overall Counts\")\n",
"plt.xlabel(\"Mode\"); plt.ylabel(\"Trips\")\n",
"savefig(\"05_trips_modes_overall_bar.png\")\n",
"\n",
"mode_era = trips.groupby([\"era\",\"mode\"]).size().unstack(fill_value=0)\n",
"mode_era_pct = mode_era.div(mode_era.sum(axis=1), axis=0)\n",
"savetab(mode_era.reset_index(), \"mobility_mode_counts_by_era.csv\")\n",
"savetab(mode_era_pct.reset_index(), \"mobility_mode_share_by_era.csv\")\n",
"\n",
"plt.figure(figsize=(8,5))\n",
"for m in mode_era.columns:\n",
" plt.plot(mode_era.index, mode_era[m], marker=\"o\")\n",
"plt.title(\"Trips by Mode over Eras\")\n",
"plt.xlabel(\"Era\"); plt.ylabel(\"Count\")\n",
"plt.xticks(rotation=45, ha=\"right\")\n",
"savefig(\"06_trips_by_mode_over_eras_line.png\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "50bd51c1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" category | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" traffic | \n",
" 1740 | \n",
"
\n",
" \n",
" | 1 | \n",
" theft | \n",
" 1739 | \n",
"
\n",
" \n",
" | 2 | \n",
" noise | \n",
" 1735 | \n",
"
\n",
" \n",
" | 3 | \n",
" vandalism | \n",
" 1720 | \n",
"
\n",
" \n",
" | 4 | \n",
" disturbance | \n",
" 1702 | \n",
"
\n",
" \n",
" | 5 | \n",
" animal | \n",
" 1702 | \n",
"
\n",
" \n",
" | 6 | \n",
" other | \n",
" 1662 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" category count\n",
"0 traffic 1740\n",
"1 theft 1739\n",
"2 noise 1735\n",
"3 vandalism 1720\n",
"4 disturbance 1702\n",
"5 animal 1702\n",
"6 other 1662"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\safety_incidents_by_category.csv\n",
"Saved figure: ./figs\\07_safety_category_bar.png\n"
]
}
],
"source": [
"safety_cat = safety[\"category\"].value_counts().reset_index()\n",
"safety_cat.columns = [\"category\",\"count\"]\n",
"display(safety_cat)\n",
"savetab(safety_cat, \"safety_incidents_by_category.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"safety_cat.set_index(\"category\")[\"count\"].plot(kind=\"bar\")\n",
"plt.title(\"Public Safety Incidents by Category\")\n",
"plt.xlabel(\"Category\"); plt.ylabel(\"Count\")\n",
"savefig(\"07_safety_category_bar.png\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "c8e99a14",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved figure: ./figs\\08_noise_hist.png\n",
"Saved figure: ./figs\\09_aqi_hist.png\n"
]
}
],
"source": [
"noise = obs[obs[\"metric\"]==\"noise_db\"].copy()\n",
"aqi = obs[obs[\"metric\"]==\"aqi\"].copy()\n",
"\n",
"# Noise distribution\n",
"plt.figure(figsize=(7,4))\n",
"noise[\"value\"].plot(kind=\"hist\", bins=30)\n",
"plt.title(\"Noise (dB) — Distribution\")\n",
"plt.xlabel(\"dB\"); plt.ylabel(\"Frequency\")\n",
"savefig(\"08_noise_hist.png\")\n",
"\n",
"# AQI distribution\n",
"plt.figure(figsize=(7,4))\n",
"aqi[\"value\"].plot(kind=\"hist\", bins=30)\n",
"plt.title(\"AQI — Distribution\")\n",
"plt.xlabel(\"AQI\"); plt.ylabel(\"Frequency\")\n",
"savefig(\"09_aqi_hist.png\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "fc79bee7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" year | \n",
" attendees_est | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 1946 | \n",
" 2999 | \n",
"
\n",
" \n",
" | 1 | \n",
" 1947 | \n",
" 2999 | \n",
"
\n",
" \n",
" | 2 | \n",
" 1948 | \n",
" 3000 | \n",
"
\n",
" \n",
" | 3 | \n",
" 1949 | \n",
" 2999 | \n",
"
\n",
" \n",
" | 4 | \n",
" 1950 | \n",
" 2999 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" year attendees_est\n",
"0 1946 2999\n",
"1 1947 2999\n",
"2 1948 3000\n",
"3 1949 2999\n",
"4 1950 2999"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\events_attendance_by_year.csv\n",
"Saved figure: ./figs\\10_events_attendance_by_year.png\n"
]
}
],
"source": [
"events['year'] = pd.to_datetime(events['date']).dt.year\n",
"att_year = events.groupby('year')[\"attendees_est\"].sum().reset_index()\n",
"display(att_year.head())\n",
"savetab(att_year, \"events_attendance_by_year.csv\")\n",
"\n",
"plt.figure(figsize=(8,4))\n",
"plt.plot(att_year[\"year\"], att_year[\"attendees_est\"])\n",
"plt.title(\"Street‑Band Festival — Total Attendance by Year\")\n",
"plt.xlabel(\"Year\"); plt.ylabel(\"Estimated attendees\")\n",
"savefig(\"10_events_attendance_by_year.png\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "f75741ef",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\transit_ridership_by_year_pivot.csv\n",
"Saved figure: ./figs\\11_transit_ridership_lines.png\n"
]
}
],
"source": [
"ridership['year'] = pd.to_datetime(ridership['date']).dt.year\n",
"rid_pvt = ridership.pivot_table(index=\"year\", columns=\"line_id\", values=\"boardings_est\", aggfunc=\"sum\")\n",
"savetab(rid_pvt.reset_index(), \"transit_ridership_by_year_pivot.csv\")\n",
"\n",
"plt.figure(figsize=(9,5))\n",
"for col in rid_pvt.columns:\n",
" plt.plot(rid_pvt.index, rid_pvt[col])\n",
"plt.title(\"Transit Ridership — Boardings by Line over Time\")\n",
"plt.xlabel(\"Year\"); plt.ylabel(\"Boardings (est.)\")\n",
"savefig(\"11_transit_ridership_lines.png\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c857cfce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved figure: ./figs\\12_traffic_scatter.png\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" facility_type | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" painted_lane | \n",
" 6 | \n",
"
\n",
" \n",
" | 1 | \n",
" protected_lane | \n",
" 2 | \n",
"
\n",
" \n",
" | 2 | \n",
" shared | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" facility_type count\n",
"0 painted_lane 6\n",
"1 protected_lane 2\n",
"2 shared 1"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\bike_facility_counts.csv\n",
"Saved figure: ./figs\\13_bike_facility_bar.png\n"
]
}
],
"source": [
"plt.figure(figsize=(6,5))\n",
"plt.scatter(traffic[\"vehicles_per_day\"], traffic[\"bikes_per_day\"], alpha=0.5)\n",
"plt.title(\"Traffic: Vehicles vs Bikes per Day\")\n",
"plt.xlabel(\"Vehicles/day\"); plt.ylabel(\"Bikes/day\")\n",
"savefig(\"12_traffic_scatter.png\")\n",
"\n",
"bike_counts = bike[\"facility_type\"].value_counts().reset_index()\n",
"bike_counts.columns = [\"facility_type\",\"count\"]\n",
"display(bike_counts)\n",
"savetab(bike_counts, \"bike_facility_counts.csv\")\n",
"\n",
"plt.figure(figsize=(6,4))\n",
"bike_counts.set_index(\"facility_type\")[\"count\"].plot(kind=\"bar\")\n",
"plt.title(\"Bike Facility Types\")\n",
"plt.xlabel(\"Facility type\"); plt.ylabel(\"Count\")\n",
"savefig(\"13_bike_facility_bar.png\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "9a238c42",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\weather_temp_monthly_mean.csv\n",
"Saved figure: ./figs\\14_weather_temp_monthly.png\n",
"Saved table: ./tables\\weather_rain_monthly_sum.csv\n",
"Saved figure: ./figs\\15_weather_rain_monthly.png\n",
"Saved table: ./tables\\weather_snow_monthly_sum.csv\n",
"Saved figure: ./figs\\16_weather_snow_monthly.png\n"
]
}
],
"source": [
"weather['month'] = pd.to_datetime(weather['date']).dt.month\n",
"temp_month = weather.groupby('month')[\"temp_c\"].mean().reset_index()\n",
"savetab(temp_month, \"weather_temp_monthly_mean.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"plt.plot(temp_month[\"month\"], temp_month[\"temp_c\"], marker=\"o\")\n",
"plt.title(\"Avg Temperature by Month\")\n",
"plt.xlabel(\"Month\"); plt.ylabel(\"Temp (°C)\")\n",
"savefig(\"14_weather_temp_monthly.png\")\n",
"\n",
"rain_month = weather.groupby('month')[\"precip_mm\"].sum().reset_index()\n",
"savetab(rain_month, \"weather_rain_monthly_sum.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"plt.bar(rain_month[\"month\"], rain_month[\"precip_mm\"])\n",
"plt.title(\"Total Rainfall by Month\")\n",
"plt.xlabel(\"Month\"); plt.ylabel(\"Precipitation (mm)\")\n",
"savefig(\"15_weather_rain_monthly.png\")\n",
"\n",
"snow_month = weather.groupby('month')[\"snow_cm\"].sum().reset_index()\n",
"savetab(snow_month, \"weather_snow_monthly_sum.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"plt.bar(snow_month[\"month\"], snow_month[\"snow_cm\"])\n",
"plt.title(\"Total Snowfall by Month\")\n",
"plt.xlabel(\"Month\"); plt.ylabel(\"Snow (cm)\")\n",
"savefig(\"16_weather_snow_monthly.png\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "d7b08fa7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" type | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 3 | \n",
" water_main_break | \n",
" 89 | \n",
"
\n",
" \n",
" | 0 | \n",
" power_outage | \n",
" 86 | \n",
"
\n",
" \n",
" | 1 | \n",
" road_potholes | \n",
" 54 | \n",
"
\n",
" \n",
" | 2 | \n",
" tree_fall | \n",
" 24 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" type count\n",
"3 water_main_break 89\n",
"0 power_outage 86\n",
"1 road_potholes 54\n",
"2 tree_fall 24"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\infra_event_counts.csv\n",
"Saved figure: ./figs\\17_infra_events_bar.png\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" type | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" hvac_failure | \n",
" 676 | \n",
"
\n",
" \n",
" | 2 | \n",
" roof_leak | \n",
" 662 | \n",
"
\n",
" \n",
" | 1 | \n",
" plumbing_issue | \n",
" 276 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" type count\n",
"0 hvac_failure 676\n",
"2 roof_leak 662\n",
"1 plumbing_issue 276"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved table: ./tables\\building_issue_counts.csv\n",
"Saved figure: ./figs\\18_building_issues_bar.png\n"
]
}
],
"source": [
"infra['year'] = pd.to_datetime(infra['date']).dt.year\n",
"infra_counts = infra.groupby(['type'])[\"infra_event_id\"].count().reset_index(name=\"count\").sort_values(\"count\", ascending=False)\n",
"display(infra_counts)\n",
"savetab(infra_counts, \"infra_event_counts.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"infra_counts.set_index(\"type\")[\"count\"].plot(kind=\"bar\")\n",
"plt.title(\"Infrastructure Events by Type\")\n",
"plt.xlabel(\"Type\"); plt.ylabel(\"Count\")\n",
"savefig(\"17_infra_events_bar.png\")\n",
"\n",
"bissues['year'] = pd.to_datetime(bissues['date']).dt.year\n",
"bissues_counts = bissues.groupby(['type'])[\"building_issue_id\"].count().reset_index(name=\"count\").sort_values(\"count\", ascending=False)\n",
"display(bissues_counts)\n",
"savetab(bissues_counts, \"building_issue_counts.csv\")\n",
"\n",
"plt.figure(figsize=(7,4))\n",
"bissues_counts.set_index(\"type\")[\"count\"].plot(kind=\"bar\")\n",
"plt.title(\"Building Issues by Type\")\n",
"plt.xlabel(\"Type\"); plt.ylabel(\"Count\")\n",
"savefig(\"18_building_issues_bar.png\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "8e57aa50",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved figure: ./figs\\19_prices_housing_line.png\n",
"Saved figure: ./figs\\20_prices_food_line.png\n",
"Saved figure: ./figs\\21_prices_transit_line.png\n"
]
}
],
"source": [
"plt.figure(figsize=(8,4))\n",
"plt.plot(prices[\"year\"], prices[\"housing_idx\"])\n",
"plt.title(\"Housing Price Index (Synthetic)\")\n",
"plt.xlabel(\"Year\"); plt.ylabel(\"Index\")\n",
"savefig(\"19_prices_housing_line.png\")\n",
"\n",
"plt.figure(figsize=(8,4))\n",
"plt.plot(prices[\"year\"], prices[\"staple_food_idx\"])\n",
"plt.title(\"Staple Food Index (Synthetic)\")\n",
"plt.xlabel(\"Year\"); plt.ylabel(\"Index\")\n",
"savefig(\"20_prices_food_line.png\")\n",
"\n",
"plt.figure(figsize=(8,4))\n",
"plt.plot(prices[\"year\"], prices[\"transit_fare_idx\"])\n",
"plt.title(\"Transit Fare Index (Synthetic)\")\n",
"plt.xlabel(\"Year\"); plt.ylabel(\"Index\")\n",
"savefig(\"21_prices_transit_line.png\")"
]
},
{
"cell_type": "markdown",
"id": "765e0518",
"metadata": {},
"source": [
"### Outputs\n",
"- Figures saved under `FIGS_DIR`\n",
"- Summary tables saved under `TABLES_DIR`\n",
"\n",
"You can zip these folders or publish selected images wherever you like.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}