# Report figures from the results tables (analysis/results.py): the same numbers as the dashboard, drawn with ggplot2.
# Nothing is computed here: every value comes from results/*.parquet; names and colours from config/.
#
#   Rscript report/plots.R                              # the latest session per setup and firmware -> report/figures/all/
#   Rscript report/plots.R b1413,b1436                  # only these firmwares -> report/figures/b1413-b1436/
#   Rscript report/plots.R b1413,b1436 --channels intan # each Intan's median channel (0-63, 64-127) -> *-intan.*
#
# Writes overview.csv (the numbers, as on the dashboard), messages-shared and messages-free (the averages around each
# message: one y scale per message, or each panel its own), spectrum (.pdf and .png).
# Packages: nanoparquet, ggplot2, dplyr, tidyr, jsonlite.
suppressPackageStartupMessages({
  library(nanoparquet); library(ggplot2); library(dplyr); library(jsonlite)
})

here <- function() {                          # gate-bench/, wherever the script is run from
  f <- sub("--file=", "", grep("--file=", commandArgs(FALSE), value = TRUE))
  if (length(f)) normalizePath(file.path(dirname(f), "..")) else getwd()
}
root <- here()
args <- commandArgs(TRUE)
per_intan <- "intan" %in% args[which(args == "--channels") + 1]
args <- args[!args %in% c("--channels", "intan", "all")]
groups <- if (per_intan) c("intan1", "intan2") else "all"           # channel groups (config/channels.json)
suffix <- if (per_intan) "-intan" else ""
# one folder per firmware selection, so that several can sit side by side: report/figures/b1436-b1441/, .../all/
out <- file.path(root, "report", "figures", if (length(args)) gsub(",", "-", args[1]) else "all")
dir.create(out, showWarnings = FALSE, recursive = TRUE)


# ---- the registries: firmware names and fixed colours, setup order
registry <- function(name) { x <- fromJSON(file.path(root, "config", paste0(name, ".json"))); x[!startsWith(names(x), "_")] }
firmwares <- registry("firmwares"); palette <- fromJSON(file.path(root, "config", "palette.json"))$light
fw_label <- sapply(names(firmwares), function(b) sprintf("%s (%s)", firmwares[[b]]$name, b))
fw_colour <- setNames(sapply(names(firmwares), function(b) palette[firmwares[[b]]$colour + 1]), fw_label)
setup_order <- names(registry("setups"))
channels <- registry("channels"); group_label <- sapply(channels, `[[`, "short")

# ---- the results
sessions <- read_parquet(file.path(root, "results", "sessions.parquet")) |>
  filter(is.na(excluded), latest)                     # per setup and firmware, the latest session (results.py)
if (length(args)) sessions <- filter(sessions, firmware %in% strsplit(args[1], ",")[[1]])
label <- function(d) d |>
  inner_join(select(sessions, session, setup, firmware), by = intersect(c("session", "setup", "firmware"), names(d))) |>
  mutate(firmware = factor(fw_label[firmware], levels = fw_label), setup = factor(setup, levels = setup_order))
measures <- read_parquet(file.path(root, "results", "measures.parquet")) |> label() |> filter(channels %in% groups) |>
  mutate(channels = factor(group_label[channels], levels = group_label[groups]))
curves <- read_parquet(file.path(root, "results", "curves.parquet")) |> label() |> filter(channels %in% groups) |>
  mutate(channels = factor(group_label[channels], levels = group_label[groups]))

theme_set(theme_minimal(base_size = 10) + theme(panel.grid.minor = element_blank(), legend.position = "bottom",
                                                strip.text = element_text(face = "bold", hjust = 0)))
save <- function(p, name, w, h) {
  name <- paste0(name, suffix)
  p <- p + guides(colour = guide_legend(ncol = 1))                 # firmware names are long: one per line fits any width
  ggsave(file.path(out, paste0(name, ".pdf")), p, width = w, height = h, device = cairo_pdf)   # cairo: µ, √
  ggsave(file.path(out, paste0(name, ".png")), p, width = w, height = h, dpi = 200)
  cat(file.path(basename(out), name), ".pdf, .png\n", sep = "")
}

# ---- 1. overview, as a table (as on the dashboard): per quantity (and Intan), per setup and firmware the value and its
#      floor (what noise alone reads; a peak within ±floor is not distinguishable from noise), and per setup the ratio
#      of magnitudes of each firmware to the first one ("<" when the compared value is within its floor: a bound)
quantities <- c(vrect_peak = "Vrect message", vrect_level = "Vrect message: level", burst_peak = "1 s burst",
                temp_peak = "Temperature message", keepalive = "7 s keep-alive", line_976 = "976 Hz line",
                line_122 = "122 Hz line", noise_channel = "Noise, per channel", noise_median = "Noise, median channel",
                mains = "Mains, 50 Hz (RMS)")
ov <- measures |> filter(quantity %in% names(quantities)) |> droplevels() |>
  mutate(quantity = factor(quantity, levels = names(quantities))) |> arrange(quantity, channels, setup, firmware)
reference <- levels(ov$firmware)[1]
ratios <- ov |> group_by(quantity, channels, setup) |>
  mutate(ref = value[firmware == reference][1], ref_floor = floor[firmware == reference][1]) |> ungroup() |>
  filter(firmware != reference, quantity != "vrect_level") |>
  mutate(ratio = case_when(is.na(value) | is.na(ref) | ref == 0 | (!is.na(ref_floor) & abs(ref) < ref_floor) ~ NA_character_,
                           !is.na(floor) & abs(value) < floor ~ paste0("<", signif(floor / abs(ref), 2)),
                           TRUE ~ as.character(signif(abs(value / ref), 2))),     # magnitudes
         column = paste0(setup, ": ", firmware, " / ", reference)) |> select(quantity, channels, column, ratio)
wide <- ov |> mutate(column = paste0(setup, ": ", firmware)) |>
  select(quantity, channels, unit, column, value, floor) |> mutate(value = signif(value, 4), floor = signif(floor, 3)) |>
  tidyr::pivot_wider(names_from = column, values_from = c(value, floor), names_glue = "{column} ({.value})") |>
  left_join(tidyr::pivot_wider(ratios, names_from = column, values_from = ratio), by = c("quantity", "channels")) |>
  mutate(quantity = quantities[as.character(quantity)])
file <- file.path(out, paste0("overview", suffix, ".csv"))
write.csv(wide |> select(where(~ !all(is.na(.x)))), file, row.names = FALSE, na = "")
cat(file.path(basename(out), basename(file)), "\n", sep = "")

# ---- 2. the average around each message, -10..+10 ms: rows = message, columns = setup (setup and Intan), colour =
#      firmware, one line per session. Two versions: one y scale per message shared across the columns (-shared, for
#      comparing setups) and each panel on its own scale (-free, for the shapes).
kinds <- c(vrect = "Vrect message", burst = "1 s burst", temp = "Temperature message", keepalive = "7 s keep-alive")
mc <- curves |> filter(curve %in% names(kinds), x >= -10, x <= 10) |>
  mutate(kind = factor(kinds[curve], levels = kinds),
         column = if (per_intan) factor(paste(setup, channels, sep = " · "), levels = as.vector(t(outer(setup_order, group_label[groups], paste, sep = " · ")))) else setup)
messages <- function(facets) ggplot(mc, aes(x, y, colour = firmware, group = session)) +
  geom_hline(yintercept = 0, colour = "grey85", linewidth = 0.3) +
  geom_vline(xintercept = 0, colour = "grey40", linetype = "dashed", linewidth = 0.3) +
  geom_line(linewidth = 0.6, alpha = 0.9) +
  scale_colour_manual(values = fw_colour, drop = TRUE) + scale_x_continuous(breaks = seq(-10, 10, 5)) + facets +
  labs(x = "ms from the notification (keep-alive: from its peak)", y = "µV, median channel", colour = NULL)
columns <- nlevels(droplevels(mc$column))
save(messages(facet_grid(kind ~ column, scales = "free_y")), "messages-shared", 2.2 * columns + 1.5, 9)
save(messages(facet_wrap(vars(kind, column), ncol = columns, scales = "free_y", labeller = label_value)),
     "messages-free", 2.2 * columns + 1.5, 9.5)

# ---- 3. the noise spectrum, one panel per setup (per setup and Intan)
sp <- curves |> filter(curve == "spectrum", x > 0, y > 0)
p <- ggplot(sp, aes(x, y, colour = firmware, group = session)) + geom_line(linewidth = 0.35, alpha = 0.9) +
  scale_x_log10() + scale_y_log10() + scale_colour_manual(values = fw_colour, drop = TRUE) +
  facet_grid(setup ~ channels) + labs(x = "Hz", y = "µV/√Hz, median channel", colour = NULL)
save(p, "spectrum", 3.5 * length(groups) + 1.5, 6)
