Skip to content

Commit

Permalink
bugfix: unsorted "from_data" observation
Browse files Browse the repository at this point in the history
  • Loading branch information
katarzynam-165 committed Jun 27, 2024
1 parent 146e7e9 commit 70419bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
15 changes: 10 additions & 5 deletions R/get_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
#'
#' @return `data.frame` object with geographic coordinates, time steps,
#' estimated abundance, observation error (if `obs_error_param` is
#' provided), and observer identifiers (if `type = "monitoring_based"`).
#' provided), and observer identifiers (if `type = "monitoring_based"`). If `type = "from_data"`, returned object is sorted in the same order as the input `points`.
#'
#' @export
#'
Expand Down Expand Up @@ -323,18 +323,22 @@ get_observations_from_data <- function(N_rast, points) {
## points
assert_that(is.data.frame(points) || is.matrix(points))
points <- as.data.frame(points)
assert_that(ncol(points) == 3)
assert_that(ncol(points) >= 3, msg = "not enough columns in \"points\"")
assert_that(
all(c("x", "y", "time_step") %in% names(points)),
msg = "points parameter should contain columns with the following names: \"x\", \"y\", \"time_step\"")
assert_that(nrow(points) > 0)

points <- points[c("x", "y", "time_step")]
assert_that(
all(!is.na(points)),
msg = "missing data found in \"points\"")
assert_that(
all(names(points) == c("x", "y", "time_step")),
msg = "columns in points parameter should have the following names: \"x\", \"y\", \"time_step\"")
assert_that(
all(apply(points, 2, is.numeric)),
msg = "some element of point are not numeric")

points$order <- seq_len(nrow(points))
points <- points[order(points$time_step),]
# get "observations" from cells given in points dataset
value <- unlist(lapply(
seq_len(nlyr(N_rast)),
Expand All @@ -348,6 +352,7 @@ get_observations_from_data <- function(N_rast, points) {

# column bind points and "observations"
out <- cbind(points, value = value)
out <- out[order(out$order),]
}


Expand Down
2 changes: 1 addition & 1 deletion man/get_observations.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-get_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test_that("get_observations works", {
expect_error(
get_observations(test_sim_data, test_sim_res,
type = "from_data", points = test_points[, c(1, 2)]),
"ncol(points) not equal to 3",
"not enough columns in \"points\"",
fixed = TRUE)

expect_error(
Expand All @@ -126,7 +126,7 @@ test_that("get_observations works", {
X = test_points[, 1],
Y = test_points[, 2],
time = test_points[, 3])),
"columns in points parameter should have the following names: \"x\", \"y\", \"time_step\"", #nolint
"points parameter should contain columns with the following names: \"x\", \"y\", \"time_step\"", #nolint
fixed = TRUE)

expect_error(
Expand Down

0 comments on commit 70419bb

Please sign in to comment.