Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -86,6 +86,35 @@ heatMap <- function(x, y, z,
|
|
| 86 |
}
|
| 87 |
}
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# UI Definition
|
| 90 |
ui <- fluidPage(
|
| 91 |
titlePanel("Multiscale Heatmap Explorer"),
|
|
@@ -97,11 +126,14 @@ ui <- fluidPage(
|
|
| 97 |
selectInput("model", "Model",
|
| 98 |
choices = unique(sm$optimizeImageRep),
|
| 99 |
selected = "clip"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
selectInput("metric", "Metric",
|
| 101 |
-
choices =
|
| 102 |
-
"AUTOC_rate_std_ratio_mean_pc", "AUTOC_rate_mean_pc", "AUTOC_rate_std_mean_pc",
|
| 103 |
-
"MeanVImportHalf1", "MeanVImportHalf2", "FracTopkHalf1", "RMSE"),
|
| 104 |
selected = "AUTOC_rate_std_ratio_mean"),
|
|
|
|
| 105 |
checkboxInput("compareToBest", "Compare to best single scale", value = FALSE)
|
| 106 |
),
|
| 107 |
mainPanel(
|
|
@@ -141,9 +173,9 @@ server <- function(input, output) {
|
|
| 141 |
group_by(MaxImageDimsLeft, MaxImageDimsRight) %>%
|
| 142 |
summarise(
|
| 143 |
mean_metric = mean(as.numeric(get(input$metric)), na.rm = TRUE),
|
| 144 |
-
se_metric
|
| 145 |
-
n
|
| 146 |
-
.groups
|
| 147 |
)
|
| 148 |
|
| 149 |
better_dir <- get_better_direction(input$metric)
|
|
@@ -171,29 +203,30 @@ server <- function(input, output) {
|
|
| 171 |
}
|
| 172 |
|
| 173 |
# Compute interpolated grid
|
| 174 |
-
s_ <- akima::interp(
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
| 181 |
# Find optimal point from interpolated grid
|
| 182 |
max_idx <- if (input$compareToBest || better_dir == "max") {
|
| 183 |
which.max(s_$z)
|
| 184 |
} else {
|
| 185 |
which.min(s_$z)
|
| 186 |
}
|
| 187 |
-
|
| 188 |
row_col <- arrayInd(max_idx, .dim = dim(s_$z))
|
| 189 |
optimal_x <- s_$x[row_col[1,1]]
|
| 190 |
optimal_y <- s_$y[row_col[1,2]]
|
| 191 |
optimal_z <- s_$z[row_col[1,1], row_col[1,2]]
|
| 192 |
|
| 193 |
-
list(
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
})
|
| 198 |
|
| 199 |
# Heatmap Output
|
|
@@ -227,79 +260,85 @@ server <- function(input, output) {
|
|
| 227 |
best_single_scale_metric - mean_metric
|
| 228 |
})
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
|
|
|
| 232 |
if (input$compareToBest) {
|
| 233 |
z <- grouped_data$improvement
|
| 234 |
-
|
| 235 |
-
#max_abs <- max(abs(z), na.rm = TRUE)
|
| 236 |
-
#zlim <- if (!is.na(max_abs)) c(-max_abs, max_abs) else NULL
|
| 237 |
-
zlim <- range(z, na.rm = TRUE)
|
| 238 |
} else {
|
| 239 |
z <- grouped_data$mean_metric
|
| 240 |
-
|
| 241 |
-
zlim <- range(z, na.rm = TRUE)
|
| 242 |
}
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
customPalette <- colorRampPalette(c("blue", "white", "red"))(50)
|
| 245 |
-
heatMap(
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
| 257 |
})
|
| 258 |
|
| 259 |
# Contextual Note Output
|
| 260 |
output$contextNote <- renderText({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
if (input$compareToBest) {
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
"• <b>Compare to best single-scale:</b> Toggle showing metric improvement relative to the best single-scale baseline.<br>",
|
| 273 |
-
"• <b>ImageDim1, ImageDim2:</b> Image sizes (e.g., 64×64, 128×128) for multi-scale analysis.<br>",
|
| 274 |
-
"• <b>RATE Ratio:</b> Statistic indicating how well the model detects treatment-effect variation.<br>",
|
| 275 |
-
"• <b>PC:</b> Principal Components; a compression step of neural representations.<br>",
|
| 276 |
-
"• <b>MeanDiff, MeanDiff_pc:</b> Gain in RATE Ratio from multi-scale vs. single-scale, with '_pc' for compressed data.<br>",
|
| 277 |
-
"• <b>RMSE:</b> Root Mean Squared Error, measuring prediction accuracy in simulations.<br>",
|
| 278 |
-
"</div>"
|
| 279 |
-
)
|
| 280 |
} else {
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
"• <b>Metric:</b> The criterion (e.g., RATE Ratio, RMSE) measuring performance or heterogeneity detection.<br>",
|
| 292 |
-
"• <b>Compare to best single-scale:</b> Toggle showing metric improvement relative to the best single-scale baseline.<br>",
|
| 293 |
-
"• <b>ImageDim1, ImageDim2:</b> Image sizes (e.g., 64×64, 128×128) for multi-scale analysis.<br>",
|
| 294 |
-
"• <b>RATE Ratio:</b> Statistic indicating how well the model detects treatment-effect variation.<br>",
|
| 295 |
-
"• <b>PC:</b> Principal Components; a compression step of neural representations.<br>",
|
| 296 |
-
"• <b>MeanDiff, MeanDiff_pc:</b> Gain in RATE Ratio from multi-scale vs. single-scale, with '_pc' for compressed data.<br>",
|
| 297 |
-
"• <b>RMSE:</b> Root Mean Squared Error, measuring prediction accuracy in simulations.<br>",
|
| 298 |
-
"</div>"
|
| 299 |
-
)
|
| 300 |
}
|
| 301 |
})
|
| 302 |
}
|
| 303 |
|
| 304 |
# Run the Shiny App
|
| 305 |
-
shinyApp(ui = ui, server = server)
|
|
|
|
|
|
| 86 |
}
|
| 87 |
}
|
| 88 |
|
| 89 |
+
##############################################################################
|
| 90 |
+
# IMPORTANT: Store the meaningful labels for metric in a named vector.
|
| 91 |
+
# The "name" is what is displayed to the user in the dropdown,
|
| 92 |
+
# while the "value" is the underlying column in the dataset.
|
| 93 |
+
##############################################################################
|
| 94 |
+
metric_choices <- c(
|
| 95 |
+
"Mean AUTOC RATE Ratio" = "AUTOC_rate_std_ratio_mean",
|
| 96 |
+
"Mean AUTOC RATE" = "AUTOC_rate_mean",
|
| 97 |
+
"Mean SD of AUTOC RATE" = "AUTOC_rate_std_mean",
|
| 98 |
+
"Mean AUTOC RATE Ratio with PC" = "AUTOC_rate_std_ratio_mean_pc",
|
| 99 |
+
"Mean AUTOC RATE with PC" = "AUTOC_rate_mean_pc",
|
| 100 |
+
"Mean SD of AUTOC RATE with PC" = "AUTOC_rate_std_mean_pc",
|
| 101 |
+
"Mean Variable Importance (Split 1)" = "MeanVImportHalf1",
|
| 102 |
+
"Mean Variable Importance (Split 2)" = "MeanVImportHalf2",
|
| 103 |
+
"Mean Fraction of Top k Features (Split 1)" = "FracTopkHalf1",
|
| 104 |
+
"Mean RMSE" = "RMSE"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
##############################################################################
|
| 108 |
+
# Helper function to retrieve the *label* from its code
|
| 109 |
+
##############################################################################
|
| 110 |
+
getMetricLabel <- function(metric_value) {
|
| 111 |
+
# This returns, e.g., "Mean AUTOC RATE" if metric_value == "AUTOC_rate_mean".
|
| 112 |
+
# If it doesn't find a match, return the code itself.
|
| 113 |
+
lbl <- names(metric_choices)[which(metric_choices == metric_value)]
|
| 114 |
+
if (length(lbl) == 0) return(metric_value)
|
| 115 |
+
lbl
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
# UI Definition
|
| 119 |
ui <- fluidPage(
|
| 120 |
titlePanel("Multiscale Heatmap Explorer"),
|
|
|
|
| 126 |
selectInput("model", "Model",
|
| 127 |
choices = unique(sm$optimizeImageRep),
|
| 128 |
selected = "clip"),
|
| 129 |
+
|
| 130 |
+
########################################################################
|
| 131 |
+
# Use our named vector 'metric_choices' directly in selectInput
|
| 132 |
+
########################################################################
|
| 133 |
selectInput("metric", "Metric",
|
| 134 |
+
choices = metric_choices,
|
|
|
|
|
|
|
| 135 |
selected = "AUTOC_rate_std_ratio_mean"),
|
| 136 |
+
|
| 137 |
checkboxInput("compareToBest", "Compare to best single scale", value = FALSE)
|
| 138 |
),
|
| 139 |
mainPanel(
|
|
|
|
| 173 |
group_by(MaxImageDimsLeft, MaxImageDimsRight) %>%
|
| 174 |
summarise(
|
| 175 |
mean_metric = mean(as.numeric(get(input$metric)), na.rm = TRUE),
|
| 176 |
+
se_metric = sd(as.numeric(get(input$metric)), na.rm = TRUE) / sqrt(n()),
|
| 177 |
+
n = n(),
|
| 178 |
+
.groups = "drop"
|
| 179 |
)
|
| 180 |
|
| 181 |
better_dir <- get_better_direction(input$metric)
|
|
|
|
| 203 |
}
|
| 204 |
|
| 205 |
# Compute interpolated grid
|
| 206 |
+
s_ <- akima::interp(
|
| 207 |
+
x = x,
|
| 208 |
+
y = y,
|
| 209 |
+
z = z_to_interpolate,
|
| 210 |
+
xo = seq(min(x), max(x), length = 50),
|
| 211 |
+
yo = seq(min(y), max(y), length = 50),
|
| 212 |
+
duplicate = "mean"
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
# Find optimal point from interpolated grid
|
| 216 |
max_idx <- if (input$compareToBest || better_dir == "max") {
|
| 217 |
which.max(s_$z)
|
| 218 |
} else {
|
| 219 |
which.min(s_$z)
|
| 220 |
}
|
|
|
|
| 221 |
row_col <- arrayInd(max_idx, .dim = dim(s_$z))
|
| 222 |
optimal_x <- s_$x[row_col[1,1]]
|
| 223 |
optimal_y <- s_$y[row_col[1,2]]
|
| 224 |
optimal_z <- s_$z[row_col[1,1], row_col[1,2]]
|
| 225 |
|
| 226 |
+
list(
|
| 227 |
+
s_ = s_,
|
| 228 |
+
optimal_point = list(x = optimal_x, y = optimal_y, z = optimal_z)
|
| 229 |
+
)
|
| 230 |
})
|
| 231 |
|
| 232 |
# Heatmap Output
|
|
|
|
| 260 |
best_single_scale_metric - mean_metric
|
| 261 |
})
|
| 262 |
|
| 263 |
+
# Retrieve the *label* for the chosen metric:
|
| 264 |
+
chosen_metric_label <- getMetricLabel(input$metric)
|
| 265 |
+
|
| 266 |
if (input$compareToBest) {
|
| 267 |
z <- grouped_data$improvement
|
| 268 |
+
main_title <- paste(input$application, "-", chosen_metric_label, "improvement over best single scale")
|
|
|
|
|
|
|
|
|
|
| 269 |
} else {
|
| 270 |
z <- grouped_data$mean_metric
|
| 271 |
+
main_title <- paste(input$application, "-", chosen_metric_label)
|
|
|
|
| 272 |
}
|
| 273 |
|
| 274 |
+
x <- grouped_data$MaxImageDimsLeft
|
| 275 |
+
y <- grouped_data$MaxImageDimsRight
|
| 276 |
+
zlim <- range(z, na.rm = TRUE)
|
| 277 |
+
|
| 278 |
customPalette <- colorRampPalette(c("blue", "white", "red"))(50)
|
| 279 |
+
heatMap(
|
| 280 |
+
x = x,
|
| 281 |
+
y = y,
|
| 282 |
+
z = z,
|
| 283 |
+
N = 50,
|
| 284 |
+
main = main_title,
|
| 285 |
+
xlab = "Image Dimension 1",
|
| 286 |
+
ylab = "Image Dimension 2",
|
| 287 |
+
useLog = "xy",
|
| 288 |
+
myCol = customPalette,
|
| 289 |
+
cex.lab = 1.4,
|
| 290 |
+
zlim = zlim,
|
| 291 |
+
optimal_point = interp_data$optimal_point
|
| 292 |
+
)
|
| 293 |
})
|
| 294 |
|
| 295 |
# Contextual Note Output
|
| 296 |
output$contextNote <- renderText({
|
| 297 |
+
SharedContextText <- c(
|
| 298 |
+
"The Peru RCT involves a multifaceted graduation program treatment to reduce poverty outcomes.",
|
| 299 |
+
"The Uganda RCT involves a cash grant program to stimulate human capital and living conditions among the poor.",
|
| 300 |
+
"For more information, see <a href='https://arxiv.org/abs/2411.02134' target='_blank'>https://arxiv.org/abs/2411.02134</a>",
|
| 301 |
+
"<div style='font-size: 10px; line-height: 1.5;'>",
|
| 302 |
+
"<b>Glossary:</b><br>",
|
| 303 |
+
"• <b>Model:</b> The neural-network backbone (e.g., clip-rsicd) transforming satellite images into numerical representations.<br>",
|
| 304 |
+
"• <b>Metric:</b> The criterion (e.g., RATE Ratio, RMSE) measuring performance or heterogeneity detection.<br>",
|
| 305 |
+
"• <b>Compare to best single-scale:</b> Toggle showing metric improvement relative to the best single-scale baseline.<br>",
|
| 306 |
+
"• <b>ImageDim1, ImageDim2:</b> Image sizes (e.g., 64×64, 128×128) for multi-scale analysis.<br>",
|
| 307 |
+
"• <b>RATE Ratio:</b> A t-statistic-like quantity indicating how much a data-model combination captures treatment-effect variation. Ratio of the RATE and its standard error. It can employ two weighting scemes (AUTOC and Qini).<br>",
|
| 308 |
+
"• <b>PC:</b> Principal Components; a compression step of neural representations.<br>",
|
| 309 |
+
"• <b>MeanDiff, MeanDiff_pc:</b> Gain in RATE Ratio from multi-scale vs. single-scale, with '_pc' for compressed data.<br>",
|
| 310 |
+
"• <b>RMSE:</b> Root Mean Squared Error, measuring prediction accuracy in simulations.<br>",
|
| 311 |
+
"</div>"
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
chosen_metric_label <- getMetricLabel(input$metric)
|
| 315 |
+
|
| 316 |
if (input$compareToBest) {
|
| 317 |
+
c(
|
| 318 |
+
paste(
|
| 319 |
+
"This heatmap shows the improvement in",
|
| 320 |
+
paste0("'", chosen_metric_label, "'"),
|
| 321 |
+
"over the best single scale for",
|
| 322 |
+
input$application,
|
| 323 |
+
"using the", input$model, "model. The green star marks the optimal point."
|
| 324 |
+
),
|
| 325 |
+
SharedContextText
|
| 326 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
} else {
|
| 328 |
+
c(
|
| 329 |
+
paste(
|
| 330 |
+
"This heatmap displays",
|
| 331 |
+
paste0("'", chosen_metric_label, "'"),
|
| 332 |
+
"for", input$application,
|
| 333 |
+
"using the", input$model,
|
| 334 |
+
"model across different image dimension combinations. The green star marks the optimal point."
|
| 335 |
+
),
|
| 336 |
+
SharedContextText
|
| 337 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
}
|
| 339 |
})
|
| 340 |
}
|
| 341 |
|
| 342 |
# Run the Shiny App
|
| 343 |
+
shinyApp(ui = ui, server = server)
|
| 344 |
+
|