-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial refactor * Update plotting.jl * refactor plotting for DRY * Figure 2
- Loading branch information
1 parent
ccbbed3
commit ae5c564
Showing
6 changed files
with
231 additions
and
194 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,75 @@ | ||
function _make_captions!(df, scenario_dict, target_dict) | ||
scenario_titles = [scenario_dict[scenario].title for scenario in df.Scenario] | ||
target_titles = [target_dict[target].title for target in df.Target] | ||
df.Scenario_Target .= scenario_titles .* "\n" .* target_titles | ||
return nothing | ||
function _fig2_pred_filter(predictions, scenario, target, latent_model, horizon; | ||
true_gi_choice, used_gi_choice, horizon_diff = 7) | ||
df = predictions |> | ||
df -> @subset(df, :Latent_Model.==latent_model) |> | ||
df -> @subset(df, :True_GI_Mean.==true_gi_choice) |> | ||
df -> @subset(df, :Used_GI_Mean.==used_gi_choice) |> | ||
df -> @subset(df, | ||
horizon-horizon_diff.<(:target_times.-:Reference_Time).<=horizon) |> | ||
df -> @subset(df, :Scenario.==scenario) |> | ||
df -> @subset(df, :Target.==target) | ||
return df | ||
end | ||
|
||
function _figure_two_truth_data( | ||
truth_df, scenario_dict, target_dict; true_gi_choice, gi_choices = [ | ||
2.0, 10.0, 20.0]) | ||
_truth_df = mapreduce(vcat, gi_choices) do used_gi | ||
df = deepcopy(truth_df) | ||
df.Used_GI_Mean .= used_gi | ||
df | ||
function figuretwo( | ||
prediction_df, truth_data_df, scenarios, targets, horizon; | ||
scenario_dict, target_dict, latent_model_dict, | ||
latent_model = "ar", igps = ["DirectInfections", "ExpGrowthRate", "Renewal"], | ||
true_gi_choice = 10.0, other_gi_choices = [2.0, 10.0, 20.0], data_color = :black, | ||
colors = [:red, :blue, :green], iqr_alpha = 0.3, horizon_diff = 7) | ||
fig = Figure(; size = (1000, 800 * length(scenarios))) | ||
axs = mapreduce(vcat, enumerate(scenarios)) do (i, scenario) | ||
n = length(targets) | ||
Label(fig[(n * (i - 1) + 1):(n * i), 0], | ||
scenario_dict[scenario].title, rotation = pi / 2, fontsize = 36) | ||
mapreduce(hcat, enumerate(targets)) do (j, target) | ||
map(enumerate(other_gi_choices)) do (k, used_gi_choice) | ||
row = j + (i - 1) * length(targets) | ||
ax = Axis(fig[row, k]) | ||
# #Filter the data for fig2 panels | ||
pred_df = _fig2_pred_filter( | ||
prediction_df, scenario, target, latent_model, horizon; | ||
true_gi_choice, used_gi_choice, horizon_diff) | ||
truth_df = _fig_truth_filter( | ||
truth_data_df, scenario, target; true_gi_choice) | ||
# #Plot onto axes | ||
_plot_predictions!(ax, pred_df; igps, colors, iqr_alpha) | ||
_plot_truth!(ax, truth_df; color = data_color) | ||
vlines!(ax, pred_df.Reference_Time |> unique, color = :black, | ||
linestyle = :dash, label = "Reference time") | ||
# axes | ||
if row == 1 | ||
if k == 1 | ||
ax.title = "Underestimating mean GI" | ||
elseif k == 2 | ||
ax.title = "Good estimation of GI" | ||
elseif k == 3 | ||
ax.title = "Overestimating mean GI" | ||
end | ||
end | ||
if row == length(targets) * length(scenarios) | ||
ax.xlabel = "Time" | ||
end | ||
if k == 1 | ||
ax.ylabel = target_dict[target].title | ||
end | ||
ax.limits = ( | ||
(minimum(pred_df.Reference_Time) - horizon_diff, | ||
maximum(pred_df.Reference_Time) + 1), | ||
nothing) | ||
ax.xticks = vcat(minimum(pred_df.Reference_Time) - horizon_diff, | ||
pred_df.Reference_Time |> unique) | ||
ax | ||
end | ||
end | ||
end | ||
_make_captions!(_truth_df, scenario_dict, target_dict) | ||
|
||
truth_plotting_data = _truth_df |> | ||
df -> @subset(df, :True_GI_Mean.==true_gi_choice) |> | ||
df -> @transform(df, :Data="Truth data") |> data | ||
plt_truth = truth_plotting_data * | ||
mapping(:target_times => "T", :target_values => "Process values", | ||
row = :Scenario_Target, | ||
col = :Used_GI_Mean => renamer([2.0 => "Underestimate GI", | ||
10.0 => "Good GI", 20.0 => "Overestimate GI"]), | ||
color = :Data => AlgebraOfGraphics.scale(:color2)) * | ||
visual(AlgebraOfGraphics.Scatter) | ||
return plt_truth | ||
end | ||
|
||
function _figure_two_scenario( | ||
analysis_df, igp, scenario_dict, target_dict; true_gi_choice, | ||
lower_sym = :q_025, upper_sym = :q_975) | ||
min_ref_time = minimum(analysis_df.Reference_Time) | ||
early_df = analysis_df |> | ||
df -> @subset(df, :Reference_Time.==min_ref_time) |> | ||
df -> @subset(df, :IGP_Model.==igp) |> | ||
df -> @subset(df, :True_GI_Mean.==true_gi_choice) |> | ||
df -> @subset(df, :target_times.<=min_ref_time - 7) | ||
|
||
seqn_df = analysis_df |> | ||
df -> @subset(df, :True_GI_Mean.==true_gi_choice) |> | ||
df -> @subset(df, :IGP_Model.==igp) |> | ||
df -> @subset(df, | ||
:Reference_Time .- :target_times.∈fill(0:6, size(df, 1))) | ||
|
||
full_df = vcat(early_df, seqn_df) | ||
_make_captions!(full_df, scenario_dict, target_dict) | ||
|
||
model_plotting_data = full_df |> data | ||
|
||
plt_model = model_plotting_data * | ||
mapping(:target_times => "T", :q_5 => "Process values", | ||
row = :Scenario_Target, | ||
col = :Used_GI_Mean => renamer([2.0 => "Underestimate GI", | ||
10.0 => "Good GI", 20.0 => "Overestimate GI"]), | ||
color = :Latent_Model => "Latent models") * | ||
mapping(lower = lower_sym, upper = upper_sym) * | ||
visual(LinesFill) | ||
|
||
return plt_model | ||
end | ||
|
||
function figuretwo(truth_df, analysis_df, igp, scenario_dict, | ||
target_dict; fig_kws = (; size = (1000, 2800)), | ||
true_gi_choice = 10.0, gi_choices = [2.0, 10.0, 20.0]) | ||
|
||
# Perform checks on the dataframes | ||
_dataframe_checks(truth_df, analysis_df, scenario_dict) | ||
|
||
f_td = _figure_two_truth_data( | ||
truth_df, scenario_dict, target_dict; true_gi_choice, gi_choices) | ||
f_mdl = _figure_two_scenario( | ||
analysis_df, igp, scenario_dict, target_dict; true_gi_choice) | ||
|
||
fg = draw(f_mdl + f_td; facet = (; linkyaxes = :none), | ||
legend = (; orientation = :horizontal, position = :bottom), | ||
figure = fig_kws, | ||
axis = (; xlabel = "T", ylabel = "Process values")) | ||
for g in fg.grid[1:3:end, :] | ||
g.axis.limits = (nothing, target_dict["rt"].ylims) | ||
end | ||
for g in fg.grid[2:3:end, :] | ||
g.axis.limits = (nothing, target_dict["Rt"].ylims) | ||
end | ||
for g in fg.grid[3:3:end, :] | ||
g.axis.limits = (nothing, target_dict["log_I_t"].ylims) | ||
end | ||
|
||
return fg | ||
leg = Legend(fig[length(targets) * length(scenarios) + 1, 1:2], | ||
last(axs), "Infection generating process"; | ||
orientation = :horizontal, tellwidth = false, framevisible = false) | ||
lab = Label(fig[length(targets) * length(scenarios) + 1, length(other_gi_choices)], | ||
"Latent model for \n infection generating\n process: $(latent_model_dict[latent_model].title) \n True mean GI: $(true_gi_choice) days \n Horizon: $(horizon) days"; | ||
tellwidth = false, | ||
fontsize = 18) | ||
resize_to_layout!(fig) | ||
fig | ||
end |
Oops, something went wrong.