{ "cells": [ { "cell_type": "markdown", "id": "f80b9ce2", "metadata": {}, "source": [ "# Mapping All Collisions with Pedestrians and Cyclists in NYC\n", "\n", "___NYC residents and visitors may be surprised to see how many vehicle collisions injured or killed a pedestrian or cyclist where they live, work, and travel___\n", "\n", "### Definitions\n", "- __Collision:__ A motor vehicle collision involving injuries, deaths, or a significant amount of property damage (~> $1000) reported on a New York State form, MV104-AN\n", "\n", "### Data Sources\n", "\n", "- Collision data obtained from https://data.cityofnewyork.us/Public-Safety/Motor-Vehicle-Collisions-Crashes/h9gi-nx95\n", "- Data was processed by running `process_raw.data.py` which saves processed data to local directory specified in `process_raw.data.py` script\n", "- _Note that \\~10% of collisions are missing lat-long coordinates. These collisions are excluded from the following geographic analyses._" ] }, { "cell_type": "code", "execution_count": 1, "id": "5eb23132", "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "import os.path\n", "import pandas as pd\n", "from src import visualizations as viz" ] }, { "cell_type": "markdown", "id": "d07634ca", "metadata": {}, "source": [ "# Parameters " ] }, { "cell_type": "code", "execution_count": 2, "id": "27e5928d", "metadata": {}, "outputs": [], "source": [ "PROCESSED_CRASH_DATA = \"data/processed/crashes.pkl\"\n", "IMG_DIR = \"output/maps\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "a23c9b01", "metadata": {}, "outputs": [], "source": [ "crashes = pd.read_pickle(PROCESSED_CRASH_DATA)" ] }, { "cell_type": "code", "execution_count": 4, "id": "db9d3add-ac3a-4990-b211-09d3b6338d47", "metadata": {}, "outputs": [], "source": [ "pedestrian = crashes[crashes[\"valid_lat_long\"] & crashes[\"pedestrian\"]]\n", "cyclist = crashes[crashes[\"valid_lat_long\"] & crashes[\"cyclist\"]]" ] }, { "cell_type": "code", "execution_count": 5, "id": "bd0de749-af7d-4a91-a3f3-083d752cd9b9", "metadata": {}, "outputs": [], "source": [ "pedestrian_cols = [\n", " \"LAT\",\n", " \"LONG\",\n", " \"DATE\",\n", " \"TIME\",\n", " \"PEDESTRIAN INJURED\",\n", " \"PEDESTRIAN KILLED\",\n", "]\n", "pedestrian_format = (\n", " \"'DATE: ' + row[2] + '
' +\"\n", " \"'TIME: ' + row[3] + '
' +\"\n", " \"'PEDESTRIANS INJURED: ' + row[4] + '
' +\"\n", " \"'PEDESTRIANS KILLED: ' + row[5]};\"\n", ")\n", "cyclist_cols = [\n", " \"LAT\",\n", " \"LONG\",\n", " \"DATE\",\n", " \"TIME\",\n", " \"CYCLIST INJURED\",\n", " \"CYCLIST KILLED\",\n", "]\n", "cyclist_format = (\n", " \"'DATE: ' + row[2] + '
' +\"\n", " \"'TIME: ' + row[3] + '
' +\"\n", " \"'CYCLISTS INJURED: ' + row[4] + '
' +\"\n", " \"'CYCLISTS KILLED: ' + row[5]};\"\n", ")" ] }, { "cell_type": "markdown", "id": "fdcf9b45", "metadata": {}, "source": [ "# Map of all Collisions with Pedestrians 2018 - 2023 (inclusive)\n", "\n", "_Zoom in to see serious collisions and the date, time, number injured, and number killed_" ] }, { "cell_type": "code", "execution_count": 6, "id": "c5e319f2-4942-4c79-a93b-7ccc1f07045f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start = datetime(year=2018, month=1, day=1)\n", "end = datetime(year=2024, month=1, day=1)\n", "map_data = pedestrian[pedestrian[\"datetime\"].between(start, end, inclusive=\"left\")][\n", " pedestrian_cols\n", "]\n", "\n", "pedestrian_map = viz.make_marker_map(map_data, pedestrian_format)\n", "pedestrian_map.save(os.path.join(IMG_DIR, \"pedestrian_map_18_23.html\"))\n", "pedestrian_map" ] }, { "cell_type": "markdown", "id": "2e34653b-e2d5-426a-b0f3-31ed15494b20", "metadata": {}, "source": [ "# Map of all Collisions with Cyclist 2018 - 2023 (inclusive)\n", "\n", "_Zoom in to see serious collisions and the date, time, number injured, and number killed_" ] }, { "cell_type": "code", "execution_count": 7, "id": "6565ee6d-827d-437a-b7a0-bff0dfcb624a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start = datetime(year=2018, month=1, day=1)\n", "end = datetime(year=2024, month=1, day=1)\n", "map_data = cyclist[cyclist[\"datetime\"].between(start, end, inclusive=\"left\")][\n", " cyclist_cols\n", "]\n", "\n", "cyclist_map = viz.make_marker_map(map_data, cyclist_format)\n", "cyclist_map.save(os.path.join(IMG_DIR, \"cyclist_map_18_23.html\"))\n", "cyclist_map" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 5 }