diff --git a/CITATION.cff b/CITATION.cff index 3f20f0ff..09c31426 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -8,7 +8,7 @@ message: 'To cite package "spatsoc" in publications use:' type: software license: GPL-3.0-only title: 'spatsoc: Group Animal Relocation Data by Spatial and Temporal Relationship' -version: 0.2.4.9000 +version: 0.2.4.9001 identifiers: - type: doi value: 10.32614/CRAN.package.spatsoc diff --git a/DESCRIPTION b/DESCRIPTION index fff9c703..a2416336 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: spatsoc Title: Group Animal Relocation Data by Spatial and Temporal Relationship -Version: 0.2.4.9000 +Version: 0.2.4.9001 Authors@R: c( person("Alec L.", "Robitaille", , "robit.alec@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-4706-1762")), diff --git a/NEWS.md b/NEWS.md index 7645d780..c66731ce 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# v 0.2.4.9001 + +* improve tests of `fusion_id` with tests for expected number of output fusionIDs [PR 83](https://github.com/ropensci/spatsoc/pull/83) + # v 0.2.4.9000 * (experimental) `fusion_id` function for flexibly identifying fission-fusion diff --git a/R/fusion_id.R b/R/fusion_id.R index 461a4cbd..11151e02 100644 --- a/R/fusion_id.R +++ b/R/fusion_id.R @@ -19,9 +19,9 @@ #' The \code{n_min_length} argument defines the minimum number of successive #' fixes that are required to establish a fusion event. The \code{n_max_missing} #' argument defines the the maximum number of allowable missing observations for -#' either individual in a dyad within a fusion event. The \code{allow_split} -#' argument defines if a single observation can be greater than the threshold -#' distance without initiating fission event. +#' the dyad within a fusion event. The \code{allow_split} argument defines if a +#' single observation can be greater than the threshold distance without +#' initiating fission event. #' #' @return \code{fusion_id} returns the input \code{edges} appended with a #' \code{fusionID} column. @@ -160,14 +160,6 @@ fusion_id <- function(edges = NULL, both_rleid := (both_rleid + seq.int(.N)) * -1, by = dyadID] - # Correct if (looking forward) the loc is part of a new fusion run - unique_edges[, both_rleid := data.table::fifelse( - timegroup - data.table::shift(timegroup, -1) == -1 & - within & !(tg_diff), - data.table::shift(both_rleid, -1), - both_rleid - ), by = dyadID] - # If n minimum length > 0, check nrows and return NA if less than min if (n_min_length > 0) { unique_edges[!is.na(both_rleid), both_rleid := data.table::fifelse( diff --git a/codemeta.json b/codemeta.json index 7b61cb03..63c91ace 100644 --- a/codemeta.json +++ b/codemeta.json @@ -8,7 +8,7 @@ "codeRepository": "https://github.com/ropensci/spatsoc", "issueTracker": "https://github.com/ropensci/spatsoc/issues", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.2.4.9000", + "version": "0.2.4.9001", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", @@ -190,7 +190,7 @@ }, "SystemRequirements": "GDAL (>= 2.0.1), GEOS (>= 3.4.0), PROJ (>= 4.8.0),\n sqlite3" }, - "fileSize": "1881.983KB", + "fileSize": "1884.503KB", "citation": [ { "@type": "ScholarlyArticle", diff --git a/man/fusion_id.Rd b/man/fusion_id.Rd index 27641225..2fc06ea6 100644 --- a/man/fusion_id.Rd +++ b/man/fusion_id.Rd @@ -60,9 +60,9 @@ indicate a 50 m distance threshold. The \code{n_min_length} argument defines the minimum number of successive fixes that are required to establish a fusion event. The \code{n_max_missing} argument defines the the maximum number of allowable missing observations for -either individual in a dyad within a fusion event. The \code{allow_split} -argument defines if a single observation can be greater than the threshold -distance without initiating fission event. +the dyad within a fusion event. The \code{allow_split} argument defines if a +single observation can be greater than the threshold distance without +initiating fission event. } \examples{ diff --git a/tests/testthat/test-fusion-id.R b/tests/testthat/test-fusion-id.R index 3b4ca4d3..44030259 100644 --- a/tests/testthat/test-fusion-id.R +++ b/tests/testthat/test-fusion-id.R @@ -121,3 +121,143 @@ test_that('larger n_min_length returns less unique fusionID', { fusion_id(edges, n_min_length = 0)[, uniqueN(fusionID)] ) }) + +edges_expected <- data.table( + dyadID = rep('A-B', 11), + timegroup = seq.int(12)[-5], + distance = c(1, 50, 1, 1, 1, 50, 50, 1, 1, 50, 1) +) +threshold <- 25 + +test_that('n_min_length returns expected number of unique fusionIDs', { + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 0 + )[, uniqueN(fusionID, na.rm = TRUE)], + 5 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2 + )[, uniqueN(fusionID, na.rm = TRUE)], + 2 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 3 + )[, uniqueN(fusionID, na.rm = TRUE)], + 0 + ) + +}) + + +test_that('allow_split returns expected number of unique fusionIDs', { + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + allow_split = FALSE + )[, uniqueN(fusionID, na.rm = TRUE)], + 2 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + allow_split = FALSE + )[!is.na(fusionID), .N, fusionID][, max(N)], + 2 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + allow_split = TRUE + )[, uniqueN(fusionID, na.rm = TRUE)], + 2 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + allow_split = TRUE + )[!is.na(fusionID), .N, fusionID][, max(N)], + 4 + ) +}) + + +test_that('n_max_missing returns expected number of unique fusionIDs', { + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 0, + n_max_missing = 1, + allow_split = FALSE + )[, uniqueN(fusionID, na.rm = TRUE)], + 4 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + n_max_missing = 1, + allow_split = FALSE + )[, uniqueN(fusionID, na.rm = TRUE)], + 2 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + n_max_missing = 1, + allow_split = FALSE + )[!is.na(fusionID), .N, fusionID][, max(N)], + 3 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + n_max_missing = 1, + allow_split = TRUE + )[, uniqueN(fusionID, na.rm = TRUE)], + 2 + ) + + expect_equal( + fusion_id( + edges_expected, + threshold = threshold, + n_min_length = 2, + n_max_missing = 1, + allow_split = TRUE + )[!is.na(fusionID), .N, fusionID][, max(N)], + 5 + ) + +})