refactor: multi-dashboard structural migration
Some checks failed
CI / lint-and-test (pull_request) Has been cancelled
Some checks failed
CI / lint-and-test (pull_request) Has been cancelled
- Rename dbt project from toronto_housing to portfolio - Restructure dbt models into domain subdirectories: - shared/ for cross-domain dimensions (dim_time) - staging/toronto/, intermediate/toronto/, marts/toronto/ - Update SQLAlchemy models for raw_toronto schema - Add explicit cross-schema FK relationships for FactRentals - Namespace figure factories under figures/toronto/ - Namespace notebooks under notebooks/toronto/ - Update Makefile with domain-specific targets and env loading - Update all documentation for multi-dashboard structure This enables adding new dashboard projects (e.g., /football, /energy) without structural conflicts or naming collisions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
0
notebooks/toronto/safety/.gitkeep
Normal file
0
notebooks/toronto/safety/.gitkeep
Normal file
200
notebooks/toronto/safety/crime_breakdown_bar.ipynb
Normal file
200
notebooks/toronto/safety/crime_breakdown_bar.ipynb
Normal file
@@ -0,0 +1,200 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Crime Type Breakdown Bar Chart\n",
|
||||
"\n",
|
||||
"Stacked bar chart showing crime composition by Major Crime Indicator (MCI) categories."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Data Reference\n",
|
||||
"\n",
|
||||
"### Source Tables\n",
|
||||
"\n",
|
||||
"| Table | Grain | Key Columns |\n",
|
||||
"|-------|-------|-------------|\n",
|
||||
"| `mart_neighbourhood_safety` | neighbourhood × year | assault_count, auto_theft_count, break_enter_count, robbery_count, etc. |\n",
|
||||
"\n",
|
||||
"### SQL Query"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from sqlalchemy import create_engine\n",
|
||||
"\n",
|
||||
"# Load .env from project root\n",
|
||||
"load_dotenv(\"../../.env\")\n",
|
||||
"\n",
|
||||
"engine = create_engine(os.environ[\"DATABASE_URL\"])\n",
|
||||
"\n",
|
||||
"query = \"\"\"\n",
|
||||
"SELECT\n",
|
||||
" neighbourhood_name,\n",
|
||||
" assault_count,\n",
|
||||
" auto_theft_count,\n",
|
||||
" break_enter_count,\n",
|
||||
" robbery_count,\n",
|
||||
" theft_over_count,\n",
|
||||
" homicide_count,\n",
|
||||
" total_incidents,\n",
|
||||
" crime_rate_per_100k\n",
|
||||
"FROM public_marts.mart_neighbourhood_safety\n",
|
||||
"WHERE year = (SELECT MAX(year) FROM public_marts.mart_neighbourhood_safety)\n",
|
||||
"ORDER BY total_incidents DESC\n",
|
||||
"LIMIT 15\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"df = pd.read_sql(query, engine)\n",
|
||||
"print(f\"Loaded top {len(df)} neighbourhoods by crime volume\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Transformation Steps\n",
|
||||
"\n",
|
||||
"1. Select top 15 neighbourhoods by total incidents\n",
|
||||
"2. Melt crime type columns into rows\n",
|
||||
"3. Pass to stacked bar figure factory"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_melted = df.melt(\n",
|
||||
" id_vars=[\"neighbourhood_name\", \"total_incidents\"],\n",
|
||||
" value_vars=[\n",
|
||||
" \"assault_count\",\n",
|
||||
" \"auto_theft_count\",\n",
|
||||
" \"break_enter_count\",\n",
|
||||
" \"robbery_count\",\n",
|
||||
" \"theft_over_count\",\n",
|
||||
" \"homicide_count\",\n",
|
||||
" ],\n",
|
||||
" var_name=\"crime_type\",\n",
|
||||
" value_name=\"count\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Clean labels\n",
|
||||
"df_melted[\"crime_type\"] = (\n",
|
||||
" df_melted[\"crime_type\"].str.replace(\"_count\", \"\").str.replace(\"_\", \" \").str.title()\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"data = df_melted.to_dict(\"records\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sample Output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[\n",
|
||||
" [\n",
|
||||
" \"neighbourhood_name\",\n",
|
||||
" \"assault_count\",\n",
|
||||
" \"auto_theft_count\",\n",
|
||||
" \"break_enter_count\",\n",
|
||||
" \"total_incidents\",\n",
|
||||
" ]\n",
|
||||
"].head(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Data Visualization\n",
|
||||
"\n",
|
||||
"### Figure Factory\n",
|
||||
"\n",
|
||||
"Uses `create_stacked_bar` from `portfolio_app.figures.toronto.bar_charts`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"sys.path.insert(0, \"../..\")\n",
|
||||
"\n",
|
||||
"from portfolio_app.figures.toronto.bar_charts import create_stacked_bar\n",
|
||||
"\n",
|
||||
"fig = create_stacked_bar(\n",
|
||||
" data=data,\n",
|
||||
" x_column=\"neighbourhood_name\",\n",
|
||||
" value_column=\"count\",\n",
|
||||
" category_column=\"crime_type\",\n",
|
||||
" title=\"Crime Type Breakdown - Top 15 Neighbourhoods\",\n",
|
||||
" color_map={\n",
|
||||
" \"Assault\": \"#d62728\",\n",
|
||||
" \"Auto Theft\": \"#ff7f0e\",\n",
|
||||
" \"Break Enter\": \"#9467bd\",\n",
|
||||
" \"Robbery\": \"#8c564b\",\n",
|
||||
" \"Theft Over\": \"#e377c2\",\n",
|
||||
" \"Homicide\": \"#1f77b4\",\n",
|
||||
" },\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### MCI Categories\n",
|
||||
"\n",
|
||||
"| Category | Description |\n",
|
||||
"|----------|------------|\n",
|
||||
"| Assault | Physical attacks |\n",
|
||||
"| Auto Theft | Vehicle theft |\n",
|
||||
"| Break & Enter | Burglary |\n",
|
||||
"| Robbery | Theft with force/threat |\n",
|
||||
"| Theft Over | Theft > $5,000 |\n",
|
||||
"| Homicide | Murder/manslaughter |"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
185
notebooks/toronto/safety/crime_rate_choropleth.ipynb
Normal file
185
notebooks/toronto/safety/crime_rate_choropleth.ipynb
Normal file
@@ -0,0 +1,185 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Crime Rate Choropleth Map\n",
|
||||
"\n",
|
||||
"Displays crime rates per 100,000 population across Toronto's 158 neighbourhoods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Data Reference\n",
|
||||
"\n",
|
||||
"### Source Tables\n",
|
||||
"\n",
|
||||
"| Table | Grain | Key Columns |\n",
|
||||
"|-------|-------|-------------|\n",
|
||||
"| `mart_neighbourhood_safety` | neighbourhood × year | crime_rate_per_100k, crime_index, safety_tier, geometry |\n",
|
||||
"\n",
|
||||
"### SQL Query"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from sqlalchemy import create_engine\n",
|
||||
"\n",
|
||||
"# Load .env from project root\n",
|
||||
"load_dotenv(\"../../.env\")\n",
|
||||
"\n",
|
||||
"engine = create_engine(os.environ[\"DATABASE_URL\"])\n",
|
||||
"\n",
|
||||
"query = \"\"\"\n",
|
||||
"SELECT\n",
|
||||
" neighbourhood_id,\n",
|
||||
" neighbourhood_name,\n",
|
||||
" geometry,\n",
|
||||
" year,\n",
|
||||
" crime_rate_per_100k,\n",
|
||||
" crime_index,\n",
|
||||
" safety_tier,\n",
|
||||
" total_incidents,\n",
|
||||
" population\n",
|
||||
"FROM public_marts.mart_neighbourhood_safety\n",
|
||||
"WHERE year = (SELECT MAX(year) FROM public_marts.mart_neighbourhood_safety)\n",
|
||||
"ORDER BY crime_rate_per_100k DESC\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"df = pd.read_sql(query, engine)\n",
|
||||
"print(f\"Loaded {len(df)} neighbourhoods\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Transformation Steps\n",
|
||||
"\n",
|
||||
"1. Filter to most recent year\n",
|
||||
"2. Convert geometry to GeoJSON\n",
|
||||
"3. Use reversed color scale (green=low crime, red=high crime)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"import geopandas as gpd\n",
|
||||
"\n",
|
||||
"gdf = gpd.GeoDataFrame(\n",
|
||||
" df, geometry=gpd.GeoSeries.from_wkb(df[\"geometry\"]), crs=\"EPSG:4326\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"geojson = json.loads(gdf.to_json())\n",
|
||||
"data = df.drop(columns=[\"geometry\"]).to_dict(\"records\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sample Output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[\n",
|
||||
" [\n",
|
||||
" \"neighbourhood_name\",\n",
|
||||
" \"crime_rate_per_100k\",\n",
|
||||
" \"crime_index\",\n",
|
||||
" \"safety_tier\",\n",
|
||||
" \"total_incidents\",\n",
|
||||
" ]\n",
|
||||
"].head(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Data Visualization\n",
|
||||
"\n",
|
||||
"### Figure Factory\n",
|
||||
"\n",
|
||||
"Uses `create_choropleth_figure` from `portfolio_app.figures.toronto.choropleth`.\n",
|
||||
"\n",
|
||||
"**Key Parameters:**\n",
|
||||
"- `color_column`: 'crime_rate_per_100k'\n",
|
||||
"- `color_scale`: 'RdYlGn_r' (red=high crime, green=low crime)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"sys.path.insert(0, \"../..\")\n",
|
||||
"\n",
|
||||
"from portfolio_app.figures.toronto.choropleth import create_choropleth_figure\n",
|
||||
"\n",
|
||||
"fig = create_choropleth_figure(\n",
|
||||
" geojson=geojson,\n",
|
||||
" data=data,\n",
|
||||
" location_key=\"neighbourhood_id\",\n",
|
||||
" color_column=\"crime_rate_per_100k\",\n",
|
||||
" hover_data=[\"neighbourhood_name\", \"crime_index\", \"total_incidents\"],\n",
|
||||
" color_scale=\"RdYlGn_r\",\n",
|
||||
" title=\"Toronto Crime Rate per 100,000 Population\",\n",
|
||||
" zoom=10,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Safety Tier Interpretation\n",
|
||||
"\n",
|
||||
"| Tier | Meaning |\n",
|
||||
"|------|--------|\n",
|
||||
"| 1 | Highest crime (top 20%) |\n",
|
||||
"| 2-4 | Middle tiers |\n",
|
||||
"| 5 | Lowest crime (bottom 20%) |"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
198
notebooks/toronto/safety/crime_trend_line.ipynb
Normal file
198
notebooks/toronto/safety/crime_trend_line.ipynb
Normal file
@@ -0,0 +1,198 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Crime Trend Line Chart\n",
|
||||
"\n",
|
||||
"Shows 5-year crime rate trends across Toronto."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Data Reference\n",
|
||||
"\n",
|
||||
"### Source Tables\n",
|
||||
"\n",
|
||||
"| Table | Grain | Key Columns |\n",
|
||||
"|-------|-------|-------------|\n",
|
||||
"| `mart_neighbourhood_safety` | neighbourhood × year | year, crime_rate_per_100k, crime_yoy_change_pct |\n",
|
||||
"\n",
|
||||
"### SQL Query"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from sqlalchemy import create_engine\n",
|
||||
"\n",
|
||||
"# Load .env from project root\n",
|
||||
"load_dotenv(\"../../.env\")\n",
|
||||
"\n",
|
||||
"engine = create_engine(os.environ[\"DATABASE_URL\"])\n",
|
||||
"\n",
|
||||
"query = \"\"\"\n",
|
||||
"SELECT\n",
|
||||
" year,\n",
|
||||
" AVG(crime_rate_per_100k) as avg_crime_rate,\n",
|
||||
" AVG(assault_rate_per_100k) as avg_assault_rate,\n",
|
||||
" AVG(auto_theft_rate_per_100k) as avg_auto_theft_rate,\n",
|
||||
" AVG(break_enter_rate_per_100k) as avg_break_enter_rate,\n",
|
||||
" SUM(total_incidents) as total_city_incidents,\n",
|
||||
" AVG(crime_yoy_change_pct) as avg_yoy_change\n",
|
||||
"FROM public_marts.mart_neighbourhood_safety\n",
|
||||
"WHERE year >= (SELECT MAX(year) - 5 FROM public_marts.mart_neighbourhood_safety)\n",
|
||||
"GROUP BY year\n",
|
||||
"ORDER BY year\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"df = pd.read_sql(query, engine)\n",
|
||||
"print(f\"Loaded {len(df)} years of crime data\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Transformation Steps\n",
|
||||
"\n",
|
||||
"1. Aggregate by year (city-wide)\n",
|
||||
"2. Convert year to datetime\n",
|
||||
"3. Melt for multi-line by crime type"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[\"date\"] = pd.to_datetime(df[\"year\"].astype(str) + \"-01-01\")\n",
|
||||
"\n",
|
||||
"# Melt for multi-line\n",
|
||||
"df_melted = df.melt(\n",
|
||||
" id_vars=[\"year\", \"date\"],\n",
|
||||
" value_vars=[\"avg_assault_rate\", \"avg_auto_theft_rate\", \"avg_break_enter_rate\"],\n",
|
||||
" var_name=\"crime_type\",\n",
|
||||
" value_name=\"rate_per_100k\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"df_melted[\"crime_type\"] = df_melted[\"crime_type\"].map(\n",
|
||||
" {\n",
|
||||
" \"avg_assault_rate\": \"Assault\",\n",
|
||||
" \"avg_auto_theft_rate\": \"Auto Theft\",\n",
|
||||
" \"avg_break_enter_rate\": \"Break & Enter\",\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sample Output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[[\"year\", \"avg_crime_rate\", \"total_city_incidents\", \"avg_yoy_change\"]]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Data Visualization\n",
|
||||
"\n",
|
||||
"### Figure Factory\n",
|
||||
"\n",
|
||||
"Uses `create_price_time_series` (reused for any numeric trend)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"sys.path.insert(0, \"../..\")\n",
|
||||
"\n",
|
||||
"from portfolio_app.figures.toronto.time_series import create_price_time_series\n",
|
||||
"\n",
|
||||
"data = df_melted.to_dict(\"records\")\n",
|
||||
"\n",
|
||||
"fig = create_price_time_series(\n",
|
||||
" data=data,\n",
|
||||
" date_column=\"date\",\n",
|
||||
" price_column=\"rate_per_100k\",\n",
|
||||
" group_column=\"crime_type\",\n",
|
||||
" title=\"Toronto Crime Trends by Type (5 Years)\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Remove dollar sign formatting since this is rate data\n",
|
||||
"fig.update_layout(yaxis_tickprefix=\"\", yaxis_title=\"Rate per 100K\")\n",
|
||||
"\n",
|
||||
"fig.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Overall Trend"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Total crime rate trend\n",
|
||||
"total_data = (\n",
|
||||
" df[[\"date\", \"avg_crime_rate\"]]\n",
|
||||
" .rename(columns={\"avg_crime_rate\": \"total_rate\"})\n",
|
||||
" .to_dict(\"records\")\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig2 = create_price_time_series(\n",
|
||||
" data=total_data,\n",
|
||||
" date_column=\"date\",\n",
|
||||
" price_column=\"total_rate\",\n",
|
||||
" title=\"Toronto Overall Crime Rate Trend\",\n",
|
||||
")\n",
|
||||
"fig2.update_layout(yaxis_tickprefix=\"\", yaxis_title=\"Rate per 100K\")\n",
|
||||
"fig2.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
Reference in New Issue
Block a user