{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fannie Mae mortgage data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import cuxfilter" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from cuxfilter import charts\n", "import cuxfilter\n", "from bokeh import palettes\n", "from cuxfilter.layouts import *\n", "import panel as pn" ] }, { "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('mortgage', base_dir=DATA_DIR)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add static legend to the dashboard widget section\n", "def generate_legend_markdown():\n", " range_ = cux_df.data.delinquency_12_prediction.describe().round(2)\n", " legend = pn.GridBox(ncols=2)\n", " colors = palettes.Purples4\n", "\n", " legend.append(f'{range_[\"min\"]} - {range_[\"25%\"]}')\n", " legend.append(pn.pane.HTML(background=colors[0], width=30, height=10))\n", " legend.append(f'{range_[\"25%\"]} - {range_[\"50%\"]}')\n", " legend.append(pn.pane.HTML(background=colors[1], width=30, height=10))\n", " legend.append(f'{range_[\"50%\"]} - {range_[\"75%\"]}')\n", " legend.append(pn.pane.HTML(background=colors[2], width=30, height=10))\n", " legend.append(f'{range_[\"75%\"]} <')\n", " legend.append(pn.pane.HTML(background=colors[3], width=30, height=10))\n", " return legend" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define charts" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cux_df = cuxfilter.DataFrame.from_arrow(DATA_DIR+'/146M_predictions_v2.arrow')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# set map_api_key parameter on choropleth to get bg map tiles\n", "chart0 = charts.choropleth(x='zip', y='delinquency_12_prediction', color_column='delinquency_12_prediction', color_aggregate_fn='mean', geo_color_palette=palettes.Purples4,\n", " geoJSONSource = 'https://raw.githubusercontent.com/rapidsai/cuxfilter/GTC-2018-mortgage-visualization/javascript/demos/GTC%20demo/src/data/zip3-ms-rhs-lessprops.json',\n", " data_points=1000) #, mapbox_api_key=\"\")\n", "\n", "chart2 = charts.bar('delinquency_12_prediction',data_points=50)\n", "chart3 = charts.range_slider('borrower_credit_score',data_points=50)\n", "chart4 = charts.card(generate_legend_markdown(), widget=True, title=\"zip legend\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add mappings for chart1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mapper1 = {}\n", "\n", "for val in cux_df.data.dti.unique().to_pandas().tolist():\n", " mapper1[int(val)] = 'l_'+str(val)\n", "\n", "chart1 = charts.panel_widgets.drop_down('dti', label_map=mapper1)\n", "#also available: panel_widgets.multi_select" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a dashboard object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = cux_df.dashboard([chart0], sidebar=[chart1, chart4], layout=feature_and_base, theme=cuxfilter.themes.rapids_dark, title=\"Ajay's Dashboard\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#add more charts\n", "d.add_charts(charts=[chart2], sidebar=[chart3])" ] }, { "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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![mortgage](../../../_images/mortgage.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 }