Update location_barnett_metrics.R to handle empty dataframes and location sequences without flights
parent
d2e9b79af6
commit
44bbe17435
|
@ -2,6 +2,26 @@ source("packrat/init.R")
|
||||||
|
|
||||||
library(dplyr)
|
library(dplyr)
|
||||||
|
|
||||||
|
write_empty_file <- function(file_path){
|
||||||
|
write.csv(data.frame(local_date= character(),
|
||||||
|
hometime= numeric(),
|
||||||
|
disttravelled= numeric(),
|
||||||
|
rog= numeric(),
|
||||||
|
maxdiam= numeric(),
|
||||||
|
maxhomedist= numeric(),
|
||||||
|
siglocsvisited= numeric(),
|
||||||
|
avgflightlen= numeric(),
|
||||||
|
stdflightlen= numeric(),
|
||||||
|
avgflightdur= numeric(),
|
||||||
|
stdflightdur= numeric(),
|
||||||
|
probpause= numeric(),
|
||||||
|
siglocentropy= numeric(),
|
||||||
|
minsmissing= numeric(),
|
||||||
|
circdnrtn= numeric(),
|
||||||
|
wkenddayrtn= numeric()
|
||||||
|
), file_path, row.names = F)
|
||||||
|
}
|
||||||
|
|
||||||
# Load Ian Barnett's code. Taken from https://scholar.harvard.edu/ibarnett/software/gpsmobility
|
# Load Ian Barnett's code. Taken from https://scholar.harvard.edu/ibarnett/software/gpsmobility
|
||||||
file.sources = list.files(c("src/features/location_barnett"), pattern="*.R$", full.names=TRUE, ignore.case=TRUE)
|
file.sources = list.files(c("src/features/location_barnett"), pattern="*.R$", full.names=TRUE, ignore.case=TRUE)
|
||||||
sapply(file.sources,source,.GlobalEnv)
|
sapply(file.sources,source,.GlobalEnv)
|
||||||
|
@ -12,13 +32,19 @@ timezone <- snakemake@params[["timezone"]]
|
||||||
location <- read.csv(snakemake@input[[1]], stringsAsFactors = F) %>%
|
location <- read.csv(snakemake@input[[1]], stringsAsFactors = F) %>%
|
||||||
select(timestamp, latitude = double_latitude, longitude = double_longitude, altitude = double_altitude, accuracy)
|
select(timestamp, latitude = double_latitude, longitude = double_longitude, altitude = double_altitude, accuracy)
|
||||||
|
|
||||||
if (nrow(location) > 0){
|
if (nrow(location) > 1){
|
||||||
features <- MobilityFeatures(location, ACCURACY_LIM = accuracy_limit, tz = timezone)
|
features <- MobilityFeatures(location, ACCURACY_LIM = accuracy_limit, tz = timezone)
|
||||||
|
if(is.null(features)){
|
||||||
# Copy index (dates) as a column
|
write_empty_file(snakemake@output[[1]])
|
||||||
outmatrix = cbind(rownames(features$featavg), features$featavg)
|
} else{
|
||||||
colnames(outmatrix)=c("local_date",tolower(colnames(features$featavg)))
|
# Copy index (dates) as a column
|
||||||
write.csv(outmatrix,snakemake@output[[1]], row.names = F)
|
outmatrix <- cbind(rownames(features$featavg), features$featavg)
|
||||||
|
outmatrix <- as.data.frame(outmatrix)
|
||||||
|
outmatrix[-1] <- lapply(lapply(outmatrix[-1], as.character), as.numeric)
|
||||||
|
colnames(outmatrix)=c("local_date",tolower(colnames(features$featavg)))
|
||||||
|
write.csv(outmatrix,snakemake@output[[1]], row.names = F)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
write.csv(data.frame(),snakemake@output[[1]])
|
write_empty_file(snakemake@output[[1]])
|
||||||
}
|
}
|
Loading…
Reference in New Issue