<<cache=TRUE>>=
    get_method_ids <- function(project) {
        filename <- paste(results_dir, project, 'current', 'temporary', 'MethodIdentifier.out', sep='/')
        ids <- data.frame(id=NA, method=NA, revision=NA, event_id=NA)
        if(file.exists(filename)){
            ids <- read.table(filename, col.names=c('id', 'method', 'revision', 'event_id'), sep='\t', comment.char='#', strip.white=TRUE)
        }
        return(ids)
    }

    get_links <- function(report) {
        filename <- paste(results_dir, report$archive, 'BugMethodFinder.out', sep='/')
        links <- data.frame(method=NA, revision=NA, event_id=NA)
        if(file.exists(filename)){
            links <- read.table(filename, col.names=c('method', 'revision', 'event_id'), comment.char='#', strip.white=TRUE)
        }
        return(links)
    }

    cumulative_mean <- function(x) {
        finite <- is.finite(x)
        means <- cumsum(ifelse(finite, x, 0)) / cumsum(finite)
        means[!finite] <- NA
        means
    }

    get_related_methods <- function(report, method_ids) {
        links <- get_links(report)
        link_ids <- method_ids[method_ids$method %in% links$method, 'id']
        return(data.frame(related.methods=I(list(link_ids))))
    }

    get_events <- function(project) {
        filename <- paste(results_dir, project, 'events', 'EventLogCreator.out', sep='/')
        events <- read.table(filename, col.names=c('event_id', 'bug_id', 'type', 'revision', 'timestamp'), sep='\t', comment.char='#', strip.white=TRUE, stringsAsFactors=TRUE)
        events$bug_number <- as.numeric(factor(events$bug_id))
        events$project <- factor(project)
        events$timestamp <- as.POSIXct(events$timestamp, origin='1970-01-01')
        events$archive <- paste(events$project, 'current', events$bug_id, ifelse(events$type=='FIX', as.character(events$revision), ''), sep='/')
        return(events)
    }

    get_fixes <- function(events) {
        fixes <- events[events$type=='FIX',]
        fixes <- fixes[order(fixes$bug_id, fixes$timestamp),]
        fixes$latest <- fixes$bug_id != c(fixes[2:nrow(fixes),'bug_id'],Inf)
        return(fixes)
    }

    get_evaluator_positions <- function(evaluator, method_count) {
        evaluator_name <- paste(evaluator$evaluator, 'positions', sep='.')
        filename <- paste(results_dir, evaluator$archive.fix, 'PositionEvaluator.out', evaluator_name, sep='/')
        positions <- data.frame(method=NA, position=method_count+1, frm=TRUE)
        if(file.exists(filename)){
            positions <- read.table(filename, col.names=c('method', 'position'), sep='\t', comment.char='#', strip.white=TRUE)
            positions <- positions[positions$method %in% unlist(evaluator$related.methods),]
            if(nrow(positions)>0){
                positions$frm <- FALSE
                positions[which.min(positions$position), 'frm'] <- TRUE
            }
        }
        return(positions)
    }

    get_bug_positions <- function(bug, method_count, evaluators) {
        bug_positions <- data.frame(evaluator=evaluators)
        bug_positions <- cbind(bug, bug_positions, row.names=bug_positions$evaluator)
        bug_positions <- adply(bug_positions, 1, get_evaluator_positions, method_count)
        return(bug_positions)
    }

    get_positions <- function(bugs, method_ids, evaluators) {
        positions <- bugs[,c('project', 'bug_number', 'bug_id', 'revision.fix', 'archive.fix', 'latest', 'related.methods')]
        positions <- adply(positions, 1, get_bug_positions, max(method_ids$id), evaluators)
        positions$rr <- 1/positions$position
        return(positions)
    }

    get_bug_model <- function(bug, model_name, evaluators) {
        filename <- paste(results_dir, bug$archive.report, model_name, sep='/')
        model <- data.frame(t(rep(NA, length(evaluators))))
        colnames(model) <- evaluators
        if(file.exists(filename)){
            input <- read.table(filename, col.names=c('evaluator', 'coefficient'), sep='\t', comment.char='#', strip.white=TRUE)
            model <- data.frame(t(input[,2]))
            colnames(model) <- input[,1]
        }
        return(model)
    }

    get_models <- function(bugs, model_name, evaluators) {
        models <- bugs[bugs$latest,c('project', 'bug_number', 'bug_id', 'archive.report')]
        models <- adply(models, 1, get_bug_model, model_name, evaluators)
        return(models)
    }
@
