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>
183 lines
4.3 KiB
Plaintext
183 lines
4.3 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Amenity Index Choropleth Map\n",
|
||
"\n",
|
||
"Displays total amenities per 1,000 residents 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_amenities` | neighbourhood × year | amenity_index, total_amenities_per_1000, amenity_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",
|
||
" total_amenities_per_1000,\n",
|
||
" amenity_index,\n",
|
||
" amenity_tier,\n",
|
||
" parks_per_1000,\n",
|
||
" schools_per_1000,\n",
|
||
" transit_per_1000,\n",
|
||
" total_amenities,\n",
|
||
" population\n",
|
||
"FROM public_marts.mart_neighbourhood_amenities\n",
|
||
"WHERE year = (SELECT MAX(year) FROM public_marts.mart_neighbourhood_amenities)\n",
|
||
"ORDER BY total_amenities_per_1000 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"
|
||
]
|
||
},
|
||
{
|
||
"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",
|
||
" [\"neighbourhood_name\", \"total_amenities_per_1000\", \"amenity_index\", \"amenity_tier\"]\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`."
|
||
]
|
||
},
|
||
{
|
||
"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=\"total_amenities_per_1000\",\n",
|
||
" hover_data=[\n",
|
||
" \"neighbourhood_name\",\n",
|
||
" \"amenity_index\",\n",
|
||
" \"parks_per_1000\",\n",
|
||
" \"schools_per_1000\",\n",
|
||
" ],\n",
|
||
" color_scale=\"Greens\",\n",
|
||
" title=\"Toronto Amenities per 1,000 Population\",\n",
|
||
" zoom=10,\n",
|
||
")\n",
|
||
"\n",
|
||
"fig.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Amenity Tier Interpretation\n",
|
||
"\n",
|
||
"| Tier | Meaning |\n",
|
||
"|------|--------|\n",
|
||
"| 1 | Best served (top 20%) |\n",
|
||
"| 2-4 | Middle tiers |\n",
|
||
"| 5 | Underserved (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
|
||
}
|