Skip to content

Commit

Permalink
Update grid_components() and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UchidaMizuki committed May 26, 2024
1 parent 4343c2b commit 75e424c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
15 changes: 8 additions & 7 deletions R/move.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ grid_neighborhood <- function(grid,
neighbor <- tibble::tibble(grid = grid) |>
vec_unique() |>
tidyr::expand_grid(n_XY)
neighbor$grid_neighbor <- neighbor$grid |>
neighbor$grid_neighborhood <- neighbor$grid |>
grid_move(n_X = neighbor$n_X,
n_Y = neighbor$n_Y)
neighbor <- neighbor |>
Expand All @@ -68,7 +68,7 @@ grid_neighborhood <- function(grid,
neighbor$neighbor <- neighbor$neighbor |>
purrr::map(function(neighbor) {
neighbor |>
purrr::chuck("grid_neighbor")
purrr::chuck("grid_neighborhood")
})
}

Expand All @@ -82,19 +82,20 @@ grid_neighborhood <- function(grid,
#'
#' @param grid A `grid` vector.
#' @param n A numeric vector of degrees. By default, `0:1`.
#' @param moore Moore neighborhood (`TRUE`) or Von Neumann neighborhood
#' @param type A character vector of neighborhood types, `"von_neumann"` or
#' `"moore"`. By default, `"von_neumann"`.
#' (`FALSE`, default).
#'
#' @return A integer vector of group IDs.
#'
#' @export
grid_components <- function(grid,
n = 0:1,
moore = FALSE) {
type = NULL) {
edges <- tibble::tibble(grid_from = grid,
grid_to = grid_neighbor(grid,
n = n,
moore = moore)) |>
grid_to = grid_neighborhood(grid,
n = n,
type = type)) |>
tidyr::unnest("grid_to") |>
dplyr::filter(.data$grid_to %in% grid)

Expand Down
5 changes: 3 additions & 2 deletions man/grid_components.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-move.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ test_that("neighborhood", {
523877, 523970, 523971),
grid_size = "10km"))))
})

test_that("components", {
grid_1km <- parse_grid(c(53394620,
53394631, 53394632,
53394507, 53394508, 53394509),
"1km")

expect_equal(grid_components(grid_1km, n = 0:1, type = "von_neumann"),
c(3, 2, 2, 1, 1, 1))
expect_equal(grid_components(grid_1km, n = 0:1, type = "moore"),
c(1, 1, 1, 2, 2, 2))
expect_equal(grid_components(grid_1km, n = 0:2, type = "von_neumann"),
c(1, 1, 1, 2, 2, 2))
expect_equal(grid_components(grid_1km, n = 0:2, type = "moore"),
c(1, 1, 1, 1, 1, 1))
})

0 comments on commit 75e424c

Please sign in to comment.