# Manuel Dahnert
# User study: Looking beyond the horizon: Evaluation of four compact visualization techniques for time series in a spatial context
# Evaluation of reported reported diffuculty
# Generate boxplots
# Generate summary statistics

source("eval-functions.R")

# Load necessary librarys
loadLibrary("RColorBrewer")
loadLibrary("data.table")

# Load raw study data and perform general filtering
data.csv <- read.csv("rawdata_study.csv", header = TRUE, sep =";")
data <- filterData(data.csv, max.skips = 10, min.time = 30)

data <- data[data$questionId == "seq", ]
data <- data[, c(1, 13, 16, 18)] # Exclude unnecessary columns
data$givenAnswer <- as.numeric(factor(data$givenAnswer))

data <- data.table(data)

data2 <- data.table()
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  
  task.dt <- data[task.1 == task.name, ]
  task.techs <- task.dt[, .("tech" = length(technique)), by = c("participant")]
  
  if (index == 3) {
    task.techs <- task.techs[tech == 1, ]
  } else {
    task.techs <- task.techs[tech == 4, ]
  }
  
  task.data <- task.dt[task.techs, participant : technique, on = "participant"]
  data2 <- rbind(data2, task.data)
}
data <- data2

# Define output directory
export.root <- "export/difficulty/"
dir.create(export.root, showWarnings = FALSE) # Create directory

# Generate boxplots for task 1,4-10
output.dir <- paste(export.root, "boxplots/", sep = "") # Assume folder exists
dir.create(output.dir, showWarnings = FALSE) # Create directory
output.width <- 560
output.height <- 325

for (index in c(1,4,5,6,7,8,9,10)) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  
  # Start output rendering
  png(paste(output.dir, task.name, ".png", sep = ""), w = output.width, h = output.height) 
  boxplot(task.data$givenAnswer ~ factor(task.data$technique), horizontal = TRUE, 
          outline = TRUE, names = c("bp", "bhg", "chg", "hg"), las = TRUE,
          notch = FALSE, ylim = c(1,7),
          col = brewer.pal(4, "Set2"), outcol = brewer.pal(4, "Set2"), outpch = 16,
          xlab = "ease", main = paste(index, "-", tasks.names[[index]]))
  dev.off()
}


# Special case task 2 & 3
# Combine both in one plot
task.data.t3 <- data[data$task.1 == "task3", ]
task.data.t3$technique <- "colhorgraphs2"
task.data <- data[data$task.1 == "task2", ] 
task.data <- rbind(task.data, task.data.t3) # Combine both data sets

# Sort data such that chg and chg2 are next to each other
task.data$technique <- factor(task.data$technique, c("boxplots", "braidedgraphs",
                                                     "colhorgraphs", "colhorgraphs2",
                                                     "horizongraphs"))

boxplot.cols <- brewer.pal(5, "Set2") # Same color for chg and chg2
boxplot.cols[5] <- boxplot.cols[4]
boxplot.cols[4] <- boxplot.cols[3]
png(paste(output.dir, "task2.png", sep = ""), w = output.width, h = output.height)
boxplot(task.data$givenAnswer ~ factor(task.data$technique), horizontal = TRUE, 
        outline = TRUE, las = TRUE, names = c("bp", "bhg", "chg", "chg2", "hg"),
        notch = FALSE, ylim = c(1,7),
        col = boxplot.cols, outcol = boxplot.cols, outpch = 16,
        xlab = "difficulty", main = "2/3 - Slope increase / decrease")
dev.off()



# ------------------------------------------------------------------------------
#  SUMMARY
# ------------------------------------------------------------------------------

output.file <- paste(export.root, "summary.txt", sep = "")
sink(output.file)

for (index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  summary.table <- unclass(as.table(summary(task.data$givenAnswer)))
  
  # Write header and task summary
  cat("\t ", paste0(names(summary(0)), sep = "\t"), "\n", sep = "")
  cat(sprintf("%02d",index), ": ")
  cat(paste0(sprintf("%0.2f",summary.table)), "\n", sep ="\t")
  
  # Write technique summaries
  for (tech in techniques.long) {
    tech.data <- task.data[task.data$technique == tech, ]
    
    if (nrow(tech.data) == 0) next
    
    summary.table <- unclass(as.table(summary(tech.data$givenAnswer)))
    tech.short <- techniques[which(techniques.long == tech)]
    cat(sprintf("%-3s",tech.short), ": ", sep = "")
    cat(paste0(sprintf("%0.2f",summary.table)), "\n", sep ="\t")
  }
  cat("\n")
}
sink()
