Last active
January 9, 2021 17:07
-
-
Save Caellwyn/add34fef7ddf61b4472ad78461729d72 to your computer and use it in GitHub Desktop.
dash-example-src.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "dash-example-src.ipynb", | |
"private_outputs": true, | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyOMO/+1NsOStjfVLuIulkC6", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Caellwyn/add34fef7ddf61b4472ad78461729d72/dash-example-src.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Z5j43XRVwvRi" | |
}, | |
"source": [ | |
"import pandas as pd\r\n", | |
"from statsmodels.tsa.statespace.sarimax import SARIMAX\r\n", | |
"from numpy import cbrt\r\n", | |
"from plotly import graph_objects as go\r\n", | |
"\r\n", | |
"def get_covid_data():\r\n", | |
" \"\"\"\r\n", | |
" Download latest covid data.\r\n", | |
" \"\"\"\r\n", | |
" DATA_URL = 'https://raw.githubusercontent.com/OxCGRT/covid-policy-tracker/master/data/OxCGRT_latest.csv'\r\n", | |
" full_df = pd.read_csv(DATA_URL,\r\n", | |
" parse_dates=['Date'],\r\n", | |
" encoding=\"ISO-8859-1\",\r\n", | |
" dtype={\"RegionName\": str,\r\n", | |
" \"RegionCode\": str,\r\n", | |
" \"CountryName\": str,\r\n", | |
" \"CountryCode\": str},\r\n", | |
" usecols = ['Date','CountryName','RegionName',\r\n", | |
" 'ConfirmedCases','ConfirmedDeaths','Jurisdiction'],\r\n", | |
" error_bad_lines=False)\r\n", | |
" full_df = full_df.set_index('Date', drop=True)\r\n", | |
" new_cols = {'ConfirmedCases':'Cumulative Cases','ConfirmedDeaths':'Cumulative Deaths'}\r\n", | |
" full_df = full_df.rename(columns = new_cols)\r\n", | |
" return full_df\r\n", | |
"\r\n", | |
"def get_graph(df, state, country, stats):\r\n", | |
" \"\"\"\r\n", | |
" get_graph(df, state, country, stats)\r\n", | |
" ---\r\n", | |
" Helper function for graph_stat to create and return plotly figure of chosen statistics.\r\n", | |
" ---\r\n", | |
" \"\"\"\r\n", | |
"\r\n", | |
" if state != 'None':\r\n", | |
" title = f'COVID-19 in {state}, {country}'\r\n", | |
" else:\r\n", | |
" title = f'COVID-19 in {country}'\r\n", | |
" \r\n", | |
" fig = go.Figure(layout_title_text = title)\r\n", | |
" for stat in stats:\r\n", | |
" fig.add_trace(go.Scatter(x=df.index, y=df[stat], name=stat))\r\n", | |
" fig.update_layout(showlegend=True)\r\n", | |
"\r\n", | |
" return fig\r\n", | |
"\r\n", | |
"def graph_stat(full_df, country='United States',\r\n", | |
" state=None, stats=['Cumulative Cases']):\r\n", | |
" \"\"\"\r\n", | |
" graph_stat(full_df, country='United States',\r\n", | |
" state=None, stat='Cumulative Cases')\r\n", | |
" ---\r\n", | |
" Graphs chosen statistic(s)\r\n", | |
" full_df should be a Pandas DataFrame with a time series index. \r\n", | |
" Function subsets full_df in respect to state and/or country. \r\n", | |
" Uses a SARIMA model to graph up to date COVID cases or deaths.\r\n", | |
" Returns a plotly figure of deaths or cases\r\n", | |
" ---\r\n", | |
" \"\"\"\r\n", | |
" if not stats:\r\n", | |
" stats = ['Cumulative Cases']\r\n", | |
"\r\n", | |
" if state in full_df['RegionName'].dropna().unique():\r\n", | |
" df = full_df[(full_df['Jurisdiction'] == 'STATE_TOTAL') \r\n", | |
" & (full_df['RegionName'] == state)][:-1]\r\n", | |
" else: \r\n", | |
" df = full_df[(full_df['Jurisdiction'] == 'NAT_TOTAL') \r\n", | |
" & (full_df['CountryName'] == country)][:-1]\r\n", | |
"\r\n", | |
" df = df.interpolate(method='time', limit_direction='forward', \r\n", | |
" limit_area='inside', downcast='infer')\r\n", | |
" df['Daily Cases'] = df['Cumulative Cases'].diff()\r\n", | |
" df['Daily Deaths'] = df['Cumulative Deaths'].diff()\r\n", | |
" df = df[stats]\r\n", | |
"\r\n", | |
" df = df.dropna()\r\n", | |
" \r\n", | |
" fig = get_graph(df, state, country, stats)\r\n", | |
"\r\n", | |
" return fig" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment