-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from andyquinterom/T19
feat: Adds `get_roots` and `subset_graph` functions
- Loading branch information
Showing
14 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' @title Get the root nodes of a graph | ||
#' | ||
#' @description | ||
#' Retrieves the nodes in a graph that have no parents | ||
#' | ||
#' If no `node_ids` argument is supplied then all of | ||
#' the roots in the graph are returned. | ||
#' @param graph A graph object | ||
#' @param node_ids The node ids of which the roots must be descendants of | ||
#' @return A character vector of node ids | ||
#' @export | ||
get_roots <- function(graph, node_ids) { | ||
UseMethod("get_roots") | ||
} | ||
|
||
#' @export | ||
get_roots.DirectedGraph <- function(graph, node_ids) { | ||
if (missing(node_ids)) { | ||
return(graph$get_roots()) | ||
} | ||
graph$get_roots_over(node_ids) | ||
} | ||
|
||
#' @export | ||
get_roots.DirectedAcyclicGraph <- function(graph, node_ids) { | ||
if (missing(node_ids)) { | ||
return(graph$get_roots()) | ||
} | ||
graph$get_roots_over(node_ids) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#' @title Get a subset or a graph | ||
#' @description todo! | ||
#' @param graph A graph object | ||
#' @param node_id The node id from which to split the tree | ||
#' @return A new graph object | ||
#' @export | ||
subset_graph <- function(graph, node_id) { | ||
UseMethod("subset_graph") | ||
} | ||
|
||
#' @export | ||
subset_graph.DirectedGraph <- function(graph, node_id) { | ||
graph$subset(node_id) | ||
} | ||
|
||
#' @export | ||
subset_graph.DirectedAcyclicGraph <- function(graph, node_id) { | ||
graph$subset(node_id) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
test_that("can find least common parents between selected nodes", { | ||
|
||
graph <- new_directed_graph() | ||
|
||
graph_edges <- data.frame( | ||
parent = c("A", "B", "C", "C", "F"), | ||
child = c("B", "C", "D", "E", "D") | ||
) | ||
|
||
graph_nodes <- data.frame( | ||
node_id = c("A", "B", "C", "D", "E", "F") | ||
) | ||
|
||
graph |> | ||
populate_nodes(graph_nodes, "node_id") |> | ||
populate_edges(graph_edges, "parent", "child") | ||
|
||
|
||
graph_under_d <- graph |> | ||
subset_graph("D") | ||
|
||
nodes <- graph_under_d |> | ||
get_nodes() | ||
|
||
expect_equal(length(nodes), 1) | ||
|
||
graph_under_c <- graph |> | ||
subset_graph("C") | ||
|
||
nodes <- graph_under_c |> | ||
get_nodes() | ||
|
||
expect_equal(length(nodes), 3) | ||
expect_equal(find_path(graph_under_c, "C", "E"), c("C", "E")) | ||
|
||
graph_under_b <- graph |> | ||
subset_graph("B") | ||
|
||
nodes <- graph_under_b |> | ||
get_nodes() | ||
|
||
expect_equal(length(nodes), 4) | ||
expect_equal(find_path(graph_under_b, "B", "E"), c("B", "C", "E")) | ||
|
||
}) |