Refactor call features to produce a single file

replace/e74889acb8c115f5ad69fa14a554563c550e76d7
JulioV 2019-11-06 14:47:33 -05:00
parent 79e126bc92
commit 1d1c8e6bf1
4 changed files with 42 additions and 37 deletions

View File

@ -14,16 +14,10 @@ rule all:
sms_type = config["COM_SMS"]["SMS_TYPES"], sms_type = config["COM_SMS"]["SMS_TYPES"],
day_segment = config["COM_SMS"]["DAY_SEGMENTS"], day_segment = config["COM_SMS"]["DAY_SEGMENTS"],
metric = config["COM_SMS"]["METRICS"]), metric = config["COM_SMS"]["METRICS"]),
expand("data/processed/{pid}/com_call_{call_type}_{segment}_{metric}.csv", expand("data/processed/{pid}/call_{call_type}_{segment}.csv",
pid=config["PIDS"], pid=config["PIDS"],
call_type = config["COM_CALL"]["CALL_TYPE_MISSED"], call_type=config["CALLS"]["TYPES"],
segment = config["COM_CALL"]["DAY_SEGMENTS"], segment = config["CALLS"]["DAY_SEGMENTS"]),
metric = config["COM_CALL"]["METRICS_MISSED"]),
expand("data/processed/{pid}/com_call_{call_type}_{segment}_{metric}.csv",
pid=config["PIDS"],
call_type = config["COM_CALL"]["CALL_TYPE_TAKEN"],
segment = config["COM_CALL"]["DAY_SEGMENTS"],
metric = config["COM_CALL"]["METRICS_TAKEN"]),
expand("data/processed/{pid}/location_barnett.csv", pid=config["PIDS"]), expand("data/processed/{pid}/location_barnett.csv", pid=config["PIDS"]),
expand("data/processed/{pid}/bluetooth_{segment}.csv", expand("data/processed/{pid}/bluetooth_{segment}.csv",
pid=config["PIDS"], pid=config["PIDS"],

View File

@ -30,12 +30,13 @@ COM_SMS:
# Communication call features config # Communication call features config
# Separate configurations for missed and taken calls # Separate configurations for missed and taken calls
COM_CALL: CALLS:
CALL_TYPE_MISSED : [missed] TYPES: [missed, incoming, outgoing]
CALL_TYPE_TAKEN : [incoming, outgoing] METRICS:
missed: [count, distinctcontacts]
incoming: [count, distinctcontacts, meanduration, sumduration, hubermduration, varqnduration, entropyduration]
outgoing: [count, distinctcontacts, meanduration, sumduration, hubermduration, varqnduration, entropyduration]
DAY_SEGMENTS: *day_segments DAY_SEGMENTS: *day_segments
METRICS_MISSED: [count, distinctcontacts]
METRICS_TAKEN: [count, distinctcontacts, meanduration, sumduration, hubermduration, varqnduration, entropyduration]
PHONE_VALID_SENSED_DAYS: PHONE_VALID_SENSED_DAYS:
BIN_SIZE: 5 # (in minutes) BIN_SIZE: 5 # (in minutes)

View File

@ -10,15 +10,15 @@ rule communication_sms_metrics:
script: script:
"../src/features/communication_sms_metrics.R" "../src/features/communication_sms_metrics.R"
rule communication_call_metrics: rule call_metrics:
input: input:
"data/raw/{pid}/calls_with_datetime.csv" "data/raw/{pid}/calls_with_datetime.csv"
params: params:
call_type = "{call_type}", call_type = "{call_type}",
day_segment = "{day_segment}", day_segment = "{day_segment}",
metric = "{metric}" metrics = lambda wildcards: config["CALLS"]["METRICS"][wildcards.call_type]
output: output:
"data/processed/{pid}/com_call_{call_type}_{day_segment}_{metric}.csv" "data/processed/{pid}/call_{call_type}_{day_segment}.csv"
script: script:
"../src/features/communication_call_metrics.R" "../src/features/communication_call_metrics.R"

View File

@ -4,27 +4,37 @@ library(dplyr)
library(entropy) library(entropy)
library(robustbase) library(robustbase)
calls <- read.csv(snakemake@input[[1]]) filter_by_day_segment <- function(data, day_segment) {
day_segment <- snakemake@params[["day_segment"]] if(day_segment %in% c("morning", "afternoon", "evening", "night"))
metric <- snakemake@params[["metric"]] data <- data %>% filter(local_day_segment == day_segment)
type <- snakemake@params[["call_type"]]
output_file <- snakemake@output[[1]]
metrics <- calls %>% filter(call_type == ifelse(type == "incoming", "1", ifelse(type == "outgoing", "2", "3"))) return(data %>% group_by(local_date))
if(day_segment == "daily"){
metrics <- metrics %>% group_by(local_date)
} else {
metrics <- metrics %>% filter(day_segment == local_day_segment) %>% group_by(local_date)
} }
metrics <- switch(metric, compute_call_feature <- function(calls, metric, day_segment){
"count" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := n()), calls <- calls %>% filter_by_day_segment(day_segment)
"distinctcontacts" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := n_distinct(trace)), feature <- switch(metric,
"meanduration" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := mean(call_duration)), "count" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := n()),
"sumduration" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := sum(call_duration)), "distinctcontacts" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := n_distinct(trace)),
"hubermduration" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := huberM(call_duration)$mu), "meanduration" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := mean(call_duration)),
"varqnduration" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := Qn(call_duration)), "sumduration" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := sum(call_duration)),
"entropyduration" = metrics %>% summarise(!!paste("com", "call", type, day_segment, metric, sep = "_") := entropy.MillerMadow(call_duration))) "hubermduration" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := huberM(call_duration)$mu),
"varqnduration" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := Qn(call_duration)),
"entropyduration" = calls %>% summarise(!!paste("call", type, day_segment, metric, sep = "_") := entropy.MillerMadow(call_duration)))
return(feature)
}
write.csv(na.omit(metrics), output_file, row.names = F) calls <- read.csv(snakemake@input[[1]], stringsAsFactors = FALSE)
day_segment <- snakemake@params[["day_segment"]]
metrics <- snakemake@params[["metrics"]]
type <- snakemake@params[["call_type"]]
features = data.frame(local_date = character(), stringsAsFactors = FALSE)
calls <- calls %>% filter(call_type == ifelse(type == "incoming", "1", ifelse(type == "outgoing", "2", "3")))
for(metric in metrics){
feature <- compute_call_feature(calls, metric, day_segment)
features <- merge(features, feature, by="local_date", all = TRUE)
}
write.csv(features, snakemake@output[[1]], row.names = FALSE)