{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Auto Accidents(1975-2017)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import cuxfilter" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import cuxfilter\n", "from cuxfilter import charts\n", "from cuxfilter.layouts import feature_and_base" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#update data_dir if you have downloaded datasets elsewhere\n", "DATA_DIR = './data'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download required datasets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from cuxfilter.sampledata import datasets_check\n", "datasets_check('auto_accidents', base_dir=DATA_DIR)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cux_df = cuxfilter.DataFrame.from_arrow('./data/auto_accidents.arrow')\n", "cux_df.data['ST_CASE'] = cux_df.data['ST_CASE'].astype('float64')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define Charts" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add mappings for day of week\n", "label_map = {\n", " 1: 'Sunday',\n", " 2: 'Monday',\n", " 3: 'Tuesday',\n", " 4: 'Wednesday',\n", " 5: 'Thursday',\n", " 6: 'Friday',\n", " 7: 'Saturday',\n", " 9: 'Unknown'\n", "}\n", "cux_df.data['DAY_WEEK_STR'] = cux_df.data.DAY_WEEK.map(label_map)\n", "gtc_demo_red_blue_palette = [ \"#3182bd\", \"#6baed6\", \"#7b8ed8\", \"#e26798\", \"#ff0068\" , \"#323232\" ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uncomment the below lines and replace MAPBOX_TOKEN with mapbox token string if you want to use mapbox map-tiles. Can be created for free here -https://www.mapbox.com/help/define-access-token/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "chart1 = cuxfilter.charts.scatter(x='dropoff_x', y='dropoff_y', aggregate_col='DAY_WEEK', aggregate_fn='mean',\n", " tile_provider=\"CartoLight\",\n", " color_palette=gtc_demo_red_blue_palette,pixel_shade_type='linear', unselected_alpha=0.2)\n", "\n", "# Since cuxfilter 23.08, bar charts work with str columns\n", "chart2 = cuxfilter.charts.bar('DAY_WEEK_STR')\n", "\n", "chart3 = cuxfilter.charts.multi_select('YEAR')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a dashboard object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = cux_df.dashboard([chart1, chart2], sidebar=[chart3], layout=cuxfilter.layouts.feature_and_base,theme = cuxfilter.themes.default, title='Auto Accident Dataset')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#run dashboard inline within the notebook cell\n", "d.app()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Starting the dashboard" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. d.show('current_notebook_url:current_notebook_port') remote dashboard\n", "\n", "2. d.app() inline within the notebook cell\n", "\n", "Incase you need to stop the server:\n", "\n", "- d.stop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![auto-accidents](../../../_images/auto-accidents.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export the queried data into a dataframe" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "queried_df = d.export()" ] } ], "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }