Skip to content

Commit

Permalink
Merge pull request #37 from DOI-USGS/div-topo_sort
Browse files Browse the repository at this point in the history
assign topo_sort for divergences correctly fixes #36
  • Loading branch information
dblodgett-usgs committed Jul 30, 2024
2 parents f7d5aea + 227ef4e commit 98f7178
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
10 changes: 8 additions & 2 deletions R/sort_network.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,13 @@ add_topo_sort.data.frame <- function(x, outlets = NULL) {
#'
add_topo_sort.hy <- function(x, outlets = NULL) {

sort_network(x, outlets = outlets) |>
mutate(topo_sort = n():1)
out <- sort_network(x, outlets = outlets)

ids <- unique(out$id)

dplyr::left_join(out,
data.frame(id = ids,
topo_sort = seq(from = length(ids), to = 1, by = -1)),
by = "id")

}
47 changes: 47 additions & 0 deletions tests/testthat/test_sort_network.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,50 @@ test_that("loop warning", {
toid = c(3, 4, 4, 5, 6, 3, 7, 0))
expect_warning(sort_network(test_data), "loops")
})

test_that("add_topo_sort deals with diversions", {
base_network <- data.frame(
id = c(
8316857L, 8323683L, 8323693L, 8323713L, 8317381L, 8317555L, 8317415L,
8317403L, 8317409L, 8323701L, 8317397L, 8323681L, 8317385L, 8317547L,
8323689L, 8317411L, 8316869L, 8317405L, 8316827L, 8317549L, 8323687L,
8323685L, 8323711L, 8317551L, 8316843L, 8317545L, 8317383L, 8316851L,
8317391L, 8316865L, 8317407L, 8317401L, 8323705L, 8317399L, 8323707L,
8323703L, 8317387L, 8317393L, 8323695L, 8317389L, 8323699L, 8316855L,
8323691L, 8316825L, 8317379L
),
divergence = rep(
c(0L, 2L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 2L, 0L, 2L, 0L, 1L, 0L, 2L, 0L, 1L, 0L),
c(2L, 2L, 4L, 1L, 2L, 3L, 3L, 1L, 5L, 1L, 1L, 1L, 7L, 1L, 3L, 1L, 1L, 1L, 5L)
),
fromnode = c(
10035895, 10036484, 10036483, 10035994, 10035985, 10036070, 10036002,
10035996, 10035997, 10036493, 10035993, 10036483, 10035987, 10036066,
10036487, 10036000, 10035899, 10035997, 10111342, 10036067, 10036486,
10036485, 10036498, 10036066, 10035889, 10035987, 10035986, 10111345,
10035990, 10111346, 10035998, 10035995, 10036495, 10035994, 10036496,
10036494, 10035988, 10035989, 10036490, 10035989, 10036492, 10035894,
10036488, 10111341, 10035984
),
tonode = c(
10036484, 10036493, 10035895, 10036483, 10036070, 10036066, 10036001,
10035997, 10036002, 10035996, 10036493, 10035993, 10036487, 10035987,
10035988, 10036002, 10036000, 10035899, 10036067, 10035985, 10036485,
10035988, 10036486, 10036498, 10035986, 10035889, 10036487, 10035990,
10036490, 10035998, 10036496, 10035996, 10036496, 10036494, 10035995,
10036495, 10035989, 10036490, 10035994, 10036492, 10035894, 10036488,
10035993, 10035984, 10036070
)
)

base_network <- hydroloom::add_toids(base_network, return_dendritic = FALSE)

base_network <- hydroloom::add_topo_sort(base_network)

expect_equal(length(unique(base_network$topo_sort[base_network$id == 8317403])), 1)

expect_true(
base_network$topo_sort[base_network$id == 8317409] <
unique(base_network$topo_sort[base_network$id == 8317403]))

})

0 comments on commit 98f7178

Please sign in to comment.