Skip to content

Commit

Permalink
Finalise maps and start automatic result writing
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-ragonnet committed Nov 20, 2023
1 parent 19da4f2 commit 06486d4
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import pandas as pd
from math import ceil
from math import floor, ceil
from numpy import arange

import plotly.graph_objects as go
import plotly.express as px

import seaborn as sns

YLAB_LOOKUP_SPLIT = {
"cases_averted_relative": "% infections averted<br>by school closure",
"deaths_averted_relative": "% deaths averted<br>by school closure",
"delta_hospital_peak_relative": "Relative reduction in<br>peak hospital occupancy (%)"
"delta_hospital_peak_relative": "Relative reduction in<br>peak hospital<br>occupancy (%)"
}

BOX_COLORS= {
Expand Down Expand Up @@ -278,6 +281,7 @@ def plot_ll_comparison(combined_ll_df, output="loglikelihood"):


def plot_relative_map(output_dfs_dict: dict[str, pd.DataFrame], req_output="delta_hospital_peak_relative"):

this_iso3_list = list(output_dfs_dict.keys())
values = [- 100 * output_dfs_dict[iso3][req_output].loc[0.5] for iso3 in this_iso3_list]
data_df = pd.DataFrame.from_dict({"iso3": this_iso3_list, "values": values})
Expand Down Expand Up @@ -308,4 +312,73 @@ def plot_relative_map(output_dfs_dict: dict[str, pd.DataFrame], req_output="delt
height=500,
width=1200,
)
return fig
return fig


def plot_relative_map_with_bins(diff_quantiles_dfs, req_output="cases_averted_relative", panel=""):

this_iso3_list = list(diff_quantiles_dfs.keys())
values = [- 100 * diff_quantiles_dfs[iso3][req_output].loc[0.5] for iso3 in this_iso3_list]

# create bins
step = 10.
min_bin = step * floor(min(values) / step)
max_bin = step * ceil(max(values) / step)
bins = arange(min_bin, max_bin + step, step)
data_df = pd.DataFrame.from_dict({"iso3": this_iso3_list, "values": values})
data_df['category'] = pd.cut(data_df['values'], bins).astype(str)

# work out colors and bin order
neg_color_palette_6 = px.colors.sequential.BuGn[3:9]
n_negative = len([b for b in bins if b < 0.])
neg_colors = neg_color_palette_6[0:n_negative]

n_positive = len(bins) - 1 - n_negative
pos_color_palette = px.colors.sequential.Reds[1:] if n_positive < 9 else px.colors.sequential.Reds
pos_colors = pos_color_palette[0:n_positive]

all_colors = neg_colors[::-1] + pos_colors
bin_orders = [f"({lower}, {lower + step}]" for lower in bins[:-1]]

legend_title = YLAB_LOOKUP_SPLIT[req_output]

fig = px.choropleth(
data_df,
locations=data_df["iso3"],
color=data_df["category"],
color_discrete_sequence = all_colors,
category_orders={"category": bin_orders},
labels={"category": legend_title},
fitbounds='locations'
)
fig.update_layout(
geo=dict(
showframe=False,
showcoastlines=False,
projection_type='equirectangular' #'natural earth' 'equirectangular'
),
margin={"r":0,"t":0,"l":0,"b":0},
autosize=False,
height=500,
width=1200,
font=dict(
family="Times New Roman",
size=16,
color="black"
),
legend=dict(
y=.5,
x=1,
itemwidth=50
),
)

fig.add_annotation(x=0.02, y=.99, text=panel, showarrow=False,
font=dict(
family="Times New Roman",
size=25, # Set the font size here
color="black"
)
)

return fig
61 changes: 49 additions & 12 deletions user/rragonnet/remote_run_outputs/file_gatherer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import shutil\n",
"from autumn.projects.sm_covid2.common_school.runner_tools import INCLUDED_COUNTRIES\n",
"import pandas as pd\n",
"from pathlib import Path\n",
"from autumn.projects.sm_covid2.common_school.output_plots.multicountry import plot_multic_relative_outputs, plot_relative_map\n",
"from autumn.projects.sm_covid2.common_school.output_plots.multicountry import plot_multic_relative_outputs, plot_relative_map_with_bins\n",
"from matplotlib import pyplot as plt \n",
"\n",
"full_iso3_list = list(INCLUDED_COUNTRIES['all'].keys())"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -108,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -117,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -130,7 +130,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -142,16 +142,53 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# RUN THIS CELL TWICE TO AVOID ANY WARNING TEXT SHOWING UP IN FIRST FIGURE\n",
"(common_dir / \"maps\").mkdir(exist_ok=True)\n",
"for output in [\"cases_averted_relative\", \"delta_hospital_peak_relative\", \"deaths_averted_relative\"]:\n",
" fig = plot_relative_map(diff_quantiles_dfs, output)\n",
"for i, output in enumerate([\"cases_averted_relative\", \"deaths_averted_relative\", \"delta_hospital_peak_relative\"]): \n",
" panel = [\"A\", \"B\", \"C\"][i]\n",
" fig = plot_relative_map_with_bins(diff_quantiles_dfs, output, panel)\n",
" plt.close()\n",
" for file_format in FILE_FORMATS:\n",
" fig.write_image(common_dir / \"maps\" / f\"map_{output}.{file_format}\")"
" fig.write_image(common_dir / \"maps\" / f\"map_{output}.{file_format}\")\n",
"\n",
"# Then combine the three figures using Nitro Pro\n",
"# Finally print to pdf from Nitro, using the custom Page Format called MapFigure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_main_result_text(diff_quantiles_dfs):\n",
" n_countries = len(diff_quantiles_dfs)\n",
"\n",
" n_pos_effects = {}\n",
" perc_pos_effects = {}\n",
" for q in [0.5, 0.975]:\n",
" n_pos_effects[q] = {}\n",
" perc_pos_effects[q] = {}\n",
" for output in [\"cases_averted\", \"deaths_averted\", \"delta_hospital_peak\"]:\n",
" n_pos_effects[q][output] = len([iso3 for iso3 in diff_quantiles_dfs if diff_quantiles_dfs[iso3][output][q] < 0.])\n",
" perc_pos_effects[q][output] = int(100. * n_pos_effects[q][output] / n_countries)\n",
"\n",
" text = f\"According to our median estimates alone, school closures were associated with a beneficial effect on infections in {n_pos_effects[0.5]['cases_averted']} ({perc_pos_effects[0.5]['cases_averted']}%) countries, on deaths in {n_pos_effects[0.5]['deaths_averted']} ({perc_pos_effects[0.5]['deaths_averted']}%) countries, and on peak hospital occupancy in {n_pos_effects[0.5]['delta_hospital_peak']} ({perc_pos_effects[0.5]['delta_hospital_peak']}%) countries.\"\n",
" \n",
" text += \" However, the 95% credible intervals (95-CI) around our estimates for the three disease indicators were wide for most countries, such that even the direction of the effect was difficult to evaluate with certainty.\"\n",
"\n",
" text += f\" Only {n_pos_effects[.975]['cases_averted']} ({perc_pos_effects[.975]['cases_averted']}%) countries were associated with a 95-CI consistent with a positive effect of school closures on infections,\"\n",
" text += f\" whereas {n_pos_effects[.975]['deaths_averted']} ({perc_pos_effects[.975]['deaths_averted']}%) countries had a 95-CI suggesting a positive effect on deaths,\"\n",
" text += f\" and {n_pos_effects[.975]['delta_hospital_peak']} ({perc_pos_effects[.975]['delta_hospital_peak']}%) had a 95-CI suggesting a positive effect on peak hospital occupancy.\"\n",
"\n",
" print(text)\n",
"\n",
"\n",
"print_main_result_text(diff_quantiles_dfs)"
]
},
{
Expand Down

0 comments on commit 06486d4

Please sign in to comment.