library(ggplot2)

# Usage: Rscript single_scatter.r exp_abspath

# TODO: Allow filtering resulttypes (or at least exclude OK_MARKER)

args <- commandArgs(trailingOnly = TRUE)
experiment <- args[1]
faults_file <- if (length(args) >= 2) args[2] else "faults.csv"
suffix <- gsub("^faults|\\.csv$", "", faults_file)
datafile <- file.path(experiment, faults_file)

if (!file.exists(datafile)) {
  print(paste("Input file", datafile, "is missing"))
  stop()
}

data <- readr::read_csv(datafile)
data$fault_address <- strtoi(data$fault_address)
tibble::glimpse(data)

plot <- ggplot(data, aes(x = fault_address, y = faults)) +
  geom_point(aes(color = resulttype)) +
  scale_x_continuous(
    labels = function(x) sprintf("0x%X", as.integer(x))
  ) +
  scale_y_log10() +
  labs(x = "Address", y = "Faults", color = "Type") +
  theme_minimal()

ggsave(
  paste0(experiment, "/scatter", suffix, ".svg"),
  plot = plot,
)
