diff --git a/R/indexing.R b/R/indexing.R index 453392e1582..2e3466ab7b7 100644 --- a/R/indexing.R +++ b/R/indexing.R @@ -59,7 +59,7 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) { if (missing(i)) { i_seq <- seq_len(vcount(x)) has_i <- FALSE - } else{ + } else { i_seq <- i has_i <- TRUE } @@ -78,10 +78,10 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) { to_id <- unlist(adj) edge_list <- data.frame(from = as.integer(from_id), to = as.integer(to_id)) - if(has_j){ + if (has_j) { edge_list <- edge_list[edge_list$to %in% j_seq, ] } - + row_indices <- edge_list[[1]] col_indices <- edge_list[[2]] @@ -214,21 +214,21 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) { ## Argument checks if ((!missing(from) || !missing(to)) && (!missing(i) || !missing(j))) { - stop("Cannot give 'from'/'to' together with regular indices") + cli::cli_abort("Cannot use {.arg from}/{.arg to} together with regular indices") } if ((!missing(from) && missing(to)) || (missing(from) && !missing(to))) { - stop("Cannot give 'from'/'to' without the other") + cli::cli_abort("Cannot use {.arg from}/{.arg to} without the other") } if (!missing(from)) { if ((!is.numeric(from) && !is.character(from)) || any(is.na(from))) { - stop("'from' must be a numeric or character vector without NAs") + cli::cli_abort("{.arg from} must be a numeric or character vector without NAs") } if ((!is.numeric(to) && !is.character(to)) || any(is.na(to))) { - stop("'to' must be a numeric or character vector without NAs") + cli::cli_abort("{.arg to} must be a numeric or character vector without NAs") } if (length(from) != length(to)) { - stop("'from' and 'to' must have the same length") + cli::cli_abort("{.arg from} and {.arg to} must have the same length") } } @@ -286,10 +286,9 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) { if (!sparse) { as.matrix(sub_adjmat[, , drop = drop]) - } else{ + } else { sub_adjmat[, , drop = drop] - } - + } } #' Query and manipulate a graph as it were an adjacency list @@ -353,8 +352,8 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) { edges = FALSE, exact = TRUE) { getfun <- if (edges) as_adj_edge_list else as_adj_list - if (!missing(i) && !missing(from)) stop("Cannot give both 'i' and 'from'") - if (!missing(j) && !missing(to)) stop("Cannot give both 'j' and 'to'") + if (!missing(i) && !missing(from)) cli::cli_abort("Cannot use both {.arg i} and {.arg from}") + if (!missing(j) && !missing(to)) cli::cli_abort("Cannot use both {.arg j} and {.arg to}") if (missing(i) && !missing(from)) i <- from if (missing(j) && !missing(to)) j <- to @@ -425,28 +424,28 @@ expand.grid.unordered <- function(i, j, loops = FALSE, directed = FALSE) { ## Argument checks if ((!missing(from) || !missing(to)) && (!missing(i) || !missing(j))) { - stop("Cannot give 'from'/'to' together with regular indices") + cli::cli_abort("Cannot use {.arg from}/{.arg to} together with regular indices") } if ((!missing(from) && missing(to)) || (missing(from) && !missing(to))) { - stop("Cannot give 'from'/'to' without the other") + cli::cli_abort("Cannot use {.arg from}/{.arg to} without the other") } if (is.null(attr) && (!is.null(value) && !is.numeric(value) && !is.logical(value))) { - stop("New value should be NULL, numeric or logical") + cli::cli_abort("New value should be NULL, numeric or logical") } if (is.null(attr) && !is.null(value) && length(value) != 1) { - stop("Logical or numeric value must be of length 1") + cli::cli_abort("Logical or numeric value must be of length 1") } if (!missing(from)) { if ((!is.numeric(from) && !is.character(from)) || any(is.na(from))) { - stop("'from' must be a numeric or character vector without NAs") + cli::cli_abort("{.arg from} must be a numeric or character vector without NAs") } if ((!is.numeric(to) && !is.character(to)) || any(is.na(to))) { - stop("'to' must be a numeric or character vector without NAs") + cli::cli_abort("{.arg to} must be a numeric or character vector without NAs") } if (length(from) != length(to)) { - stop("'from' and 'to' must have the same length") + cli::cli_abort("{.arg from} and {.arg to} must have the same length") } } @@ -498,7 +497,7 @@ expand.grid.unordered <- function(i, j, loops = FALSE, directed = FALSE) { if (is.null(attr)) { if (value > 1) { - cli::cli_abort("value greater than one but graph is not weighted and no attribute was specified.") + cli::cli_abort("{.arg value} greater than one but graph is not weighted and {.arg attr} was not specified.") } x <- add_edges(x, toadd) } else { diff --git a/tests/testthat/helper-indexing.R b/tests/testthat/helper-indexing.R index c1c00ae9b84..ce45361a6b6 100644 --- a/tests/testthat/helper-indexing.R +++ b/tests/testthat/helper-indexing.R @@ -3,7 +3,7 @@ vector_to_square_matrix <- function(...) { matrix(v, nrow = sqrt(length(v))) } -canonicalize_matrix <- function(x) { +as_unnamed_dense_matrix <- function(x) { x <- as.matrix(x) dimnames(x) <- NULL x diff --git a/tests/testthat/test-bug-1073705-indexing.R b/tests/testthat/test-bug-1073705-indexing.R deleted file mode 100644 index 17f5e04cb04..00000000000 --- a/tests/testthat/test-bug-1073705-indexing.R +++ /dev/null @@ -1,16 +0,0 @@ -test_that("Weighted indexing does not remove edges", { - g <- make_ring(10) - g[1, 2, attr = "weight"] <- 0 - expect_true("weight" %in% edge_attr_names(g)) - expect_equal(E(g)$weight, c(0, rep(NA, 9))) - - el <- as_edgelist(g) - g[from = el[, 1], to = el[, 2], attr = "sim"] <- rep(0:1, length.out = ecount(g)) - expect_true("sim" %in% edge_attr_names(g)) - expect_equal(E(g)$sim, rep(0:1, 5)) - - V(g)$name <- letters[seq_len(vcount(g))] - el <- as_edgelist(g) - g[from = el[, 1], to = el[, 2], attr = "sim"] <- rep(1:0, length.out = ecount(g)) - expect_equal(E(g)$sim, rep(1:0, 5)) -}) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 176b8ff9f02..7d731578b5d 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -282,7 +282,7 @@ test_that("as_adjacency_matrix() works -- dense + weights", { mat[lower.tri(mat)] <- 1:10 mat <- mat + t(mat) A <- as_adjacency_matrix(g, attr = "weight", sparse = FALSE) - expect_equal(canonicalize_matrix(A), mat) + expect_equal(as_unnamed_dense_matrix(A), mat) }) test_that("as_biadjacency_matrix() works -- dense + weights", { @@ -295,5 +295,5 @@ test_that("as_biadjacency_matrix() works -- dense + weights", { ncol = 2L, dimnames = list(c("1", "3", "5", "6"), c("2", "4")) ) - expect_equal(canonicalize_matrix(A), canonicalize_matrix(mat)) + expect_equal(as_unnamed_dense_matrix(A), as_unnamed_dense_matrix(mat)) }) diff --git a/tests/testthat/test-deprecated_indexing_functions.R b/tests/testthat/test-deprecated_indexing_functions.R deleted file mode 100644 index aac16c9c3fe..00000000000 --- a/tests/testthat/test-deprecated_indexing_functions.R +++ /dev/null @@ -1,16 +0,0 @@ -test_that("deprecated indexing functions are indeed deprecated", { - g <- make_ring(10) - - expect_error(V(g)[nei(1)], "was deprecated") - expect_error(V(g)[innei(1)], "was deprecated") - expect_error(V(g)[outnei(1)], "was deprecated") - expect_error(V(g)[inc(1)], "was deprecated") - expect_error(V(g)[adj(1)], "was deprecated") - expect_error(V(g)[from(1)], "was deprecated") - expect_error(V(g)[to(1)], "was deprecated") - - expect_error(E(g)[adj(1)], "was deprecated") - expect_error(E(g)[inc(1)], "was deprecated") - expect_error(E(g)[from(1)], "was deprecated") - expect_error(E(g)[to(1)], "was deprecated") -}) diff --git a/tests/testthat/test-index-es.R b/tests/testthat/test-index-es.R deleted file mode 100644 index ef34f43a98a..00000000000 --- a/tests/testthat/test-index-es.R +++ /dev/null @@ -1,35 +0,0 @@ -test_that("I can index a vs twice", { - edges <- data.frame( - stringsAsFactors = TRUE, - from = c("BOS", "JFK", "DEN", "BOS", "JFK", "DEN"), - to = c("JFK", "DEN", "ABQ", "JFK", "DEN", "ABQ"), - carrier = c("foo", "foo", "foo", "bar", "bar", "bar") - ) - - vertices <- data.frame( - stringsAsFactors = TRUE, - id = c("BOS", "JFK", "DEN", "ABQ"), - state = c("MA", "NY", "CO", "NM") - ) - - g <- graph_from_data_frame(edges, vertices = vertices) - - x <- V(g)[3:4][state == "NM"] - - expect_equal(ignore_attr = TRUE, x, V(g)["ABQ"]) -}) - -test_that("I can index an es twice", { - edges <- data.frame( - stringsAsFactors = TRUE, - from = c("BOS", "JFK", "DEN", "BOS", "JFK", "DEN"), - to = c("JFK", "DEN", "ABQ", "JFK", "DEN", "ABQ"), - carrier = c("foo", "foo", "foo", "bar", "bar", "bar") - ) - - g <- graph_from_data_frame(edges) - - x <- E(g)["BOS" %->% "JFK"][carrier == "foo"] - - expect_equal(ignore_attr = TRUE, x, E(g)[carrier == "foo" & .from("BOS") & .to("JFK")]) -}) diff --git a/tests/testthat/test-indexing.R b/tests/testthat/test-indexing.R index 2bc8a2894f1..1c610867845 100644 --- a/tests/testthat/test-indexing.R +++ b/tests/testthat/test-indexing.R @@ -1,12 +1,24 @@ test_that("[ indexing works", { skip_if_not_installed("Matrix", minimum_version = "1.6.0") g <- make_tree(20) - ## Are these vertices connected? + expect_equal(g[1, 2], 1) - expect_equal(canonicalize_matrix(g[c(1, 1, 7), c(2, 3, 14)]), vector_to_square_matrix(1, 1, 0, 1, 1, 0, 0, 0, 1)) - expect_equal(canonicalize_matrix(g[c(1, 1, 7), c(5, 3, 12)]), vector_to_square_matrix(0, 0, 0, 1, 1, 0, 0, 0, 0)) - expect_equal(canonicalize_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), matrix(1, 4, 4)) - expect_equal(canonicalize_matrix(g[c(8, 17), c(17, 8)]), vector_to_square_matrix(1, 0, 0, 0)) + expect_equal( + as_unnamed_dense_matrix(g[c(1, 1, 7), c(2, 3, 14)]), + vector_to_square_matrix(1, 1, 0, 1, 1, 0, 0, 0, 1) + ) + expect_equal( + as_unnamed_dense_matrix(g[c(1, 1, 7), c(5, 3, 12)]), + vector_to_square_matrix(0, 0, 0, 1, 1, 0, 0, 0, 0) + ) + expect_equal( + as_unnamed_dense_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), + matrix(1, 4, 4) + ) + expect_equal( + as_unnamed_dense_matrix(g[c(8, 17), c(17, 8)]), + vector_to_square_matrix(1, 0, 0, 0) + ) }) test_that("[ indexing works with symbolic names", { @@ -15,18 +27,21 @@ test_that("[ indexing works with symbolic names", { expect_equal(g["a", "b"], 1) expect_equal( - canonicalize_matrix(g[c("a", "a", "g"), c("b", "c", "n")]), + as_unnamed_dense_matrix(g[c("a", "a", "g"), c("b", "c", "n")]), vector_to_square_matrix(1, 1, 0, 1, 1, 0, 0, 0, 1) ) expect_equal( - canonicalize_matrix(g[c("a", "a", "g"), c("e", "c", "l")]), + as_unnamed_dense_matrix(g[c("a", "a", "g"), c("e", "c", "l")]), vector_to_square_matrix(0, 0, 0, 1, 1, 0, 0, 0, 0) ) expect_equal( - canonicalize_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), + as_unnamed_dense_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), matrix(1, 4, 4) ) - expect_equal(canonicalize_matrix(g[c("h", "q"), c("q", "h")]), vector_to_square_matrix(1, 0, 0, 0)) + expect_equal( + as_unnamed_dense_matrix(g[c("h", "q"), c("q", "h")]), + vector_to_square_matrix(1, 0, 0, 0) + ) }) test_that("[ indexing works with logical vectors", { @@ -77,13 +92,22 @@ test_that("[ indexing works with weighted graphs", { g <- make_test_weighted_tree() expect_equal(g[1, 2], 2) - expect_equal(canonicalize_matrix(g[c(1, 1, 7), c(2, 3, 14)]), vector_to_square_matrix(2, 2, 0, 3, 3, 0, 0, 0, 98)) - expect_equal(canonicalize_matrix(g[c(1, 1, 7), c(5, 3, 12)]), vector_to_square_matrix(0, 0, 0, 3, 3, 0, 0, 0, 0)) expect_equal( - canonicalize_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), + as_unnamed_dense_matrix(g[c(1, 1, 7), c(2, 3, 14)]), + vector_to_square_matrix(2, 2, 0, 3, 3, 0, 0, 0, 98) + ) + expect_equal( + as_unnamed_dense_matrix(g[c(1, 1, 7), c(5, 3, 12)]), + vector_to_square_matrix(0, 0, 0, 3, 3, 0, 0, 0, 0) + ) + expect_equal( + as_unnamed_dense_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), vector_to_square_matrix(2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2) ) - expect_equal(canonicalize_matrix(g[c(8, 17), c(17, 8)]), vector_to_square_matrix(136, 0, 0, 0)) + expect_equal( + as_unnamed_dense_matrix(g[c(8, 17), c(17, 8)]), + vector_to_square_matrix(136, 0, 0, 0) + ) }) test_that("[ indexing works with weighted graphs and symbolic names", { @@ -92,18 +116,21 @@ test_that("[ indexing works with weighted graphs and symbolic names", { expect_equal(g["a", "b"], 2) expect_equal( - canonicalize_matrix(g[c("a", "a", "g"), c("b", "c", "n")]), + as_unnamed_dense_matrix(g[c("a", "a", "g"), c("b", "c", "n")]), vector_to_square_matrix(2, 2, 0, 3, 3, 0, 0, 0, 98) ) expect_equal( - canonicalize_matrix(g[c("a", "a", "g"), c("e", "c", "l")]), + as_unnamed_dense_matrix(g[c("a", "a", "g"), c("e", "c", "l")]), vector_to_square_matrix(0, 0, 0, 3, 3, 0, 0, 0, 0) ) expect_equal( - canonicalize_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), + as_unnamed_dense_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), vector_to_square_matrix(2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2) ) - expect_equal(canonicalize_matrix(g[c("h", "q"), c("q", "h")]), vector_to_square_matrix(136, 0, 0, 0)) + expect_equal( + as_unnamed_dense_matrix(g[c("h", "q"), c("q", "h")]), + vector_to_square_matrix(136, 0, 0, 0) + ) }) test_that("[[ indexing works with adjacent vertices", { @@ -122,8 +149,16 @@ test_that("[[ indexing works with adjacent vertices", { ignore_attr = TRUE ) - expect_equal(g[[1:3, ]], list(a = V(g)[2:3], b = V(g)[4:5], c = V(g)[6:7]), ignore_attr = TRUE) - expect_equal(g[[, 1:3]], list(a = V(g)[numeric()], b = V(g)[1], c = V(g)[1]), ignore_attr = TRUE) + expect_equal( + g[[1:3, ]], + list(a = V(g)[2:3], b = V(g)[4:5], c = V(g)[6:7]), + ignore_attr = TRUE + ) + expect_equal( + g[[, 1:3]], + list(a = V(g)[numeric()], b = V(g)[1], c = V(g)[1]), + ignore_attr = TRUE + ) }) test_that("[[ indexing works with symbolic names", { @@ -276,12 +311,12 @@ test_that("[ handles from and to properly", { test_that("[[ works with from and to", { g <- make_tree(20) - expect_equal(ignore_attr = TRUE, g[[1, ]], g[[from = 1]]) - expect_equal(ignore_attr = TRUE, g[[, 1]], g[[to = 1]]) - expect_equal(ignore_attr = TRUE, g[[1:5, 4:10]], g[[from = 1:5, to = 4:10]]) + expect_equal(g[[1, ]], g[[from = 1]], ignore_attr = TRUE) + expect_equal(g[[, 1]], g[[to = 1]], ignore_attr = TRUE) + expect_equal(g[[1:5, 4:10]], g[[from = 1:5, to = 4:10]], ignore_attr = TRUE) - expect_error(g[[1, from = 1]], "Cannot give both") - expect_error(g[[, 2, to = 10]], "Cannot give both") + expect_error(g[[1, from = 1]], "Cannot use both") + expect_error(g[[, 2, to = 10]], "Cannot use both") }) test_that("[[ returns vertex and edges sequences", { @@ -323,9 +358,9 @@ test_that("[ handles all combinations of i and/or j", { ncol = 10L ) g <- graph_from_adjacency_matrix(A, "directed") - expect_equal(canonicalize_matrix(g[1:3, ]), A[1:3, ]) - expect_equal(canonicalize_matrix(g[, 4:7]), A[, 4:7]) - expect_equal(canonicalize_matrix(g[1:3, 4:7]), A[1:3, 4:7]) + expect_equal(as_unnamed_dense_matrix(g[1:3, ]), A[1:3, ]) + expect_equal(as_unnamed_dense_matrix(g[, 4:7]), A[, 4:7]) + expect_equal(as_unnamed_dense_matrix(g[1:3, 4:7]), A[1:3, 4:7]) }) test_that("[ handles duplicated i/j well", { @@ -341,7 +376,316 @@ test_that("[ handles duplicated i/j well", { ncol = 10L ) g <- graph_from_adjacency_matrix(A, "directed") - expect_equal(canonicalize_matrix(g[c(1, 2, 2), ]), A[c(1, 2, 2), ]) - expect_equal(canonicalize_matrix(g[, c(3, 3, 4, 4)]), A[, c(3, 3, 4, 4)]) - expect_equal(canonicalize_matrix(g[c(1, 2, 2), c(3, 3, 4, 4)]), A[c(1, 2, 2), c(3, 3, 4, 4)]) + expect_equal(as_unnamed_dense_matrix(g[c(1, 2, 2), ]), A[c(1, 2, 2), ]) + expect_equal(as_unnamed_dense_matrix(g[, c(3, 3, 4, 4)]), A[, c(3, 3, 4, 4)]) + expect_equal(as_unnamed_dense_matrix(g[c(1, 2, 2), c(3, 3, 4, 4)]), A[c(1, 2, 2), c(3, 3, 4, 4)]) +}) + +test_that("[ can add and delete edges", { + g <- make_empty_graph(10) + A <- matrix(0, 10, 10) + + A[1, 2] <- g[1, 2] <- TRUE + expect_equal(as_unnamed_dense_matrix(g[]), A) + + A[2, 1] <- g[2, 1] <- TRUE + expect_equal(as_unnamed_dense_matrix(g[]), A) + + g[2, 1] <- NULL + A[2, 1] <- 0 + expect_equal(as_unnamed_dense_matrix(g[]), A) + + A[1, 2] <- g[1, 2] <- FALSE + expect_equal(as_unnamed_dense_matrix(g[]), A) + + g <- make_empty_graph(10) + A <- matrix(0, 10, 10) + A[-1, 1] <- g[-1, 1] <- 1 + expect_equal(as_unnamed_dense_matrix(g[]), A) +}) + +test_that("[ can set weights and delete weighted edges", { + g <- make_empty_graph(10) + A <- matrix(0, 10, 10) + g <- set_edge_attr(g, "weight", c(), 1) + A[1, 2] <- g[1, 2] <- 1 + expect_equal(as_unnamed_dense_matrix(g[]), A) + + A[2, 1] <- g[2, 1] <- 2 + expect_equal(as_unnamed_dense_matrix(g[]), A) + + A[1, 2] <- g[1, 2] <- 3 + expect_equal(as_unnamed_dense_matrix(g[]), A) + + A[1:2, 2:3] <- g[1:2, 2:3, loops = TRUE] <- -1 + expect_equal(as_unnamed_dense_matrix(g[]), A) + + g[1, 2] <- NULL + A[1, 2] <- 0 + expect_equal(as_unnamed_dense_matrix(g[]), A) +}) + +test_that("[ can add edges and ste weights via vertex names", { + g <- make_empty_graph(10) + A <- matrix(0, 10, 10) + V(g)$name <- letters[1:vcount(g)] + rownames(A) <- colnames(A) <- letters[1:vcount(g)] + + A["a", "b"] <- g["a", "b"] <- TRUE + A["b", "c"] <- g["b", "c"] <- TRUE + expect_equal(as_unnamed_dense_matrix(g[]), as_unnamed_dense_matrix(A)) + + A[c("a", "f"), c("f", "a")] <- g[c("a", "f"), c("f", "a"), loops = TRUE] <- TRUE + expect_equal(as_unnamed_dense_matrix(g[]), as_unnamed_dense_matrix(A)) + + A[A == 1] <- NA + A[c("a", "c", "h"), c("a", "b", "c")] <- + g[c("a", "c", "h"), c("a", "b", "c"), attr = "weight", loops = TRUE] <- 3 + expect_equal(as_unnamed_dense_matrix(g[]), as_unnamed_dense_matrix(A)) +}) + +test_that("[ and the from-to notation", { + g <- make_empty_graph(10) + A <- matrix(0, 10, 10) + V(g)$name <- letters[1:vcount(g)] + rownames(A) <- colnames(A) <- letters[1:vcount(g)] + + g[from = c("a", "c", "h"), to = c("a", "b", "c")] <- 1 + A["a", "a"] <- A["c", "b"] <- A["h", "c"] <- 1 + expect_equal( + g[from = c("a", "c", "h", "d"), to = c("a", "b", "c", "e")], + c(1, 1, 1, 0) + ) + expect_equal(as_unnamed_dense_matrix(g[]), as_unnamed_dense_matrix(A)) + + g[from = c("a", "c", "h", "a"), to = c("a", "a", "a", "e"), attr = "weight"] <- 3 + A[A != 0] <- NA + A["a", "a"] <- A["c", "a"] <- A["h", "a"] <- A["a", "e"] <- 3 + expect_equal( + g[from = c("a", "c", "h", "a", "c", "c"), to = c("a", "a", "a", "e", "f", "b")], + c(3, 3, 3, 3, 0, NA) + ) + expect_equal(as_unnamed_dense_matrix(g[]), as_unnamed_dense_matrix(A)) +}) + +test_that("[ and from-to with multiple values", { + g <- make_empty_graph(10) + A <- matrix(0, 10, 10) + V(g)$name <- letters[1:vcount(g)] + rownames(A) <- colnames(A) <- letters[1:vcount(g)] + + g[from = c("a", "c", "h"), to = c("a", "b", "c")] <- 1 + A["a", "a"] <- A["c", "b"] <- A["h", "c"] <- 1 + g[from = c("a", "c", "h", "a"), to = c("a", "a", "a", "e"), attr = "weight"] <- 5:8 + A[A != 0] <- NA + A["a", "a"] <- 5 + A["c", "a"] <- 6 + A["h", "a"] <- 7 + A["a", "e"] <- 8 + expect_equal( + g[from = c("a", "c", "h", "a", "c", "c"), to = c("a", "a", "a", "e", "f", "b")], + c(5:8, 0, NA) + ) + expect_equal(as_unnamed_dense_matrix(g[]), as_unnamed_dense_matrix(A)) +}) + +test_that("[ manipulation works as intended for unweighted", { + # see issue https://github.com/igraph/rigraph/issues/1662 + g1 <- make_empty_graph(n = 10, directed = FALSE) + A1 <- matrix(0, 10, 10) + A1[1:5, ] <- A1[, 1:5] <- g1[1:5, ] <- 1 + diag(A1) <- 0 + expect_equal(as_unnamed_dense_matrix(g1[]), A1) + + g2 <- make_empty_graph(n = 10, directed = FALSE) + A2 <- matrix(0, 10, 10) + A2[1:5, ] <- A2[, 1:5] <- g2[, 1:5] <- 1 + diag(A2) <- 0 + expect_equal(as_unnamed_dense_matrix(g2[]), A2) + + g3 <- make_empty_graph(n = 10, directed = TRUE) + A3 <- matrix(0, 10, 10) + A3[1:5, ] <- g3[1:5, ] <- 1 + diag(A3) <- 0 + expect_equal(as_unnamed_dense_matrix(g3[]), A3) + + g4 <- make_empty_graph(n = 10, directed = TRUE) + A4 <- matrix(0, 10, 10) + A4[, 1:5] <- g4[, 1:5] <- 1 + diag(A4) <- 0 + expect_equal(as_unnamed_dense_matrix(g4[]), A4) + + g5 <- make_empty_graph(n = 10, directed = TRUE) + A5 <- matrix(0, 10, 10) + g5[1, 2] <- g5[2, 1] <- A5[1, 2] <- A5[2, 1] <- 1 + expect_equal(as_unnamed_dense_matrix(g5[]), A5) + + g6 <- make_empty_graph(n = 10, directed = FALSE) + A6 <- matrix(0, 10, 10) + A6[6:10, 1:5] <- A6[1:5, 6:10] <- g6[6:10, 1:5] <- 1 + expect_equal(as_unnamed_dense_matrix(g6[]), A6) + + g7 <- make_empty_graph(n = 10, directed = TRUE) + A7 <- matrix(0, 10, 10) + g7[6:10, 1:5] <- A7[6:10, 1:5] <- 1 + diag(A7) <- 0 + expect_equal(as_unnamed_dense_matrix(g7[]), A7) + + g8 <- make_empty_graph(n = 10, directed = TRUE) + A8 <- matrix(0, 10, 10) + g8[1:5, 6:10] <- A8[1:5, 6:10] <- 1 + diag(A8) <- 0 + expect_equal(as_unnamed_dense_matrix(g8[]), A8) +}) + +test_that("[ manipulation works as intended for weighted", { + # see issue https://github.com/igraph/rigraph/issues/1662 + + g1 <- make_empty_graph(n = 10, directed = FALSE) + A1 <- matrix(0, 10, 10) + A1[1:5, 1:5] <- g1[1:5, 1:5, attr = "weight"] <- 2 + diag(A1) <- 0 + expect_equal(as_unnamed_dense_matrix(g1[]), A1) + + g2 <- make_empty_graph(n = 10, directed = FALSE) + E(g2)$weight <- 1 + A2 <- matrix(0, 10, 10) + A2[1:3, 1:3] <- g2[1:3, 1:3] <- -2 + diag(A2) <- 0 + expect_equal(as_unnamed_dense_matrix(g2[]), A2) +}) + +test_that("[ manipulation handles errors properly", { + g1 <- make_empty_graph(n = 10, directed = FALSE) + expect_error(g1[1:5, ] <- 2) +}) + +test_that("[ deletion works as intended", { + # see issue https://github.com/igraph/rigraph/issues/1662 + g1 <- make_full_graph(n = 10, directed = FALSE) + A1 <- matrix(1, 10, 10) + diag(A1) <- 0 + A1[1:5, ] <- A1[, 1:5] <- g1[1:5, ] <- 0 + expect_equal(as_unnamed_dense_matrix(g1[]), A1) + + g2 <- make_full_graph(n = 10, directed = FALSE) + A2 <- matrix(1, 10, 10) + diag(A2) <- 0 + A2[1:5, ] <- A2[, 1:5] <- g2[, 1:5] <- 0 + expect_equal(as_unnamed_dense_matrix(g2[]), A2) + + g3 <- make_full_graph(n = 10, directed = TRUE) + A3 <- matrix(1, 10, 10) + diag(A3) <- 0 + A3[1:5, ] <- g3[1:5, ] <- 0 + expect_equal(as_unnamed_dense_matrix(g3[]), A3) + + g4 <- make_full_graph(n = 10, directed = TRUE) + A4 <- matrix(1, 10, 10) + diag(A4) <- 0 + A4[, 1:5] <- g4[, 1:5] <- 0 + expect_equal(as_unnamed_dense_matrix(g4[]), A4) + + g5 <- make_full_graph(n = 10, directed = TRUE) + A5 <- matrix(1, 10, 10) + diag(A5) <- 0 + g5[1, 2] <- g5[2, 1] <- A5[1, 2] <- A5[2, 1] <- 0 + expect_equal(as_unnamed_dense_matrix(g5[]), A5) + + g6 <- make_full_graph(n = 10, directed = FALSE) + A6 <- matrix(1, 10, 10) + diag(A6) <- 0 + A6[6:10, 1:5] <- A6[1:5, 6:10] <- g6[6:10, 1:5] <- 0 + expect_equal(as_unnamed_dense_matrix(g6[]), A6) + + g7 <- make_full_graph(n = 10, directed = TRUE) + A7 <- matrix(1, 10, 10) + diag(A7) <- 0 + g7[6:10, 1:5] <- A7[6:10, 1:5] <- 0 + expect_equal(as_unnamed_dense_matrix(g7[]), A7) + + g8 <- make_full_graph(n = 10, directed = TRUE) + A8 <- matrix(1, 10, 10) + diag(A8) <- 0 + g8[1:5, 6:10] <- A8[1:5, 6:10] <- 0 + expect_equal(as_unnamed_dense_matrix(g8[]), A8) +}) + +test_that("Indexing multi-graphs as adjacency list", { + g <- make_graph(~ A - +B:C, A - +B:C:D, simplify = FALSE) + e <- g[["A", "B", edges = TRUE]] + + expect_equal(ignore_attr = TRUE, sort(e[[1]]), E(g)[1, 3]) +}) + +test_that("Weighted indexing does not remove edges", { + # labeled as bug 1073705 + g <- make_ring(10) + g[1, 2, attr = "weight"] <- 0 + expect_in("weight", edge_attr_names(g)) + expect_equal(E(g)$weight, c(0, rep(NA, 9))) + + el <- as_edgelist(g) + g[from = el[, 1], to = el[, 2], attr = "sim"] <- rep(0:1, length.out = ecount(g)) + expect_in("sim", edge_attr_names(g)) + expect_equal(E(g)$sim, rep(0:1, 5)) + + V(g)$name <- letters[seq_len(vcount(g))] + el <- as_edgelist(g) + g[from = el[, 1], to = el[, 2], attr = "sim"] <- rep(1:0, length.out = ecount(g)) + expect_equal(E(g)$sim, rep(1:0, 5)) +}) + +test_that("indexing a vs twice works", { + edges <- data.frame( + stringsAsFactors = TRUE, + from = c("BOS", "JFK", "DEN", "BOS", "JFK", "DEN"), + to = c("JFK", "DEN", "ABQ", "JFK", "DEN", "ABQ"), + carrier = c("foo", "foo", "foo", "bar", "bar", "bar") + ) + + vertices <- data.frame( + stringsAsFactors = TRUE, + id = c("BOS", "JFK", "DEN", "ABQ"), + state = c("MA", "NY", "CO", "NM") + ) + + g <- graph_from_data_frame(edges, vertices = vertices) + + x <- V(g)[3:4][state == "NM"] + + expect_equal(ignore_attr = TRUE, x, V(g)["ABQ"]) +}) + +test_that("indexing an es twice works", { + edges <- data.frame( + stringsAsFactors = TRUE, + from = c("BOS", "JFK", "DEN", "BOS", "JFK", "DEN"), + to = c("JFK", "DEN", "ABQ", "JFK", "DEN", "ABQ"), + carrier = c("foo", "foo", "foo", "bar", "bar", "bar") + ) + + g <- graph_from_data_frame(edges) + + x <- E(g)["BOS" %->% "JFK"][carrier == "foo"] + + expect_equal(ignore_attr = TRUE, x, E(g)[carrier == "foo" & .from("BOS") & .to("JFK")]) +}) + + +test_that("deprecated indexing functions are indeed deprecated", { + g <- make_ring(10) + + expect_error(V(g)[nei(1)], "was deprecated") + expect_error(V(g)[innei(1)], "was deprecated") + expect_error(V(g)[outnei(1)], "was deprecated") + expect_error(V(g)[inc(1)], "was deprecated") + expect_error(V(g)[adj(1)], "was deprecated") + expect_error(V(g)[from(1)], "was deprecated") + expect_error(V(g)[to(1)], "was deprecated") + + expect_error(E(g)[adj(1)], "was deprecated") + expect_error(E(g)[inc(1)], "was deprecated") + expect_error(E(g)[from(1)], "was deprecated") + expect_error(E(g)[to(1)], "was deprecated") }) diff --git a/tests/testthat/test-indexing2.R b/tests/testthat/test-indexing2.R deleted file mode 100644 index b771fff68ae..00000000000 --- a/tests/testthat/test-indexing2.R +++ /dev/null @@ -1,229 +0,0 @@ -test_that("[ can add and delete edges", { - g <- make_empty_graph(10) - A <- matrix(0, 10, 10) - - A[1, 2] <- g[1, 2] <- TRUE - expect_equal(canonicalize_matrix(g[]), A) - - A[2, 1] <- g[2, 1] <- TRUE - expect_equal(canonicalize_matrix(g[]), A) - - g[2, 1] <- NULL - A[2, 1] <- 0 - expect_equal(canonicalize_matrix(g[]), A) - - A[1, 2] <- g[1, 2] <- FALSE - expect_equal(canonicalize_matrix(g[]), A) - - g <- make_empty_graph(10) - A <- matrix(0, 10, 10) - A[-1, 1] <- g[-1, 1] <- 1 - expect_equal(canonicalize_matrix(g[]), A) -}) - -test_that("[ can set weights and delete weighted edges", { - g <- make_empty_graph(10) - A <- matrix(0, 10, 10) - g <- set_edge_attr(g, "weight", c(), 1) - A[1, 2] <- g[1, 2] <- 1 - expect_equal(canonicalize_matrix(g[]), A) - - A[2, 1] <- g[2, 1] <- 2 - expect_equal(canonicalize_matrix(g[]), A) - - A[1, 2] <- g[1, 2] <- 3 - expect_equal(canonicalize_matrix(g[]), A) - - A[1:2, 2:3] <- g[1:2, 2:3, loops = TRUE] <- -1 - expect_equal(canonicalize_matrix(g[]), A) - - g[1, 2] <- NULL - A[1, 2] <- 0 - expect_equal(canonicalize_matrix(g[]), A) -}) - -test_that("[ can add edges and ste weights via vertex names", { - g <- make_empty_graph(10) - A <- matrix(0, 10, 10) - V(g)$name <- letters[1:vcount(g)] - rownames(A) <- colnames(A) <- letters[1:vcount(g)] - - A["a", "b"] <- g["a", "b"] <- TRUE - A["b", "c"] <- g["b", "c"] <- TRUE - expect_equal(canonicalize_matrix(g[]), canonicalize_matrix(A)) - - A[c("a", "f"), c("f", "a")] <- g[c("a", "f"), c("f", "a"), loops = TRUE] <- TRUE - expect_equal(canonicalize_matrix(g[]), canonicalize_matrix(A)) - - A[A == 1] <- NA - A[c("a", "c", "h"), c("a", "b", "c")] <- - g[c("a", "c", "h"), c("a", "b", "c"), attr = "weight", loops = TRUE] <- 3 - expect_equal(canonicalize_matrix(g[]), canonicalize_matrix(A)) -}) - -test_that("[ and the from-to notation", { - g <- make_empty_graph(10) - A <- matrix(0, 10, 10) - V(g)$name <- letters[1:vcount(g)] - rownames(A) <- colnames(A) <- letters[1:vcount(g)] - - g[from = c("a", "c", "h"), to = c("a", "b", "c")] <- 1 - A["a", "a"] <- A["c", "b"] <- A["h", "c"] <- 1 - expect_equal( - g[from = c("a", "c", "h", "d"), to = c("a", "b", "c", "e")], - c(1, 1, 1, 0) - ) - expect_equal(canonicalize_matrix(g[]), canonicalize_matrix(A)) - - g[from = c("a", "c", "h", "a"), to = c("a", "a", "a", "e"), attr = "weight"] <- 3 - A[A != 0] <- NA - A["a", "a"] <- A["c", "a"] <- A["h", "a"] <- A["a", "e"] <- 3 - expect_equal( - g[from = c("a", "c", "h", "a", "c", "c"), to = c("a", "a", "a", "e", "f", "b")], - c(3, 3, 3, 3, 0, NA) - ) - expect_equal(canonicalize_matrix(g[]), canonicalize_matrix(A)) -}) - -test_that("[ and from-to with multiple values", { - g <- make_empty_graph(10) - A <- matrix(0, 10, 10) - V(g)$name <- letters[1:vcount(g)] - rownames(A) <- colnames(A) <- letters[1:vcount(g)] - - g[from = c("a", "c", "h"), to = c("a", "b", "c")] <- 1 - A["a", "a"] <- A["c", "b"] <- A["h", "c"] <- 1 - g[from = c("a", "c", "h", "a"), to = c("a", "a", "a", "e"), attr = "weight"] <- 5:8 - A[A != 0] <- NA - A["a", "a"] <- 5 - A["c", "a"] <- 6 - A["h", "a"] <- 7 - A["a", "e"] <- 8 - expect_equal( - g[from = c("a", "c", "h", "a", "c", "c"), to = c("a", "a", "a", "e", "f", "b")], - c(5:8, 0, NA) - ) - expect_equal(canonicalize_matrix(g[]), canonicalize_matrix(A)) -}) - -test_that("[ manipulation works as intended for unweighted", { - # see issue https://github.com/igraph/rigraph/issues/1662 - g1 <- make_empty_graph(n = 10, directed = FALSE) - A1 <- matrix(0, 10, 10) - A1[1:5, ] <- A1[, 1:5] <- g1[1:5, ] <- 1 - diag(A1) <- 0 - expect_equal(canonicalize_matrix(g1[]), A1) - - g2 <- make_empty_graph(n = 10, directed = FALSE) - A2 <- matrix(0, 10, 10) - A2[1:5, ] <- A2[, 1:5] <- g2[, 1:5] <- 1 - diag(A2) <- 0 - expect_equal(canonicalize_matrix(g2[]), A2) - - g3 <- make_empty_graph(n = 10, directed = TRUE) - A3 <- matrix(0, 10, 10) - A3[1:5, ] <- g3[1:5, ] <- 1 - diag(A3) <- 0 - expect_equal(canonicalize_matrix(g3[]), A3) - - g4 <- make_empty_graph(n = 10, directed = TRUE) - A4 <- matrix(0, 10, 10) - A4[, 1:5] <- g4[, 1:5] <- 1 - diag(A4) <- 0 - expect_equal(canonicalize_matrix(g4[]), A4) - - g5 <- make_empty_graph(n = 10, directed = TRUE) - A5 <- matrix(0, 10, 10) - g5[1, 2] <- g5[2, 1] <- A5[1, 2] <- A5[2, 1] <- 1 - expect_equal(canonicalize_matrix(g5[]), A5) - - g6 <- make_empty_graph(n = 10, directed = FALSE) - A6 <- matrix(0, 10, 10) - A6[6:10, 1:5] <- A6[1:5, 6:10] <- g6[6:10, 1:5] <- 1 - expect_equal(canonicalize_matrix(g6[]), A6) - - g7 <- make_empty_graph(n = 10, directed = TRUE) - A7 <- matrix(0, 10, 10) - g7[6:10, 1:5] <- A7[6:10, 1:5] <- 1 - diag(A7) <- 0 - expect_equal(canonicalize_matrix(g7[]), A7) - - g8 <- make_empty_graph(n = 10, directed = TRUE) - A8 <- matrix(0, 10, 10) - g8[1:5, 6:10] <- A8[1:5, 6:10] <- 1 - diag(A8) <- 0 - expect_equal(canonicalize_matrix(g8[]), A8) -}) - -test_that("[ manipulation works as intended for weighted", { - # see issue https://github.com/igraph/rigraph/issues/1662 - - g1 <- make_empty_graph(n = 10, directed = FALSE) - A1 <- matrix(0, 10, 10) - A1[1:5, 1:5] <- g1[1:5, 1:5, attr = "weight"] <- 2 - diag(A1) <- 0 - expect_equal(canonicalize_matrix(g1[]), A1) - - g2 <- make_empty_graph(n = 10, directed = FALSE) - E(g2)$weight <- 1 - A2 <- matrix(0, 10, 10) - A2[1:3, 1:3] <- g2[1:3, 1:3] <- -2 - diag(A2) <- 0 - expect_equal(canonicalize_matrix(g2[]), A2) -}) - -test_that("[ manipulation handles errors properly", { - g1 <- make_empty_graph(n = 10, directed = FALSE) - expect_error(g1[1:5, ] <- 2) -}) - -test_that("[ deletion works as intended", { - # see issue https://github.com/igraph/rigraph/issues/1662 - g1 <- make_full_graph(n = 10, directed = FALSE) - A1 <- matrix(1, 10, 10) - diag(A1) <- 0 - A1[1:5, ] <- A1[, 1:5] <- g1[1:5, ] <- 0 - expect_equal(canonicalize_matrix(g1[]), A1) - - g2 <- make_full_graph(n = 10, directed = FALSE) - A2 <- matrix(1, 10, 10) - diag(A2) <- 0 - A2[1:5, ] <- A2[, 1:5] <- g2[, 1:5] <- 0 - expect_equal(canonicalize_matrix(g2[]), A2) - - g3 <- make_full_graph(n = 10, directed = TRUE) - A3 <- matrix(1, 10, 10) - diag(A3) <- 0 - A3[1:5, ] <- g3[1:5, ] <- 0 - expect_equal(canonicalize_matrix(g3[]), A3) - - g4 <- make_full_graph(n = 10, directed = TRUE) - A4 <- matrix(1, 10, 10) - diag(A4) <- 0 - A4[, 1:5] <- g4[, 1:5] <- 0 - expect_equal(canonicalize_matrix(g4[]), A4) - - g5 <- make_full_graph(n = 10, directed = TRUE) - A5 <- matrix(1, 10, 10) - diag(A5) <- 0 - g5[1, 2] <- g5[2, 1] <- A5[1, 2] <- A5[2, 1] <- 0 - expect_equal(canonicalize_matrix(g5[]), A5) - - g6 <- make_full_graph(n = 10, directed = FALSE) - A6 <- matrix(1, 10, 10) - diag(A6) <- 0 - A6[6:10, 1:5] <- A6[1:5, 6:10] <- g6[6:10, 1:5] <- 0 - expect_equal(canonicalize_matrix(g6[]), A6) - - g7 <- make_full_graph(n = 10, directed = TRUE) - A7 <- matrix(1, 10, 10) - diag(A7) <- 0 - g7[6:10, 1:5] <- A7[6:10, 1:5] <- 0 - expect_equal(canonicalize_matrix(g7[]), A7) - - g8 <- make_full_graph(n = 10, directed = TRUE) - A8 <- matrix(1, 10, 10) - diag(A8) <- 0 - g8[1:5, 6:10] <- A8[1:5, 6:10] <- 0 - expect_equal(canonicalize_matrix(g8[]), A8) -}) diff --git a/tests/testthat/test-indexing3.R b/tests/testthat/test-indexing3.R deleted file mode 100644 index a5a0aaeb88c..00000000000 --- a/tests/testthat/test-indexing3.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("Indexing multi-graphs as adjacency list", { - g <- make_graph(~ A -+ B:C, A -+ B:C:D, simplify = FALSE) - e <- g[["A", "B", edges = TRUE]] - - expect_equal(ignore_attr = TRUE, sort(e[[1]]), E(g)[1, 3]) -})