Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: dodgr
Title: Distances on Directed Graphs
Version: 0.4.3.038
Version: 0.4.3.042
Authors@R: c(
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre")),
person("Andreas", "Petutschnig", role = "aut"),
Expand Down Expand Up @@ -31,15 +31,15 @@ Depends:
R (>= 3.5.0)
Imports:
callr,
digest,
fs,
geodist (>= 0.1.0),
magrittr,
memoise,
methods,
osmdata,
Rcpp (>= 0.12.6),
RcppParallel
RcppParallel,
secretbase
Suggests:
bench,
dplyr,
Expand All @@ -65,4 +65,4 @@ LazyData: true
NeedsCompilation: yes
Roxygen: list(markdown = TRUE)
SystemRequirements: GNU make
Config/roxygen2/version: 8.0.0
Config/roxygen2/version: 8.1.0
14 changes: 8 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ importFrom(graphics,plot)
importFrom(magrittr,"%>%")
importFrom(memoise,memoise)
importFrom(methods,is)
importFrom(osmdata,add_osm_feature)
importFrom(osmdata,getbb)
importFrom(osmdata,opq)
importFrom(osmdata,osm_poly2line)
importFrom(osmdata,osmdata_sf)
importFrom(osmdata,trim_osmdata)
importFrom(osmdata,
add_osm_feature,
getbb,
opq,
osm_poly2line,
osmdata_sf,
trim_osmdata
)
useDynLib(dodgr, .registration = TRUE)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

## Minor changes

- Replace `digest` dependency with `secretbase` for all internal graph hashing/caching
- Graph hashes used for caching now also incorporate `d`, `d_weighted`, `time`, and `time_weighted` columns
- Fix a few minor bugs with compound junction construction (#305, #316)
- Add `pairwise` parameter to `dodgr_times()`; thanks to @leoniedu (#314)
- Fix bug with categorical distances that neglected edges through compound junctions (#305)
Expand Down
52 changes: 36 additions & 16 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,32 @@ get_hash <- function (graph, verts = NULL, contracted = FALSE, force = FALSE) {
if (contracted) {
if (is.null (hash)) {
gr_cols <- dodgr_graph_cols (graph)
hash <- digest::digest (list (
graph [[gr_cols$edge_id]], names (graph), verts
hash <- secretbase::siphash13 (list (
hash_cols (graph, gr_cols), names (graph), verts
))
}
} else {
if (is.null (hash)) {
gr_cols <- dodgr_graph_cols (graph)
hash <- digest::digest (list (
graph [[gr_cols$edge_id]], names (graph)
hash <- secretbase::siphash13 (list (
hash_cols (graph, gr_cols), names (graph)
))
}
}
return (hash)
}

# Columns beyond `edge_id` whose values affect `dodgr` output, and which must
# therefore also contribute to graph hashes, so that direct edits to weights
# or distances (without also changing `edge_id`) invalidate caches rather
# than silently serving stale results. See `?clear_dodgr_cache`.
hash_cols <- function (graph, gr_cols) {
nms <- c ("edge_id", "d", "d_weighted", "time", "time_weighted")
idx <- do.call (c, gr_cols [nms])
idx <- idx [!is.na (idx)]
graph [, idx, drop = FALSE]
}

get_edge_map <- function (graph) {

hashc <- get_hash (graph, contracted = TRUE)
Expand Down Expand Up @@ -61,11 +72,11 @@ get_edge_map <- function (graph) {
#' re-loaded,
#' and the uncontracted version returned.
#' @noRd
cache_graph <- function (graph, edge_col) {
cache_graph <- function (graph, gr_cols) {

td <- fs::path_temp ()

f <- function (graph, edge_col, td) {
f <- function (graph, gr_cols, td) {

# the following line does not generate a coverage symbol because it is
# cached, so # nocov:
Expand All @@ -81,12 +92,18 @@ cache_graph <- function (graph, edge_col) {
fname <- fs::path (td, paste0 ("dodgr_graph_", hash, ".Rds"))
saveRDS (graph, fname)

# The hash for the contracted graph is generated from the edge IDs of
# the full graph plus default NULL vertices. Internal functions can not
# be called here, so code copied directly from `get_hash`:
# The hash for the contracted graph is generated from the edge IDs
# and weight/distance columns of the full graph plus default NULL
# vertices. Internal functions can not be called here, so code
# copied directly from `get_hash` and `hash_cols`:
# hashc <-
# get_hash (graph, verts = NULL, contracted = TRUE, force = TRUE)
hashc <- digest::digest (list (graph [[edge_col]], names (graph), NULL))
hash_nms <- c ("edge_id", "d", "d_weighted", "time", "time_weighted")
hash_idx <- do.call (c, gr_cols [hash_nms])
hash_idx <- hash_idx [!is.na (hash_idx)]
hashc <- secretbase::siphash13 (list (
graph [, hash_idx, drop = FALSE], names (graph), NULL
))

graphc <- dodgr::dodgr_contract_graph (graph)
fname_c <- fs::path (td, paste0 ("dodgr_graphc_", hashc, ".Rds"))
Expand Down Expand Up @@ -119,7 +136,7 @@ cache_graph <- function (graph, edge_col) {
}

sink (file = fs::path (fs::path_temp (), "Rout.txt"))
res <- callr::r_bg (f, list (graph, edge_col, td))
res <- callr::r_bg (f, list (graph, gr_cols, td))
sink ()

return (res) # R6 processx object
Expand All @@ -128,11 +145,14 @@ cache_graph <- function (graph, edge_col) {
#' Remove cached versions of `dodgr` graphs.
#'
#' This function should generally \emph{not} be needed, except if graph
#' structure has been directly modified other than through `dodgr` functions;
#' for example by modifying edge weights or distances. Graphs are cached based
#' on the vector of edge IDs, so manual changes to any other attributes will not
#' necessarily be translated into changes in `dodgr` output unless the cached
#' versions are cleared using this function. See
#' structure has been directly modified other than through `dodgr` functions.
#' Graphs are cached based on a hash of the `edge_id`, `d`, `d_weighted`,
#' `time`, and `time_weighted` columns, so manual changes to any of those will
#' be detected automatically. Manual changes to any other column or
#' attribute (for example, `from`/`to` vertex identifiers) will not be
#' reflected in the hash, and so will not necessarily be translated into
#' changes in `dodgr` output unless the cached versions are cleared using this
#' function. See
#' \url{https://github.com/UrbanAnalyst/dodgr/wiki/Caching-of-streetnets-and-contracted-graphs} # nolint
#' for details of caching process.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ dodgr_centrality <- function (graph,

if (is_dodgr_cache_on () && edges) {
# re-cache graph with centrality measure:
attr (res, "px") <- cache_graph (res, gr_cols$edge_id)
attr (res, "px") <- cache_graph (res, gr_cols)
}

return (res)
Expand Down
2 changes: 1 addition & 1 deletion R/fund-cycles.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ dodgr_fundamental_cycles <- function (graph,
# each element of res is a list, so flatten these:
res <- flatten_list (res)
# These hash each and remove any duplicated ones:
dig <- unlist (lapply (res, digest::digest))
dig <- unlist (lapply (res, secretbase::siphash13))
res <- res [which (!duplicated (dig))]
}

Expand Down
6 changes: 4 additions & 2 deletions R/graph-contraction.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ dodgr_contract_graph <- function (graph, verts = NULL, nocache = FALSE) {
graph_contracted <- dodgr_contract_graph_internal (graph, v, verts)

gr_cols <- dodgr_graph_cols (graph_contracted$graph)
hashe <- digest::digest (graph_contracted$graph [[gr_cols$edge_id]])
hashe <- secretbase::siphash13 (
hash_cols (graph_contracted$graph, gr_cols)
)
attr (graph_contracted$graph, "hash") <- hash
attr (graph_contracted$graph, "hashc") <- hashc
attr (graph_contracted$graph, "hashe") <- hashe
Expand Down Expand Up @@ -308,7 +310,7 @@ dodgr_uncontract_graph <- function (graph) {

gr_cols <- dodgr_graph_cols (graph)
hashe_ref <- attr (graph, "hashe")
hashe <- digest::digest (graph [[gr_cols$edge_id]])
hashe <- secretbase::siphash13 (hash_cols (graph, gr_cols))

hash <- attr (graph, "hash")
fname <- fs::path (fs::path_temp (), paste0 ("dodgr_graph_", hash, ".Rds"))
Expand Down
4 changes: 2 additions & 2 deletions R/graph-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ dodgr_vertices <- function (graph) {
hash <- attr (graph, hash)
# make sure rows of graph have not been changed
gr_cols <- dodgr_graph_cols (graph)
hashe <- digest::digest (graph [[gr_cols$edge_id]])
hashe <- secretbase::siphash13 (hash_cols (graph, gr_cols))
if (!identical (hashe, hash)) {
hash <- NULL
}
if (!is.null (hash)) {
hashe_ref <- attr (graph, "hashe")
hashe_ref <- ifelse (is.null (hashe_ref), "", hashe_ref)
hashe <- digest::digest (graph [[gr_cols$edge_id]])
hashe <- secretbase::siphash13 (hash_cols (graph, gr_cols))
if (hashe != hashe_ref) {
hash <- hashe
}
Expand Down
4 changes: 2 additions & 2 deletions R/weight-streetnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ weight_streetnet.sf <- function (x,
hash <- get_hash (graph, contracted = FALSE, force = TRUE)
attr (graph, "hash") <- hash
if (is_dodgr_cache_on ()) {
attr (graph, "px") <- cache_graph (graph, gr_cols$edge_id)
attr (graph, "px") <- cache_graph (graph, gr_cols)
}

return (graph)
Expand Down Expand Up @@ -772,7 +772,7 @@ weight_streetnet.sc <- function (x,
get_hash (graph, contracted = FALSE, force = TRUE)

if (is_dodgr_cache_on ()) {
attr (graph, "px") <- cache_graph (graph, gr_cols$edge_id)
attr (graph, "px") <- cache_graph (graph, gr_cols)
}

return (graph)
Expand Down
42 changes: 21 additions & 21 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"codeRepository": "https://github.com/UrbanAnalyst/dodgr",
"issueTracker": "https://github.com/UrbanAnalyst/dodgr/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.4.3.038",
"version": "0.4.3.042",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -270,18 +270,6 @@
"sameAs": "https://CRAN.R-project.org/package=callr"
},
"3": {
"@type": "SoftwareApplication",
"identifier": "digest",
"name": "digest",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=digest"
},
"4": {
"@type": "SoftwareApplication",
"identifier": "fs",
"name": "fs",
Expand All @@ -293,7 +281,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=fs"
},
"5": {
"4": {
"@type": "SoftwareApplication",
"identifier": "geodist",
"name": "geodist",
Expand All @@ -306,7 +294,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=geodist"
},
"6": {
"5": {
"@type": "SoftwareApplication",
"identifier": "magrittr",
"name": "magrittr",
Expand All @@ -318,7 +306,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=magrittr"
},
"7": {
"6": {
"@type": "SoftwareApplication",
"identifier": "memoise",
"name": "memoise",
Expand All @@ -330,12 +318,12 @@
},
"sameAs": "https://CRAN.R-project.org/package=memoise"
},
"8": {
"7": {
"@type": "SoftwareApplication",
"identifier": "methods",
"name": "methods"
},
"9": {
"8": {
"@type": "SoftwareApplication",
"identifier": "osmdata",
"name": "osmdata",
Expand All @@ -347,7 +335,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=osmdata"
},
"10": {
"9": {
"@type": "SoftwareApplication",
"identifier": "Rcpp",
"name": "Rcpp",
Expand All @@ -360,7 +348,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=Rcpp"
},
"11": {
"10": {
"@type": "SoftwareApplication",
"identifier": "RcppParallel",
"name": "RcppParallel",
Expand All @@ -372,9 +360,21 @@
},
"sameAs": "https://CRAN.R-project.org/package=RcppParallel"
},
"11": {
"@type": "SoftwareApplication",
"identifier": "secretbase",
"name": "secretbase",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=secretbase"
},
"SystemRequirements": "GNU make"
},
"fileSize": "36594.088KB",
"fileSize": "36598.339KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down
13 changes: 8 additions & 5 deletions man/clear_dodgr_cache.Rd

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