Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Rename R function from AppendSlopeFeatures to append_slope_features #25

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ someDT <- data.table::data.table(
, assetId = c(rep("ABC", 5), rep("DEF", 5))
)

groundhog::AppendSlopeFeatures(someDT, hostName = "localhost", port = 5005)
groundhog::append_slope_features(someDT, hostName = "localhost", port = 5005)
```

### Python client
Expand Down
2 changes: 2 additions & 0 deletions r-client/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
^run_tests.R$

^.*\.Rproj$
^\.Rproj\.user$
2 changes: 1 addition & 1 deletion r-client/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(AppendSlopeFeatures)
export(append_slope_features)
import(data.table)
importFrom(assertthat,assert_that)
importFrom(assertthat,has_name)
Expand Down
51 changes: 25 additions & 26 deletions r-client/R/client.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

#' @title Slope and Elevation Features
#' @name AppendSlopeFeatures
#' @name append_slope_features
#' @description This function takes a \code{\link{data.table}} with GPS coordinates
#' and enriches it with some elevation and slope features. The function
#' hits the public Shuttle Radar Topography Mission (SRTM) dataset
Expand All @@ -16,9 +15,9 @@
#' in degrees. If not supplied, this will be calculated
#' from consecutive observations}
#' }
#' @param hostName A string with the host running groundhog. By default, this
Copy link

@Haoen-Cui Haoen-Cui Aug 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jayqi: can you ignore white space changes?
Also occurred multiple times elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Haoen-Cui if you click on the Gear icon when viewing the diff, you can hide whitespace changes in the UI.

Typically, we consider not-having-trailing-whitespace to be a good best practice, and we encourage people to turn on automatic-stripping in RStudio and other text editors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it's totally fine to have your editor touch these other lines. I'm cool with it.

#' @param hostName A string with the host running groundhog. By default, this
#' function expects that you're running the app on \code{localhost}.
#' @param port Port that the service is running on. 5005 by default. You PROBABLY
#' @param port Port that the service is running on. 5005 by default. You PROBABLY
#' won't ever have to change this.
#' @importFrom assertthat assert_that has_name
#' @importFrom data.table := as.data.table key rbindlist setnames setorderv
Expand All @@ -29,7 +28,7 @@
#' @examples
#' \dontrun{
#' library(data.table)
#'
#'
#' # Create a sample dataset
#' someDT <- data.table::data.table(
#' longitude = runif(10, -110, -109)
Expand All @@ -39,41 +38,41 @@
#' , length.out = 10)
#' , assetId = c(rep("ABC", 5), rep("DEF", 5))
#' )
#'
#'
#' # Append slope fearures
#' groundhog::AppendSlopeFeatures(someDT, hostName = "localhost", port = 5005)
#' groundhog::append_slope_features(someDT, hostName = "localhost", port = 5005)
#' }
AppendSlopeFeatures <- function(DT
append_slope_features <- function(DT
, hostName = "localhost"
, port = 5005
){

assertthat::assert_that(
all(c("dateTime", "assetId", "latitude", "longitude") %in% names(DT))
)

# Build a join table on the original DT. We'll ship this "unique_key"
# with the request and use it to join client-side. Otherwise, joining on
# lat-lon can fail because of different precision levels
#
# Sorting these so we know the results can be appended to DT in place
joinKeys <- sort(sapply(1:nrow(DT), uuid::UUIDgenerate))

if ("bearing" %in% names(DT)){
tempDT <- DT[, .(assetId, dateTime, latitude, longitude, bearing, unique_key = joinKeys)]
} else {
tempDT <- DT[, .(assetId, dateTime, latitude, longitude, unique_key = joinKeys)]
}

# Grab a JSON payload for each asset in DT
assets <- tempDT[, unique(assetId)]
log_info(sprintf("Running groundhog for %s assets", length(assets)))

# Submit one request per asset
payloads <- lapply(assets
, FUN = function(asset, DT){.GetPayloadJSON(tempDT[assetId == asset])}
, DT = tempDT)

# Submit requests
assetNumber <- 1
numAssets <- length(assets)
Expand All @@ -89,17 +88,17 @@ AppendSlopeFeatures <- function(DT
}
, hostName = hostName
, port = port)

# Parse into one data.table
resultDT <- data.table::rbindlist(
responseList
, fill = TRUE
)

data.table::setnames(resultDT, old = c("geo_point.lat", "geo_point.lon")
, new = c("latitude", "longitude"))
# Instead of copying the whole DT (which could be yuge), we can do a join on

# Instead of copying the whole DT (which could be yuge), we can do a join on
# the relevant fields then do in-place assignments....check this out
joinDT <- merge(
x = tempDT
Expand All @@ -109,12 +108,12 @@ AppendSlopeFeatures <- function(DT
, sort = FALSE
)
data.table::setorderv(joinDT, "unique_key")

# Hopefully nothing broke
assertthat::assert_that(nrow(DT) == nrow(joinDT)
, is.null(data.table::key(joinDT))
, identical(joinDT[, unique_key], tempDT[, unique_key]))

# Add any cols that we got from the API
# NOTE: ignoring unique_key and stride
newCols <- base::setdiff(names(resultDT), c(names(DT), "unique_key", "stride"))
Expand All @@ -123,7 +122,7 @@ AppendSlopeFeatures <- function(DT
log_info(sprintf("Appending %s", newCol))
DT[, (newCol) := `__values__`]
}

log_info("Done appending elevation features")
return(invisible(NULL))
}
Expand All @@ -138,14 +137,14 @@ AppendSlopeFeatures <- function(DT
#' @importFrom httr add_headers content RETRY stop_for_status
#' @importFrom jsonlite fromJSON
.GroundhogQuery <- function(hostName, port, payloadJSON){

response <- httr::RETRY(verb = "POST"
, paste0("http://", hostName, ":", port, "/groundhog")
, body = payloadJSON
, httr::add_headers("Content-Type" = "application/json")
, times = 5)
httr::stop_for_status(response)

responseDT <- data.table::as.data.table(
jsonlite::fromJSON(
# Suppress that annoying message about encodings from httr::content
Expand All @@ -168,7 +167,7 @@ AppendSlopeFeatures <- function(DT
#' @importFrom data.table setorderv
#' @importFrom jsonlite toJSON
.GetPayloadJSON <- function(DT){

# TODO: fix this if statement it's gross
if ("bearing" %in% names(DT)){
uniqueDT <- unique(
Expand All @@ -183,11 +182,11 @@ AppendSlopeFeatures <- function(DT
, by = c('latitude', 'longitude')
)
}

# Order the table in ascending order by date to be sure
# bearing calcs work correctly
data.table::setorderv(uniqueDT, c("dateTime"))
uniqueDT[, dateTime := NULL]

return(jsonlite::toJSON(uniqueDT))
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions r-client/tests/testthat/test-client.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

context("\n>> AppendSlopeFeatures")
context("\n>> append_slope_features")

# Configure logger (suppress all logs in testing)
loggerOptions <- futile.logger::logger.options()
if (!identical(loggerOptions, list())){
origLogThreshold <- loggerOptions[[1]][['threshold']]
origLogThreshold <- loggerOptions[[1]][['threshold']]
} else {
origLogThreshold <- futile.logger::INFO
}
futile.logger::flog.threshold(0)

test_that("AppendSlopeFeatures should work", {
test_that("append_slope_features should work", {

# Make a test dataset
someDT <- data.table::data.table(
longitude = runif(10, -110, -109)
Expand All @@ -21,12 +21,12 @@ test_that("AppendSlopeFeatures should work", {
, length.out = 10)
, assetId = c(rep("ABC", 5), rep("DEF", 5))
)

# Append features
groundhog::AppendSlopeFeatures(someDT
groundhog::append_slope_features(someDT
, hostName = "localhost"
, port = 5005)

expect_true(data.table::is.data.table(someDT))
expect_true(nrow(someDT) == 10)
expect_named(someDT
Expand Down