ML model to predict crash risk levels for road segments in Toronto using open traffic collision data. The model outputs risk labels (Low/Medium/High) and confidence scores per road segment, suitable for visualization as heatmaps or geo-layers.
The project consists of three main components:
- ML Pipeline: Python-based data processing, feature engineering, and model training
- Backend API: Flask REST API server that serves predictions to the iOS app
- iOS App: SwiftUI mobile application for visualizing risk predictions on an interactive map
risk-map-model/
βββ data/ # Raw datasets (not in git)
β βββ Traffic_Collisions_Open_Data_*.xlsx
β βββ TOTAL_KSI_*.csv
β βββ Centreline - Version 2 - 4326.geojson
βββ src/ # Source code
β βββ data_processing/ # Data cleaning and spatial joins
β β βββ data_loader.py
β β βββ spatial_join_fast.py
β βββ feature_engineering/ # Feature creation and risk labeling
β β βββ feature_creator.py
β β βββ label_generator.py
β βββ models/ # Model training and evaluation
β β βββ model_trainer.py
β β βββ model_evaluator.py
β βββ visualization/ # Interactive maps and dashboards
β βββ risk_mapper.py
βββ outputs/ # Generated outputs (not in git)
β βββ maps/ # Interactive risk maps (HTML)
β βββ models/ # Trained model artifacts
β β βββ toronto_risk_model.joblib
β βββ reports/ # Analysis reports and data exports
β βββ toronto_road_risk.geojson
βββ ios-app/ # iOS mobile application
β βββ RiskMapApp/ # SwiftUI iOS application
β β βββ RiskMapApp.swift # App entry point
β β βββ ContentView.swift # Main tab view
β β βββ Models/
β β β βββ RiskModels.swift # Data models
β β βββ Services/
β β β βββ RiskService.swift # API service
β β β βββ RouteService.swift # Route planning service
β β βββ Views/
β β βββ MapView.swift # Map with risk annotations
β β βββ RiskDetailView.swift
β β βββ RiskListView.swift
β β βββ SettingsView.swift
β βββ README.md
β βββ QUICK_START.md
βββ backend-api/ # Flask API server
β βββ app.py # API endpoints
β βββ requirements.txt # Python dependencies
β βββ README.md
β βββ QUICK_SETUP.md # Google Maps API setup
β βββ INTEGRATION.md # Integration details
βββ run_risk_analysis.py # Main pipeline script
βββ config.py # Configuration settings
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ TECHNICAL_OVERVIEW.md # Detailed technical documentation
- Python 3.8+
- iOS 17.0+ (for iOS app)
- Xcode 15.0+ (for iOS app)
First, place your datasets in the data/ folder, then run:
python run_risk_analysis.pycd backend-api
python app.py-
Open in Xcode:
cd ios-app/RiskMapApp open RiskMapApp.xcodeproj -
Configure API endpoint in
RiskMapApp/Services/RiskService.swift:private let baseURL = "http://localhost:8000/api"
-
Set up signing:
- Select the RiskMapApp target
- Go to Signing & Capabilities
- Select your development team
-
Build and run (β + R)
- Technical Overview: See
TECHNICAL_OVERVIEW.mdfor detailed technical documentation - Backend API: See
backend-api/README.mdandbackend-api/INTEGRATION.md - iOS App: See
ios-app/README.mdandios-app/QUICK_START.md - Google Maps Setup: See
backend-api/QUICK_SETUP.md