From a88bb1edee1d3e32c3fa808f0ca1cb89c66c6776 Mon Sep 17 00:00:00 2001 From: Henrik Baktoft Date: Fri, 20 Oct 2023 12:45:02 +0200 Subject: [PATCH] too many adjustments... --- DESCRIPTION | 4 +- NAMESPACE | 3 + R/checkHydros.R | 21 +++ R/checkInp.R | 15 +- R/checkInpSync.R | 30 ---- R/getBounds.R | 54 +++++++ R/getDatTmb.R | 83 +++++++++++ R/getDistMat.R | 2 +- R/getInits.R | 59 ++++++++ R/getInp.R | 3 + R/getInpParams.R | 14 ++ R/getParams.R | 59 ++++++++ R/getParamsXYFromCOA.R | 18 +++ R/getToaRbi.R | 111 ++++++++++++++ R/prepTmb.R | 287 ------------------------------------- R/removeMultipath.R | 12 ++ R/runYaps.R | 2 +- R/sync_checkInpSyncData.R | 16 +-- R/sync_checkSyncCoverage.R | 30 ++++ R/sync_getDatTmbSync.R | 2 +- R/sync_getInpSync.R | 2 +- R/sync_plotSyncCheck.R | 8 +- R/sync_plotSyncNetwork.R | 8 +- R/yapsify.R | 24 ++-- man/checkHydros.Rd | 11 ++ man/getSyncCoverage.Rd | 2 +- man/getToaRbi.Rd | 11 ++ man/removeMultipath.Rd | 11 ++ man/yapsify.Rd | 11 ++ 29 files changed, 555 insertions(+), 358 deletions(-) create mode 100644 R/checkHydros.R create mode 100644 R/getBounds.R create mode 100644 R/getDatTmb.R create mode 100644 R/getInits.R create mode 100644 R/getInpParams.R create mode 100644 R/getParams.R create mode 100644 R/getParamsXYFromCOA.R create mode 100644 R/getToaRbi.R create mode 100644 R/removeMultipath.R create mode 100644 R/sync_checkSyncCoverage.R create mode 100644 man/checkHydros.Rd create mode 100644 man/getToaRbi.Rd create mode 100644 man/removeMultipath.Rd create mode 100644 man/yapsify.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 9d1093a..87681cc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,9 +12,9 @@ License: GPL-3 Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.1 +RoxygenNote: 7.2.3 LinkingTo: Rcpp, TMB, RcppEigen -Imports: circular, cowplot, data.table, ggplot2, ggrepel, nloptr, plyr, Rcpp, reshape2, splusTimeSeries, stats, TMB, viridis, zoo +Imports: circular, cowplot, data.table, ggplot2, ggrepel, nloptr, plyr, Rcpp, reshape2, stats, TMB, viridis, zoo Suggests: caTools, covr, diff --git a/NAMESPACE b/NAMESPACE index 9fb7add..e53d53c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(alignBurstSeq) export(applyLinCor) export(applySync) export(applySync_old) +export(checkHydros) export(checkInp) export(checkInpSync) export(checkInpSyncData) @@ -29,6 +30,7 @@ export(getSyncCheckDat) export(getSyncCoverage) export(getSyncModel) export(getSyncToa) +export(getToaRbi) export(getToaYaps) export(plotBbox) export(plotSyncCheck) @@ -41,6 +43,7 @@ export(plotSyncNetwork) export(plotYaps) export(prepDetections) export(prepSyncParams) +export(removeMultipath) export(runTmb) export(runYaps) export(simHydros) diff --git a/R/checkHydros.R b/R/checkHydros.R new file mode 100644 index 0000000..e2c9600 --- /dev/null +++ b/R/checkHydros.R @@ -0,0 +1,21 @@ +#' Internal function to check hydros data.tables +#' @export +checkHydros <- function(hydros){ + if(is.null(attr(hydros, 'yapsified'))){ + cat("ERROR: The hydros data.table is not yapsified. Run e.g. hydros <- yapsify(hydros) to do so. \n") + stopSilent() + } + + if(!is.null(attr(hydros, 'yapsified')) & attr(hydros, 'yapsified') == FALSE){ + cat("ERROR: The hydros data.table is not yapsified. Run e.g. hydros <- yapsify(hydros) to do so. \n") + stopSilent() + } + + if(length(unique(hydros$h_sn)) != nrow(hydros)){ + cat("ERROR: At least one hydrophone serial number is used more than once in sync_dat$hydros!\n") + stopSilent() + } + + + +} \ No newline at end of file diff --git a/R/checkInp.R b/R/checkInp.R index 4dcfe44..b4b137e 100644 --- a/R/checkInp.R +++ b/R/checkInp.R @@ -10,13 +10,17 @@ checkInp <- function(inp){ # only relevant for ping_types 'rbi' and 'pbi'? if(inp$datTmb$pingType != 'sbi'){ if(inp$datTmb$rbi_min > min(diff(inp$params$top))) { - print(paste0("WARNING: inp$datTmb$rbi_min > min(diff(inp$params$top)) | ",inp$datTmb$rbi_min," > ",min(diff(inp$params$top)),"")) - inp$datTmb$rbi_min <- min(diff(inp$params$top)) * 0.90 + cat(paste0("ERROR: inp$datTmb$rbi_min > min(diff(inp$params$top)) | ",inp$datTmb$rbi_min," > ",min(diff(inp$params$top)),"\n")) + # inp$datTmb$rbi_min <- min(diff(inp$params$top)) * 0.90 + # cat("...inp$datTmb$rbi_min adjusted to ", inp$datTmb$rbi_min, "\n") + stopSilent() } if(inp$datTmb$rbi_max < max(diff(inp$params$top))){ - print(paste0("WARNING: inp$datTmb$rbi_max < max(diff(inp$params$top)) | ",inp$datTmb$rbi_max," < ",max(diff(inp$params$top)),"")) - inp$datTmb$rbi_max <- max(diff(inp$params$top)) * 1.10 + cat(paste0("ERROR: inp$datTmb$rbi_max < max(diff(inp$params$top)) | ",inp$datTmb$rbi_max," < ",max(diff(inp$params$top)),"\n")) + # inp$datTmb$rbi_max <- max(diff(inp$params$top)) * 1.10 + # cat("...inp$datTmb$rbi_max adjusted to ", inp$datTmb$rbi_max, "\n") + stopSilent() } } @@ -40,7 +44,4 @@ checkInp <- function(inp){ } print("Pre-flight checkInp() passed!") - - return(inp) - } \ No newline at end of file diff --git a/R/checkInpSync.R b/R/checkInpSync.R index 55b196c..e824d59 100644 --- a/R/checkInpSync.R +++ b/R/checkInpSync.R @@ -56,33 +56,3 @@ checkInpSync <- function(inp_sync, silent_check){ } -#' Quick overview to check if all hydros have enough data within each offset period. -#' -#' @inheritParams checkInpSync -#' @param plot Logical indicating whether to plot a visual or not. -#' @export -#' @return A data.table containing number of pings included in each hydro x offset combination. -#' @example man/examples/example-yaps_ssu1.R -getSyncCoverage <- function(inp_sync, plot=FALSE){ - toa <- inp_sync$dat_tmb_sync$toa - nh <- ncol(toa) - offset_idx <- inp_sync$dat_tmb_sync$offset_idx - - toa_long <- data.table::data.table(reshape2::melt(toa)) - colnames(toa_long) <- c('ping', 'h','toa') - toa_long[, offset_idx := rep(offset_idx, nh)] - sync_coverage <- data.table::data.table(reshape2::melt(with(toa_long[!is.na(toa)], table(h, offset_idx)))) - colnames(sync_coverage) <- c('h', 'offset_idx' ,'N') - - if(plot){ - p <- ggplot2::ggplot(sync_coverage) - p <- p + geom_point(aes(offset_idx, N), col="steelblue") - p <- p + geom_point(data=sync_coverage[N < 50], aes(offset_idx, N), col="blue", size=2) - p <- p + geom_point(data=sync_coverage[N < 10], aes(offset_idx, N), col="orange", size=2) - p <- p + geom_point(data=sync_coverage[N <= 5], aes(offset_idx, N), col="red", size=2) - p <- p + facet_wrap(~h) + ylim(0, max(sync_coverage$N)) - print(p) - } - - return(sync_coverage) -} \ No newline at end of file diff --git a/R/getBounds.R b/R/getBounds.R new file mode 100644 index 0000000..f5dd242 --- /dev/null +++ b/R/getBounds.R @@ -0,0 +1,54 @@ +#' Get bounds restricting the optimizer +#* +#' Compile a matrix of lower (bounds[,1]) and upper (bounds[,2]) bounds for the parameters to be estimated. +#' @param datTmb Object obtained using getDatTmb() +#' @return Matrix of bounds restricting the optimizer when running runYaps(). +#' @noRd +getBounds <- function(datTmb) { + lu_logD_xy <- c(-50, 2) + lu_logD_z <- c(-50, 2) + + lu_logSigma_toa <- c(-12, -2) + if(datTmb$Edist[2] == 1){ # mixture + lu_logScale <- c(-30, 10) + } else if (datTmb$Edist[3] == 1) { # t + lu_logScale <- c(-10,2) + } + lu_log_t_part <- c(-100, 100) + if(datTmb$pingType == 'rbi'){ + lu_logSigma_bi <- c(-20, 20) + } else { + lu_logSigma_bi <- c(-20, -2) + } + lu_logD_v <- c(-20, 2) + + bounds <- c() + bounds <- rbind(bounds, lu_logD_xy) + + if(datTmb$how_3d == 'est'){ + bounds <- rbind(bounds, lu_logD_z) + } + + if(datTmb$ss_data_what == 'est'){ + bounds <- rbind(bounds, lu_logD_v) + } + + if(datTmb$Edist[1] == 1){ + bounds <- rbind(bounds, lu_logSigma_toa) + } else if(datTmb$Edist[2] == 1){ + bounds <- rbind(bounds, lu_logSigma_toa, lu_logScale, lu_log_t_part) + } else if(datTmb$Edist[3] == 1){ + bounds <- rbind(bounds, lu_logScale) + } + + if(datTmb$pingType == 'sbi'){ + bounds <- rbind(bounds, lu_logSigma_bi) + } else if (datTmb$pingType == 'rbi'){ + bounds <- rbind(bounds, lu_logSigma_bi) + } else if (datTmb$pingType == 'pbi'){ + bounds <- rbind(bounds, lu_logSigma_bi) + } + + return(bounds) +} + diff --git a/R/getDatTmb.R b/R/getDatTmb.R new file mode 100644 index 0000000..60f1547 --- /dev/null +++ b/R/getDatTmb.R @@ -0,0 +1,83 @@ +#' Internal function - get data for input to TMB +#' +#' Compile data for input to TMB. +#' @param inp_params Selection of parameters used to setup and run YAPS. +#' @inheritParams getInp +#' +#' @return List for use in TMB. +#' @noRd +getDatTmb <- function(hydros, toa, E_dist, n_ss, pingType, rbi_min, rbi_max, ss_data_what, ss_data, biTable, inp_params, z_vec, bbox){ + T0 <- inp_params$T0 + Hx0 <- inp_params$Hx0 + Hy0 <- inp_params$Hy0 + + toa <- toa - T0 + + # allowing slight out-of-bounds BIs + rbi_min <- rbi_min - rbi_min * 0.05 + rbi_max <- rbi_max + rbi_max * 0.05 + + # attempting to make sure toa is oriented correct + if(!nrow(toa) == nrow(hydros)){ + toa <- t(toa) + } + + if(n_ss > 1){ + ss_idx <- cut(1:ncol(toa), n_ss, labels=FALSE) - 1 #-1 because zero-indexing in TMB + } else { + ss_idx <- rep(0, ncol(toa)) + } + approxBI <- mean(diff(colMeans(toa, na.rm=TRUE), na.rm=TRUE), na.rm=TRUE) + + if(ss_data_what == 'data') { stopifnot(length(ss_data) == ncol(toa))} + + Edist <- rep(0,3) + if(E_dist == "Gaus") {Edist[1] <- 1} + if(E_dist == "Mixture") {Edist[2] <- 1} + if(E_dist == "t") {Edist[3] <- 1} + + if(is.null(z_vec)){ + how_3d <- 'none' + z_vec <- c(1) + } else if(z_vec[1] == "est") { + how_3d <- 'est' + z_vec <- c(1) + } else { + how_3d <- 'data' + } + + if(is.null(bbox)){ + bbox <- NA + } else { + bbox[1] <- bbox[1] - inp_params$Hx0 + bbox[2] <- bbox[2] - inp_params$Hx0 + bbox[3] <- bbox[3] - inp_params$Hy0 + bbox[4] <- bbox[4] - inp_params$Hy0 + } + + datTmb <- list( + model = "yaps_track", + H = matrix(c(hydros$h_x-Hx0, hydros$h_y-Hy0, hydros$h_z), ncol=3), + nh = nrow(hydros), + np = ncol(toa), + Edist = Edist, + toa = toa, + bi_epsilon = 1E-6, + bi_penalty = 1E9, + rbi_min = rbi_min, + rbi_max = rbi_max, + pingType = pingType, + n_ss = n_ss, + ss_idx = ss_idx, + ss_data_what = ss_data_what, + ss_data = ss_data, + approxBI = approxBI, + biTable = c(1), + how_3d = how_3d, + z_vec = z_vec, + bbox = bbox + ) + if(pingType == 'pbi') {datTmb$biTable = biTable} + + return(datTmb) +} diff --git a/R/getDistMat.R b/R/getDistMat.R index 600c7be..4f128d0 100644 --- a/R/getDistMat.R +++ b/R/getDistMat.R @@ -6,7 +6,7 @@ getDistMat <- function(hydros){ colnames(dist_mat) <- hydros$h_sn for(hx in 1:nrow(hydros)){ for(hy in 1:nrow(hydros)){ - dist_mat[hx, hy] <- sqrt((hydros[hx, x] - hydros[hy, x] )^2 + (hydros[hx, y] - hydros[hy, y] )^2 + (hydros[hx, z] - hydros[hy, z] )^2) + dist_mat[hx, hy] <- sqrt((hydros[hx, h_x] - hydros[hy, h_x] )^2 + (hydros[hx, h_y] - hydros[hy, h_y] )^2 + (hydros[hx, h_z] - hydros[hy, h_z] )^2) } } return(dist_mat) diff --git a/R/getInits.R b/R/getInits.R new file mode 100644 index 0000000..bdb768f --- /dev/null +++ b/R/getInits.R @@ -0,0 +1,59 @@ +#' Get inits for use in TMB +#' +#' Compile a vector of initial values to use in TMB. One value for each estimated parameter (not random effects). +#' Should all be in a credible range. +#' @param datTmb Object obtained using getDatTmb() +#' @inheritParams getInp +#' @return Vector of initial values to use in TMB +#' @noRd +getInits <- function(datTmb, sdInits=1) { + init_logD_xy <- -1 + + if(datTmb$pingType == 'sbi') { + init_logSigma_bi <- -6 + } else if(datTmb$pingType == 'rbi'){ + if(datTmb$rbi_max >= 10){ + init_logSigma_bi <- 4 + } else { + init_logSigma_bi <- -3 + } + } else if(datTmb$pingType == 'pbi'){ + init_logSigma_bi <- -5 + } + + init_logD_v <- 0 + init_logSigma_toa <- -3 # used in Gaussian and mixture + init_logScale <- 1 # used in mixture and pure t + init_log_t_part <- -4 # only used in mixture + + inits <- c(init_logD_xy) + + if(datTmb$how_3d == 'est'){ + init_logD_z <- 0 + inits <- c(inits, init_logD_z) + } + + if(datTmb$ss_data_what == 'est'){ + inits <- c(inits, init_logD_v) + } + + + if(datTmb$Edist[1] == 1){ + inits <- c(inits, init_logSigma_toa) + } else if(datTmb$Edist[2] == 1){ + inits <- c(inits, init_logSigma_toa, init_logScale, init_log_t_part) + } else if(datTmb$Edist[3] == 1){ + inits <- c(inits, init_logScale) + } + + if(datTmb$pingType == 'sbi'){ + inits <- c(inits, init_logSigma_bi)#, init_logD_v)#, init_logSigma_toa, init_logScale, init_log_t_part) + } else if (datTmb$pingType == 'rbi'){ + inits <- c(inits, init_logSigma_bi)#, init_logD_v)#, init_logSigma_toa, init_logScale, init_log_t_part) + } else if (datTmb$pingType == 'pbi'){ + inits <- c(inits, init_logSigma_bi)#, init_logD_v)#, init_logSigma_toa, init_logScale, init_log_t_part) + } + + inits <- stats::rnorm(length(inits), mean=inits, sd=sdInits) + return(inits) +} diff --git a/R/getInp.R b/R/getInp.R index ce98ff3..95660d9 100644 --- a/R/getInp.R +++ b/R/getInp.R @@ -19,6 +19,9 @@ #' @example man/examples/example-yaps_ssu1.R getInp <- function(hydros, toa, E_dist, n_ss, pingType, sdInits=1, rbi_min=0, rbi_max=0, ss_data_what='est', ss_data=0, biTable=NULL, z_vec=NULL, bbox=NULL){ stopifnot(pingType %in% c('sbi', 'pbi', 'rbi')) + + checkHydros(hydros) + inp_params <- getInpParams(hydros, toa, pingType) datTmb <- getDatTmb(hydros, toa, E_dist, n_ss, pingType, rbi_min, rbi_max, ss_data_what, ss_data, biTable, inp_params, z_vec, bbox) params <- getParams(datTmb) diff --git a/R/getInpParams.R b/R/getInpParams.R new file mode 100644 index 0000000..78dcf9c --- /dev/null +++ b/R/getInpParams.R @@ -0,0 +1,14 @@ +#' Get parameters for this specific data set +#' +#' Compile a list of relevant parameters (e.g. T0) to use later on +#' @inheritParams getInp +#' @noRd +getInpParams <- function(hydros, toa, pingType){ + T0 <- min(toa, na.rm=TRUE) + + Hx0 <- hydros[1,h_x] + Hy0 <- hydros[1,h_y] + + return(list(T0=T0, Hx0=Hx0, Hy0=Hy0)) + +} \ No newline at end of file diff --git a/R/getParams.R b/R/getParams.R new file mode 100644 index 0000000..12ae007 --- /dev/null +++ b/R/getParams.R @@ -0,0 +1,59 @@ +#' Internal function - get params-list for use in TMB +#' +#' Compile a list of parameters for use in TMB. +#' @param datTmb Object obtained using getDatTmb() +#' @return List of params for use in TMB +#' @noRd +getParams <- function(datTmb){ + params_XY <- yaps:::getParamsXYFromCOA(datTmb) + out <- list( + X = params_XY$X + stats::rnorm(ncol(datTmb$toa), sd=10) + , Y = params_XY$Y + stats::rnorm(ncol(datTmb$toa), sd=10) + # X = 0 + stats::rnorm(ncol(datTmb$toa), sd=10) + # , Y = 0 + stats::rnorm(ncol(datTmb$toa), sd=10) + , top = zoo::na.approx(apply(datTmb$toa, 2, function(k) {stats::median(k, na.rm=TRUE)}), rule=2) #time of ping + # , SS=stats::rnorm(datTmb$n_ss, 1450, 5) #speed of sound + , logD_xy = 0 #diffusivity of transmitter movement (D_xy in ms) + # , logD_v = 0 #diffusivity of speed of sound (D_v in ms) + # , logSigma_toa = 0 #sigma for Gaussian + # , logScale = 0 #scale parameter for t-distribution + # , log_t_part = 0 #Mixture ratio between Gaussian and t + ) + + # # # If estimating 3D + if(datTmb$how_3d == "est"){ + out$Z <- stats::runif(ncol(datTmb$toa), -10, 0) + out$logD_z <- 0 #diffusivity of transmitter vertical movement (D_z in ms) + } + + + # # # ss related + if(datTmb$ss_data_what == 'est'){ + out$logD_v <- 0 #diffusivity of speed of sound (D_v in ms) + out$SS <- stats::rnorm(datTmb$n_ss, 1450, 5) #speed of sound + } + + # # # Edist related + if(datTmb$Edist[1] == 1){ + out$logSigma_toa = 0 #sigma for Gaussian + } else if(datTmb$Edist[2] == 1){ + out$logSigma_toa = 0 #sigma for Gaussian + out$logScale = 0 #scale parameter for t-distribution + out$log_t_part = 0 #Mixture ratio between Gaussian and t + } else if(datTmb$Edist[3] == 1){ + out$logScale = 0 #scale parameter for t-distribution + } + + # # # Ping type related + if(datTmb$pingType %in% c('sbi', 'sbi_double', 'rbi')){ + out$logSigma_bi <- 0 #sigma burst interval (sigma_bi in ms) + } + + if(datTmb$pingType == 'pbi'){ + out$logSigma_bi <- 0 #sigma burst interval (sigma_bi in ms) + out$tag_drift <- stats::rnorm(datTmb$np, 0, 1e-2) + } + + + return(out) +} diff --git a/R/getParamsXYFromCOA.R b/R/getParamsXYFromCOA.R new file mode 100644 index 0000000..2175756 --- /dev/null +++ b/R/getParamsXYFromCOA.R @@ -0,0 +1,18 @@ +#' Internal function - get initial values for X and Y based on Center Of Activity - i.e. hydrophones positions +#' +#' Attempts to give meaningful initial values for X and Y based on which hydros detected each ping +#' @inheritParams getInp +#' @noRd +getParamsXYFromCOA <- function(datTmb){ + toa <- datTmb$toa + hydros <- datTmb$H + + toa_detect <- toa + toa_detect[!is.na(toa_detect)] <- 1 + + X <- zoo::na.approx(colMeans((toa_detect) * hydros[,1], na.rm=TRUE)) + Y <- zoo::na.approx(colMeans((toa_detect) * hydros[,2], na.rm=TRUE)) + + return(list(X=X, Y=Y)) + +} diff --git a/R/getToaRbi.R b/R/getToaRbi.R new file mode 100644 index 0000000..a1f5af5 --- /dev/null +++ b/R/getToaRbi.R @@ -0,0 +1,111 @@ +#' Get TOA matrix for random burst interval transmitters +#' @export +getToaRbi <- function(dets, hydros, rbi_min, rbi_max){ + + data.table::setorder(dets, eposync) + + dets <- merge(dets, hydros[, .(h_sn, h_idx)], all.x=TRUE) + + # # # TODO + # # # Consider to add a better function to remove too short BIs... + # # # ...current version is blind to e.g. nobs + + + # remove multipath... + dets <- removeMultipath(dets, mp_threshold=0.5) + + # build seq to catch pings + seq_factor <- ifelse(rbi_min < 10, 0.1, 1.0) + seq <- matrix(seq(from=floor(min(dets$eposync)), to=ceiling(max(dets$eposync)), by=seq_factor)) + nobs <- apply(seq, 1, function(k) nrow(dets[eposync %between% c(k-.5*seq_factor, k+.5*seq_factor)])) + + # which times along seq have pings + hits <- data.table(epo = seq[nobs > 0], nobs=nobs[nobs > 0]) + + # identify lines representing new pings + # threshold for new ping is dependent on rbi_min. For very fast transmitters, we need a lower threshold + # threshold could be based on some metric of max possible distance in area...? + new_ping_thres <- ifelse(rbi_min >= 5, 1, .5) + if(rbi_min >= 2){ + hits[, new_ping := c(1, ifelse(diff(epo) > new_ping_thres, 1, 0))] + } else { + hits[, new_ping := c(1, ifelse(diff(epo) > new_ping_thres, 1, 0))] + } + + hits[, ping := cumsum(new_ping)] + + # identify median time for each ping + ping_times <- hits[, .(t_ping = median(epo), nobs=sum(nobs)), by=ping] + + # prep to roll hits and dets + ping_times[, roll_ping_times := t_ping] + dets[, roll_dat := eposync] + setkey(dets, roll_dat) + setkey(ping_times, roll_ping_times) + + dets[, ping := ping_times[dets, roll="nearest"] [, ping] ] + dets[, roll_dat := NULL] + + # pings only detected by one hydro are useless for ping_type = rbi + # excluded as they might as well be noise + dets <- dets[!ping %in% dets[, .N, by=ping][N <= 1, ping]] + + # take the first if more than one detection from one hydro per ping has survived so far... + dets <- dets[, .(eposync=min(eposync)), by=.(h_idx, ping)] + + # ...re-adjust ping number to start from 1 and be continous + dets[, new_ping := ping != shift(ping, type="lag")] + dets[1, new_ping := TRUE] + dets[, ping := cumsum(new_ping)] + dets[, new_ping := NULL] + + + # identify and remove pings with too short BIs + ping_times2 <- dets[, .(ping_time=median(eposync), .N), by=ping] + ping_times2[, ping_diff := c(diff(ping_time), NA)] + if(rbi_min > 5){ + ping_times2[, next_ping_too_soon := ping_diff < rbi_min-1] + } else { + ping_times2[, next_ping_too_soon := ping_diff < rbi_min-.5] + } + + dets <- dets[ping %in% ping_times2[next_ping_too_soon == FALSE, ping]] + + # ...re-adjust ping number to start from 1 and be continous + dets[, new_ping := ping != shift(ping, type="lag")] + dets[1, new_ping := TRUE] + dets[, ping := cumsum(new_ping)] + dets[, new_ping := NULL] + + + # build toa matrix + dets[, h_idx_factor := factor(h_idx, levels=(1:nrow(hydros)))] + toa <- as.matrix(reshape2::acast(dets, ping~h_idx_factor, value.var="eposync", drop=FALSE)) + dets[, h_idx_factor := NULL] + + + # remake toa-matrix to include pings missed by all hydros... + pings <- dets[, .(top=median(eposync)), by=ping] + pings[, diff := c(diff(top), NA)] + pings[, ping2next := 1] + + if(rbi_max > 10){ + pings[, next_ping_too_late := diff > rbi_max+1] + } else { + pings[, next_ping_too_late := diff > rbi_max+.5] + } + + pings[next_ping_too_late==TRUE, ping2next:=ceiling(diff/(rbi_max-1))] + + pings[, ping_idx:=cumsum(c(1,ping2next[-.N]))] + + toa_all <- matrix(ncol=ncol(toa), nrow=max(pings$ping_idx)) + toa_all[pings$ping_idx, ] <- toa + + toa <- toa_all + + diff(rowMeans(toa, na.rm=TRUE)) + + # print(p1) + return(toa) +} diff --git a/R/prepTmb.R b/R/prepTmb.R index 7ebd969..fd40910 100644 --- a/R/prepTmb.R +++ b/R/prepTmb.R @@ -1,291 +1,4 @@ -#' Internal function - get data for input to TMB -#' -#' Compile data for input to TMB. -#' @param inp_params Selection of parameters used to setup and run YAPS. -#' @inheritParams getInp -#' -#' @return List for use in TMB. -#' @noRd -getDatTmb <- function(hydros, toa, E_dist, n_ss, pingType, rbi_min, rbi_max, ss_data_what, ss_data, biTable, inp_params, z_vec, bbox){ - T0 <- inp_params$T0 - Hx0 <- inp_params$Hx0 - Hy0 <- inp_params$Hy0 - - toa <- toa - T0 - - # allowing slight out-of-bounds BIs - rbi_min <- rbi_min - rbi_min * 0.05 - rbi_max <- rbi_max + rbi_max * 0.05 - - # attempting to make sure toa is oriented correct - if(!nrow(toa) == nrow(hydros)){ - toa <- t(toa) - } - if(n_ss > 1){ - ss_idx <- cut(1:ncol(toa), n_ss, labels=FALSE) - 1 #-1 because zero-indexing in TMB - } else { - ss_idx <- rep(0, ncol(toa)) - } - approxBI <- mean(diff(colMeans(toa, na.rm=TRUE), na.rm=TRUE), na.rm=TRUE) - if(ss_data_what == 'data') { stopifnot(length(ss_data) == ncol(toa))} - Edist <- rep(0,3) - if(E_dist == "Gaus") {Edist[1] <- 1} - if(E_dist == "Mixture") {Edist[2] <- 1} - if(E_dist == "t") {Edist[3] <- 1} - - if(is.null(z_vec)){ - how_3d <- 'none' - z_vec <- c(1) - } else if(z_vec[1] == "est") { - how_3d <- 'est' - z_vec <- c(1) - } else { - how_3d <- 'data' - } - - if(is.null(bbox)){ - bbox <- NA - } else { - bbox[1] <- bbox[1] - inp_params$Hx0 - bbox[2] <- bbox[2] - inp_params$Hx0 - bbox[3] <- bbox[3] - inp_params$Hy0 - bbox[4] <- bbox[4] - inp_params$Hy0 - } - datTmb <- list( - model = "yaps_track", - H = matrix(c(hydros$hx-Hx0, hydros$hy-Hy0, hydros$hz), ncol=3), - nh = nrow(hydros), - np = ncol(toa), - Edist = Edist, - toa = toa, - bi_epsilon = 1E-6, - bi_penalty = 1E9, - rbi_min = rbi_min, - rbi_max = rbi_max, - pingType = pingType, - n_ss = n_ss, - ss_idx = ss_idx, - ss_data_what = ss_data_what, - ss_data = ss_data, - approxBI = approxBI, - biTable = c(1), - how_3d = how_3d, - z_vec = z_vec, - bbox = bbox - ) - if(pingType == 'pbi') {datTmb$biTable = biTable} - - return(datTmb) -} - -#' Internal function - get params-list for use in TMB -#' -#' Compile a list of parameters for use in TMB. -#' @param datTmb Object obtained using getDatTmb() -#' @return List of params for use in TMB -#' @noRd -getParams <- function(datTmb){ - params_XY <- getParamsXYFromCOA(datTmb) - out <- list( - X = params_XY$X + stats::rnorm(ncol(datTmb$toa), sd=10) - , Y = params_XY$Y + stats::rnorm(ncol(datTmb$toa), sd=10) - # X = 0 + stats::rnorm(ncol(datTmb$toa), sd=10) - # , Y = 0 + stats::rnorm(ncol(datTmb$toa), sd=10) - , top = zoo::na.approx(apply(datTmb$toa, 2, function(k) {stats::median(k, na.rm=TRUE)}), rule=2) #time of ping - # , SS=stats::rnorm(datTmb$n_ss, 1450, 5) #speed of sound - , logD_xy = 0 #diffusivity of transmitter movement (D_xy in ms) - # , logD_v = 0 #diffusivity of speed of sound (D_v in ms) - # , logSigma_toa = 0 #sigma for Gaussian - # , logScale = 0 #scale parameter for t-distribution - # , log_t_part = 0 #Mixture ratio between Gaussian and t - ) - - # # # If estimating 3D - if(datTmb$how_3d == "est"){ - out$Z <- stats::runif(ncol(datTmb$toa), -10, 0) - out$logD_z <- 0 #diffusivity of transmitter vertical movement (D_z in ms) - } - - - # # # ss related - if(datTmb$ss_data_what == 'est'){ - out$logD_v <- 0 #diffusivity of speed of sound (D_v in ms) - out$SS <- stats::rnorm(datTmb$n_ss, 1450, 5) #speed of sound - } - - # # # Edist related - if(datTmb$Edist[1] == 1){ - out$logSigma_toa = 0 #sigma for Gaussian - } else if(datTmb$Edist[2] == 1){ - out$logSigma_toa = 0 #sigma for Gaussian - out$logScale = 0 #scale parameter for t-distribution - out$log_t_part = 0 #Mixture ratio between Gaussian and t - } else if(datTmb$Edist[3] == 1){ - out$logScale = 0 #scale parameter for t-distribution - } - - # # # Ping type related - if(datTmb$pingType %in% c('sbi', 'sbi_double', 'rbi')){ - out$logSigma_bi <- 0 #sigma burst interval (sigma_bi in ms) - } - - if(datTmb$pingType == 'pbi'){ - out$logSigma_bi <- 0 #sigma burst interval (sigma_bi in ms) - out$tag_drift <- stats::rnorm(datTmb$np, 0, 1e-2) - } - - - return(out) -} - -#' Internal function - get initial values for X and Y based on Center Of Activity - i.e. hydrophones positions -#' -#' Attempts to give meaningful initial values for X and Y based on which hydros detected each ping -#' @inheritParams getInp -#' @noRd -getParamsXYFromCOA <- function(datTmb){ - toa <- datTmb$toa - hydros <- datTmb$H - - toa_detect <- toa - toa_detect[!is.na(toa_detect)] <- 1 - - X <- zoo::na.approx(colMeans((toa_detect) * hydros[,1], na.rm=TRUE)) - Y <- zoo::na.approx(colMeans((toa_detect) * hydros[,2], na.rm=TRUE)) - - return(list(X=X, Y=Y)) - -} - -#' Get inits for use in TMB -#' -#' Compile a vector of initial values to use in TMB. One value for each estimated parameter (not random effects). -#' Should all be in a credible range. -#' @param datTmb Object obtained using getDatTmb() -#' @inheritParams getInp -#' @return Vector of initial values to use in TMB -#' @noRd -getInits <- function(datTmb, sdInits=1) { - init_logD_xy <- -1 - - if(datTmb$pingType == 'sbi') { - init_logSigma_bi <- -6 - } else if(datTmb$pingType == 'rbi'){ - if(datTmb$rbi_max >= 10){ - init_logSigma_bi <- 4 - } else { - init_logSigma_bi <- -3 - } - } else if(datTmb$pingType == 'pbi'){ - init_logSigma_bi <- -5 - } - - init_logD_v <- 0 - init_logSigma_toa <- -3 # used in Gaussian and mixture - init_logScale <- 1 # used in mixture and pure t - init_log_t_part <- -4 # only used in mixture - - inits <- c(init_logD_xy) - - if(datTmb$how_3d == 'est'){ - init_logD_z <- 0 - inits <- c(inits, init_logD_z) - } - - if(datTmb$ss_data_what == 'est'){ - inits <- c(inits, init_logD_v) - } - - - if(datTmb$Edist[1] == 1){ - inits <- c(inits, init_logSigma_toa) - } else if(datTmb$Edist[2] == 1){ - inits <- c(inits, init_logSigma_toa, init_logScale, init_log_t_part) - } else if(datTmb$Edist[3] == 1){ - inits <- c(inits, init_logScale) - } - - if(datTmb$pingType == 'sbi'){ - inits <- c(inits, init_logSigma_bi)#, init_logD_v)#, init_logSigma_toa, init_logScale, init_log_t_part) - } else if (datTmb$pingType == 'rbi'){ - inits <- c(inits, init_logSigma_bi)#, init_logD_v)#, init_logSigma_toa, init_logScale, init_log_t_part) - } else if (datTmb$pingType == 'pbi'){ - inits <- c(inits, init_logSigma_bi)#, init_logD_v)#, init_logSigma_toa, init_logScale, init_log_t_part) - } - - inits <- stats::rnorm(length(inits), mean=inits, sd=sdInits) - return(inits) -} - -#' Get bounds restricting the optimizer -#* -#' Compile a matrix of lower (bounds[,1]) and upper (bounds[,2]) bounds for the parameters to be estimated. -#' @param datTmb Object obtained using getDatTmb() -#' @return Matrix of bounds restricting the optimizer when running runYaps(). -#' @noRd -getBounds <- function(datTmb) { - lu_logD_xy <- c(-50, 2) - lu_logD_z <- c(-50, 2) - - lu_logSigma_toa <- c(-12, -2) - if(datTmb$Edist[2] == 1){ # mixture - lu_logScale <- c(-30, 10) - } else if (datTmb$Edist[3] == 1) { # t - lu_logScale <- c(-10,2) - } - lu_log_t_part <- c(-100, 100) - if(datTmb$pingType == 'rbi'){ - lu_logSigma_bi <- c(-20, 20) - } else { - lu_logSigma_bi <- c(-20, -2) - } - lu_logD_v <- c(-20, 2) - - bounds <- c() - bounds <- rbind(bounds, lu_logD_xy) - - if(datTmb$how_3d == 'est'){ - bounds <- rbind(bounds, lu_logD_z) - } - - if(datTmb$ss_data_what == 'est'){ - bounds <- rbind(bounds, lu_logD_v) - } - - if(datTmb$Edist[1] == 1){ - bounds <- rbind(bounds, lu_logSigma_toa) - } else if(datTmb$Edist[2] == 1){ - bounds <- rbind(bounds, lu_logSigma_toa, lu_logScale, lu_log_t_part) - } else if(datTmb$Edist[3] == 1){ - bounds <- rbind(bounds, lu_logScale) - } - - if(datTmb$pingType == 'sbi'){ - bounds <- rbind(bounds, lu_logSigma_bi) - } else if (datTmb$pingType == 'rbi'){ - bounds <- rbind(bounds, lu_logSigma_bi) - } else if (datTmb$pingType == 'pbi'){ - bounds <- rbind(bounds, lu_logSigma_bi) - } - - return(bounds) -} - -#' Get parameters for this specific data set -#' -#' Compile a list of relevant parameters (e.g. T0) to use later on -#' @inheritParams getInp -#' @noRd -getInpParams <- function(hydros, toa, pingType){ - T0 <- min(toa, na.rm=TRUE) - - Hx0 <- hydros[1,hx] - Hy0 <- hydros[1,hy] - - return(list(T0=T0, Hx0=Hx0, Hy0=Hy0)) - -} \ No newline at end of file diff --git a/R/removeMultipath.R b/R/removeMultipath.R new file mode 100644 index 0000000..49ced33 --- /dev/null +++ b/R/removeMultipath.R @@ -0,0 +1,12 @@ +#' Remove multipath detections from detection table +#' @export +removeMultipath <- function(dets, mp_threshold=0.5){ + dets[, epodiff := c(NA, diff(eposync)), by=h_idx] + mps <- which(dets$epodiff < mp_threshold) + if(length(mps) > 0){ + dets <- dets[-mps] + } + dets[, epodiff := NULL] + dets[] + return(dets) +} diff --git a/R/runYaps.R b/R/runYaps.R index 244ce5d..423d29c 100644 --- a/R/runYaps.R +++ b/R/runYaps.R @@ -29,7 +29,7 @@ runYaps <- function(inp, maxIter=1000, getPlsd=TRUE, getRep=TRUE, silent=TRUE, opt_fun='nlminb', opt_controls=list(), tmb_smartsearch=TRUE){ # making sure inp is correct... - inp <- checkInp(inp) + checkInp(inp) nobs <- z <- z_sd <- NULL print("Running yaps...") diff --git a/R/sync_checkInpSyncData.R b/R/sync_checkInpSyncData.R index 4f3a559..52ea297 100644 --- a/R/sync_checkInpSyncData.R +++ b/R/sync_checkInpSyncData.R @@ -5,14 +5,12 @@ checkInpSyncData <- function(hydros, dat_sync, dat_ss, sync_params){ Es <- 0 Ws <- 0 - if(is.null(attr(hydros, 'yapsified'))){ - Es <- Es + 1 - stop("ERROR: The hydros data.table is not yapsified. Run e.g. hydros <- yapsify(hydros) to do so. \n") - } - - if(!is.null(attr(hydros, 'yapsified')) & attr(hydros, 'yapsified') == FALSE){ + checkHydros(hydros) + + if(nrow(hydros[sync_params$time_keeper %in% h_sn]) == 0){ Es <- Es + 1 - stop("ERROR: The hydros data.table is not yapsified. Run e.g. hydros <- yapsify(hydros) to do so. \n") + cat("ERROR: The specified sync_params$time_keeper is not present in hydros. \n") + stopSilent() } if(!"epo" %in% colnames(dat_sync)){ @@ -25,10 +23,6 @@ checkInpSyncData <- function(hydros, dat_sync, dat_ss, sync_params){ dat_sync[, epofrac := epo+frac] } - if(length(unique(hydros$h_sn)) != nrow(hydros)){ - Es <- Es+1 - stop("ERROR: At least one hydrophone serial number is used more than once in sync_dat$hydros!\n") - } if(sync_params$keep_rate <=0 | (sync_params$keep_rate > 1 & sync_params$keep_rate < 10) | (sync_params$keep_rate >= 10 & sync_params$keep_rate %% 1 != 0)){ Es <- Es+1 diff --git a/R/sync_checkSyncCoverage.R b/R/sync_checkSyncCoverage.R new file mode 100644 index 0000000..59322e3 --- /dev/null +++ b/R/sync_checkSyncCoverage.R @@ -0,0 +1,30 @@ +#' Quick overview to check if all hydros have enough data within each offset period. +#' +#' @inheritParams checkInpSync +#' @param plot Logical indicating whether to plot a visual or not. +#' @export +#' @return A data.table containing number of pings included in each hydro x offset combination. +#' @example man/examples/example-yaps_ssu1.R +getSyncCoverage <- function(inp_sync, plot=FALSE){ + toa <- inp_sync$dat_tmb_sync$toa + nh <- ncol(toa) + offset_idx <- inp_sync$dat_tmb_sync$offset_idx + + toa_long <- data.table::data.table(reshape2::melt(toa)) + colnames(toa_long) <- c('ping', 'h','toa') + toa_long[, offset_idx := rep(offset_idx, nh)] + sync_coverage <- data.table::data.table(reshape2::melt(with(toa_long[!is.na(toa)], table(h, offset_idx)))) + colnames(sync_coverage) <- c('h', 'offset_idx' ,'N') + + if(plot){ + p <- ggplot2::ggplot(sync_coverage) + p <- p + geom_point(aes(offset_idx, N), col="steelblue") + p <- p + geom_point(data=sync_coverage[N < 50], aes(offset_idx, N), col="blue", size=2) + p <- p + geom_point(data=sync_coverage[N < 10], aes(offset_idx, N), col="orange", size=2) + p <- p + geom_point(data=sync_coverage[N <= 5], aes(offset_idx, N), col="red", size=2) + p <- p + facet_wrap(~h) + ylim(0, max(sync_coverage$N)) + print(p) + } + + return(sync_coverage) +} \ No newline at end of file diff --git a/R/sync_getDatTmbSync.R b/R/sync_getDatTmbSync.R index 246f9e6..8617379 100644 --- a/R/sync_getDatTmbSync.R +++ b/R/sync_getDatTmbSync.R @@ -4,7 +4,7 @@ #' @export getDatTmbSync <- function(hydros, dat_sync, sync_params, inp_toa_list, offset_vals, T0, ss_vec){ # H <- as.matrix(inp_H_info$inp_H) - H <- as.matrix(hydros[, .(x,y,z)]) + H <- as.matrix(hydros[, .(h_x,h_y,h_z)]) dimnames(H) <- NULL toa_0 <- inp_toa_list$toa - T0 diff --git a/R/sync_getInpSync.R b/R/sync_getInpSync.R index 31485c1..469fb9c 100644 --- a/R/sync_getInpSync.R +++ b/R/sync_getInpSync.R @@ -95,7 +95,7 @@ getInpSync <- function(hydros, dat_sync, dat_ss=NA, sync_params, plot=TRUE){ # check to ensure that the timekeeper has data in all offset_idxs dat_tk <- data.table(cbind(toa=inp_toa_list$toa[, colnames(inp_toa_list$toa) == sync_params$time_keeper], offset_idx = factor(dat_tmb_sync$offset_idx))) - + toa <- inp_toa_list$toa if(nrow(dat_tk[!is.na(toa), .N, by=offset_idx]) != length(unique(dat_tk$offset_idx)) | nrow(dat_tk[!is.na(toa), .N, by=offset_idx][N < 10]) > 0){ cat("WARNING: The time keeper should have data (N >= 10) in all offset_idx!\n") cat("...Data in these periods will not be synced!\n") diff --git a/R/sync_plotSyncCheck.R b/R/sync_plotSyncCheck.R index 29987b8..f1116f3 100644 --- a/R/sync_plotSyncCheck.R +++ b/R/sync_plotSyncCheck.R @@ -56,14 +56,14 @@ plotSyncCheck <- function(sync_model, type=1){ if(type == 4){ dat_plot4 <- sync_check_dat[, .(mean=mean(abs(delta), na.rm=TRUE), median=median(abs(delta), na.rm=TRUE)), by=.(focal_hsn, sync_tag)] - dat_plot4 <- merge(dat_plot4, hydros[, .(sync_tag, x_from=x, y_from=y)], by.x="sync_tag", by.y="sync_tag") - dat_plot4 <- merge(dat_plot4, hydros[, .(h_sn, x_to=x, y_to=y)], by.x="focal_hsn", by.y="h_sn") + dat_plot4 <- merge(dat_plot4, hydros[, .(sync_tag, x_from=h_x, y_from=h_y)], by.x="sync_tag", by.y="sync_tag") + dat_plot4 <- merge(dat_plot4, hydros[, .(h_sn, x_to=h_x, y_to=h_y)], by.x="focal_hsn", by.y="h_sn") p4 <- ggplot2::ggplot() - p4 <- p4 + geom_point(data=hydros, aes(x=x, y=y)) + coord_fixed(ratio=1) + p4 <- p4 + geom_point(data=hydros, aes(x=h_x, y=h_y)) + coord_fixed(ratio=1) p4 <- p4 + geom_segment(data=dat_plot4, aes(x=x_from, xend=x_to, y=y_from, yend=y_to, col=mean), lwd=1) p4 <- p4 + viridis::scale_color_viridis(limits=c(0,max(dat_plot4$mean)), name="Mean dev\n(m)") - p4 <- p4 + ggrepel::geom_label_repel(data=hydros, aes(x=x, y=y, label=paste0(h_sn,'\n',sync_tag))) + p4 <- p4 + ggrepel::geom_label_repel(data=hydros, aes(x=h_x, y=h_y, label=paste0(h_sn,'\n',sync_tag))) return(p4) } diff --git a/R/sync_plotSyncNetwork.R b/R/sync_plotSyncNetwork.R index 5a99aa0..bd70217 100644 --- a/R/sync_plotSyncNetwork.R +++ b/R/sync_plotSyncNetwork.R @@ -7,12 +7,12 @@ plotSyncNetwork <- function(hydros, dat_sync){ } sync_at_hydros <- dat_sync[, .N, by=.(h_sn, tag)] - sync_at_hydros <- merge(sync_at_hydros, hydros[, .(sync_tag, x_from=x, y_from=y)], by.x="tag", by.y="sync_tag") - sync_at_hydros <- merge(sync_at_hydros, hydros[, .(h_sn, x_to=x, y_to=y)], by="h_sn") + sync_at_hydros <- merge(sync_at_hydros, hydros[, .(sync_tag, x_from=h_x, y_from=h_y)], by.x="tag", by.y="sync_tag") + sync_at_hydros <- merge(sync_at_hydros, hydros[, .(h_sn, x_to=h_x, y_to=h_y)], by="h_sn") p1 <- ggplot2::ggplot() - p1 <- p1 + geom_point(data=hydros, aes(x=x, y=y)) + coord_fixed(ratio=1) + p1 <- p1 + geom_point(data=hydros, aes(x=h_x, y=h_y)) + coord_fixed(ratio=1) p1 <- p1 + geom_segment(data=sync_at_hydros, aes(x=x_from, xend=x_to, y=y_from, yend=y_to, col=N), lwd=2) + viridis::scale_color_viridis(limits=c(0,max(sync_at_hydros$N))) - p1 <- p1 + ggrepel::geom_label_repel(data=hydros, aes(x=x, y=y, label=paste0(h_sn,'\n',sync_tag))) + p1 <- p1 + ggrepel::geom_label_repel(data=hydros, aes(x=h_x, y=h_y, label=paste0(h_sn,'\n',sync_tag))) p1 } \ No newline at end of file diff --git a/R/yapsify.R b/R/yapsify.R index f68d500..03786ad 100644 --- a/R/yapsify.R +++ b/R/yapsify.R @@ -1,5 +1,4 @@ #' Internal function. yapsify / standarize hydros and detections for sync and yaps -#' @noRd #' @export yapsify <- function(yapsify_me){ @@ -10,24 +9,33 @@ yapsify <- function(yapsify_me){ } } - if(sum(c("x", "y", "z") %in% colnames(yapsify_me)) >= 1){ # seems to be a hydro data.table + if(sum(c('h_sn', 'h_x','h_y','h_z','sync_tag') %in% colnames(yapsify_me)) != 5 & sum(c('ts','h_sn','tag','frac') %in% colnames(yapsify_me)) != 4){ + cat("ERROR: Data must contain the following one of the following combinations of colnames: + ...hydros: c('h_sn', 'h_x', 'h_y', 'h_z', 'sync_tag') + ...detections: c('ts', 'h_sn', 'tag', 'frac') + ...You have colnames(yapsify_me) = ",paste0(colnames(yapsify_me), collapse=", ") ," + ") + stopSilent() + } + + if(sum(c("h_x", "h_y", "h_z") %in% colnames(yapsify_me)) >= 1){ # seems to be a hydro data.table - std_h <- yapsify_me[, c('h_sn', 'x','y','z','sync_tag')] + std_h <- yapsify_me[, c('h_sn', 'h_x','h_y','h_z','sync_tag')] setorder(std_h, h_sn) std_h[, h_idx := 1:.N] - Hx0 <- std_h[1,x] - Hy0 <- std_h[1,y] + Hx0 <- std_h[1,h_x] + Hy0 <- std_h[1,h_y] - std_h[, x:=x-Hx0] - std_h[, y:=y-Hy0] + std_h[, h_x:=h_x-Hx0] + std_h[, h_y:=h_y-Hy0] std_h[] attr(std_h, 'yapsified') <- TRUE attr(std_h, 'Hx0') <- Hx0 attr(std_h, 'Hy0') <- Hy0 - return(std_h=std_h) + return(std_h) } else if(sum(c("ts", "tag", "epo", "frac") %in% colnames(yapsify_me)) >= 1){ # should be a detection table std_dat <- yapsify_me[, c('ts','h_sn','tag','frac')] std_dat[, epo := floor(as.numeric(ts))] diff --git a/man/checkHydros.Rd b/man/checkHydros.Rd new file mode 100644 index 0000000..ac64b69 --- /dev/null +++ b/man/checkHydros.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/checkHydros.R +\name{checkHydros} +\alias{checkHydros} +\title{Internal function to check hydros data.tables} +\usage{ +checkHydros(hydros) +} +\description{ +Internal function to check hydros data.tables +} diff --git a/man/getSyncCoverage.Rd b/man/getSyncCoverage.Rd index 0b41006..ad8912b 100644 --- a/man/getSyncCoverage.Rd +++ b/man/getSyncCoverage.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/checkInpSync.R +% Please edit documentation in R/sync_checkSyncCoverage.R \name{getSyncCoverage} \alias{getSyncCoverage} \title{Quick overview to check if all hydros have enough data within each offset period.} diff --git a/man/getToaRbi.Rd b/man/getToaRbi.Rd new file mode 100644 index 0000000..2d55953 --- /dev/null +++ b/man/getToaRbi.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/getToaRbi.R +\name{getToaRbi} +\alias{getToaRbi} +\title{Get TOA matrix for random burst interval transmitters} +\usage{ +getToaRbi(dets, hydros, rbi_min, rbi_max) +} +\description{ +Get TOA matrix for random burst interval transmitters +} diff --git a/man/removeMultipath.Rd b/man/removeMultipath.Rd new file mode 100644 index 0000000..daaf20b --- /dev/null +++ b/man/removeMultipath.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/removeMultipath.R +\name{removeMultipath} +\alias{removeMultipath} +\title{Remove multipath detections from detection table} +\usage{ +removeMultipath(dets, mp_threshold = 0.5) +} +\description{ +Remove multipath detections from detection table +} diff --git a/man/yapsify.Rd b/man/yapsify.Rd new file mode 100644 index 0000000..c6121c2 --- /dev/null +++ b/man/yapsify.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/yapsify.R +\name{yapsify} +\alias{yapsify} +\title{Internal function. yapsify / standarize hydros and detections for sync and yaps} +\usage{ +yapsify(yapsify_me) +} +\description{ +Internal function. yapsify / standarize hydros and detections for sync and yaps +}