Skip to content

Commit

Permalink
rm fst to fix #26
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Feb 20, 2024
1 parent 2303120 commit b499f83
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 44 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: m4ra
Title: Many-to-Many Multi-Modal Routing Aggregator
Version: 0.1.1.031
Version: 0.1.1.032
Authors@R:
person(given = "Mark",
family = "Padgham",
Expand All @@ -19,7 +19,6 @@ Imports:
digest,
dodgr,
fs,
fst,
gtfsrouter,
methods,
memoise,
Expand Down
31 changes: 6 additions & 25 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ m4ra_network_hash <- function (graph) {
#' Save a weighted street network in both full and contracted forms to local
#' cache.
#'
#' This uses \pkg{fst}, which strips all attributes, so they are saved
#' separately, and re-attached in `load_cached_network()`.
#'
#' @param graph The full network to be cached.
#' @param city Name of city; used to name cached network files.
#' @family cache
Expand Down Expand Up @@ -116,7 +113,6 @@ cache_one_graph <- function (graph, city) {

contracted <- inherits (graph, "dodgr_contracted")

atag <- ifelse (contracted, "attrc", "attr")
gtag <- ifelse (contracted, "graphc", "graph")

a <- get_graph_attributes (graph)
Expand All @@ -125,22 +121,15 @@ cache_one_graph <- function (graph, city) {

cache_dir <- fs::path (m4ra_cache_dir (), city)

aname <- paste0 (
gname <- paste0 (
"m4ra-", city, "-", mode, "-",
atag, "-", hash, ".Rds"
gtag, "-", hash, ".Rds"
)
apath <- fs::path (cache_dir, aname)

saveRDS (a, apath)
gpath <- fs::path (cache_dir, gname)

gpath <- gsub (
paste0 ("\\-", atag, "\\-"),
paste0 ("-", gtag, "-"),
apath
)
fst::write_fst (graph, gpath, compress = 0)
saveRDS (graph, gpath)

return (c (apath, gpath))
return (gpath)
}


Expand Down Expand Up @@ -194,17 +183,9 @@ m4ra_load_cached_network <- function (city = NULL, mode = "foot",

}

graph <- m_read_fst (f)

ptn <- gsub ("graph", "attr", ptn) # retains 'attrc' for contracted
a <- readRDS (grep (ptn, flist, value = TRUE))
for (i in seq_along (a)) {
attr (graph, names (a) [i]) <- a [[i]]
}
graph <- m_readRDS (f)

return (graph)
}

m_readRDS <- memoise::memoise (function (path) readRDS (path))

m_read_fst <- memoise::memoise (function (path) fst::read_fst (path))
11 changes: 5 additions & 6 deletions R/graph-contraction.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#' m4rar_contract_graph
#'
#' Removes redundant (straight-line) vertices from graph, leaving only junction
#' vertices. This is mostly a copy of the same function in \pkg{dodgr}, but uses
#' \pkg{fst} to write and read locally-cached versions of graphs, does that in
#' the `m4ra_cache_dir` rather than tempdir, and does not expose the `verts`
#' parameter, so graph contraction leaves junction vertices only, with no
#' optional extra vertices.
#' vertices. This is mostly a copy of the same function in \pkg{dodgr}, but
#' writes and reads locally-cached versions of graphs in the `m4ra_cache_dir`
#' rather than tempdir, and does not expose the `verts` parameter, so graph
#' contraction leaves junction vertices only, with no optional extra vertices.
#'
#' @param graph A flat table of graph edges. Must contain columns labelled
#' `from` and `to`, or `start` and `stop`. May also contain
Expand Down Expand Up @@ -103,7 +102,7 @@ m4ra_contract_graph <- function (graph, city) {
substring (hash, 1, 6), ".Rds"
)
)
fst::write_fst (edge_map, fname_e, compress = 0)
saveRDS (edge_map, fname_e)
}

return (graph_c)
Expand Down
9 changes: 4 additions & 5 deletions R/vertices.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#'
#' Extract vertices of graph, including spatial coordinates if included.
#' Modified version of equivalent \pkg{dodgr} function that uses different hash
#' comparisons, and uses \pkg{fst} for write/read functions. This version also
#' does none of the checks implemented in the \pkg{dodgr} version, and assumes
#' graphs remain unmodified throughout.
#' comparisons. This version also does none of the checks implemented in the
#' \pkg{dodgr} version, and assumes graphs remain unmodified throughout.
#'
#' @param graph A flat table of graph edges. Must contain columns labelled
#' `from` and `to`, or `start` and `stop`. May also contain
Expand Down Expand Up @@ -41,10 +40,10 @@ m4ra_vertices <- function (graph, city) {
fpath <- fs::path (cache_dir, fname)

if (fs::file_exists (fpath)) {
verts <- fst::read_fst (fpath)
verts <- readRDS (fpath)
} else {
verts <- dodgr_vertices_internal (graph)
fst::write_fst (verts, fpath, compress = 0)
saveRDS (verts, fpath)
}

return (verts)
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/UrbanAnalyst/m4ra",
"issueTracker": "https://github.com/UrbanAnalyst/m4ra/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.1.031",
"version": "0.1.1.032",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
4 changes: 2 additions & 2 deletions man/m4ra_cache_network.Rd

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

5 changes: 2 additions & 3 deletions man/m4ra_vertices.Rd

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

0 comments on commit b499f83

Please sign in to comment.