From 3b92594fb7a8992a4bba6ce9dcf39d5e0d3ac999 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Fri, 18 Dec 2015 18:06:28 +0000 Subject: [PATCH] Reorganise R files and connect with python test code --- other.R => analysis.R | 33 ++++++--------------------------- flow.py | 4 ++-- stash.R | 2 ++ test.R | 36 ++++++++++++++++++++++++++++++++++-- test.py | 2 +- xtest.R | 10 +++++++--- 6 files changed, 52 insertions(+), 35 deletions(-) rename other.R => analysis.R (68%) create mode 100644 stash.R diff --git a/other.R b/analysis.R similarity index 68% rename from other.R rename to analysis.R index bb0661b..baba40f 100644 --- a/other.R +++ b/analysis.R @@ -1,34 +1,10 @@ -library(XML) +# Calculate distances (in metres) using the function pointDistance from the 'raster' package. library(raster) -shift.vec <- function (vec, shift) { - if(length(vec) <= abs(shift)) { - rep(NA ,length(vec)) - }else{ - if (shift >= 0) { - c(rep(NA, shift), vec[1:(length(vec)-shift)]) } - else { - c(vec[(abs(shift)+1):length(vec)], rep(NA, abs(shift))) } } } - -pfile <- htmlTreeParse("Rik_Veenboer_2015-11-10_17-21-59.gpx", useInternalNodes = T) - -# Get all elevations, times and coordinates via the respective xpath -elevations <- as.numeric(xpathSApply(pfile, path = "//trkpt/ele", xmlValue)) -times = xpathSApply(pfile, path = "//trkpt/time", xmlValue) -coords <- xpathSApply(pfile, path = "//trkpt", xmlAttrs) - -# Extract latitude and longitude from the coordinates -lats <- as.numeric(coords["lat",]) -lons <- as.numeric(coords["lon",]) -# Put everything in a dataframe and get rid of old variables -geodf <- data.frame(lat = lats, lon = lons, ele = elevations, time = times) -rm(list=c("elevations", "lats", "lons", "pfile", "times", "coords")) -head(geodf) - # Shift vectors for lat and lon so that each row also contains the next position. geodf$lat.p1 <- shift.vec(geodf$lat, -1) geodf$lon.p1 <- shift.vec(geodf$lon, -1) -# Calculate distances (in metres) using the function pointDistance from the 'raster' package. + # Parameter 'lonlat' has to be TRUE! geodf$dist.to.prev <- apply(geodf, 1, FUN = function (row) { pointDistance(c(as.numeric(row["lat.p1"]), @@ -36,12 +12,16 @@ geodf$dist.to.prev <- apply(geodf, 1, FUN = function (row) { c(as.numeric(row["lat"]), as.numeric(row["lon"])), lonlat = T) }) + # Transform the column 'time' so that R knows how to interpret it. geodf$time <- strptime(geodf$time, format = "%Y-%m-%dT%H:%M:%OS") + # Shift the time vector, too. geodf$time.p1 <- shift.vec(geodf$time, -1) + # Calculate the number of seconds between two positions. geodf$time.diff.to.prev <- as.numeric(difftime(geodf$time.p1, geodf$time)) + # Calculate metres per seconds, kilometres per hour and two LOWESS smoothers to get rid of some noise. geodf$speed.m.per.sec <- geodf$dist.to.prev / geodf$time.diff.to.prev geodf$speed.km.per.h <- geodf$speed.m.per.sec * 3.6 @@ -50,7 +30,6 @@ geodf$speed.km.per.h <- ifelse(geodf$speed.km.per.h > 40, 0, geodf$speed.km.per. geodf$lowess.speed <- lowess(geodf$speed.km.per.h, f = 0.01)$y geodf$lowess.ele <- lowess(geodf$ele, f = 0.02)$y - # Plot elevations and smoother plot(geodf$ele, type = "l", bty = "n", xaxt = "n", ylab = "Elevatio", xlab = "", col = "grey40") lines(geodf$lowess.ele, col = "red", lwd = 3) diff --git a/flow.py b/flow.py index d4fb850..fdce862 100644 --- a/flow.py +++ b/flow.py @@ -114,8 +114,8 @@ class Flow: for item in contents: self.downloadTraining(item['listItemId']) - def processTraining(self): - proc = subprocess.Popen(['RScript','test.R', 'Rik_Veenboer_2015-04-25_10-14-43'], stdout = subprocess.PIPE, universal_newlines = True) + def processTraining(self, session): + proc = subprocess.Popen(['RScript','test.R', session], stdout = subprocess.PIPE, universal_newlines = True) while True: line = proc.stdout.readline() if line != '': diff --git a/stash.R b/stash.R new file mode 100644 index 0000000..7645fae --- /dev/null +++ b/stash.R @@ -0,0 +1,2 @@ +cat("\014") +setwd(dirname(parent.frame(2)$ofile)) diff --git a/test.R b/test.R index 0cbf371..e36ae4e 100644 --- a/test.R +++ b/test.R @@ -1,2 +1,34 @@ -args <- commandArgs(trailingOnly = TRUE) -print(args) \ No newline at end of file +library(XML) + +args = commandArgs(trailingOnly = TRUE) +datadir = 'data' +session = args[1] +filename = paste(datadir, '/', session, '.gpx', sep = '') +print(filename) + +shift.vec <- function (vec, shift) { + if(length(vec) <= abs(shift)) { + rep(NA ,length(vec)) + }else{ + if (shift >= 0) { + c(rep(NA, shift), vec[1:(length(vec)-shift)]) } + else { + c(vec[(abs(shift)+1):length(vec)], rep(NA, abs(shift))) } } } + +pfile <- htmlTreeParse("data/Rik_Veenboer_2015-11-10_17-21-59.gpx", useInternalNodes = T) + +# Get all elevations, times and coordinates via the respective xpath +elevations <- as.numeric(xpathSApply(pfile, path = "//trkpt/ele", xmlValue)) +times = xpathSApply(pfile, path = "//trkpt/time", xmlValue) +coords <- xpathSApply(pfile, path = "//trkpt", xmlAttrs) + +# Extract latitude and longitude from the coordinates +lats <- as.numeric(coords["lat",]) +lons <- as.numeric(coords["lon",]) + +# Put everything in a dataframe and get rid of old variables +geodf <- data.frame(lat = lats, lon = lons, ele = elevations, time = times) +rm(list=c("elevations", "lats", "lons", "pfile", "times", "coords")) + +# Output head of frame +print(head(geodf)) \ No newline at end of file diff --git a/test.py b/test.py index e0b5847..8cb47f1 100644 --- a/test.py +++ b/test.py @@ -21,7 +21,7 @@ with Flow() as flow: # print(start <= test <= end) # flow.getEventsInRange(start, end) # flow.parseCalenderEvents() - flow.processTraining() + flow.processTraining('Rik_Veenboer_2015-11-10_17-21-59') sys.exit() diff --git a/xtest.R b/xtest.R index 2e1b9ba..2683ee4 100644 --- a/xtest.R +++ b/xtest.R @@ -2,12 +2,15 @@ #install.packages("plotKML") #install.packages("maptools") #install.packages("gpsbabel") -cat("\014") + library(XML) +cat("\014") +setwd(dirname(parent.frame(2)$ofile)) -file = "Rik_Veenboer_2015-11-10_17-21-59.gpx" -file = "test.gpx" + +file = "data/Rik_Veenboer_2015-11-10_17-21-59.gpx" +#file = "test.gpx" data = xmlParse(file) @@ -28,3 +31,4 @@ colnames(xx) = c("lat", "lon") xxx=merge(result, xx) +