-
-
Notifications
You must be signed in to change notification settings - Fork 205
test: add tests for graph_from_biadjacency_matrix()
#1520
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
Changes from all commits
0df4c0b
5268956
b03e24a
ce249b2
44bd8f4
be6845b
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # graph_from_biadjacency_matrix() works -- dense | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc)) | ||
| Output | ||
| IGRAPH UN-B 8 7 -- | ||
| + attr: type (v/l), name (v/c) | ||
| + edges (vertex names): | ||
| [1] A--c A--d B--b B--c B--e C--b C--d | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| (weighted_g <- graph_from_biadjacency_matrix(inc, weighted = TRUE)) | ||
| Output | ||
| IGRAPH UNWB 8 7 -- | ||
| + attr: type (v/l), name (v/c), weight (e/n) | ||
| + edges (vertex names): | ||
| [1] A--c A--d B--b B--c B--e C--b C--d | ||
|
|
||
| # graph_from_biadjacency_matrix() works -- dense + multiple | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc, multiple = TRUE)) | ||
| Output | ||
| IGRAPH UN-B 8 10 -- | ||
| + attr: type (v/l), name (v/c) | ||
| + edges (vertex names): | ||
| [1] A--c A--d A--d A--e B--b B--e C--b C--c C--c C--e | ||
|
|
||
| # graph_from_biadjacency_matrix() works -- sparse | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc)) | ||
| Output | ||
| IGRAPH UN-B 8 7 -- | ||
| + attr: type (v/l), name (v/c) | ||
| + edges (vertex names): | ||
| [1] B--b C--b A--c B--c A--d C--d B--e | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| (weighted_g <- graph_from_biadjacency_matrix(inc, weighted = TRUE)) | ||
| Output | ||
| IGRAPH UNWB 8 7 -- | ||
| + attr: type (v/l), name (v/c), weight (e/n) | ||
| + edges (vertex names): | ||
| [1] B--b C--b A--c B--c A--d C--d B--e | ||
|
|
||
| # graph_from_biadjacency_matrix() works -- sparse + multiple | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc, multiple = TRUE)) | ||
| Output | ||
| IGRAPH UN-B 8 10 -- | ||
| + attr: type (v/l), name (v/c) | ||
| + edges (vertex names): | ||
| [1] B--b C--b A--c C--c C--c A--d A--d A--e B--e C--e | ||
|
|
||
| # graph_from_biadjacency_matrix() errors well | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc, weight = FALSE)) | ||
| Condition | ||
| Error in `graph_from_biadjacency_matrix()`: | ||
| ! `weighted` can't be `FALSE`. | ||
| i See `?graph_from_biadjacency_matrix()`'s manual page. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc, weight = 42)) | ||
| Condition | ||
| Error in `graph_from_biadjacency_matrix()`: | ||
| ! `weighted` can't be a number. | ||
| i See `?graph_from_biadjacency_matrix()`'s manual page. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| (g <- graph_from_biadjacency_matrix(inc, multiple = TRUE, weighted = TRUE)) | ||
| Condition | ||
| Error in `graph_from_biadjacency_matrix()`: | ||
| ! `multiple` and `weighted` cannot be both `TRUE`. | ||
| igraph either interprets numbers larger than 1 as weights or as multiplicities, but it cannot be both. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| test_that("graph_from_biadjacency_matrix() works -- dense", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| expect_snapshot((g <- graph_from_biadjacency_matrix(inc))) | ||
| expect_false(is_weighted(g)) | ||
|
|
||
| expect_snapshot((weighted_g <- graph_from_biadjacency_matrix(inc, weighted = TRUE))) | ||
| expect_true(is_weighted(weighted_g)) | ||
| }) | ||
|
|
||
|
|
||
| test_that("graph_from_biadjacency_matrix() works -- dense + multiple", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:2, 15, repl = TRUE), 3, 5) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| expect_snapshot((g <- graph_from_biadjacency_matrix(inc, multiple = TRUE))) | ||
| expect_false(is_weighted(g)) | ||
| }) | ||
|
|
||
|
|
||
| test_that("graph_from_biadjacency_matrix() works - dense, modes", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| out_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "out") | ||
| expect_true(is_directed(out_g)) | ||
| expect_length(E(out_g), 7) | ||
| expect_equal(as_adj_list(out_g, mode = "out")$A %>% as.numeric(), c(6, 7)) | ||
|
|
||
| in_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "in") | ||
| expect_true(is_directed(in_g)) | ||
| expect_length(E(in_g), 7) | ||
| expect_equal(as_adj_list(in_g, mode = "in")$A %>% as.numeric(), c(6, 7)) | ||
|
|
||
| mutual_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "all") | ||
| expect_true(is_directed(mutual_g)) | ||
| expect_length(E(mutual_g), 14) | ||
| expect_equal(as_adj_list(mutual_g, mode = "all")$A %>% as.numeric(), c(6, 6, 7, 7)) | ||
| }) | ||
|
|
||
| test_that("graph_from_biadjacency_matrix() works - dense, modes, weighted", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:2, 15, repl = TRUE), 3, 5) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| out_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "out", weighted = TRUE) | ||
| expect_true(is_directed(out_g)) | ||
| expect_length(E(out_g), 8) | ||
| expect_equal(as_adj_list(out_g, mode = "out")$A %>% as.numeric(), c(6, 7, 8)) | ||
|
|
||
| in_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "in", weighted = TRUE) | ||
| expect_true(is_directed(in_g)) | ||
| expect_length(E(in_g), 8) | ||
| expect_equal(as_adj_list(in_g, mode = "in")$A %>% as.numeric(), c(6, 7, 8)) | ||
|
|
||
| mutual_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "all", weighted = TRUE) | ||
| expect_true(is_directed(mutual_g)) | ||
| expect_length(E(mutual_g), 16) | ||
| expect_equal(as_adj_list(mutual_g, mode = "all")$A %>% as.numeric(), c(6, 6, 7, 7, 8, 8)) | ||
| }) | ||
|
|
||
| test_that("graph_from_biadjacency_matrix() works -- sparse", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) | ||
| inc <- Matrix::Matrix(inc, sparse = TRUE) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| expect_snapshot((g <- graph_from_biadjacency_matrix(inc))) | ||
| expect_false(is_weighted(g)) | ||
|
|
||
| expect_snapshot((weighted_g <- graph_from_biadjacency_matrix(inc, weighted = TRUE))) | ||
| expect_true(is_weighted(weighted_g)) | ||
| }) | ||
|
|
||
| test_that("graph_from_biadjacency_matrix() works -- sparse + multiple", { | ||
|
maelle marked this conversation as resolved.
|
||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:2, 15, repl = TRUE), 3, 5) | ||
| inc <- Matrix::Matrix(inc, sparse = TRUE) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| expect_snapshot((g <- graph_from_biadjacency_matrix(inc, multiple = TRUE))) | ||
| expect_false(is_weighted(g)) | ||
| }) | ||
|
|
||
| test_that("graph_from_biadjacency_matrix() works - sparse, modes", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) | ||
| inc <- Matrix::Matrix(inc, sparse = TRUE) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| out_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "out") | ||
| expect_true(is_directed(out_g)) | ||
| expect_length(E(out_g), 7) | ||
| expect_equal(as_adj_list(out_g, mode = "out")$A %>% as.numeric(), c(6, 7)) | ||
|
|
||
| in_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "in") | ||
| expect_true(is_directed(in_g)) | ||
| expect_length(E(in_g), 7) | ||
| expect_equal(as_adj_list(in_g, mode = "in")$A %>% as.numeric(), c(6, 7)) | ||
|
|
||
| mutual_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "all") | ||
| expect_true(is_directed(mutual_g)) | ||
| expect_length(E(mutual_g), 14) | ||
| expect_equal(as_adj_list(mutual_g, mode = "all")$A %>% as.numeric(), c(6, 6, 7, 7)) | ||
| }) | ||
|
|
||
| test_that("graph_from_biadjacency_matrix() works - sparse, modes, weighted", { | ||
| local_igraph_options(print.id = FALSE) | ||
| withr::local_seed(42) | ||
|
|
||
| inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) | ||
|
Member
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. Same comment about weights as before. Have a diversity of weights. Have at least two different kinds of weights (
Contributor
Author
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. |
||
| inc <- Matrix::Matrix(inc, sparse = TRUE) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| out_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "out", weighted= TRUE) | ||
| expect_true(is_directed(out_g)) | ||
| expect_length(E(out_g), 7) | ||
| expect_equal(as_adj_list(out_g, mode = "out")$A %>% as.numeric(), c(6, 7)) | ||
|
|
||
| in_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "in", weighted= TRUE) | ||
| expect_true(is_directed(in_g)) | ||
| expect_length(E(in_g), 7) | ||
| expect_equal(as_adj_list(in_g, mode = "in")$A %>% as.numeric(), c(6, 7)) | ||
|
|
||
| mutual_g <- graph_from_biadjacency_matrix(inc, directed = TRUE, mode = "all", weighted= TRUE) | ||
| expect_true(is_directed(mutual_g)) | ||
| expect_length(E(mutual_g), 14) | ||
| expect_equal(as_adj_list(mutual_g, mode = "all")$A %>% as.numeric(), c(6, 6, 7, 7)) | ||
| }) | ||
|
|
||
| test_that("graph_from_biadjacency_matrix() errors well", { | ||
| inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) | ||
| colnames(inc) <- letters[1:5] | ||
| rownames(inc) <- LETTERS[1:3] | ||
|
|
||
| expect_snapshot(error= TRUE, { | ||
| (g <- graph_from_biadjacency_matrix(inc, weight = FALSE)) | ||
| }) | ||
| expect_snapshot(error = TRUE, { | ||
| (g <- graph_from_biadjacency_matrix(inc, weight = 42)) | ||
| }) | ||
| expect_snapshot(error = TRUE, { | ||
| (g <- graph_from_biadjacency_matrix(inc, multiple = TRUE, weighted = TRUE)) | ||
| }) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.