Debian Codegraph @

R/performance_plots.R

raw
1 ## ----------------------------------------------------------------------------
2 ## plot method for objects of class 'performance'
3 ## ----------------------------------------------------------------------------
4
5 #' @importFrom graphics plot.default plot.xy par
6 .get.arglist <- function( fname, arglist ) {
7 if (fname=='plot')
8 return(.select.args(arglist,
9 union(names(formals(graphics::plot.default)),
10 names(graphics::par()))))
11 else if (fname=='plot.xy')
12 return(.select.args(arglist,
13 union( names(formals(graphics::plot.xy)),
14 names(graphics::par()))))
15 else return( .select.prefix( arglist, fname) )
16 }
17
18 .downsample <- function( perf, downsampling ) {
19 for (i in 1:length([email protected])) {
20 if (downsampling < 1 && downsampling > 0)
21 ind <- round(seq(1, length([email protected][[i]]),
22 length=(length([email protected][[i]]) *
23 downsampling)))
24 else if (downsampling > 1)
25 ind <- round(seq(1, length([email protected][[i]]),
26 length=downsampling))
27 else ind <- 1:length([email protected][[i]])
28 [email protected][[i]] <- [email protected][[i]][ind]
29 [email protected][[i]] <- [email protected][[i]][ind]
30 [email protected][[i]] <- [email protected][[i]][ind]
31 }
32 return(perf)
33 }
34
35 .check_performance_for_plotting <- function(perf, colorize, print.cutoffs.at,
36 avg){
37 if (length([email protected]) != length([email protected])) {
38 stop("Performance object cannot be plotted. Length of x and y values ",
39 "does not match.",
40 call. = FALSE)
41 }
42 if ((is.null([email protected]) || length([email protected]) == 0L) &&
43 (colorize==TRUE || length(print.cutoffs.at) > 0L)) {
44 stop("Threshold coloring or labeling cannot be performed: ",
45 "performance object has no threshold information.",
46 call. = FALSE)
47 }
48 if ((avg=="vertical" || avg=="horizontal") &&
49 (colorize==TRUE || length(print.cutoffs.at) > 0L)) {
50 stop("Threshold coloring or labeling is only well-defined for",
51 "'no' or 'threshold' averaging.",
52 call. = FALSE)
53 }
54 }
55
56 #' @importFrom grDevices rainbow
57 .plot.performance <-
58 function(perf,
59 ...,
60 avg = "none",
61 spread.estimate = "none",
62 spread.scale = 1,
63 show.spread.at = c(),
64 colorize = FALSE,
65 colorize.palette = rev(grDevices::rainbow(256, start = 0, end = 4 / 6)),
66 colorkey = colorize,
67 colorkey.relwidth = 0.25,
68 colorkey.pos = "right",
69 print.cutoffs.at = c(),
70 cutoff.label.function = function(x) {
71 round(x, 2)
72 },
73 downsampling = 0,
74 add = FALSE) {
75 # Input checks
76 .check_performance_for_plotting(perf, colorize, print.cutoffs.at, avg)
77 # getting the arguments
78 arglist <- c(lapply(as.list(environment()), eval ), list(...) )
79
80 if (downsampling >0 ) perf <- .downsample( perf, downsampling)
81
82 ## for infinite cutoff, assign maximal finite cutoff + mean difference
83 ## between adjacent cutoff pairs
84 if (length([email protected]) != 0) {
85 FUN <- function(x) {
86 isfin <- is.finite(x)
87 # if only one finite is available the mean cannot be calculated without
88 # the first/last value, since the leaves no value
89 if(sum(isfin) > 1L){
90 inf_replace <- max(x[isfin]) +
91 mean(abs(x[isfin][-1] - x[isfin][-length(x[isfin])]))
92 } else {
93 inf_replace <- 0
94 }
95 x[is.infinite(x)] <- inf_replace
96 x
97 }
98 [email protected] <- lapply([email protected],FUN)
99 }
100 ## remove samples with x or y not finite
101 for (i in 1:length([email protected])) {
102 ind.bool <- (is.finite([email protected][[i]]) &
103 is.finite([email protected][[i]]))
104
105 if (length([email protected])>0)
106 [email protected][[i]] <- [email protected][[i]][ind.bool]
107
108 [email protected][[i]] <- [email protected][[i]][ind.bool]
109 [email protected][[i]] <- [email protected][[i]][ind.bool]
110 }
111 arglist <- .sarg( arglist, perf=perf)
112
113 if (add==FALSE) do.call( ".performance.plot.canvas", arglist )
114
115 if (avg=="none") do.call(".performance.plot.no.avg", arglist)
116 else if (avg=="vertical")
117 do.call(".performance.plot.vertical.avg", arglist)
118 else if (avg=="horizontal")
119 do.call(".performance.plot.horizontal.avg", arglist)
120 else if (avg=="threshold")
121 do.call(".performance.plot.threshold.avg", arglist)
122 }
123
124 ## ---------------------------------------------------------------------------
125 ## initializing plots and plotting a canvas
126 ## (can be skipped using 'plot( ..., add=TRUE)'
127 ## ---------------------------------------------------------------------------
128
129 #' @import stats
130 #' @import graphics
131 .performance.plot.canvas <- function(perf, avg, ...) {
132
133 # requireNamespace("stats")
134 # requireNamespace("graphics")
135
136 arglist <- list(...)
137
138 axis.names <- list([email protected], [email protected])
139 if (avg=="horizontal" || avg=="threshold")
140 axis.names$x <- paste("Average", tolower(axis.names$x))
141 if (avg=="vertical" || avg=="threshold")
142 axis.names$y <- paste("Average", tolower(axis.names$y))
143 arglist <- .farg(arglist, xlab=axis.names$x, ylab=axis.names$y)
144
145 arglist <-
146 .farg(arglist,
147 xlim=c(min(unlist([email protected])), max(unlist([email protected]))),
148 ylim=c(min(unlist([email protected])), max(unlist([email protected]))))
149
150 do.call("plot", .sarg(.slice.run(.get.arglist('plot', arglist)),
151 x=0.5, y=0.5, type='n', axes=FALSE))
152 do.call( "axis", .sarg(.slice.run(.get.arglist('xaxis', arglist)),
153 side=1))
154 do.call( "axis", .sarg(.slice.run(.get.arglist('yaxis', arglist)),
155 side=2))
156
157 if (.garg(arglist,'colorkey')==TRUE) {
158 colors <- rev( .garg(arglist,'colorize.palette') )
159 max.alpha <- max(unlist([email protected]))
160 min.alpha <- min(unlist([email protected]))
161 col.cutoffs <- rev(seq(min.alpha,max.alpha, length=length( colors )))
162
163 if ( .garg(arglist,'colorkey.pos')=="right") {
164
165 ## axis drawing (ticks + labels)
166 ## The interval [min.alpha,max.alpha] needs to be mapped onto
167 ## the interval [min.y,max.y], rather than onto the interval
168 ## [ylim[1],ylim[2]] ! In the latter case, NAs could occur in
169 ## approxfun below, because axTicks can be out of the ylim-range
170 ## ('yxaxs': 4%region)
171 max.y <- max(axTicks(4))
172 min.y <- min(axTicks(4))
173 alpha.ticks <- .garg( arglist, c("coloraxis.at"))
174 if (length(alpha.ticks)==0)
175 alpha.ticks <- approxfun(c(min.y, max.y),
176 c(min.alpha, max.alpha)) (axTicks(4))
177 alpha2y <- approxfun(c(min(alpha.ticks), max(alpha.ticks)),
178 c(min.y,max.y))
179 arglist <-
180 .sarg(arglist,
181 coloraxis.labels=.garg(arglist,
182 'cutoff.label.function')(alpha.ticks),
183 coloraxis.at=alpha2y(alpha.ticks))
184
185 do.call("axis",
186 .sarg(.slice.run(.get.arglist('coloraxis', arglist)),
187 side=4))
188
189 ## draw colorkey
190 ## each entry in display.bool corresponds to one rectangle of
191 ## the colorkey.
192 ## Only rectangles within the alpha.ticks range are plotted.
193 ## y.lower, y.upper, and colors, are the attributes of the visible
194 ## rectangles (those for which display.bool=TRUE)
195 display.bool <- (col.cutoffs >= min(alpha.ticks) &
196 col.cutoffs < max(alpha.ticks))
197 y.lower <- alpha2y( col.cutoffs )[display.bool]
198 colors <- colors[display.bool]
199 if (length(y.lower>=2)) {
200 y.width <- y.lower[2] - y.lower[1]
201 y.upper <- y.lower + y.width
202 x.left <- .garg(arglist,'xlim')[2] +
203 ((.garg(arglist,'xlim')[2] - .garg(arglist,'xlim')[1]) *
204 (1-.garg(arglist,'colorkey.relwidth'))*0.04)
205 x.right <- .garg(arglist,'xlim')[2] +
206 (.garg(arglist,'xlim')[2] -.garg(arglist,'xlim')[1]) * 0.04
207 rect(x.left, y.lower, x.right, y.upper,
208 col=colors, border=colors,xpd=NA)
209 }
210 } else if (.garg(arglist, 'colorkey.pos') == "top") {
211 ## axis drawing (ticks + labels)
212 max.x <- max(axTicks(3))
213 min.x <- min(axTicks(3))
214 alpha.ticks <- .garg( arglist, c("coloraxis.at"))
215 if (length(alpha.ticks)==0) {
216 alpha.ticks <- approxfun(c(min.x, max.x),
217 c(min.alpha, max.alpha))(axTicks(3))
218 }
219 alpha2x <- approxfun(c( min(alpha.ticks), max(alpha.ticks)),
220 c( min.x, max.x))
221 arglist <- .sarg(arglist,
222 coloraxis.labels=.garg(arglist,
223 'cutoff.label.function')(alpha.ticks),
224 coloraxis.at= alpha2x(alpha.ticks))
225 do.call("axis",
226 .sarg(.slice.run( .get.arglist('coloraxis', arglist)),
227 side=3))
228
229 ## draw colorkey
230 display.bool <- (col.cutoffs >= min(alpha.ticks) &
231 col.cutoffs < max(alpha.ticks))
232 x.left <- alpha2x( col.cutoffs )[display.bool]
233 colors <- colors[display.bool]
234 if (length(x.left)>=2) {
235 x.width <- x.left[2] - x.left[1]
236 x.right <- x.left + x.width
237 y.lower <- .garg(arglist,'ylim')[2] +
238 (.garg(arglist,'ylim')[2] - .garg(arglist,'ylim')[1]) *
239 (1-.garg(arglist,'colorkey.relwidth'))*0.04
240 y.upper <- .garg(arglist,'ylim')[2] +
241 (.garg(arglist,'ylim')[2] - .garg(arglist,'ylim')[1]) * 0.04
242 rect(x.left, y.lower, x.right, y.upper,
243 col=colors, border=colors, xpd=NA)
244 }
245 }
246 }
247
248 do.call( "box", .slice.run( .get.arglist( 'box', arglist)))
249 }
250
251 ## ----------------------------------------------------------------------------
252 ## plotting performance objects when no curve averaging is wanted
253 ## ----------------------------------------------------------------------------
254
255 #' @importFrom grDevices xy.coords
256 #' @importFrom stats approxfun
257 .performance.plot.no.avg <- function( perf, ... ) {
258
259 arglist <- list(...)
260 arglist <- .farg(arglist, type= 'l')
261
262 if (.garg(arglist, 'colorize') == TRUE) {
263 colors <- rev( .garg( arglist, 'colorize.palette') )
264 max.alpha <- max(unlist([email protected]))
265 min.alpha <- min(unlist([email protected]))
266 col.cutoffs <- rev(seq(min.alpha,max.alpha, length=length(colors)+1))
267 col.cutoffs <- col.cutoffs[2:length(col.cutoffs)]
268 }
269
270 for (i in 1:length([email protected])) {
271 if (.garg(arglist, 'colorize') == FALSE) {
272 do.call("plot.xy",
273 .sarg(.slice.run(.get.arglist('plot.xy', arglist), i),
274 xy=(grDevices::xy.coords([email protected][[i]],
275 [email protected][[i]]))))
276 } else {
277 for (j in 1:(length([email protected][[i]])-1)) {
278 segment.coloring <-
279 colors[min(which(col.cutoffs <= [email protected][[i]][j]))]
280 do.call("plot.xy",
281 .sarg(.slice.run(.get.arglist('plot.xy', arglist), i),
282 xy=(grDevices::xy.coords([email protected][[i]][j:(j+1)],
283 [email protected][[i]][j:(j+1)])),
284 col= segment.coloring))
285 }
286 }
287
288 print.cutoffs.at <- .garg(arglist, 'print.cutoffs.at',i)
289 if (! is.null(print.cutoffs.at)) {
290 text.x <- stats::approxfun([email protected][[i]], [email protected][[i]],
291 rule=2, ties=mean)(print.cutoffs.at)
292 text.y <- stats::approxfun([email protected][[i]], [email protected][[i]],
293 rule=2, ties=mean)(print.cutoffs.at)
294 do.call("points",
295 .sarg(.slice.run(.get.arglist('points', arglist),i),
296 x= text.x,
297 y= text.y))
298 do.call("text",
299 .farg(.slice.run( .get.arglist('text', arglist),i),
300 x= text.x,
301 y= text.y,
302 labels=(.garg(arglist,
303 'cutoff.label.function',
304 i)(print.cutoffs.at))))
305 }
306 }
307 }
308
309 ## ----------------------------------------------------------------------------
310 ## plotting performance objects when vertical curve averaging is wanted
311 ## ----------------------------------------------------------------------------
312
313 #' @importFrom stats approxfun sd
314 .performance.plot.vertical.avg <- function( perf, ...) {
315 arglist <- list(...)
316 arglist <- .farg(arglist,
317 show.spread.at= (seq(min(unlist([email protected])),
318 max(unlist([email protected])),
319 length=11)))
320 perf.avg <- perf
321 x.values <- seq(min(unlist([email protected])), max(unlist([email protected])),
322 length=max( sapply([email protected], length)))
323 for (i in 1:length([email protected])) {
324 [email protected][[i]] <-
325 stats::approxfun([email protected][[i]], [email protected][[i]],
326 ties=mean, rule=2)(x.values)
327 }
328 [email protected] <- list(rowMeans( data.frame( [email protected] )))
329 [email protected] <- list(x.values)
330 [email protected] <- list()
331
332 ## y.values at show.spread.at (midpoint of error bars )
333 show.spread.at.y.values <-
334 lapply(as.list(1:length([email protected])),
335 function(i) {
336 stats::approxfun([email protected][[i]], [email protected][[i]],
337 rule=2,
338 ties=mean)( .garg(arglist, 'show.spread.at'))
339 })
340
341 show.spread.at.y.values <- as.matrix(data.frame(show.spread.at.y.values ))
342 colnames(show.spread.at.y.values) <- c()
343 ## now, show.spread.at.y.values[i,] contains the curve y values at the
344 ## sampling x value .garg(arglist,'show.spread.at')[i]
345
346 if (.garg(arglist, 'spread.estimate') == "stddev" ||
347 .garg(arglist, 'spread.estimate') == "stderror") {
348 bar.width <- apply(show.spread.at.y.values, 1, stats::sd)
349 if (.garg(arglist, 'spread.estimate') == "stderror") {
350 bar.width <- bar.width / sqrt( ncol(show.spread.at.y.values) )
351 }
352 bar.width <- .garg(arglist, 'spread.scale') * bar.width
353
354 suppressWarnings(do.call(gplots::plotCI,
355 .farg(.sarg(.get.arglist('plotCI', arglist),
356 x=.garg(arglist,
357 'show.spread.at'),
358 y=rowMeans(
359 show.spread.at.y.values),
360 uiw= bar.width,
361 liw= bar.width,
362 err= 'y',
363 add= TRUE),
364 gap= 0,
365 type= 'n')))
366 }
367
368 if (.garg(arglist, 'spread.estimate') == "boxplot") {
369 do.call("boxplot",
370 .farg(.sarg(.get.arglist( 'boxplot', arglist),
371 x= data.frame(t(show.spread.at.y.values)),
372 at= .garg(arglist, 'show.spread.at'),
373 add= TRUE,
374 axes= FALSE),
375 boxwex= (1/(2*(length(.garg(arglist,
376 'show.spread.at')))))))
377 do.call("points",
378 .sarg(.get.arglist( 'points', arglist),
379 x= .garg(arglist, 'show.spread.at'),
380 y= rowMeans(show.spread.at.y.values)))
381 }
382
383 do.call( ".plot.performance", .sarg(arglist,
384 perf= perf.avg,
385 avg= 'none',
386 add= TRUE))
387 }
388
389 ## ----------------------------------------------------------------------------
390 ## plotting performance objects when horizontal curve averaging is wanted
391 ## ----------------------------------------------------------------------------
392
393 #' @importFrom stats approxfun sd
394 .performance.plot.horizontal.avg <- function( perf, ...) {
395 arglist <- list(...)
396 arglist <- .farg(arglist,
397 show.spread.at= seq(min(unlist([email protected])),
398 max(unlist([email protected])),
399 length=11))
400 perf.avg <- perf
401 y.values <- seq(min(unlist([email protected])), max(unlist([email protected])),
402 length=max( sapply([email protected], length)))
403 for (i in 1:length([email protected])) {
404 [email protected][[i]] <- stats::approxfun([email protected][[i]],
405 [email protected][[i]],
406 ties=mean, rule=2)(y.values)
407 }
408 [email protected] <- list(rowMeans( data.frame( [email protected] )))
409 [email protected] <- list(y.values)
410 [email protected] <- list()
411
412 ## x.values at show.spread.at (midpoint of error bars )
413 show.spread.at.x.values <-
414 lapply(as.list(1:length([email protected])),
415 function(i) {
416 stats::approxfun([email protected][[i]],
417 [email protected][[i]],
418 rule=2, ties=mean)(.garg(arglist,'show.spread.at'))
419 } )
420 show.spread.at.x.values <- as.matrix(data.frame(show.spread.at.x.values))
421 colnames(show.spread.at.x.values) <- c()
422 ## now, show.spread.at.x.values[i,] contains the curve x values at the
423 ## sampling y value .garg(arglist,'show.spread.at')[i]
424
425 if (.garg(arglist,'spread.estimate') == 'stddev' ||
426 .garg(arglist,'spread.estimate') == 'stderror') {
427 bar.width <- apply(show.spread.at.x.values, 1, stats::sd)
428 if (.garg(arglist,'spread.estimate')== 'stderror') {
429 bar.width <- bar.width / sqrt( ncol(show.spread.at.x.values) )
430 }
431 bar.width <- .garg(arglist,'spread.scale') * bar.width
432
433 suppressWarnings(do.call(gplots::plotCI,
434 .farg(.sarg(.get.arglist('plotCI', arglist),
435 x= rowMeans(
436 show.spread.at.x.values),
437 y= .garg(arglist,
438 'show.spread.at'),
439 uiw= bar.width,
440 liw= bar.width,
441 err= 'x',
442 add= TRUE),
443 gap= 0,
444 type= 'n')))
445 }
446
447 if (.garg(arglist,'spread.estimate') == "boxplot") {
448 do.call("boxplot",
449 .farg(.sarg(.get.arglist( 'boxplot', arglist),
450 x= data.frame(t(show.spread.at.x.values)),
451 at= .garg(arglist,'show.spread.at'),
452 add= TRUE,
453 axes= FALSE,
454 horizontal= TRUE),
455 boxwex= 1/(2*(length(.garg(arglist,'show.spread.at'))))))
456 do.call("points", .sarg(.get.arglist( 'points', arglist),
457 x= rowMeans(show.spread.at.x.values),
458 y= .garg(arglist,'show.spread.at')))
459 }
460
461 do.call( ".plot.performance", .sarg(arglist,
462 perf= perf.avg,
463 avg= 'none',
464 add= TRUE))
465 }
466
467 ## ----------------------------------------------------------------------------
468 ## plotting performance objects when threshold curve averaging is wanted
469 ## ----------------------------------------------------------------------------
470
471 #' @importFrom stats approxfun sd
472 .performance.plot.threshold.avg <- function( perf, ...) {
473 arglist <- list(...)
474 arglist <- .farg(arglist,
475 show.spread.at= seq(min(unlist([email protected])),
476 max(unlist([email protected])),
477 length=11))
478
479 perf.sampled <- perf
480 alpha.values <- rev(seq(min(unlist([email protected])),
481 max(unlist([email protected])),
482 length=max( sapply([email protected], length))))
483 for (i in 1:length([email protected])) {
484 [email protected][[i]] <-
485 stats::approxfun([email protected][[i]],[email protected][[i]],
486 rule=2, ties=mean)(alpha.values)
487 [email protected][[i]] <-
488 stats::approxfun([email protected][[i]], [email protected][[i]],
489 rule=2, ties=mean)(alpha.values)
490 }
491
492 ## compute average curve
493 perf.avg <- perf.sampled
494 [email protected] <- list( rowMeans( data.frame( [email protected])))
495 [email protected] <- list(rowMeans( data.frame( [email protected])))
496 [email protected] <- list( alpha.values )
497
498 x.values.spread <-
499 lapply(as.list(1:length([email protected])),
500 function(i) {
501 stats::approxfun([email protected][[i]], [email protected][[i]],
502 rule=2, ties=mean)(.garg(arglist,'show.spread.at'))
503 } )
504 x.values.spread <- as.matrix(data.frame( x.values.spread ))
505 y.values.spread <-
506 lapply(as.list(1:length([email protected])),
507 function(i) {
508 stats::approxfun([email protected][[i]], [email protected][[i]],
509 rule=2, ties=mean)(.garg(arglist,'show.spread.at'))
510 } )
511 y.values.spread <- as.matrix(data.frame( y.values.spread ))
512
513 if (.garg(arglist,'spread.estimate')=="stddev" ||
514 .garg(arglist,'spread.estimate')=="stderror") {
515
516 x.bar.width <- apply(x.values.spread, 1, stats::sd)
517 y.bar.width <- apply(y.values.spread, 1, stats::sd)
518 if (.garg(arglist,'spread.estimate')=="stderror") {
519 x.bar.width <- x.bar.width / sqrt( ncol(x.values.spread) )
520 y.bar.width <- y.bar.width / sqrt( ncol(x.values.spread) )
521 }
522 x.bar.width <- .garg(arglist,'spread.scale') * x.bar.width
523 y.bar.width <- .garg(arglist,'spread.scale') * y.bar.width
524
525 suppressWarnings( do.call(gplots::plotCI,
526 .farg(.sarg(.get.arglist('plotCI', arglist),
527 x= rowMeans(x.values.spread),
528 y= rowMeans(y.values.spread),
529 uiw= x.bar.width,
530 liw= x.bar.width,
531 err= 'x',
532 add= TRUE),
533 gap= 0,
534 type= 'n')))
535
536 suppressWarnings( do.call(gplots::plotCI,
537 .farg(.sarg(.get.arglist('plotCI', arglist),
538 x= rowMeans(x.values.spread),
539 y= rowMeans(y.values.spread),
540 uiw= y.bar.width,
541 liw= y.bar.width,
542 err= 'y',
543 add= TRUE),
544 gap= 0,
545 type= 'n')))
546 }
547
548 if (.garg(arglist,'spread.estimate')=="boxplot") {
549 do.call("boxplot",
550 .farg(.sarg(.get.arglist('boxplot', arglist),
551 x= data.frame(t(x.values.spread)),
552 at= rowMeans(y.values.spread),
553 add= TRUE,
554 axes= FALSE,
555 horizontal= TRUE),
556 boxwex= 1/(2*(length(.garg(arglist,'show.spread.at'))))))
557 do.call("boxplot",
558 .farg(.sarg(.get.arglist('boxplot', arglist),
559 x= data.frame(t(y.values.spread)),
560 at= rowMeans(x.values.spread),
561 add= TRUE,
562 axes= FALSE),
563 boxwex= 1/(2*(length(.garg(arglist,'show.spread.at'))))))
564 do.call("points", .sarg(.get.arglist('points', arglist),
565 x= rowMeans(x.values.spread),
566 y= rowMeans(y.values.spread)))
567 }
568
569 do.call( ".plot.performance", .sarg(arglist,
570 perf= perf.avg,
571 avg= 'none',
572 add= TRUE))
573 }