Skip to content

Commit

Permalink
Merge branch 'release-1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmarx committed Apr 3, 2021
2 parents 13b89f7 + 87814d8 commit 4c75c89
Show file tree
Hide file tree
Showing 97 changed files with 2,396 additions and 2,093 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: samc
Type: Package
Title: Spatial Absorbing Markov Chains
Version: 1.2.1
Version: 1.3.0
Authors@R: c(
person("Andrew", "Marx", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0002-7456-1631")
Expand Down Expand Up @@ -43,12 +43,13 @@ RoxygenNote: 7.1.1
Suggests: knitr,
rmarkdown,
testthat,
viridis
viridisLite
VignetteBuilder: knitr
Collate:
'RcppExports.R'
'samc-class.R'
'check.R'
'location-class.R'
'cond_passage.R'
'data.R'
'visitation.R'
Expand All @@ -58,6 +59,7 @@ Collate:
'locate.R'
'map.R'
'mortality.R'
'pairwise.R'
'samc.R'
'survival.R'
LinkingTo: Rcpp (>= 1.0.1), RcppEigen (>= 0.3.3.5.0)
13 changes: 12 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# samc 1.3.0

- Fixed an issue with the check() function when data contains NA's.
- Fixed an issue with the raster returned from locate(samc) having 0 for NA cells.
- Improved error checking and messaging for the check() and locate() functions.
- Named rows and columns for the P matrix is now supported. Previously, naming the rows and columns would cause some checks to fail. If names are not manually assigned, the names are simply the row/column numbers converted to character strings.
- Analytical functions updated to support named inputs for the origin and dest location parameters
- When both the origin and dest parameter is used in a function, the inputs can be paired vectors.
- Added the pairwise() utility function
- Created a new *Locations* tutorial vignette for new location input options.

# samc 1.2.1

- Fixed a regression in v1.2.0 where the samc() function would not work corectly unless matrix/raster layers contained at least one NA cell
- Fixed a regression in v1.2.0 where the samc() function would not work correctly unless matrix/raster layers contained at least one NA cell
- Revamped the automated test suite with more test scenarios to better catch issues before release
- Added checks during samc-class creation to prevent potential issues with discontinuous/clumped input data. Currently, this type of data will not work with the cond_passage() function, but will in a future release.
- Reworked some of the vignettes to produce cleaner pages and remove suggested dependencies (e.g. gifski, gganimate, ggplot2) from the package so that users aren't bugged about installing them if they don't need them.
Expand Down
35 changes: 23 additions & 12 deletions R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ setMethod(
function(a){

if (sum(is.infinite(a[]), na.rm = TRUE) > 0) {
stop("Data contains Inf or -Inf element")
stop("Data contains Inf or -Inf element", call. = FALSE)
} else if (sum(is.nan(a[]), na.rm = TRUE) > 0) {
stop("Data contains NaN elements")
stop("Data contains NaN elements", call. = FALSE)
}

return(TRUE)
Expand All @@ -60,7 +60,7 @@ setMethod(
"check",
signature(a = "matrix", b = "missing"),
function(a){
a <- raster::raster(a, xmn = 0.5, xmx = ncol(a) + 0.5, ymn = 0.5, ymx = nrow(a) + 0.5)
a <- .rasterize(a)

check(a)
})
Expand All @@ -76,16 +76,28 @@ setMethod(
a[] <- is.finite(a[])
b[] <- is.finite(b[])

raster::compareRaster(a, b, values = TRUE)
tryCatch(
{
raster::compareRaster(a, b, values = TRUE)
},
error = function(e) {
if(grepl("not all objects have the same values", e$message)) {
msg = "NA mismatch"
} else {
msg = e$message
}
stop(msg, " in input data", call. = FALSE)
}
)
})

#' @rdname check
setMethod(
"check",
signature(a = "matrix", b = "matrix"),
function(a, b){
a <- raster::raster(a, xmn = 0.5, xmx = ncol(a) + 0.5, ymn = 0.5, ymx = nrow(a) + 0.5)
b <- raster::raster(b, xmn = 0.5, xmx = ncol(b) + 0.5, ymn = 0.5, ymx = nrow(b) + 0.5)
a <- .rasterize(a)
b <- .rasterize(b)

check(a, b)
})
Expand All @@ -95,21 +107,20 @@ setMethod(
"check",
signature(a = "samc", b = "RasterLayer"),
function(a, b){
if (a@source != "map") stop(paste("Parameters do not apply to a samc-class object created from a", a@source))
if (a@source != "map") stop("Parameters do not apply to a samc-class object created from a ", a@source, call. = FALSE)

check(b)

b[] <- is.finite(b[])
a <- a@map
a[!a[]] <- NA

raster::compareRaster(a@map, b)
check(a, b)
})

#' @rdname check
setMethod(
"check",
signature(a = "samc", b = "matrix"),
function(a, b){
b <- raster::raster(b, xmn = 0.5, xmx = ncol(b) + 0.5, ymn = 0.5, ymx = nrow(b) + 0.5)
b <- .rasterize(b)

check(a, b)
})
36 changes: 19 additions & 17 deletions R/cond_passage.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2020 Andrew Marx. All rights reserved.
# Licensed under GPLv3.0. See LICENSE file in the project root for details.

#' @include samc-class.R
#' @include samc-class.R location-class.R
NULL

#' Conditional Mean First Passage Time
Expand All @@ -15,14 +15,13 @@ NULL
#'
#' The result is a vector where each element corresponds to a cell in the landscape,
#' and can be mapped back to the landscape using the \code{\link{map}} function.
#' Element \emph{i} is the mean number of steps for the first passage time from
#' Element \emph{i} is the mean number of steps before absorption starting from
#' location \emph{i} conditional on absorption into \emph{j}
#'
#' \item \strong{cond_passage(samc, origin, dest)}
#'
#' The result is a numeric value representing the mean number of steps for the
#' first passage time from a given origin conditional on absorption into a given
#' destination.
#' The result is a numeric value representing the mean number of steps before
#' absorption starting from a given origin conditional on absorption into \emph{j}.
#' }
#'
#' \strong{WARNING}: This function will crash when used with data representing
Expand Down Expand Up @@ -50,16 +49,19 @@ setGeneric(
standardGeneric("cond_passage")
})

# cond_passage(samc, dest) ----
#' @rdname cond_passage
setMethod(
"cond_passage",
signature(samc = "samc", origin = "missing", dest = "numeric"),
signature(samc = "samc", origin = "missing", dest = "location"),
function(samc, dest) {
if (samc@clumps > 1)
stop("This function cannot be used with discontinuous data")
stop("This function cannot be used with discontinuous data", call. = FALSE)

if (dest %% 1 != 0 || dest < 1 || dest > (ncol(samc@p) - 1))
stop("dest must be an integer that refers to a cell in the landscape")
if (length(dest) != 1)
stop("dest must be a single location that refers to a cell in the landscape", call. = FALSE)

dest <- .process_locations(samc, dest)

Q <- samc@p[-nrow(samc@p), -nrow(samc@p)]
qj <- Q[-dest, dest]
Expand All @@ -73,27 +75,27 @@ setMethod(
return(as.numeric(t))
})

# cond_passage(samc, origin, dest) ----
#' @rdname cond_passage
setMethod(
"cond_passage",
signature(samc = "samc", origin = "numeric", dest = "numeric"),
signature(samc = "samc", origin = "location", dest = "location"),
function(samc, origin, dest) {

.validate_locations(samc, origin)
.validate_locations(samc, dest)

if(length(origin) != length(dest))
stop("The 'origin' and 'dest' parameters must have the same number of values")
stop("The 'origin' and 'dest' parameters must have the same number of values", call. = FALSE)

origin <- .process_locations(samc, origin)
dest <- .process_locations(samc, dest)

result <- vector(mode = "numeric", length = length(length(origin)))
result <- vector(mode = "numeric", length = length(origin))

unique_dest <- unique(dest)

for (d in unique_dest) {
t <- cond_passage(samc, dest = d)
adj_origin <- origin
adj_origin[origin > d] <- adj_origin[origin > d] - 1
result[dest == d] <- t[origin[dest == d]]
result[dest == d] <- t[adj_origin[dest == d]]
}

result[dest == origin] <- NA
Expand Down
Loading

0 comments on commit 4c75c89

Please sign in to comment.