Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds subsets by multiple nodes #37

Merged
merged 3 commits into from
Jan 21, 2025
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orbweaver
Title: Fast and Efficient Graph Data Structures
Version: 0.16.0
Version: 0.17.1
Authors@R:
c(person(given = "ixpantia, SRL",
role = "cph",
Expand Down
4 changes: 4 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ DirectedGraph$get_roots_over <- function(node_ids) .Call(wrap__DirectedGraph__ge

DirectedGraph$subset <- function(node_id) .Call(wrap__DirectedGraph__subset, self, node_id)

DirectedGraph$subset_multi <- function(node_ids) .Call(wrap__DirectedGraph__subset_multi, self, node_ids)

DirectedGraph$print <- function() invisible(.Call(wrap__DirectedGraph__print, self))

DirectedGraph$to_bin_disk <- function(path) .Call(wrap__DirectedGraph__to_bin_disk, self, path)
Expand Down Expand Up @@ -84,6 +86,8 @@ DirectedAcyclicGraph$get_roots_over <- function(node_ids) .Call(wrap__DirectedAc

DirectedAcyclicGraph$subset <- function(node_id) .Call(wrap__DirectedAcyclicGraph__subset, self, node_id)

DirectedAcyclicGraph$subset_multi <- function(node_ids) .Call(wrap__DirectedAcyclicGraph__subset_multi, self, node_ids)

DirectedAcyclicGraph$print <- function() invisible(.Call(wrap__DirectedAcyclicGraph__print, self))

DirectedAcyclicGraph$to_bin_disk <- function(path) .Call(wrap__DirectedAcyclicGraph__to_bin_disk, self, path)
Expand Down
4 changes: 2 additions & 2 deletions R/subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
subset.DirectedGraph <- function(x, ...) {
arguments <- c(...)
if (length(arguments) > 1) {
rlang::abort("Currently only one node is supported for subset")
return(throw_if_error(x$subset_multi(arguments)))
}
throw_if_error(x$subset(arguments))
}
Expand All @@ -11,7 +11,7 @@ subset.DirectedGraph <- function(x, ...) {
subset.DirectedAcyclicGraph <- function(x, ...) {
arguments <- c(...)
if (length(arguments) > 1) {
rlang::abort("Currently only one node is supported for subset")
return(throw_if_error(x$subset_multi(arguments)))
}
throw_if_error(x$subset(arguments))
}
40 changes: 40 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3'

env:
NOT_CRAN: true

tasks:
clean:
- rm -rf src/.cargo
- rm -rf orbweaver_*.tar.gz
vendor:
dir: src/rust
cmds:
- cargo vendor-filterer > vendor-config.toml
- tar -cJf vendor.tar.xz vendor/
- rm -rf vendor/
test:
deps:
- test-rust
- test-r
test-rust:
dir: src/rust
cmds:
- cargo test
test-r:
cmds:
- Rscript -e "devtools::test()"
install:
deps:
# - document
cmds:
- Rscript -e "devtools::install()"
document:
cmds:
- Rscript -e "rextendr::document()"
check:
deps:
- clean
cmds:
- R CMD build .
- R CMD check orbweaver*.tar.gz
68 changes: 34 additions & 34 deletions src/rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = [ 'staticlib', 'lib' ]
name = 'orbweaver_r'

[dependencies]
orbweaver = { version = "0.16" }
orbweaver = { version = "0.17.1" }
extendr-api = { version = "0.7", features = ["serde", "result_condition"] }

# This will help us filter the platforms
Expand Down
6 changes: 0 additions & 6 deletions src/rust/Makefile

This file was deleted.

1 change: 1 addition & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub trait RImplDirectedGraph: Sized {
fn get_all_roots(&self) -> NodeVec;
fn get_roots_over(&self, node_ids: RNodesIn) -> Result<NodeVec>;
fn subset(&self, node_id: &str) -> Result<Self>;
fn subset_multi(&self, node_id: RNodesIn) -> Result<Self>;
fn print(&self);
fn find_all_paths(&self, from: &str, to: &str) -> Result<List>;
fn to_bin_disk(&self, path: &str) -> Result<()>;
Expand Down
5 changes: 5 additions & 0 deletions src/rust/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ macro_rules! impl_directed_graph {
fn subset(&self, node_id: &str) -> Result<Self> {
Ok(Self(self.0.subset(node_id).map_err(to_r_error)?))
}
fn subset_multi(&self, node_ids: RNodesIn) -> Result<Self> {
Ok(Self(
self.0.subset_multi(node_ids.iter()).map_err(to_r_error)?,
))
}
fn print(&self) {
println!("{:?}", self.0)
}
Expand Down
Binary file modified src/rust/vendor.tar.xz
Binary file not shown.
64 changes: 52 additions & 12 deletions tests/testthat/test-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,67 @@ test_that("can find subset acyclic graph", {
expect_equal(as.character(find_path(graph_under_b, "B", "E")), c("B", "C", "E"))
})

test_that("subset shout error with multiple arguments", {
test_that("can find subset graph from multiple nodes (directed)", {
graph_edges <- data.frame(
parent = c("A", "B", "C", "C", "F"),
child = c("B", "C", "D", "E", "D")
child = c("B", "C", "D", "E", "D")
)

graph <- graph_builder() |>
populate_edges(graph_edges, "parent", "child") |>
build_acyclic()
build_directed()

expect_error({
graph |>
subset("A", "B")
})
# Subset from two nodes: B and F
sub_graph <- graph |>
subset(c("B", "F"))

# Check that the resulting subgraph includes the
# reachable descendants from B and F (namely C, D, E) plus B, F themselves.
# A should not be included since we are not traversing upward.
expect_equal(sort(as.character(nodes(sub_graph))),
sort(c("B", "C", "D", "E", "F")))

# Some path checks:
# B -> E goes through B -> C -> E
expect_equal(as.character(find_path(sub_graph, "B", "E")),
c("B", "C", "E"))
# F -> D is direct
expect_equal(as.character(find_path(sub_graph, "F", "D")),
c("F", "D"))
# A is not in sub_graph, so the path won't exist
expect_false("A" %in% as.character(nodes(sub_graph)))
})

test_that("can find subset graph from multiple nodes (acyclic)", {
graph_edges <- data.frame(
parent = c("A", "B", "C", "C", "F"),
child = c("B", "C", "D", "E", "D")
)

graph <- graph_builder() |>
populate_edges(graph_edges, "parent", "child") |>
build_directed()
build_acyclic()

expect_error({
graph |>
subset("A", "B")
})
# Subset from two nodes: B and F
sub_graph <- graph |>
subset(c("B", "F"))

# The resulting object should still be a DirectedAcyclicGraph
expect_s3_class(sub_graph, "DirectedAcyclicGraph")

# Check that the resulting subgraph includes the
# reachable descendants from B and F (namely C, D, E) plus B, F themselves.
expect_equal(sort(as.character(nodes(sub_graph))),
sort(c("B", "C", "D", "E", "F")))

# Path checks:
# B -> E goes through B -> C -> E
expect_equal(as.character(find_path(sub_graph, "B", "E")),
c("B", "C", "E"))
# F -> D is direct
expect_equal(as.character(find_path(sub_graph, "F", "D")),
c("F", "D"))
# A is not included
expect_false("A" %in% as.character(nodes(sub_graph)))
})

Loading