Skip to content

Commit

Permalink
* R/plotAblation.R: do not create an empty Rplots.pdf file.
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez committed Sep 20, 2024
1 parent cbd324b commit 67db27c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 55 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ import(stats)
import(utils)
import(withr)
importFrom(R6,R6Class)
importFrom(grDevices,dev.off)
importFrom(grDevices,pdf)
importFrom(graphics,abline)
importFrom(graphics,axis)
importFrom(graphics,boxplot)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
* `plotAblation()` has several new options:
- `type='rank'` to plot ranks per instance instead of raw cost values.
- `n` to limit the number of parameters shown in the plot.
- `width` replaces `pdf.width`.
- `height` sets the height of the plot in the PDF file.

* The previously internal function `check.output.target.runner` is renamed to
`check_output_target_runner` and exported to allow users who write their own
Expand Down Expand Up @@ -167,6 +169,8 @@

## Fixes

* `ablation_cmdline()` and `plotAblation()` no longer create an empty `Rplots.pdf` file when specifying an output PDF file.

* Fix #66: when using `maxTime > 0`, irace estimates the time per run by
executing 2 configurations on `firstTest` instances and adjusts `boundMax`
to not go over `budgetEstimation`. This may result in a smaller `boundMax`
Expand Down
89 changes: 45 additions & 44 deletions R/ablation.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ ablation_cmdline <- function(argv = commandArgs(trailingOnly = TRUE))
params[ablation_params],
scenario))
if (!is.null(params[["plot"]]) || base::interactive()) {
plotAblation(ablog, pdf.file = params[["plot"]], type = params$plot_type)
plotAblation(ablog, pdf_file = params[["plot"]], type = params$plot_type)
}
invisible(ablog)
}
Expand Down Expand Up @@ -517,14 +517,15 @@ ablation_labels <- function(trajectory, configurations)
#'
#' @param ablog (`list()`|`character(1)`) Ablation log object returned by [ablation()]. Alternatively, the path to an `.Rdata` file, e.g., `"log-ablation.Rdata"`, from which the object will be loaded.

#' @param pdf.file Output filename.
#' @param pdf.width Width provided to create the pdf file.
#' @param pdf_file Output filename.
#' @param width Width provided to create the PDF file.
#' @param height Height provided to create the PDF file.
#' @param type Type of plot. Supported values are `"mean"` and `"boxplot"`. Adding `"rank"` will plot rank per instance instead of raw cost value.
#' @param n (`integer(1)`) Number of parameters included in the plot. By default all parameters are included.
#' @param n `integer(1)`\cr Number of parameters included in the plot. By default all parameters are included.
#' @param mar Vector with the margins for the ablation plot.
#' @param ylab Label of y-axis.
#' @param ylim Numeric vector of length 2 giving the y-axis range.
#' @param rename_labels `c()`\cr Renaming table for nicer labels. For example, `c("No value"="NA", "LongParameterName"="LPN").
#' @param rename_labels `character()`\cr Renaming table for nicer labels. For example, `c("No value"="NA", "LongParameterName"="LPN")`.
#' @param ... Further graphical parameters may also be supplied as
#' arguments. See [graphics::plot.default()].
#'
Expand All @@ -534,12 +535,14 @@ ablation_labels <- function(trajectory, configurations)
#' logfile <- file.path(system.file(package="irace"), "exdata", "log-ablation.Rdata")
#' plotAblation(ablog = logfile)
#' plotAblation(ablog = logfile, type = "mean")
#' plotAblation(ablog = logfile, type = c("rank","boxplot"))
#' plotAblation(ablog = logfile, type = c("rank","boxplot"), rename_labels = c(
#' "localsearch"="ls", algorithm="algo", source="default"))
#' @concept ablation
#' @export
plotAblation <- function (ablog, pdf.file = NULL, pdf.width = 20,
plotAblation <- function (ablog, pdf_file = NULL, width = 20,
height = 7,
type = c("mean", "boxplot", "rank"), n = 0L,
mar = par("mar"),
mar = NULL,
ylab = "Mean configuration cost", ylim = NULL,
rename_labels = NULL,
...)
Expand All @@ -556,59 +559,57 @@ plotAblation <- function (ablog, pdf.file = NULL, pdf.width = 20,
stop("The ablog shows that the ablation procedure did not complete cleanly and only contains partial information")


if (!is.null(pdf.file)) {
if (!is.file.extension(pdf.file, ".pdf"))
pdf.file <- paste0(pdf.file, ".pdf")
cat("Creating PDF file '", pdf.file, "'\n", sep="")
pdf(file = pdf.file, width = pdf.width,
title = paste0("Ablation plot: ", pdf.file))
on.exit(dev.off(), add = TRUE)
if (!is.null(pdf_file)) {
if (!is.file.extension(pdf_file, ".pdf"))
pdf_file <- paste0(pdf_file, ".pdf")
cat("Creating PDF file '", pdf_file, "'\n", sep="")
local_cairo_pdf(pdf_file, width = width, height = height, onefile= TRUE)
}

trajectory <- ablog$trajectory
if (n > 0) trajectory <- trajectory[seq_len(n+1L)]

configurations <- ablog$configurations
trajectory <- ablog$trajectory
if (n > 0L) trajectory <- trajectory[seq_len(n+1L)]
# Generate labels
labels <- ablation_labels(trajectory, configurations)
if (!is.null(rename_labels))
# stringr::str_replace_all() would be better but it has so many dependencies!
for (i in seq_along(rename_labels))
labels <- gsub(names(rename_labels)[i], rename_labels[i], labels)

inches_to_lines <- (par("mar") / par("mai"))[1L]
lab.width <- max(strwidth(labels, units = "inches")) * inches_to_lines
old.par <- par(mar = c(lab.width + 2.1, 4.1, 0.1, 0.1), cex.axis = 1)
if (!is.null(pdf.file))
on.exit(par(old.par), add = TRUE)

experiments <- ablog$experiments
if ("rank" %in% type) {
experiments <- rowRanks(experiments, ties.method = "average")
if (is.null(ylim)) ylim <- c(1L, ncol(experiments))
}
# FIXME: We could also show the other alternatives at each step not just the
# one selected. See Leonardo Bezerra's thesis.
if ("boxplot" %in% type) {
bx <- boxplot(experiments[, trajectory], plot=FALSE)
if (is.null(ylim)) {
ylim <- range(bx$stats[is.finite(bx$stats)],
bx$out[is.finite(bx$out)],
bx$conf[is.finite(bx$conf)])
costs_avg <- colMeans2(experiments, cols = trajectory)

if (is.null(mar))
mar <- par("mar")
inches_to_lines <- (mar / par("mai"))[1L]
lab_width <- max(strwidth(labels, units = "inches")) * inches_to_lines
with_par(list(mar = c(lab_width + 2.1, 4.1, 0.1, 0.1), cex.axis = 1), {
# FIXME: We could also show the other alternatives at each step not just the
# one selected. See Leonardo Bezerra's thesis.
if ("boxplot" %in% type) {
bx <- boxplot(experiments[, trajectory], plot=FALSE)
if (is.null(ylim)) {
ylim <- range(bx$stats[is.finite(bx$stats)],
bx$out[is.finite(bx$out)],
bx$conf[is.finite(bx$conf)])
}
}
}
costs.avg <- colMeans2(experiments, cols = trajectory)

plot(costs.avg, xaxt = "n", xlab = NA, ylab = ylab, ylim = ylim,
type = "b", pch = 19, ...,
panel.first = {
grid(nx = NA, ny = NULL, lwd = 2);
abline(h = c(costs.avg[1], tail(costs.avg, n = 1)),
col = "lightgray", lty = "dotted", lwd = 2) })
axis(1, at = seq_along(costs.avg), labels = labels, las = 3)
if ("boxplot" %in% type) {
bxp(bx, show.names = FALSE, add = TRUE)
}
plot(costs_avg, xaxt = "n", xlab = NA, ylab = ylab, ylim = ylim,
type = "b", pch = 19, ...,
panel.first = {
grid(nx = NA, ny = NULL, lwd = 2);
abline(h = c(costs_avg[1], tail(costs_avg, n = 1)),
col = "lightgray", lty = "dotted", lwd = 2) })
axis(1, at = seq_along(costs_avg), labels = labels, las = 3)
if ("boxplot" %in% type) {
bxp(bx, show.names = FALSE, add = TRUE)
}
})
invisible()
}

Expand Down
1 change: 0 additions & 1 deletion R/irace-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#' @name irace-package
#' @import stats utils compiler matrixStats withr data.table
#' @importFrom R6 R6Class
#' @importFrom grDevices dev.off pdf
#' @importFrom graphics abline axis boxplot par plot points strwidth bxp grid
#' @importFrom spacefillr generate_sobol_set
#'
Expand Down
20 changes: 12 additions & 8 deletions man/plotAblation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 67db27c

Please sign in to comment.