## produces boxplots ordered according to median or rank
## M is the matrix of results returned from the race
## 
## Example:
## library(race)
## if (require(nnet)&&require(datasets)){
##       example.wrapper<-file.path(system.file(package="race"),
##                                "examples","example-wrapper.R")
## D<-race(example.wrapper)
## source("plot.race.R")
## config.bx.median(D$results)
##
config.bx.median <- function(M,...) {
  dimnames(M) <- list(paste("I",seq(1:dim(M)[1])),
                      paste("c",seq(1:dim(M)[2])))
  idx <- order(apply(M,2,median))
  par(las=1)
  boxplot(as.data.frame(M[,idx]),horizontal=TRUE,...)
}


config.bx.rank <- function(M,...) {
  dimnames(M) <- list(paste("I",seq(1:dim(M)[1])),
                      paste("c",seq(1:dim(M)[2])))

  par(las=1)
  R <- t(apply(M,1,rank))
  idx <- order(apply(R,2,median))
  boxplot(as.data.frame(R[,idx]),horizontal=TRUE,...)
}


## just a wrapper for plot.race
## D<-race(example.wrapper)
## source("plot.race.R")
## plot.wrapper(D)
plot.wrapper <- function(race.results, wrapper.file)
  {
    pdf("race.pdf",width=10,height=6)
    ##par(mfrow=c(2,3))
    plot.race(race.results, wrapper.file, 9)
    dev.off()
    ##cbind(L$candidates[which(O$alive),],best=apply(O$results[,which(O$alive)],2,min,na.rm=TRUE))
  }

## produces a plot on survival time of configurations in the race
## 
## Example:
## library(race)
## if (require(nnet)&&require(datasets)){
##       example.wrapper<-file.path(system.file(package="race"),
##                                "examples","example-wrapper.R")
## D<-race(example.wrapper)
## source("plot.race.R")
## plot.race(D,example.wrapper)
##
## for the names of the configurations to appear in the plot
## you must define a field called "label" within "candidates" in race.init()
plot.race <- function(data, wrapper.file, restrict=162){
  source(wrapper.file)
  info <- race.init()
  
  y <- data$results
  b <- data$no.subtasks
  k <- ncol(y)

  if (missing(restrict))
    {
      restrict <- k
    }
      
  I <- 1:k
  final.order <- I
  J <- I

  par(mar=c(3,11,2,1),
      mgp=c(0.1,0.5,0),
      tck=-0.01,cex.axis=0.9,cex.lab=0.9,cex.main=0.9,font.main=1,las=1)
  plot(1,1,ylim=c(1,restrict),
       xlim=c(0,info$no.tasks),yaxt="n",xaxt="n",type="n",xlab="",ylab="")
  abline(v=c(1:info$no.tasks),lty=3,col="grey70")
  abline(h=c(1:restrict),lty=3,col="grey75")
  for (stage in 1:(data$no.tasks))
    {
      y1 <- y[1:(stage*b),I]
      if (is.vector(y1))
        { N <- 1 }
      else
        { N <- nrow(y1) }
      r <- N/b
      k <- length(I)
        y2 <- array(y1,dim=c(b,k,r))
        R <- array(dim=c(r,k,b))
        for (h in 1:b)
          {
            R[,,h] <- matrix(rank(y2[h,,],na.last="keep"),nrow=k,byrow=FALSE)
          }
        R.i. <- apply(R,2,sum,na.rm=FALSE)
        J <- I[order(R.i.,na.last=NA)]
      l <- 1:length(J)
      final.order[l] <- J
      ##segments(0,J,stage,J)
      segments(0,l,stage,l,lwd=3,col="grey40")
    }
  names <- as.character(info$candidate$label)
  if (restrict<k) {
    names[final.order][restrict] <- "..."
    k <- restrict
  }
  
  if (k>50) {
    N <- 20
    idx <- seq(1,k,floor(k/N))
    idx2 <- seq(ceiling(floor(k/N)/2),k,floor(k/N))
    axis(2, at = I[idx2], labels = rep("...",N), tick = 0.5, srt=0,las=1)
  }
  else
    idx <- I[1:k]

  axis(2, at = I[idx], labels = names[final.order][idx], tick = 0.5, srt=0,las=1)
  axis(1, at = 0:(info$no.tasks), labels = c(0:(info$no.tasks)), tick = 0.5, srt=0,las=1)
  abline(v=0,lty=3,col="grey60")
  mtext(paste(race.info(info)$race.name," (",info$no.tasks," Instances)",sep=""),side=3,line=0,cex=1)
  title(xlab="Stage",line=2)

}





