UrbanAnalyst / dodgr

Distances on Directed Graphs in R
https://urbananalyst.github.io/dodgr/
127 stars 16 forks source link

Edge map order is wrong #173

Closed mpadge closed 2 years ago

mpadge commented 2 years ago

The neighbourhoods package needs to uncontract edges, for which it uses the cached edge maps. The order of these is, however, all mucked up, and so can't be used in current form to directly uncontract the edges:

library (dodgr)
f <- "/<path>/<to>/bristol-sc.Rds"
net <- readRDS (f) |>
    weight_streetnet (wt_profile = "motorcar")
#> Loading required namespace: geodist
#> Loading required namespace: dplyr
netc <- dodgr_contract_graph (net)

h <- attr (netc, "hashc")
f <- list.files (tempdir (), full.names = TRUE,
                 pattern = paste0 ("edge\\_map\\_", h))
edge_map <- readRDS (f)

# group by number of edges, and choose largest contracted edge:
n <- dplyr::group_by (edge_map, edge_new) |>
    dplyr::summarise (n = dplyr::n ()) |>
    dplyr::arrange (by = dplyr::desc (n))
edge_old <- edge_map$edge_old [which (edge_map$edge_new == n$edge_new [1])]
g <- net [match (edge_old, net$edge_), ]
plot (g$.vx0_x, g$.vx0_y, type = "l")

Created on 2021-11-26 by the reprex package (v2.0.1.9000)

mpadge commented 2 years ago

The above commit just changed the old_edges from a std::set to a std::vector, so they are retained in the order in which they are input.

library (dodgr)
packageVersion ("dodgr")
#> [1] '0.2.12.22'
f <- "/<path>/<to>/bristol-sc.Rds"
net <- readRDS (f) |>
    weight_streetnet (wt_profile = "motorcar")
#> Loading required namespace: geodist
#> Loading required namespace: dplyr
netc <- dodgr_contract_graph (net)

h <- attr (netc, "hashc")
f <- list.files (tempdir (), full.names = TRUE,
                 pattern = paste0 ("edge\\_map\\_", h))
edge_map <- readRDS (f)
n <- dplyr::group_by (edge_map, edge_new) |>
    dplyr::summarise (n = dplyr::n ()) |>
    dplyr::arrange (by = dplyr::desc (n))
edge_old <- edge_map$edge_old [which (edge_map$edge_new == n$edge_new [1])]
g <- net [match (edge_old, net$edge_), ]
plot (g$.vx0_x, g$.vx0_y, type = "l")

Created on 2021-11-26 by the reprex package (v2.0.1.9000)

mpadge commented 2 years ago

Re-opening because the result is not quite what it should be:

library (dodgr)
packageVersion ("dodgr")
#> [1] '0.2.12.22'
f <- "/data/mega/code/repos/atfutures-labs/neighbourhoods/bristol-sc.Rds"
net <- readRDS (f) |>
    weight_streetnet (wt_profile = "motorcar")
#> Loading required namespace: geodist
#> Loading required namespace: dplyr
netc <- dodgr_contract_graph (net)
h <- attr (netc, "hashc")
f <- list.files (tempdir (), full.names = TRUE,
                 pattern = paste0 ("edge\\_map\\_", h))
edge_map <- readRDS (f)
n <- dplyr::group_by (edge_map, edge_new) |>
    dplyr::summarise (n = dplyr::n ()) |>
    dplyr::arrange (by = dplyr::desc (n))
edge_old <- edge_map$edge_old [which (edge_map$edge_new == n$edge_new [1])]
g <- net [match (edge_old, net$edge_), ]
par (mfrow = c (1, 2))
plot (g$.vx0_x, g$.vx0_y, type = "l")

obj <- unique (net$object_ [match (g$edge_, net$edge_)])
g2 <- net [which (net$object_ %in% obj), ]
plot (g2$.vx0_x, g2$.vx0_y, type = "l")

Created on 2021-11-26 by the reprex package (v2.0.1.9000)

The re-constructed edges still have a couple of glitches in them.

mpadge commented 2 years ago

All good now:

library (dodgr)
packageVersion ("dodgr")
#> [1] '0.2.12.24'
f <- "/<path>/<to>/bristol-sc.Rds"
graph <- x <- readRDS (f) |>
    weight_streetnet (wt_profile = "motorcar")
#> Loading required namespace: geodist
#> Loading required namespace: dplyr
v <- dodgr_vertices (graph)
verts <- NULL
graph_contracted <- dodgr_contract_graph_internal (graph, v, verts)
edge_map <- graph_contracted$edge_map
n <- dplyr::group_by (edge_map, edge_new) |>
    dplyr::summarise (n = dplyr::n ()) |>
    dplyr::arrange (by = dplyr::desc (n))
edge_old <- edge_map$edge_old [which (edge_map$edge_new == n$edge_new [1])]
g <- graph [match (edge_old, graph$edge_), ]
par (mfrow = c (1, 2))
plot (g$.vx0_x, g$.vx0_y, type = "l")

obj <- unique (graph$object_ [match (g$edge_, graph$edge_)])
g2 <- graph [which (graph$object_ %in% obj), ]
plot (g2$.vx0_x, g2$.vx0_y, type = "l")

Created on 2021-11-29 by the reprex package (v2.0.1.9000)