Skip to content

Instantly share code, notes, and snippets.

@tmtk75
Last active September 12, 2020 10:13
Show Gist options
  • Save tmtk75/d1e2bbf11d82d37d5318f0145312ff60 to your computer and use it in GitHub Desktop.
Save tmtk75/d1e2bbf11d82d37d5318f0145312ff60 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import glob\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import math\n",
"from functools import reduce\n",
"from pandas.tseries.offsets import MonthEnd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"js = [json.load(open(f))['ExposureChecks'] for f in glob.glob(\"../COVID19/Exposure*.json\")]\n",
"checks = reduce(lambda a, b: a + b, js)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(checks).drop_duplicates()\n",
"df[\"year\"] = df.Timestamp.map(lambda x: pd.to_datetime(x).year)\n",
"df[\"month\"] = df.Timestamp.map(lambda x: pd.to_datetime(x).month)\n",
"df[\"day\"] = df.Timestamp.map(lambda x: pd.to_datetime(x).day)\n",
"df.reset_index(drop=True)\n",
"del df[\"DataSource\"]\n",
"df[:1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#\n",
"ym = df.groupby([\"year\", \"month\", \"day\"]).sum(\"RandomIDCount\")\n",
"yms = pd.unique(pd.Series([e[0:2] for e in ym.index]))\n",
"\n",
"#\n",
"def p(k):\n",
" s = pd.to_datetime(\"%d-%02d-01\" % k)\n",
" idx = [e.day for e in pd.date_range(s, s + MonthEnd(1))]\n",
" h = ym.loc[k]\n",
" g = h.reindex(index=idx, fill_value=0)\n",
"# g[\"MatchCount\"][15] = 1\n",
" return g\n",
"\n",
"dfs = [(p(k), k) for k in yms]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"R = 0.77\n",
"s = len(dfs)\n",
"t = math.ceil(s / 2)\n",
"fig, axs = plt.subplots(t, 2, figsize=(16*R, 4*R*2))\n",
"fig.tight_layout(pad=3.5)\n",
"if s % 2 == 1:\n",
" fig.delaxes(axs[t-1, 1])\n",
"maxRIC = pd.Series([e[0].RandomIDCount.max() for e in dfs]).max()\n",
"\n",
"\n",
"def plot(idx, df, k):\n",
" ax = axs[int(idx/t),idx%2]\n",
" a = df.plot(kind=\"bar\", ax=ax, grid=True, colormap='Accent', y=\"RandomIDCount\")\n",
" right = a.twinx();\n",
" df.plot(ax=right, y=\"MatchCount\")\n",
"# a.legend(bbox_to_anchor=(0.4,0.8))\n",
" a.legend(loc='upper left')\n",
" a.set_title(\"%d-%02d\" % k)\n",
"# a.set_ylim([0, maxRIC*1.1])\n",
" right.set_ylim([0, 2])\n",
"# right.get_legend().remove()\n",
"# right.set_ylabel(\"MatchCount\")\n",
"# a.set_xlabel(\"day\")\n",
" return k\n",
"\n",
"[plot(i, e[0], e[1]) for i, e in enumerate(dfs)]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment