# plot

rm(list = ls())
source('./src/dual_mesh_loc.R')
source('./src/logintensity_vis.R')
load('./data/comparsion_fullcase.RData')
load('./data/comparsion_holecase.RData')

getwd()

logintensity_mean_list = list(logintensity_mean_full_df, logintensity_mean_hole_df)
names(logintensity_mean_list) = c("full", "hole")
logintensity_sd_list = list(logintensity_sd_full_df, logintensity_sd_hole_df)
names(logintensity_sd_list) = c("full", "hole")

mesh_full_hole = mesh_full
contour_step_mean = 0.3
contour_step_sd = 0.08
pl.dom_hole = cbind(c(2, 8, 8, 2), c(2, 2, 8.5, 8.5))
#----------------------------------- log-intensity posterior mean --------------------------------


layout(matrix(c(1,2,7,3,4,7,5,6,7), nrow=3, ncol=3, byrow=TRUE), widths=c(1, 1, 0.25))
par(mar=c(2, 2, 2, 1), oma=c(3,3,3,2))

logintensity_vis(mesh = mesh_full, field_value_list = logintensity_mean_list, case = "full", method = "VINNGP", contour_step = contour_step_mean)
mtext("VoGCAM", side = 2, line = 2.5, cex = 2)
logintensity_vis(mesh = mesh_full_hole, field_value_list = logintensity_mean_list, case = "hole", method = "VINNGP", contour_step = contour_step_mean)
rect(pl.dom_hole[1,1], pl.dom_hole[1,2], pl.dom_hole[2,1], pl.dom_hole[3,2], border = 'red')

logintensity_vis(mesh = mesh_full, field_value_list = logintensity_mean_list, case = "full", method = "INLA", contour_step = contour_step_mean)
mtext("INLA", side = 2, line = 2.5, cex = 2)
logintensity_vis(mesh = mesh_full_hole, field_value_list = logintensity_mean_list, case = "hole", method = "INLA", contour_step = contour_step_mean)
rect(pl.dom_hole[1,1], pl.dom_hole[1,2], pl.dom_hole[2,1], pl.dom_hole[3,2], border = 'red')

logintensity_vis(mesh = mesh_full, field_value_list = logintensity_mean_list, case = "full", method = "VIFRK", contour_step = contour_step_mean)
mtext("Full case", side = 1, line = 3.5, cex = 2)
mtext("VIFRK", side = 2, line = 2.5, cex = 1.8)
logintensity_vis(mesh = mesh_full_hole, field_value_list = logintensity_mean_list, case = "hole", method = "VIFRK", contour_step = contour_step_mean)
rect(pl.dom_hole[1,1], pl.dom_hole[1,2], pl.dom_hole[2,1], pl.dom_hole[3,2], border = 'red')
mtext("Hole case", side = 1, line = 3.5, cex = 1.8)

values_mean = c(logintensity_mean_list$hole$VINNGP, logintensity_mean_list$hole$INLA, logintensity_mean_list$hole$VIFRK, 
                logintensity_mean_list$full$VINNGP, logintensity_mean_list$full$INLA, logintensity_mean_list$full$VIFRK)
values = sort(values_mean)
val_min = min(values)
val_max = max(values)
quantiles = quantile(values, probs = seq(0, 1, by = 0.01))
# quantiles = c(0.1280661, 0.1517026, 0.1814599, 0.2730920, 0.3900023, 1)
color_breaks = c(val_min, log(quantiles + 1 - min(values)))
color_breaks = sort(color_breaks)
colors = colorRampPalette(viridis(200))(length(color_breaks)-1)

# Plot color bar
plot.new()
image.plot(legend.only=TRUE, z = matrix(values, ncol = 1), col=colors, legend.shrink = 1, legend.mar = 10, legend.width = 25,
           smallplot = c(0.35, 0.55, 0.022, 0.978),
           axis.args = list(cex.axis = 2, # Increased label size (e.g., 1.2x default)
                            font.axis = 4))
dev.off()

#----------------------------------- log-intensity posterior sd --------------------------------


layout(matrix(c(1,2,7,3,4,7,5,6,7), nrow=3, ncol=3, byrow=TRUE), widths=c(1, 1, 0.25))
par(mar=c(2, 2, 2, 1), oma=c(3,3,3,2))

logintensity_vis(mesh = mesh_full, field_value_list = logintensity_sd_list, case = "full", method = "VINNGP", contour_step = contour_step_mean)
mtext("VoGCAM", side = 2, line = 2.5, cex = 2)
logintensity_vis(mesh = mesh_full_hole, field_value_list = logintensity_sd_list, case = "hole", method = "VINNGP", contour_step = contour_step_mean)
rect(pl.dom_hole[1,1], pl.dom_hole[1,2], pl.dom_hole[2,1], pl.dom_hole[3,2], border = 'red')

logintensity_vis(mesh = mesh_full, field_value_list = logintensity_sd_list, case = "full", method = "INLA", contour_step = contour_step_mean)
mtext("INLA", side = 2, line = 2.5, cex = 2)
logintensity_vis(mesh = mesh_full_hole, field_value_list = logintensity_sd_list, case = "hole", method = "INLA", contour_step = contour_step_mean)
rect(pl.dom_hole[1,1], pl.dom_hole[1,2], pl.dom_hole[2,1], pl.dom_hole[3,2], border = 'red')

logintensity_vis(mesh = mesh_full, field_value_list = logintensity_sd_list, case = "full", method = "VIFRK", contour_step = contour_step_mean)
mtext("Full case", side = 1, line = 3.5, cex = 2)
mtext("VIFRK", side = 2, line = 2.5, cex = 1.8)
logintensity_vis(mesh = mesh_full_hole, field_value_list = logintensity_sd_list, case = "hole", method = "VIFRK", contour_step = contour_step_mean)
rect(pl.dom_hole[1,1], pl.dom_hole[1,2], pl.dom_hole[2,1], pl.dom_hole[3,2], border = 'red')
mtext("Hole case", side = 1, line = 3.5, cex = 1.8)

values_mean = c(logintensity_sd_list$hole$VINNGP, logintensity_sd_list$hole$INLA, logintensity_sd_list$hole$VIFRK, 
                logintensity_sd_list$full$VINNGP, logintensity_sd_list$full$INLA, logintensity_sd_list$full$VIFRK)
values = sort(values_mean)
val_min = min(values)
val_max = max(values)
quantiles = quantile(values, probs = seq(0, 1, by = 0.01))
# quantiles = c(0.1280661, 0.1517026, 0.1814599, 0.2730920, 0.3900023, 1)
color_breaks = c(val_min, log(quantiles + 1 - min(values)))
color_breaks = sort(color_breaks)
colors = colorRampPalette(viridis(200))(length(color_breaks)-1)

# Plot color bar
plot.new()
image.plot(legend.only=TRUE, z = matrix(values, ncol = 1), col=colors, legend.shrink = 1, legend.mar = 10, legend.width = 60,
           smallplot = c(0.35, 0.55, 0.022, 0.978),
           axis.args = list(cex.axis = 2, # Increased label size (e.g., 1.2x default)
                            font.axis = 4))
dev.off()

table_full
table_hole
