Skip to content

Commit

Permalink
Merge pull request #83 from ropensci/fix/test-expected-fusions
Browse files Browse the repository at this point in the history
Fix/test expected fusions
  • Loading branch information
robitalec authored Sep 20, 2024
2 parents 35c5d09 + 0ff69cd commit a999dea
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 3 additions & 11 deletions R/fusion_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions man/fusion_id.Rd

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

140 changes: 140 additions & 0 deletions tests/testthat/test-fusion-id.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

})

0 comments on commit a999dea

Please sign in to comment.