-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
test: merged and refactored flow.R tests #1675
base: main
Are you sure you want to change the base?
Changes from all commits
1064771
76dcbdd
3bf022a
ba73938
b11a3c9
f3ab2c5
ce6cb74
3dedcab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
#' Vertex connectivity | ||
#' | ||
#' @description | ||
|
@@ -287,27 +286,24 @@ dominator.tree <- function(graph, root, mode = c("out", "in", "all", "total")) { | |
#' @export | ||
min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value.only = TRUE) { | ||
ensure_igraph(graph) | ||
if (is.null(capacity)) { | ||
if ("capacity" %in% edge_attr_names(graph)) { | ||
capacity <- E(graph)$capacity | ||
} | ||
} | ||
if (length(source) == 0) { | ||
source <- NULL | ||
if (is.null(capacity) && "capacity" %in% edge_attr_names(graph)) { | ||
capacity <- E(graph)$capacity | ||
} | ||
if (length(target) == 0) { | ||
target <- NULL | ||
} | ||
if (is.null(source) && !is.null(target) || | ||
is.null(target) && !is.null(source)) { | ||
stop("Please give both source and target or neither") | ||
|
||
if (xor(is.null(source), is.null(target))) { | ||
cli::cli_abort(c( | ||
"{.arg source} and {.arg target} must not be specified at the same time.", | ||
i = "Specify either {.arg source} or {.arg target} or neither." | ||
)) | ||
} | ||
|
||
if (!is.null(capacity)) { | ||
capacity <- as.numeric(capacity) | ||
} | ||
|
||
value.only <- as.logical(value.only) | ||
on.exit(.Call(R_igraph_finalizer)) | ||
|
||
if (is.null(target) && is.null(source)) { | ||
if (value.only) { | ||
res <- .Call(R_igraph_mincut_value, graph, capacity) | ||
|
@@ -430,13 +426,6 @@ min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value. | |
vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE) { | ||
ensure_igraph(graph) | ||
|
||
if (length(source) == 0) { | ||
source <- NULL | ||
} | ||
if (length(target) == 0) { | ||
target <- NULL | ||
} | ||
|
||
if (is.null(source) && is.null(target)) { | ||
on.exit(.Call(R_igraph_finalizer)) | ||
.Call(R_igraph_vertex_connectivity, graph, as.logical(checks)) | ||
|
@@ -447,7 +436,10 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR | |
as_igraph_vs(graph, target) - 1 | ||
) | ||
} else { | ||
stop("either give both source and target or neither") | ||
cli::cli_abort(c( | ||
"{.arg source} and {.arg target} must not be specified at the same time.", | ||
i = "Specify either {.arg source} or {.arg target} or neither." | ||
)) | ||
} | ||
} | ||
|
||
|
@@ -534,13 +526,6 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR | |
edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE) { | ||
ensure_igraph(graph) | ||
|
||
if (length(source) == 0) { | ||
source <- NULL | ||
} | ||
if (length(target) == 0) { | ||
target <- NULL | ||
} | ||
|
||
if (is.null(source) && is.null(target)) { | ||
on.exit(.Call(R_igraph_finalizer)) | ||
.Call(R_igraph_edge_connectivity, graph, as.logical(checks)) | ||
|
@@ -551,22 +536,20 @@ edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE | |
as_igraph_vs(graph, source) - 1, as_igraph_vs(graph, target) - 1 | ||
) | ||
} else { | ||
stop("either give both source and target or neither") | ||
cli::cli_abort(c( | ||
"{.arg source} and {.arg target} must not be specified at the same time.", | ||
i = "Specify either {.arg source} or {.arg target} or neither." | ||
)) | ||
} | ||
} | ||
|
||
#' @rdname edge_connectivity | ||
#' @export | ||
edge_disjoint_paths <- function(graph, source, target) { | ||
edge_disjoint_paths <- function(graph, source = NULL, target = NULL) { | ||
ensure_igraph(graph) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why change the default value especially if the code won't work with it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it to align it with the function below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this was just to align the code with the rest. But I don't mind changing it back if you think it is better to leave it untouched |
||
|
||
if (length(source) == 0) { | ||
source <- NULL | ||
} | ||
if (length(target) == 0) { | ||
target <- NULL | ||
if (is.null(source) || is.null(target)) { | ||
cli::cli_abort("Both source and target must be given") | ||
} | ||
|
||
on.exit(.Call(R_igraph_finalizer)) | ||
.Call( | ||
R_igraph_edge_disjoint_paths, graph, | ||
|
@@ -578,12 +561,8 @@ edge_disjoint_paths <- function(graph, source, target) { | |
#' @export | ||
vertex_disjoint_paths <- function(graph, source = NULL, target = NULL) { | ||
ensure_igraph(graph) | ||
|
||
if (length(source) == 0) { | ||
source <- NULL | ||
} | ||
if (length(target) == 0) { | ||
target <- NULL | ||
if (is.null(source) || is.null(target)) { | ||
cli::cli_abort("Both source and target must be given") | ||
} | ||
|
||
on.exit(.Call(R_igraph_finalizer)) | ||
|
@@ -638,13 +617,13 @@ cohesion.igraph <- function(x, checks = TRUE, ...) { | |
#' @examples | ||
#' | ||
#' # A very simple graph | ||
#' g <- graph_from_literal(a -+ b -+ c -+ d -+ e) | ||
#' g <- graph_from_literal(a - +b - +c - +d - +e) | ||
#' st_cuts(g, source = "a", target = "e") | ||
#' | ||
#' # A somewhat more difficult graph | ||
#' g2 <- graph_from_literal( | ||
#' s --+ a:b, a:b --+ t, | ||
#' a --+ 1:2:3, 1:2:3 --+ b | ||
#' s - -+a:b, a:b - -+t, | ||
#' a - -+1:2:3, 1:2:3 - -+b | ||
#' ) | ||
#' st_cuts(g2, source = "s", target = "t") | ||
#' @family flow | ||
|
@@ -693,8 +672,8 @@ st_cuts <- all_st_cuts_impl | |
#' | ||
#' # A difficult graph, from the Provan-Shier paper | ||
#' g <- graph_from_literal( | ||
#' s --+ a:b, a:b --+ t, | ||
#' a --+ 1:2:3:4:5, 1:2:3:4:5 --+ b | ||
#' s - -+a:b, a:b - -+t, | ||
#' a - -+1:2:3:4:5, 1:2:3:4:5 - -+b | ||
#' ) | ||
#' st_min_cuts(g, source = "s", target = "t") | ||
#' @family flow | ||
|
@@ -746,9 +725,9 @@ st_min_cuts <- all_st_mincuts_impl | |
#' | ||
#' ## The example from the paper | ||
#' g <- graph_from_literal( | ||
#' R -+ A:B:C, A -+ D, B -+ A:D:E, C -+ F:G, D -+ L, | ||
#' E -+ H, F -+ I, G -+ I:J, H -+ E:K, I -+ K, J -+ I, | ||
#' K -+ I:R, L -+ H | ||
#' R - +A:B:C, A - +D, B - +A:D:E, C - +F:G, D - +L, | ||
#' E - +H, F - +I, G - +I:J, H - +E:K, I - +K, J - +I, | ||
#' K - +I:R, L - +H | ||
#' ) | ||
#' dtree <- dominator_tree(g, root = "R") | ||
#' layout <- layout_as_tree(dtree$domtree, root = "R") | ||
|
@@ -758,11 +737,13 @@ st_min_cuts <- all_st_mincuts_impl | |
#' @export | ||
dominator_tree <- function(graph, root, mode = c("out", "in", "all", "total")) { | ||
# Argument checks | ||
ensure_igraph(graph) | ||
root <- as_igraph_vs(graph, root) | ||
if (length(root) == 0) { | ||
stop("No vertex was specified") | ||
ensure_igraph(graph) | ||
|
||
if (missing(root) || is.null(root)) { | ||
cli::cli_abort("{.arg root} must be specified.") | ||
} | ||
root <- as_igraph_vs(graph, root) | ||
|
||
mode <- switch(igraph.match.arg(mode), | ||
"out" = 1, | ||
"in" = 2, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# st_cuts errors work | ||
|
||
Code | ||
st_cuts(g_path, source = "a", target = NULL) | ||
Condition | ||
Error in `st_cuts()`: | ||
! No vertex was specified | ||
|
||
--- | ||
|
||
Code | ||
st_cuts(g_path, source = NULL, target = "a") | ||
Condition | ||
Error in `st_cuts()`: | ||
! No vertex was specified | ||
|
||
--- | ||
|
||
Code | ||
st_min_cuts(g_path, source = "a", target = NULL) | ||
Condition | ||
Error in `st_min_cuts()`: | ||
! No vertex was specified | ||
|
||
--- | ||
|
||
Code | ||
st_min_cuts(g_path, source = NULL, target = "a") | ||
Condition | ||
Error in `st_min_cuts()`: | ||
! No vertex was specified | ||
|
||
# vertex_connectivity error works | ||
|
||
Code | ||
vertex_connectivity(g_path, source = 1) | ||
Condition | ||
Error in `vertex_connectivity()`: | ||
! `source` and `target` must not be specified at the same time. | ||
i Specify either `source` or `target` or neither. | ||
|
||
# edge_connectivity error works | ||
|
||
Code | ||
edge_connectivity(g_path, source = 1) | ||
Condition | ||
Error in `edge_connectivity()`: | ||
! `source` and `target` must not be specified at the same time. | ||
i Specify either `source` or `target` or neither. | ||
|
||
# edge_disjoint_paths error works | ||
|
||
Code | ||
edge_disjoint_paths(g_path, source = 1, target = NULL) | ||
Condition | ||
Error in `edge_disjoint_paths()`: | ||
! Both source and target must be given | ||
|
||
--- | ||
|
||
Code | ||
edge_disjoint_paths(g_path, source = NULL, target = 1) | ||
Condition | ||
Error in `edge_disjoint_paths()`: | ||
! Both source and target must be given | ||
|
||
# vertex_disjoint_paths error works | ||
|
||
Code | ||
vertex_disjoint_paths(g_path, source = 1) | ||
Condition | ||
Error in `vertex_disjoint_paths()`: | ||
! Both source and target must be given | ||
|
||
--- | ||
|
||
Code | ||
vertex_disjoint_paths(g_path, source = 1) | ||
Condition | ||
Error in `vertex_disjoint_paths()`: | ||
! Both source and target must be given | ||
|
||
# dominator_tree errors work | ||
|
||
Code | ||
dominator_tree(g_tree) | ||
Condition | ||
Error in `dominator_tree()`: | ||
! `root` must be specified. | ||
|
||
--- | ||
|
||
Code | ||
dominator_tree(g_tree, root = NULL) | ||
Condition | ||
Error in `dominator_tree()`: | ||
! `root` must be specified. | ||
|
||
# min_st_separators() works for the note case | ||
|
||
Code | ||
min_st_separators(g_note) | ||
Output | ||
[[1]] | ||
+ 1/5 vertex, named, from something | ||
[1] 1 | ||
|
||
[[2]] | ||
+ 2/5 vertices, named, from something | ||
[1] 2 4 | ||
|
||
[[3]] | ||
+ 2/5 vertices, named, from something | ||
[1] 1 3 | ||
|
||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OMG I did not know about
xor()
! 🤯Could you explain why we should use it instead of the operator directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason I had to switch to it was that it is shorter and expresses what we want more clearly.
But now I am note sure if it is 100% ok to use, given that it uses single
|
and&
? Could this be an issue? I think not