Updated sms sensor to messages
parent
17f41588d8
commit
533d0adab3
|
@ -37,8 +37,8 @@ MESSAGES:
|
|||
DB_TABLE: messages
|
||||
TYPES : [received, sent]
|
||||
FEATURES:
|
||||
received: [count, distinctcontacts, timefirstsms, timelastsms, countmostfrequentcontact]
|
||||
sent: [count, distinctcontacts, timefirstsms, timelastsms, countmostfrequentcontact]
|
||||
received: [count, distinctcontacts, timefirstmessage, timelastmessage, countmostfrequentcontact]
|
||||
sent: [count, distinctcontacts, timefirstmessage, timelastmessage, countmostfrequentcontact]
|
||||
DAY_SEGMENTS: *day_segments
|
||||
|
||||
# Communication call features config, TYPES and FEATURES keys need to match
|
||||
|
|
|
@ -107,8 +107,8 @@ Name Units Description
|
|||
========================= ========= =============
|
||||
count messages Number of messages of type ``messages_type`` that occurred during a particular ``day_segment``.
|
||||
distinctcontacts contacts Number of distinct contacts that are associated with a particular ``messages_type`` during a particular ``day_segment``.
|
||||
timefirstsms minutes Number of minutes between 12:00am (midnight) and the first ``message`` of a particular ``messages_type``.
|
||||
timelastsms minutes Number of minutes between 12:00am (midnight) and the last ``message`` of a particular ``messages_type``.
|
||||
timefirstmessages minutes Number of minutes between 12:00am (midnight) and the first ``message`` of a particular ``messages_type``.
|
||||
timelastmessages minutes Number of minutes between 12:00am (midnight) and the last ``message`` of a particular ``messages_type``.
|
||||
countmostfrequentcontact messages Number of messages from the contact with the most messages of ``messages_type`` during a ``day_segment`` throughout the whole dataset of each participant.
|
||||
========================= ========= =============
|
||||
|
||||
|
@ -120,7 +120,7 @@ countmostfrequentcontact messages Number of messages from the contact wi
|
|||
...
|
||||
TYPES: [sent]
|
||||
FEATURES:
|
||||
sent: [count, distinctcontacts, timefirstsms, timelastsms, countmostfrequentcontact]
|
||||
sent: [count, distinctcontacts, timefirstmessages, timelastmessages, countmostfrequentcontact]
|
||||
|
||||
|
||||
.. _call-sensor-doc:
|
||||
|
@ -747,7 +747,7 @@ unknownexpectedfraction Ration between minutesunknown an
|
|||
========================= ================= =============
|
||||
|
||||
**Assumptions/Observations:**
|
||||
|
||||
N/A
|
||||
|
||||
.. ------------------------------- Begin Fitbit Section ----------------------------------- ..
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library('tidyr')
|
||||
library('tidyr')
|
||||
|
||||
filter_by_day_segment <- function(data, day_segment) {
|
||||
if(day_segment %in% c("morning", "afternoon", "evening", "night"))
|
||||
|
@ -9,51 +9,51 @@ filter_by_day_segment <- function(data, day_segment) {
|
|||
return(data %>% head(0))
|
||||
}
|
||||
|
||||
base_sms_features <- function(sms, sms_type, day_segment, requested_features){
|
||||
base_messages_features <- function(messages, messages_type, day_segment, requested_features){
|
||||
# Output dataframe
|
||||
features = data.frame(local_date = character(), stringsAsFactors = FALSE)
|
||||
|
||||
# The name of the features this function can compute
|
||||
base_features_names <- c("countmostfrequentcontact", "count", "distinctcontacts", "timefirstsms", "timelastsms")
|
||||
base_features_names <- c("countmostfrequentcontact", "count", "distinctcontacts", "timefirstmessage", "timelastmessage")
|
||||
|
||||
# The subset of requested features this function can compute
|
||||
features_to_compute <- intersect(base_features_names, requested_features)
|
||||
|
||||
# Filter rows that belong to the sms type and day segment of interest
|
||||
sms <- sms %>% filter(message_type == ifelse(sms_type == "received", "1", ifelse(sms_type == "sent", 2, NA))) %>%
|
||||
# Filter rows that belong to the message type and day segment of interest
|
||||
messages <- messages %>% filter(message_type == ifelse(messages_type == "received", "1", ifelse(messages_type == "sent", 2, NA))) %>%
|
||||
filter_by_day_segment(day_segment)
|
||||
|
||||
# If there are not features or data to work with, return an empty df with appropiate columns names
|
||||
if(length(features_to_compute) == 0)
|
||||
return(features)
|
||||
if(nrow(sms) < 1)
|
||||
return(cbind(features, read.csv(text = paste(paste("sms", sms_type, day_segment, features_to_compute, sep = "_"), collapse = ","), stringsAsFactors = FALSE)))
|
||||
if(nrow(messages) < 1)
|
||||
return(cbind(features, read.csv(text = paste(paste("messages", messages_type, day_segment, features_to_compute, sep = "_"), collapse = ","), stringsAsFactors = FALSE)))
|
||||
|
||||
for(feature_name in features_to_compute){
|
||||
if(feature_name == "countmostfrequentcontact"){
|
||||
# Get the number of messages for the most frequent contact throughout the study
|
||||
mostfrequentcontact <- sms %>%
|
||||
mostfrequentcontact <- messages %>%
|
||||
group_by(trace) %>%
|
||||
mutate(N=n()) %>%
|
||||
ungroup() %>%
|
||||
filter(N == max(N)) %>%
|
||||
head(1) %>% # if there are multiple contacts with the same amount of messages pick the first one only
|
||||
pull(trace)
|
||||
feature <- sms %>%
|
||||
feature <- messages %>%
|
||||
filter(trace == mostfrequentcontact) %>%
|
||||
group_by(local_date) %>%
|
||||
summarise(!!paste("sms", sms_type, day_segment, feature_name, sep = "_") := n()) %>%
|
||||
summarise(!!paste("messages", messages_type, day_segment, feature_name, sep = "_") := n()) %>%
|
||||
replace(is.na(.), 0)
|
||||
features <- merge(features, feature, by="local_date", all = TRUE)
|
||||
} else {
|
||||
feature <- sms %>%
|
||||
feature <- messages %>%
|
||||
group_by(local_date)
|
||||
|
||||
feature <- switch(feature_name,
|
||||
"count" = feature %>% summarise(!!paste("sms", sms_type, day_segment, feature_name, sep = "_") := n()),
|
||||
"distinctcontacts" = feature %>% summarise(!!paste("sms", sms_type, day_segment, feature_name, sep = "_") := n_distinct(trace)),
|
||||
"timefirstsms" = feature %>% summarise(!!paste("sms", sms_type, day_segment, feature_name, sep = "_") := first(local_hour) * 60 + first(local_minute)),
|
||||
"timelastsms" = feature %>% summarise(!!paste("sms", sms_type, day_segment, feature_name, sep = "_") := last(local_hour) * 60 + last(local_minute)))
|
||||
"count" = feature %>% summarise(!!paste("messages", messages_type, day_segment, feature_name, sep = "_") := n()),
|
||||
"distinctcontacts" = feature %>% summarise(!!paste("messages", messages_type, day_segment, feature_name, sep = "_") := n_distinct(trace)),
|
||||
"timefirstmessage" = feature %>% summarise(!!paste("messages", messages_type, day_segment, feature_name, sep = "_") := first(local_hour) * 60 + first(local_minute)),
|
||||
"timelastmessage" = feature %>% summarise(!!paste("messages", messages_type, day_segment, feature_name, sep = "_") := last(local_hour) * 60 + last(local_minute)))
|
||||
|
||||
features <- merge(features, feature, by="local_date", all = TRUE)
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
# If you want to implement extra features, source(..) a new file and duplicate the line "features <- merge(...)", then
|
||||
# swap base_sms_features(...) for your own function
|
||||
# swap base_sms_features(...) for your own function
|
||||
|
||||
source("renv/activate.R")
|
||||
source("src/features/messages/messages_base.R")
|
||||
library(dplyr, warn.conflicts = FALSE)
|
||||
|
||||
sms <- read.csv(snakemake@input[[1]])
|
||||
messages <- read.csv(snakemake@input[[1]])
|
||||
day_segment <- snakemake@params[["day_segment"]]
|
||||
requested_features <- snakemake@params[["features"]]
|
||||
sms_type <- snakemake@params[["messages_type"]]
|
||||
messages_type <- snakemake@params[["messages_type"]]
|
||||
features <- data.frame(local_date = character(), stringsAsFactors = FALSE)
|
||||
|
||||
# Compute base SMS features
|
||||
features <- merge(features, base_sms_features(sms, sms_type, day_segment, requested_features), by="local_date", all = TRUE)
|
||||
features <- merge(features, base_messages_features(messages, messages_type, day_segment, requested_features), by="local_date", all = TRUE)
|
||||
|
||||
if(ncol(features) != length(requested_features) + 1)
|
||||
stop(paste0("The number of features in the output dataframe (=", ncol(features),") does not match the expected value (=", length(requested_features)," + 1). Verify your SMS feature extraction functions"))
|
||||
stop(paste0("The number of features in the output dataframe (=", ncol(features),") does not match the expected value (=", length(requested_features)," + 1). Verify your Messages (SMS) feature extraction functions"))
|
||||
|
||||
write.csv(features, snakemake@output[[1]], row.names = FALSE)
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_received_afternoon_countmostfrequentcontact","sms_received_afternoon_count","sms_received_afternoon_distinctcontacts","sms_received_afternoon_timefirstsms","sms_received_afternoon_timelastsms"
|
||||
"local_date","messages_received_afternoon_countmostfrequentcontact","messages_received_afternoon_count","messages_received_afternoon_distinctcontacts","messages_received_afternoon_timefirstmessage","messages_received_afternoon_timelastmessage"
|
||||
"2020-05-28",1,2,2,830,949
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_received_daily_countmostfrequentcontact","sms_received_daily_count","sms_received_daily_distinctcontacts","sms_received_daily_timefirstsms","sms_received_daily_timelastsms"
|
||||
"local_date","messages_received_daily_countmostfrequentcontact","messages_received_daily_count","messages_received_daily_distinctcontacts","messages_received_daily_timefirstmessage","messages_received_daily_timelastmessage"
|
||||
"2020-05-28",7,12,6,6,1382
|
||||
"2020-05-29",3,6,4,401,1382
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_received_evening_countmostfrequentcontact","sms_received_evening_count","sms_received_evening_distinctcontacts","sms_received_evening_timefirstsms","sms_received_evening_timelastsms"
|
||||
"local_date","messages_received_evening_countmostfrequentcontact","messages_received_evening_count","messages_received_evening_distinctcontacts","messages_received_evening_timefirstmessage","messages_received_evening_timelastmessage"
|
||||
"2020-05-28",2,3,2,1173,1382
|
||||
"2020-05-29",2,3,2,1173,1382
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_received_morning_countmostfrequentcontact","sms_received_morning_count","sms_received_morning_distinctcontacts","sms_received_morning_timefirstsms","sms_received_morning_timelastsms"
|
||||
"local_date","messages_received_morning_countmostfrequentcontact","messages_received_morning_count","messages_received_morning_distinctcontacts","messages_received_morning_timefirstmessage","messages_received_morning_timelastmessage"
|
||||
"2020-05-28",1,3,3,401,660
|
||||
"2020-05-29",1,3,3,401,660
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_received_night_countmostfrequentcontact","sms_received_night_count","sms_received_night_distinctcontacts","sms_received_night_timefirstsms","sms_received_night_timelastsms"
|
||||
"local_date","messages_received_night_countmostfrequentcontact","messages_received_night_count","messages_received_night_distinctcontacts","messages_received_night_timefirstmessage","messages_received_night_timelastmessage"
|
||||
"2020-05-28",4,4,1,6,312
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_sent_afternoon_countmostfrequentcontact","sms_sent_afternoon_count","sms_sent_afternoon_distinctcontacts","sms_sent_afternoon_timefirstsms","sms_sent_afternoon_timelastsms"
|
||||
"local_date","messages_sent_afternoon_countmostfrequentcontact","messages_sent_afternoon_count","messages_sent_afternoon_distinctcontacts","messages_sent_afternoon_timefirstmessage","messages_sent_afternoon_timelastmessage"
|
||||
"2020-05-28",1,3,3,722,979
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_sent_daily_countmostfrequentcontact","sms_sent_daily_count","sms_sent_daily_distinctcontacts","sms_sent_daily_timefirstsms","sms_sent_daily_timelastsms"
|
||||
"local_date","messages_sent_daily_countmostfrequentcontact","messages_sent_daily_count","messages_sent_daily_distinctcontacts","messages_sent_daily_timefirstmessage","messages_sent_daily_timelastmessage"
|
||||
"2020-05-28",3,8,6,219,1401
|
||||
"2020-05-29",1,4,4,388,1401
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_sent_evening_countmostfrequentcontact","sms_sent_evening_count","sms_sent_evening_distinctcontacts","sms_sent_evening_timefirstsms","sms_sent_evening_timelastsms"
|
||||
"local_date","messages_sent_evening_countmostfrequentcontact","messages_sent_evening_count","messages_sent_evening_distinctcontacts","messages_sent_evening_timefirstmessage","messages_sent_evening_timelastmessage"
|
||||
"2020-05-28",1,2,2,1218,1401
|
||||
"2020-05-29",1,2,2,1218,1401
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_sent_morning_countmostfrequentcontact","sms_sent_morning_count","sms_sent_morning_distinctcontacts","sms_sent_morning_timefirstsms","sms_sent_morning_timelastsms"
|
||||
"local_date","messages_sent_morning_countmostfrequentcontact","messages_sent_morning_count","messages_sent_morning_distinctcontacts","messages_sent_morning_timefirstmessage","messages_sent_morning_timelastmessage"
|
||||
"2020-05-28",1,2,2,388,654
|
||||
"2020-05-29",1,2,2,388,654
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_sent_night_countmostfrequentcontact","sms_sent_night_count","sms_sent_night_distinctcontacts","sms_sent_night_timefirstsms","sms_sent_night_timelastsms"
|
||||
"local_date","messages_sent_night_countmostfrequentcontact","messages_sent_night_count","messages_sent_night_distinctcontacts","messages_sent_night_timefirstmessage","messages_sent_night_timelastmessage"
|
||||
"2020-05-28",1,1,1,219,219
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_received_afternoon_countmostfrequentcontact","sms_received_afternoon_count","sms_received_afternoon_distinctcontacts","sms_received_afternoon_timefirstsms","sms_received_afternoon_timelastsms"
|
||||
"local_date","messages_received_afternoon_countmostfrequentcontact","messages_received_afternoon_count","messages_received_afternoon_distinctcontacts","messages_received_afternoon_timefirstmessage","messages_received_afternoon_timelastmessage"
|
||||
"2020-05-28",1,2,2,830,949
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_received_daily_countmostfrequentcontact","sms_received_daily_count","sms_received_daily_distinctcontacts","sms_received_daily_timefirstsms","sms_received_daily_timelastsms"
|
||||
"local_date","messages_received_daily_countmostfrequentcontact","messages_received_daily_count","messages_received_daily_distinctcontacts","messages_received_daily_timefirstmessage","messages_received_daily_timelastmessage"
|
||||
"2020-05-28",1,12,12,6,1382
|
||||
"2020-05-29",0,6,6,401,1382
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_received_evening_countmostfrequentcontact","sms_received_evening_count","sms_received_evening_distinctcontacts","sms_received_evening_timefirstsms","sms_received_evening_timelastsms"
|
||||
"local_date","messages_received_evening_countmostfrequentcontact","messages_received_evening_count","messages_received_evening_distinctcontacts","messages_received_evening_timefirstmessage","messages_received_evening_timelastmessage"
|
||||
"2020-05-28",1,3,3,1173,1382
|
||||
"2020-05-29",0,3,3,1173,1382
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_received_morning_countmostfrequentcontact","sms_received_morning_count","sms_received_morning_distinctcontacts","sms_received_morning_timefirstsms","sms_received_morning_timelastsms"
|
||||
"local_date","messages_received_morning_countmostfrequentcontact","messages_received_morning_count","messages_received_morning_distinctcontacts","messages_received_morning_timefirstmessage","messages_received_morning_timelastmessage"
|
||||
"2020-05-28",1,3,3,401,660
|
||||
"2020-05-29",0,3,3,401,660
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_received_night_countmostfrequentcontact","sms_received_night_count","sms_received_night_distinctcontacts","sms_received_night_timefirstsms","sms_received_night_timelastsms"
|
||||
"local_date","messages_received_night_countmostfrequentcontact","messages_received_night_count","messages_received_night_distinctcontacts","messages_received_night_timefirstmessage","messages_received_night_timelastmessage"
|
||||
"2020-05-28",1,4,4,6,312
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_sent_afternoon_countmostfrequentcontact","sms_sent_afternoon_count","sms_sent_afternoon_distinctcontacts","sms_sent_afternoon_timefirstsms","sms_sent_afternoon_timelastsms"
|
||||
"local_date","messages_sent_afternoon_countmostfrequentcontact","messages_sent_afternoon_count","messages_sent_afternoon_distinctcontacts","messages_sent_afternoon_timefirstmessage","messages_sent_afternoon_timelastmessage"
|
||||
"2020-05-28",1,3,3,722,979
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_sent_daily_countmostfrequentcontact","sms_sent_daily_count","sms_sent_daily_distinctcontacts","sms_sent_daily_timefirstsms","sms_sent_daily_timelastsms"
|
||||
"local_date","messages_sent_daily_countmostfrequentcontact","messages_sent_daily_count","messages_sent_daily_distinctcontacts","messages_sent_daily_timefirstmessage","messages_sent_daily_timelastmessage"
|
||||
"2020-05-28",1,8,8,219,1401
|
||||
"2020-05-29",0,4,4,388,1401
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_sent_evening_countmostfrequentcontact","sms_sent_evening_count","sms_sent_evening_distinctcontacts","sms_sent_evening_timefirstsms","sms_sent_evening_timelastsms"
|
||||
"local_date","messages_sent_evening_countmostfrequentcontact","messages_sent_evening_count","messages_sent_evening_distinctcontacts","messages_sent_evening_timefirstmessage","messages_sent_evening_timelastmessage"
|
||||
"2020-05-28",1,2,2,1218,1401
|
||||
"2020-05-29",0,2,2,1218,1401
|
||||
|
|
|
|
@ -1,3 +1,3 @@
|
|||
"local_date","sms_sent_morning_countmostfrequentcontact","sms_sent_morning_count","sms_sent_morning_distinctcontacts","sms_sent_morning_timefirstsms","sms_sent_morning_timelastsms"
|
||||
"local_date","messages_sent_morning_countmostfrequentcontact","messages_sent_morning_count","messages_sent_morning_distinctcontacts","messages_sent_morning_timefirstmessage","messages_sent_morning_timelastmessage"
|
||||
"2020-05-28",1,2,2,388,654
|
||||
"2020-05-29",0,2,2,388,654
|
||||
|
|
|
|
@ -1,2 +1,2 @@
|
|||
"local_date","sms_sent_night_countmostfrequentcontact","sms_sent_night_count","sms_sent_night_distinctcontacts","sms_sent_night_timefirstsms","sms_sent_night_timelastsms"
|
||||
"local_date","messages_sent_night_countmostfrequentcontact","messages_sent_night_count","messages_sent_night_distinctcontacts","messages_sent_night_timefirstmessage","messages_sent_night_timelastmessage"
|
||||
"2020-05-28",1,1,1,219,219
|
||||
|
|
|
|
@ -2,6 +2,7 @@
|
|||
# If you are extracting screen or Barnett's location features, screen and locations tables are mandatory.
|
||||
TABLES_FOR_SENSED_BINS: [messages, calls, screen]
|
||||
|
||||
|
||||
# Participants to include in the analysis
|
||||
# You must create a file for each participant named pXXX containing their device_id. This can be done manually or automatically
|
||||
PIDS: [test01, test02]
|
||||
|
@ -17,8 +18,8 @@ MESSAGES:
|
|||
DB_TABLE: messages
|
||||
TYPES : [received, sent]
|
||||
FEATURES:
|
||||
received: [count, distinctcontacts, timefirstsms, timelastsms, countmostfrequentcontact]
|
||||
sent: [count, distinctcontacts, timefirstsms, timelastsms, countmostfrequentcontact]
|
||||
received: [count, distinctcontacts, timefirstmessage, timelastmessage, countmostfrequentcontact]
|
||||
sent: [count, distinctcontacts, timefirstmessage, timelastmessage, countmostfrequentcontact]
|
||||
DAY_SEGMENTS: *day_segments
|
||||
|
||||
# Communication call features config, TYPES and FEATURES keys need to match
|
||||
|
|
Loading…
Reference in New Issue