2020-09-22 23:32:51 +02:00
|
|
|
source("renv/activate.R")
|
2020-10-23 16:41:00 +02:00
|
|
|
library("dplyr", warn.conflicts = F)
|
2020-09-22 23:32:51 +02:00
|
|
|
|
|
|
|
activity_recognition <- read.csv(snakemake@input[[1]])
|
2020-10-26 20:28:19 +01:00
|
|
|
episode_threshold_between_rows <- snakemake@params[["episode_threshold_between_rows"]]
|
2020-09-22 23:32:51 +02:00
|
|
|
|
|
|
|
if(nrow(activity_recognition) > 0){
|
2020-10-26 20:28:19 +01:00
|
|
|
episode_threshold_between_rows = episode_threshold_between_rows * 60000
|
2020-09-29 23:26:43 +02:00
|
|
|
|
2020-09-22 23:32:51 +02:00
|
|
|
ar_episodes <- activity_recognition %>%
|
2020-09-29 23:26:43 +02:00
|
|
|
mutate(start_timestamp = timestamp, # a battery level starts as soon as is logged
|
|
|
|
time_diff = (lead(timestamp) - start_timestamp), # lead diff
|
2020-10-26 20:28:19 +01:00
|
|
|
# we assume the current activity existed until the next row only if that row is logged within [episode_threshold_between_rows] minutes
|
|
|
|
end_timestamp = if_else(is.na(time_diff) | time_diff > (episode_threshold_between_rows), start_timestamp + (episode_threshold_between_rows), lead(timestamp) - 1),
|
2020-09-29 23:26:43 +02:00
|
|
|
time_diff = c(1, diff(start_timestamp)), # lag diff
|
|
|
|
type_diff = c(1, diff(activity_type)),
|
2020-10-26 20:28:19 +01:00
|
|
|
episode_id = cumsum(type_diff != 0 | time_diff > (episode_threshold_between_rows))) %>%
|
2020-09-29 23:26:43 +02:00
|
|
|
group_by(episode_id) %>%
|
|
|
|
summarise(activity_name = first(activity_name), activity_type = first(activity_type), start_timestamp=first(start_timestamp), end_timestamp = last(end_timestamp))
|
2020-09-22 23:32:51 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ar_episodes <- data.frame(start_timestamp = numeric(),
|
|
|
|
end_timestamp = numeric(),
|
2020-09-29 23:26:43 +02:00
|
|
|
episode_id = numeric(),
|
|
|
|
activity_type = numeric(),
|
|
|
|
activity_name = character())
|
2020-09-22 23:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
write.csv(ar_episodes, snakemake@output[[1]], row.names = FALSE)
|