Skip to content

Commit

Permalink
Reprex for data.table checkHzDepthLogic #157 #174
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Dec 4, 2020
1 parent f965bec commit 7d80ff3
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions misc/sandbox/datatable-checkHzDepthLogic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
library(data.table)
library(aqp, warn.conflicts = FALSE)

data(sp4)
depths(sp4) <- id ~ top + bottom

aqp_df_class(sp4) <- "data.table"
sp4 <- rebuildSPC(sp4)

# raw data.table; just validity boolean
f1 <- function() horizons(sp4)[, all(!hzDepthTests(top, bottom)), by = id]$V1

# current aqp implementation
f2 <- function() checkHzDepthLogic(sp4)$valid

# fastish check of all logic;
# fast = TRUE: no individual test results;
# fast = FALSE: same as checkHzDepthLogic output
f3 <- function(fast = TRUE) checkAllLogic(sp4, fast)$valid

#' Fast checks of horizon depth logic in entire SoilProfileCollection
#' @param object A SoilProfileCollection
#' @param fast If details about specific test results are not needed, the operation can run approximately 5x faster. Default: TRUE.
#'
#' @return A data.table containing profile IDs, validity boolean and test results if \code{fast = FALSE}
#' @export
checkAllLogic <- function(object, fast = TRUE) {
h <- horizons(object)
hzd <- horizonDepths(object)

hby <- substitute(idname(object))
top <- substitute(hzd[1])
bottom <- substitute(hzd[2])

if (!fast) {
res <- h[, .(tests = list(tests = data.frame(t(hzDepthTests(eval(top), eval(bottom)))))), by = c(eval(hby))][
, .(tests = tests, valid = all(!tests[[1]])), by = c(eval(hby))]
res <- cbind(res, rbindlist(res$tests))
res$tests <- NULL
return(res)
} else {
res <- h[, all(!hzDepthTests(eval(top), eval(bottom))), by = c(eval(hby))]
colnames(res) <- c(idname(object), "valid")
return(res)
}
}

checkAllLogic(sp4)

bench::mark(x <- f1(), # raw data.table
x <- f2(), # checkHzDepthLogic
x <- f3(), # checkAllLogic(fast=TRUE)
x <- f3(fast = FALSE)) # checkAllLogic(fast=FALSE)

0 comments on commit 7d80ff3

Please sign in to comment.