From 9750353f5be949f066d160ab621306737ad15572 Mon Sep 17 00:00:00 2001 From: JulioV Date: Wed, 27 Nov 2019 15:27:48 -0500 Subject: [PATCH] Add screen deltas --- .gitignore | 4 +++- Snakefile | 1 + rules/features.snakefile | 8 +++++++ src/features/screen_deltas.R | 44 ++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/features/screen_deltas.R diff --git a/.gitignore b/.gitignore index 9ceab86f..bcd51cf8 100644 --- a/.gitignore +++ b/.gitignore @@ -104,4 +104,6 @@ data/processed/* !/data/processed/.gitkeep reports/ .Rproj.user -*.Rproj \ No newline at end of file +*.Rproj +.RData +.Rhistory \ No newline at end of file diff --git a/Snakefile b/Snakefile index e3617e42..c3c0f1c0 100644 --- a/Snakefile +++ b/Snakefile @@ -8,6 +8,7 @@ rule all: expand("data/raw/{pid}/{sensor}_raw.csv", pid=config["PIDS"], sensor=config["SENSORS"]), expand("data/raw/{pid}/{sensor}_with_datetime.csv", pid=config["PIDS"], sensor=config["SENSORS"]), expand("data/processed/{pid}/battery_deltas.csv", pid=config["PIDS"]), + expand("data/processed/{pid}/screen_deltas.csv", pid=config["PIDS"]), expand("data/interim/{pid}/phone_valid_sensed_days.csv", pid=config["PIDS"]), expand("data/processed/{pid}/sms_{sms_type}_{day_segment}.csv", pid=config["PIDS"], diff --git a/rules/features.snakefile b/rules/features.snakefile index fd59e35c..8bd4b67a 100644 --- a/rules/features.snakefile +++ b/rules/features.snakefile @@ -30,6 +30,14 @@ rule battery_deltas: script: "../src/features/battery_deltas.R" +rule screen_deltas: + input: + "data/raw/{pid}/screen_with_datetime.csv" + output: + "data/processed/{pid}/screen_deltas.csv" + script: + "../src/features/screen_deltas.R" + rule location_barnett_metrics: input: "data/raw/{pid}/locations_with_datetime.csv" diff --git a/src/features/screen_deltas.R b/src/features/screen_deltas.R new file mode 100644 index 00000000..1276967d --- /dev/null +++ b/src/features/screen_deltas.R @@ -0,0 +1,44 @@ +source("packrat/init.R") + +library("tidyverse") + +screen <- read.csv(snakemake@input[[1]]) + +if(nrow(screen) > 0){ + unlock_episodes <- + screen %>% + # in iOS there are unlock (3) events on the same second, discard them + distinct(screen_status, utc_date_time, .keep_all = TRUE) %>% + # in Android we discard on and off events (0,1) for now (iOS does not collect them) + filter(screen_status == 2 | screen_status == 3) %>% + # create groups of consecutive unlock/lock (3/2) events + mutate(screen_episode = cumsum(c(1, head(screen_status, -1) == 2 & tail(screen_status, -1) == 3))) %>% + group_by(screen_episode) %>% + # in Android there are multiple consecutive unlock/lock events so we keep the closest pair + # this happens because ACTION_SCREEN_OFF and ON are "sent when the device becomes non-interactive + # which may have nothing to do with the screen turning off" see: + # https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF + filter((screen_status == 2 & screen_status != lag(screen_status, default="1")) | + (screen_status == 3 & screen_status != lead(screen_status, default="1"))) %>% + filter(n() == 2) %>% + summarize(episode = "unlock", + time_diff = (last(timestamp) - first(timestamp)) / (1000 * 60), + local_start_date_time = first(local_date_time), + local_end_date_time = last(local_date_time), + local_start_date = first(local_date), + local_end_date = last(local_date), + local_start_day_segment = first(local_day_segment), + local_end_day_segment = last(local_day_segment)) %>% + select(-screen_episode) +} else { + unlock_episodes <- data.frame(episode = character(), + time_diff = numeric(), + local_start_date_time = character(), + local_end_date_time = character(), + local_start_date = character(), + local_end_date = character(), + local_start_day_segment = character(), + local_end_day_segment = character()) +} + +write.csv(unlock_episodes, snakemake@output[[1]], row.names = FALSE)