## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  fig.width = 7,
  fig.height = 4.8
)

benchmark_dir <- system.file("benchmarks", package = "stepwedgepower")
if (!nzchar(benchmark_dir)) {
  candidates <- c(
    file.path("..", "inst", "benchmarks"),
    file.path("inst", "benchmarks")
  )
  benchmark_dir <- candidates[dir.exists(candidates)][1L]
}
benchmark_dir <- normalizePath(benchmark_dir, mustWork = TRUE)
old_benchmark_options <- options(stepwedgepower.benchmark_dir = benchmark_dir)

for (file in c(
  "benchmark_helpers.R",
  "table2_software_comparison.R",
  "figure3_calendar_delay.R",
  "table3_separate_time_grid.R",
  "figure5_limited_clusters.R"
)) {
  source(file.path(benchmark_dir, file), local = knitr::knit_global())
}

run_external <- tolower(Sys.getenv(
  "STEPWEDGEPOWER_RUN_EXTERNAL",
  unset = "false"
)) %in% c("1", "true", "yes", "on")

benchmark_output <- file.path(
  tempdir(), "stepwedgepower-benchmark-vignette"
)

## ----manifest-----------------------------------------------------------------
manifest <- .sp_bench_read_reference(
  "benchmark_manifest.csv", benchmark_dir
)
knitr::kable(manifest)

## ----reference-environment----------------------------------------------------
reference_environment <- .sp_bench_read_reference(
  "reference_environment.csv", benchmark_dir
)
knitr::kable(reference_environment)

## ----discrepancies------------------------------------------------------------
source_notes <- .sp_bench_read_reference(
  "source_discrepancies.csv", benchmark_dir
)
knitr::kable(source_notes)

## ----table2-------------------------------------------------------------------
table2 <- run_table2_benchmark(
  output_dir = benchmark_output,
  benchmark_dir = benchmark_dir,
  run_external = run_external,
  strict = FALSE
)

table2_display <- table2$validation[, c(
  "scenario", "method", "reference_power", "reproduced_power",
  "absolute_difference", "status", "package_version", "pass"
)]
knitr::kable(table2_display, digits = 3)

## ----figure3-results----------------------------------------------------------
figure3 <- run_figure3_benchmark(
  output_dir = benchmark_output,
  benchmark_dir = benchmark_dir,
  strict = TRUE,
  make_plot = FALSE
)

knitr::kable(
  figure3$results[, c("design", "delay", "variance", "power")],
  digits = 4
)

## ----figure3-plot-------------------------------------------------------------
.sp_bench_plot_figure3(figure3$results)

## ----figure3-endpoints--------------------------------------------------------
knitr::kable(figure3$endpoint_check, digits = 12)

## ----table3-------------------------------------------------------------------
table3 <- run_table3_benchmark(
  output_dir = benchmark_output,
  benchmark_dir = benchmark_dir,
  run_external = run_external,
  strict = FALSE
)

table3_display <- table3$validation[, c(
  "design", "p0", "p1", "icc", "reference_power",
  "reproduced_power", "absolute_difference", "status",
  "package_version", "pass"
)]
knitr::kable(table3_display, digits = 3)

## ----figure5-results----------------------------------------------------------
figure5 <- run_figure5_benchmark(
  output_dir = benchmark_output,
  benchmark_dir = benchmark_dir,
  strict = TRUE,
  make_plot = FALSE
)

knitr::kable(figure5$crossing_check, digits = 12)

## ----figure5-matrix-check-----------------------------------------------------
figure5_matrix_differences <- figure5$validation[
  figure5$validation$matrix_check_applicable,
  c(
    "classic_matrix_absolute_difference",
    "one_unit_delay_matrix_absolute_difference",
    "two_unit_delay_matrix_absolute_difference"
  ),
  drop = FALSE
]
knitr::kable(
  data.frame(
    check = names(figure5_matrix_differences),
    maximum_absolute_difference = vapply(
      figure5_matrix_differences,
      max,
      numeric(1),
      na.rm = TRUE
    )
  ),
  digits = 12
)

## ----figure5-plot-------------------------------------------------------------
.sp_bench_plot_figure5(figure5$results)

## ----cleanup, include=FALSE---------------------------------------------------
options(old_benchmark_options)

