{
"cells": [
{
"cell_type": "code",
"execution_count": 58,
"id": "61040d84",
"metadata": {
"code_folding": []
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import IPython.core.display as di\n",
"\n",
"# Hide code by default when the notebook is exported as HTML\n",
"di.display_html('', raw=True)\n",
"\n",
"# Extend width of page\n",
"from IPython.core.display import display, HTML\n",
"display(HTML(\"\"))\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "5cb911c7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import altair as alt\n",
"\n",
"df = pd.read_csv('output/pm2_5_hourly_summary.csv')"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "07c71ce8",
"metadata": {
"code_folding": [
0
]
},
"outputs": [],
"source": [
"# Filter to Zavala, Douglass, Mesita & Hawkins \n",
"\n",
"includeKeywords = [\"Zavala\", \"Douglass\", \"Mesita\", \"Hawkins\"]\n",
"\n",
"dsa_str = '|'.join(includeKeywords)\n",
"\n",
"mask = df.stack().str.contains(dsa_str).any(level=0)\n",
"\n",
"df_masked = df[mask]\n",
"\n",
"\n",
"### --- Build hurly mean and median charts\n",
"source = df_masked\n",
"\n",
"selection = alt.selection_multi(fields=['column_label'], bind='legend')\n",
"\n",
"# set colorscale\n",
"domain = ['ry/Zavala 2 B (undefined) (31.771832 -106.447092) Primary ',\n",
" 'ry/Zavala E.S (outside) (31.771649 -106.447023) Primary ', \n",
" 'ry/Zavala E.S B (undefined) (31.771649 -106.447023) Primary ',\n",
" 'ry/Zavala 2 (outside) (31.771832 -106.447092) Primary ',\n",
" 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ',\n",
" 'ry/Mesita E.S B (undefined) (31.783542 -106.50389) Primary ',\n",
" 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ',\n",
" 'ry/Hawkins E.S B (undefined) (31.777423 -106.419229) Primary ',\n",
" 'ry/Hawkins E.S (outside) (31.777423 -106.419229) Primary ',\n",
" 'ry/Douglass E.S. B (undefined) (31.766496 -106.464951) Primary ',\n",
" 'ry/Douglass E.S. (outside) (31.766496 -106.464951) Primary '\n",
" ]\n",
"\n",
"range_ = ['rgb(57, 59, 121)', 'rgb(82, 84, 163)', 'rgb(107, 110, 207)', 'rgb(156, 158, 222)', \n",
" 'rgb(99, 121, 57)', 'rgb(140, 162, 82)',\n",
" 'rgb(132, 60, 57)', 'rgb(173, 73, 74)', \n",
" 'rgb(140, 109, 49)', 'rgb(189, 158, 57)']\n",
"\n",
"chart_mean_all = alt.Chart(source).mark_line().encode(\n",
" alt.X('hour'),\n",
" alt.Y('mean_pm2_5'),\n",
" alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.1))\n",
").add_selection(\n",
" selection\n",
")\n",
"\n",
"\n",
"chart_median_all = alt.Chart(source).mark_line().encode(\n",
" alt.X('hour'),\n",
" alt.Y('median_pm2_5'),\n",
" alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.1))\n",
").add_selection(\n",
" selection\n",
")\n",
"\n",
"\n",
"# Build faceted hourly charts\n",
"\n",
"source_melt = pd.melt(df_masked, id_vars=['column_label', 'hour'], value_vars=['mean_pm2_5', 'median_pm2_5'])\n",
"\n",
"facet_all = alt.Chart(source_melt).mark_line().encode(\n",
" alt.X('hour', scale=alt.Scale(zero=False)),\n",
" y='value',\n",
" color='variable',\n",
" facet=alt.Facet('column_label:O', columns=2),\n",
").properties(\n",
" width=200,\n",
" height=100,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "0ba85870",
"metadata": {
"code_folding": [
0
]
},
"outputs": [],
"source": [
"# Adjust filter to exclude Zavala 2 (outside) \n",
"\n",
"includeKeywords = [\"Zavala E.S B\", \"Zavala E.S\", \"Zavala 2 B\", \"Douglass\", \"Mesita\", \"Hawkins\"]\n",
"\n",
"dsa_str = '|'.join(includeKeywords)\n",
"\n",
"mask = df.stack().str.contains(dsa_str).any(level=0)\n",
"\n",
"df_filtered = df[mask]\n",
"\n",
"\n",
"\n",
"## Hourly mean and median charts without Zavala 2 (outside)\n",
"source = df_filtered\n",
"\n",
"selection = alt.selection_multi(fields=['column_label'], bind='legend')\n",
"\n",
"# set colorscale\n",
"domain = ['ry/Zavala 2 B (undefined) (31.771832 -106.447092) Primary ',\n",
" 'ry/Zavala E.S (outside) (31.771649 -106.447023) Primary ', \n",
" 'ry/Zavala E.S B (undefined) (31.771649 -106.447023) Primary ',\n",
" 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ',\n",
" 'ry/Mesita E.S B (undefined) (31.783542 -106.50389) Primary ',\n",
" 'ry/Mesita E.S (outside) (31.783542 -106.50389) Primary ',\n",
" 'ry/Hawkins E.S B (undefined) (31.777423 -106.419229) Primary ',\n",
" 'ry/Hawkins E.S (outside) (31.777423 -106.419229) Primary ',\n",
" 'ry/Douglass E.S. B (undefined) (31.766496 -106.464951) Primary ',\n",
" 'ry/Douglass E.S. (outside) (31.766496 -106.464951) Primary '\n",
" ]\n",
"\n",
"range_ = ['rgb(57, 59, 121)', 'rgb(82, 84, 163)', 'rgb(107, 110, 207)', \n",
" 'rgb(99, 121, 57)', 'rgb(140, 162, 82)',\n",
" 'rgb(132, 60, 57)', 'rgb(173, 73, 74)', \n",
" 'rgb(140, 109, 49)', 'rgb(189, 158, 57)']\n",
"\n",
"chart_mean_filtered = alt.Chart(source).mark_line().encode(\n",
" alt.X('hour'),\n",
" alt.Y('mean_pm2_5'),\n",
" alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.1))\n",
").add_selection(\n",
" selection\n",
")\n",
"\n",
"\n",
"chart_median_filtered = alt.Chart(source).mark_line().encode(\n",
" alt.X('hour'),\n",
" alt.Y('median_pm2_5'),\n",
" alt.Color('column_label', scale=alt.Scale(domain=domain, range=range_)),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.1))\n",
").add_selection(\n",
" selection\n",
")\n",
"\n",
"\n",
"\n",
"# Build faceted chart\n",
"\n",
"# Build faceted hourly median charts\n",
"\n",
"source_melt_filtered = pd.melt(df_filtered, id_vars=['column_label', 'hour'], value_vars=['mean_pm2_5', 'median_pm2_5'])\n",
"\n",
"facet_filtered = alt.Chart(source_melt_filtered).mark_line().encode(\n",
" alt.X('hour', scale=alt.Scale(zero=False)),\n",
" y='value',\n",
" color='variable',\n",
" facet=alt.Facet('column_label:O', columns=2),\n",
").properties(\n",
" width=200,\n",
" height=100,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "1b8cef55",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. Mean and median pm2.5 by hour for all Zavala, Hawkins, Douglass, and Mesita monitors\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
""
],
"text/plain": [
"alt.HConcatChart(...)"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mean and median pm2.5 by hour for all Zavala, Hawkins, Douglass, and Mesita primary sensors\n",
"print('1. Mean and median pm2.5 by hour for all Zavala, Hawkins, Douglass, and Mesita monitors')\n",
"\n",
"chart_mean_all | chart_median_all"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "45361c0f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2. Mean and median pm2.5 by hour (excluding Zavala 2 (outside))\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
""
],
"text/plain": [
"alt.HConcatChart(...)"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mean and median pm2.5 by hour (excluding Zavala 2 (outside))\n",
"print('2. Mean and median pm2.5 by hour (excluding Zavala 2 (outside))')\n",
"\n",
"chart_mean_filtered | chart_median_filtered"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "c9f2baa6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3. Mean and median pm2.5 by hour (excluding Zavala 2 (outside))\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
""
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mean and median pm2.5 by hour (excluding Zavala 2 (outside))\n",
"print('3. Mean and median pm2.5 by hour (excluding Zavala 2 (outside))')\n",
"facet_filtered"
]
}
],
"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.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}