Created
April 22, 2020 20:35
-
-
Save philerooski/8e740e9d19909a074c98ea57bce050a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("synapseClient") | |
synapseLogin() | |
library("plyr") | |
library("dplyr") | |
library("ggplot2") | |
library("jsonlite") | |
library("parallel") | |
library("tidyr") | |
library("lubridate") | |
library("stringr") | |
library("doMC") | |
library("data.table") | |
#devtools::install_github("Sage-Bionetworks/mpowertools") | |
library("mpowertools") | |
WALK_ACTIVITY_TABLE_SYNID = 'syn7222425' | |
actv_walk_syntable <- synTableQuery(paste0('SELECT * FROM ', WALK_ACTIVITY_TABLE_SYNID)) | |
actv_walk <- actv_walk_syntable@values | |
actv_walk$idx <- rownames(actv_walk) | |
attr(actv_walk$createdOn, 'tzone') <- 'UTC' | |
actv_walk <- actv_walk %>% mutate( createdOn = as.character(createdOn)) | |
# # select users | |
# selected_users_walk <- fread(synGet("syn8113982")@filePath, header=T, data.table=F) %>% | |
# .$healthCode %>% as.character() | |
# actv_walk <- actv_walk %>% dplyr::filter(healthCode %in% selected_users_walk) | |
# to enable download of only filtered data | |
# actv_walk_syntable@values <- actv_walk | |
###################### | |
# Download JSON Files | |
###################### | |
#download outbound walking json file | |
outbound_Walking_json_files <- synDownloadTableColumns(actv_walk_syntable, "deviceMotion_walking_outbound.json.items") | |
outbound_Walking_json_files <- data.frame(outbound_Walking_json_fileId =names(outbound_Walking_json_files), | |
outbound_Walking_json_file = as.character(outbound_Walking_json_files)) | |
actv_walk <- merge(actv_walk,outbound_Walking_json_files, | |
by.x="deviceMotion_walking_outbound.json.items", | |
by.y="outbound_Walking_json_fileId", all=T) | |
#download pedometer data | |
outbound_pedometer_json_files <- synDownloadTableColumns(actv_walk_syntable, "pedometer_walking_outbound.json.items") | |
outbound_pedometer_json_files <- data.frame(outbound_pedometer_json_fileId =names(outbound_pedometer_json_files), | |
outbound_pedometer_json_file = as.character(outbound_pedometer_json_files)) | |
actv_walk <- merge(actv_walk, outbound_pedometer_json_files, | |
by.x="pedometer_walking_outbound.json.items", | |
by.y="outbound_pedometer_json_fileId") | |
actv_walk <- actv_walk %>% mutate(outbound_pedometer_json_file=as.character(outbound_pedometer_json_file)) | |
actv_walk <- actv_walk %>% dplyr::select(-accel_walking_outbound.json.items, | |
-accel_walking_return.json.items, | |
-accel_walking_rest.json.items, | |
-deviceMotion_walking_rest.json.items, | |
-pedometer_walking_return.json.items, | |
-deviceMotion_walking_return.json.items | |
) | |
actv_walk$outbound_Walking_json_file <- as.character(actv_walk$outbound_Walking_json_file ) | |
actv_walk$outbound_pedometer_json_file <- as.character(actv_walk$outbound_pedometer_json_file ) | |
############# | |
# Feature Extraction | |
############## | |
registerDoMC(detectCores()-2) | |
#extract pedometere features | |
pedoFeatures <- ddply(.data=actv_walk, | |
.variables=colnames(actv_walk), | |
.parallel=T, | |
.fun = function(row) { | |
tryCatch({ mpowertools::getPedometerFeatures(row$outbound_pedometer_json_file)}, | |
error = function(err){ | |
print(paste0('Unable to process ', row$outbound_pedometer_json_file)) | |
stop(err) }) | |
}) | |
pedoFeatures['error_pedometer_features'] = pedoFeatures$error | |
pedoFeatures$error <- NULL | |
pedoFeatures$outbound_pedometer_json_file <- NULL | |
# | |
# f <- synDownloadTableFile("syn7222425", "35780_4", "deviceMotion_walking_outbound.json.items") | |
# r <- mpowertools::getWalkFeatures(f) | |
# | |
# | |
# f <- synDownloadTableFile("syn7222425", "20315_2", "deviceMotion_walking_outbound.json.items") | |
# mpowertools::getWalkFeatures(f) | |
# | |
# dat <- jsonlite::fromJSON(f) | |
# dat <- mpowertools:::ShapeGaitData(dat) | |
# x <- dat$x | |
# y <- dat$y | |
# z <- dat$z | |
# aa <- sqrt(x^2 + y^2 + z^2) | |
# aj <- sqrt(diff(x)^2 + diff(y)^2 + diff(z)^2) | |
# outX <- mpowertools:::SingleAxisFeatures(x, dat$timestamp, varName = "X") | |
# | |
# mpowertools::: | |
# View(x) | |
# | |
#extract walking features | |
walkFeatures <- ddply(.data=actv_walk, | |
.variables=colnames(actv_walk), | |
.parallel=T, | |
.fun = function(row) { | |
tryCatch({ | |
res <- mpowertools::getWalkFeatures(row$outbound_Walking_json_file) | |
}, | |
error = function(err){ | |
print(paste0('Unable to process ', row$outbound_Walking_json_file)) | |
stop(err) }) | |
}) | |
# walkFeatures | |
walkFeatures['error_walking_features'] = walkFeatures$error | |
walkFeatures$error <- NULL | |
walkFeatures$pedoJsonPath <- NULL | |
walkFeatures$walkingJsonPath <- NULL | |
walkFeatures <- merge(pedoFeatures, walkFeatures, all.x=T, all.y=T) | |
############# | |
# Final Data | |
############# | |
OUTPUT_FOLDER_ID <- "syn7231638" | |
OUTPUT_FILE <- "WalkFeatures.tsv" | |
write.table(walkFeatures, OUTPUT_FILE, sep="\t", row.names=F, quote=F, na="") | |
synStore(File(OUTPUT_FILE, parentId=OUTPUT_FOLDER_ID), | |
used=WALK_ACTIVITY_TABLE_SYNID, | |
executed="https://github.com/Sage-Bionetworks/mPowerAnalysis/blob/master/featureExtraction/restModule.R") | |
unlink(OUTPUT_FILE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment