152 lines
5.9 KiB
R
152 lines
5.9 KiB
R
# ============================================================================
|
|
# VIEW DAY-BY-DAY ALERTS FOR SPECIFIC HARVEST EVENT
|
|
# Shows what alerts would trigger each day around harvest
|
|
# ============================================================================
|
|
|
|
library(dplyr)
|
|
library(here)
|
|
|
|
# Load validation results
|
|
results_file <- here("r_app/experiments/harvest_prediction/operational_validation_results.rds")
|
|
results <- readRDS(results_file)
|
|
|
|
all_results <- results$all_results
|
|
|
|
# ============================================================================
|
|
# SELECT A HARVEST EVENT TO EXAMINE
|
|
# ============================================================================
|
|
|
|
# Let's look at Field 00110, Harvest Event 2 (2022-01-06)
|
|
# This had good Stage 1 prediction
|
|
|
|
field_to_examine <- "00110"
|
|
harvest_event_num <- 2
|
|
|
|
cat("============================================================================\n")
|
|
cat("DAY-BY-DAY ALERT SIMULATION\n")
|
|
cat("============================================================================\n\n")
|
|
|
|
event_data <- all_results %>%
|
|
filter(field == field_to_examine, harvest_event == harvest_event_num) %>%
|
|
arrange(test_date)
|
|
|
|
if (nrow(event_data) > 0) {
|
|
harvest_date <- unique(event_data$harvest_date)[1]
|
|
|
|
cat("Field:", field_to_examine, "\n")
|
|
cat("Harvest Event:", harvest_event_num, "\n")
|
|
cat("Actual Harvest Date:", format(harvest_date, "%Y-%m-%d"), "\n\n")
|
|
|
|
cat("Simulating daily script runs from", format(min(event_data$test_date), "%Y-%m-%d"),
|
|
"to", format(max(event_data$test_date), "%Y-%m-%d"), "\n\n")
|
|
|
|
cat("============================================================================\n")
|
|
cat("DAILY ALERTS TABLE\n")
|
|
cat("============================================================================\n\n")
|
|
|
|
cat("Note: Day 0 = actual harvest date\n")
|
|
cat(" Day +1 = first day you'd see harvest in satellite images\n\n")
|
|
|
|
# Create readable table
|
|
display_table <- event_data %>%
|
|
mutate(
|
|
days_label = case_when(
|
|
days_from_harvest < 0 ~ paste0(days_from_harvest, " days before"),
|
|
days_from_harvest == 0 ~ "HARVEST DAY",
|
|
days_from_harvest > 0 ~ paste0("+", days_from_harvest, " days after")
|
|
),
|
|
stage1_status = ifelse(stage1_alert,
|
|
paste0("✓ ", stage1_level),
|
|
"—"),
|
|
stage2_status = ifelse(stage2_alert,
|
|
paste0("✓ ", stage2_level),
|
|
"—")
|
|
) %>%
|
|
select(
|
|
Date = test_date,
|
|
`Days from Harvest` = days_label,
|
|
`7d Rolling CI` = rolling_ci,
|
|
`Consecutive Low Days` = consecutive_days,
|
|
`Stage 1 Alert` = stage1_status,
|
|
`CI Drop` = ci_drop,
|
|
`Stage 2 Alert` = stage2_status
|
|
)
|
|
|
|
print(display_table, n = Inf, row.names = FALSE)
|
|
|
|
cat("\n============================================================================\n")
|
|
cat("INTERPRETATION\n")
|
|
cat("============================================================================\n\n")
|
|
|
|
# Find first Stage 1 alert
|
|
first_stage1 <- event_data %>%
|
|
filter(stage1_alert == TRUE) %>%
|
|
slice(1)
|
|
|
|
if (nrow(first_stage1) > 0) {
|
|
cat("STAGE 1 - First Alert:\n")
|
|
cat(" Date:", format(first_stage1$test_date, "%Y-%m-%d"), "\n")
|
|
cat(" Days before harvest:", abs(first_stage1$days_from_harvest), "\n")
|
|
cat(" Alert level:", first_stage1$stage1_level, "\n")
|
|
cat(" Rolling avg CI:", round(first_stage1$rolling_ci, 2), "\n\n")
|
|
} else {
|
|
cat("STAGE 1: No advance warning detected\n\n")
|
|
}
|
|
|
|
# Find first Stage 2 alert
|
|
first_stage2 <- event_data %>%
|
|
filter(stage2_alert == TRUE, days_from_harvest >= 0) %>%
|
|
slice(1)
|
|
|
|
if (nrow(first_stage2) > 0) {
|
|
cat("STAGE 2 - First Detection:\n")
|
|
cat(" Date:", format(first_stage2$test_date, "%Y-%m-%d"), "\n")
|
|
cat(" Days after harvest:", first_stage2$days_from_harvest, "\n")
|
|
cat(" Detection level:", first_stage2$stage2_level, "\n")
|
|
cat(" CI drop:", round(first_stage2$ci_drop, 2), "\n\n")
|
|
|
|
if (first_stage2$days_from_harvest == 1) {
|
|
cat(" ✓ EXCELLENT: Detected 1 day after (seeing yesterday's images)\n\n")
|
|
} else if (first_stage2$days_from_harvest <= 3) {
|
|
cat(" ✓ GOOD: Detected within 3 days (operational target)\n\n")
|
|
}
|
|
} else {
|
|
cat("STAGE 2: No harvest confirmation detected\n\n")
|
|
}
|
|
|
|
} else {
|
|
cat("No data found for this field/harvest event\n")
|
|
}
|
|
|
|
cat("============================================================================\n\n")
|
|
|
|
# Now show multiple harvest events for comparison
|
|
cat("============================================================================\n")
|
|
cat("SUMMARY: ALL HARVEST EVENTS FOR FIELD", field_to_examine, "\n")
|
|
cat("============================================================================\n\n")
|
|
|
|
all_events_summary <- all_results %>%
|
|
filter(field == field_to_examine) %>%
|
|
group_by(harvest_event, harvest_date) %>%
|
|
summarise(
|
|
first_stage1_date = min(test_date[stage1_alert == TRUE], na.rm = TRUE),
|
|
first_stage1_days = min(days_from_harvest[stage1_alert == TRUE], na.rm = TRUE),
|
|
first_stage2_date = min(test_date[stage2_alert == TRUE & days_from_harvest >= 0], na.rm = TRUE),
|
|
first_stage2_days = min(days_from_harvest[stage2_alert == TRUE & days_from_harvest >= 0], na.rm = TRUE),
|
|
.groups = "drop"
|
|
) %>%
|
|
mutate(
|
|
stage1_result = ifelse(is.finite(first_stage1_days),
|
|
paste0(abs(first_stage1_days), " days before"),
|
|
"Not detected"),
|
|
stage2_result = ifelse(is.finite(first_stage2_days),
|
|
paste0(first_stage2_days, " days after"),
|
|
"Not detected")
|
|
)
|
|
|
|
print(all_events_summary, n = Inf)
|
|
|
|
cat("\n============================================================================\n")
|
|
cat("This shows exactly what alerts would fire if you ran the script daily!\n")
|
|
cat("============================================================================\n")
|