Facebook
From Gentle Gibbon, 4 Years ago, written in R.
Embed
Download Paste or View Raw
Hits: 196
  1. # data schema
  2.  
  3. periods = list()
  4. periods[[1]] = c(0, 120)
  5. periods[[2]] = c(120, 300)
  6. periods[[3]] = c(300, 480)
  7. periods[[4]] = c(480, 660)
  8. periods[[5]] = c(660, 840)
  9. periods[[6]] = c(840, 1020)
  10. periods[[7]] = c(1020, 1200)
  11. periods[[8]] = c(1200, 1380)
  12. periods[[9]] = c(1200, 1380)
  13. periods[[10]] = c(1380, 1560)
  14. periods[[11]] = c(1560, 1740)
  15.  
  16. actionNames <- c("dynamic_lever_pressing", "cage_exploring", "lying_down", "grooming", "laser", "freezing", "akinesia", "slow_lever_pressing", "presence_next_to_feeder", "scratching", "chewing", "yawning")
  17.  
  18. # utility functions
  19.  
  20. decorateAction <- function(action) {
  21.   trimmedAction = trimws(action)
  22.   if(trimmedAction == "time_on_lever_pressing_exploredisspan") {
  23.     return(actionNames[1])
  24.   } else if (trimmedAction == "time_on_exploring_cage") {
  25.     return(actionNames[2])
  26.   } else if (trimmedAction == "nap") {
  27.     return(actionNames[3])
  28.   } else if (trimmedAction == "grooming") {
  29.     return(actionNames[4])
  30.   } else if (trimmedAction == "laser") {
  31.     return(actionNames[5])
  32.   } else if (trimmedAction == "freezing") {
  33.     return(actionNames[6])
  34.   } else if (trimmedAction == "catatonic") {
  35.     return(actionNames[7])
  36.   } else if (trimmedAction == "slowly_lever_presing") {
  37.     return(actionNames[8])
  38.   } else if (trimmedAction == "exloring_and_time_disspa") {
  39.     return(actionNames[9])
  40.   } else if (trimmedAction == "scratching") {
  41.     return(actionNames[10])
  42.   } else if (trimmedAction == "zucie") {
  43.     return(actionNames[11])
  44.   } else if (trimmedAction == "ziewanie") {
  45.     return(actionNames[12])
  46.   } else {
  47.     print(action)
  48.   }
  49. }
  50.  
  51. decoratePeriod <- function(period) {
  52.   return(paste(period[1], "_", period[2], sep = ""))
  53. }
  54.  
  55. indexNameForPeriod <- function(period) {
  56.   return(paste(period[1], "-", period[2], sep = ""))
  57. }
  58.  
  59. map <- function(collection, fun) {
  60.   res <- list()
  61.   counter <- 1
  62.   for (methods/html/el.html">el in collection) {
  63.     res[[counter]] <- fun(methods/html/el.html">el)
  64.     counter <- counter+1
  65.   }
  66.   return(res)
  67. }
  68.  
  69. select <- function(collection, fun) {
  70.   res <- list()
  71.   counter <- 1
  72.   for (methods/html/el.html">el in collection) {
  73.     if (fun(methods/html/el.html">el)) {
  74.       res[[counter]] <- methods/html/el.html">el
  75.       counter <- counter+1
  76.     }
  77.   }
  78.   return(res)
  79. }
  80.  
  81. sum <- function(collection, attr) {
  82.   amount <- 0
  83.   for (methods/html/el.html">el in collection) {
  84.     amount <- amount + methods/html/el.html">el[[attr]]
  85.   }
  86.   amount
  87. }
  88.  
  89. generateVectorFromCollection <- function(collection) {
  90.   vec <- c()
  91.   for (methods/html/el.html">el in collection) {
  92.     vec <- c(vec, methods/html/el.html">el)
  93.   }
  94.   return(vec)
  95. }
  96.  
  97. # business logic
  98.  
  99. getTimeRowsFromFile <- function(filename) {
  100.   lines <- readLines(filename)
  101. }
  102.  
  103. timeStrToSeconds <- function(str) {
  104.   numbers <- strsplit(str, ":")[[1]]
  105.   amount = (as.numeric(numbers[1]) * 3600) + (as.numeric(numbers[2]) * 60) + as.numeric(numbers[3]) + (as.numeric(numbers[4]) / 100)
  106.   return(amount)
  107. }
  108.  
  109. parseRow <- function(row) {
  110.   res <- list()
  111.   parts <- strsplit(row, " - ")[[1]]
  112.   res[["time_in_sec"]] <- timeStrToSeconds(parts[1])
  113.   res[["action"]] <- parts[2]
  114.   return(res)
  115. }
  116.  
  117. getActionPairsFromFile <- function(filename) {
  118.   parsedRows <- map(getTimeRowsFromFile(filename), function(row) { return(parseRow(row)) })
  119.   startTimeInSeconds <- parsedRows[[1]][["time_in_sec"]]
  120.   rowActionsCache <- list()
  121.   result <- list()
  122.   counter <- 1
  123.   for(row in parsedRows[2:(length(parsedRows)-1)]) {
  124.     calculatedTime <- row[["time_in_sec"]] - startTimeInSeconds
  125.     action <- row[["action"]]
  126.     cacheValue <- rowActionsCache[[action]]
  127.     if (is.null(cacheValue)) {
  128.       rowActionsCache[[action]] <- calculatedTime
  129.     } else {
  130.       previousTime <- cacheValue
  131.       entry <- list()
  132.       entry[["action"]] <- decorateAction(row[["action"]])
  133.       entry[["start_time"]] <- previousTime
  134.       entry[["end_time"]] <- calculatedTime
  135.       entry[["duration"]] <- (calculatedTime - previousTime)
  136.       result[[counter]] <- entry
  137.       counter <- counter+1
  138.       rowActionsCache[[action]] <- NULL
  139.     }
  140.   }
  141.   return(result)
  142. }
  143.  
  144. getActionPairsPerDuration <- function(pairs, start, end) {
  145.   matchingPairs <- select(pairs, function(pair) {
  146.     startedInPeriod <- pair[["start_time"]] > start & pair[["start_time"]] <= end
  147.     endedInPeriod <- pair[["end_time"]] > start & pair[["end_time"]] <= end
  148.     return(startedInPeriod | endedInPeriod)
  149.   })
  150.   withTimesAlignedToPeriod <- map(matchingPairs, function(pair) {
  151.     entry <- pair
  152.     if (entry[["start_time"]] < start) { entry["start_time"] <- start }
  153.     if (entry[["end_time"]] > end) { entry["end_time"] <- end }
  154.     entry[["duration"]] <- (entry[["end_time"]] - entry[["start_time"]])
  155.     return(entry)
  156.   })
  157.   return(withTimesAlignedToPeriod)
  158. }
  159.  
  160. scopeDownPairsToAction <- function(pairs, action) {
  161.   return(select(pairs, function(pair) { pair[["action"]] == action }))
  162. }
  163.  
  164. calculateAmountPerAction <- function(pairs, action) {
  165.   return(length(scopeDownPairsToAction(pairs, action)))
  166. }
  167.  
  168. calculateTotalDurationPerAction <- function(pairs, action) {
  169.   return(sum(scopeDownPairsToAction(pairs, action), "duration"))
  170. }
  171.  
  172. calculateAverageDurationPerAction <- function(pairs, action) {
  173.   amountPerAction <- calculateAmountPerAction(pairs, action)
  174.   totalDuration <- calculateTotalDurationPerAction(pairs, action)
  175.   if (amountPerAction == 0) {
  176.     return(0)
  177.   } else {
  178.     return(totalDuration / amountPerAction)
  179.   }
  180. }
  181.  
  182. filenames <- list.files("data/", "*.bvs")
  183.  
  184. # procedure
  185.  
  186. finalData <- list()
  187. for (actionName in actionNames) {
  188.   finalData[[actionName]] <- list()
  189.   for (filename in filenames) {
  190.     finalData[[actionName]][[filename]] <- list()
  191.     fullPath = paste("data", filename, sep="/")
  192.     pairsFromFile = getActionPairsFromFile(fullPath)
  193.     for(period in periods) {
  194.       periodIndex = paste(period[1], "-", period[2], sep = "")
  195.       finalData[[actionName]][[filename]][[periodIndex]] <- list()
  196.       pairsFromPeriod = getActionPairsPerDuration(pairsFromFile, period[1], period[2])
  197.       finalData[[actionName]][[filename]][[periodIndex]][["n"]] = calculateAmountPerAction(pairsFromPeriod, actionName)
  198.       finalData[[actionName]][[filename]][[periodIndex]][["total"]] = calculateTotalDurationPerAction(pairsFromPeriod, actionName)
  199.       finalData[[actionName]][[filename]][[periodIndex]][["average"]] = calculateAverageDurationPerAction(pairsFromPeriod, actionName)
  200.     }
  201.   }
  202. }
  203.  
  204. generateCsvDataForAction <- function(actionName, data) {
  205.   props <- c("n", "total", "average")
  206.   csvData <- list()
  207.   csvData[["Filenames"]] <- generateVectorFromCollection(filenames)
  208.   for(period in periods) {
  209.     for(prop in props) {
  210.       numbers <- generateVectorFromCollection(map(filenames, function(filename) {
  211.         return(round(data[[actionName]][[filename]][[indexNameForPeriod(period)]][[prop]], 2))
  212.       }))
  213.       header <- paste(prop, decoratePeriod(period), sep=" ")
  214.       csvData[[header]] <- numbers
  215.     }
  216.   }
  217.   return(csvData)
  218. }
  219.  
  220. outputFileName = "output.csv"
  221. for(actionName in actionNames) {
  222.   write(actionName, file=outputFileName, append=T)
  223.   write("", file=outputFileName, append=T)
  224.   data <- generateCsvDataForAction(actionName, finalData)
  225.   write.table(data, file=outputFileName, append=T, row.names=F)
  226.   write("", file=outputFileName, append=T)
  227.   write("", file=outputFileName, append=T)
  228. }
  229.