From 1c400415d431e0790f5ef1d83e0c6423cb9655ea Mon Sep 17 00:00:00 2001 From: TomKellyGenetics Date: Thu, 5 Nov 2020 11:59:57 +0900 Subject: [PATCH] test igraph and legacy reticulate methods #1 --- R/leiden.R | 8 ++- tests/testthat/test_igraph.R | 125 +++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/R/leiden.R b/R/leiden.R index be1b6ad..96e0dc3 100644 --- a/R/leiden.R +++ b/R/leiden.R @@ -286,7 +286,9 @@ leiden.igraph <- function(object, object <- as.undirected(object, mode = "each") } } - call_igraph <- !is_directed(object) && !is_bipartite(object) && legacy == FALSE && partition_type == "CPMVertexPartition" || partition_type == "ModularityVertexPartition" + call_igraph <- !is_directed(object) && !is_bipartite(object) && legacy == FALSE && (partition_type == "CPMVertexPartition" || partition_type == "ModularityVertexPartition") + + #print(call_igraph) if(call_igraph == TRUE){ #call igraph implementation @@ -298,7 +300,8 @@ leiden.igraph <- function(object, } #compute partitions with igraph in C - partition <- membership(cluster_leiden(graph, + if(!is.null(seed)) set.seed(seed) + partition <- membership(cluster_leiden(graph = object, objective_function = objective_function, weights = weights, resolution_parameter = resolution_parameter, @@ -306,6 +309,7 @@ leiden.igraph <- function(object, n_iterations = n_iterations, vertex_weights = NULL )) + partition <- as.numeric(partition) } else { #call python reticulate implementation diff --git a/tests/testthat/test_igraph.R b/tests/testthat/test_igraph.R index 2a20b89..4f01073 100644 --- a/tests/testthat/test_igraph.R +++ b/tests/testthat/test_igraph.R @@ -67,3 +67,128 @@ test_that("run with named adjacency matrix", { expect_length(partition, 100) }) +mat1 <- matrix(round(runif(10000, 0, 1)), 100, 100) +mat2 <- matrix(round(rbinom(10000, 1, 0.1)), 100, 100) +adj_mat <- rbind(cbind(mat1, mat2), cbind(mat2, mat1)) + +snn_graph <- graph_from_adjacency_matrix(adj_mat) +snn_graph <- as.undirected(snn_graph, mode = "each") + +test_that("run with reticulate in legacy mode with 2 clusters", { + skip_if_no_python() + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = TRUE, + resolution_parameter = 0.95) + expect_length(partition, 200) +}) + +test_that("run with igraph modewith 2 clusters", { + skip_if_no_python() + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = FALSE, + resolution_parameter = 0.95) + expect_length(partition, 200) +}) + +test_that("run consistent results bewteen igraph and reticulate legacy mode with 2 clusters", { + skip_if_no_python() + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = TRUE, + resolution_parameter = 0.95) + expect_length(partition, 200) + set.seed(42) + partition2 <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = FALSE, + resolution_parameter = 0.95) + expect_true(all(table(partition, partition2) %in% c(0, 100))) + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "CPMVertexPartition", + seed = 42L, + legacy = TRUE, + resolution_parameter = 0.5) + expect_length(partition, 200) + set.seed(42) + partition2 <- leiden(snn_graph, + partition_type = "CPMVertexPartition", + seed = 42L, + legacy = FALSE, + resolution_parameter = 0.5) + expect_true(all(table(partition, partition2) %in% c(0, 100))) +}) + +mat1 <- matrix(round(runif(10000, 0, 1)), 100, 100) +mat2 <- matrix(round(rbinom(10000, 1, 0.1)), 100, 100) +mat3 <- matrix(round(rbinom(10000, 1, 0.01)), 100, 100) +adj_mat <- rbind(cbind(mat1, mat2, mat3), cbind(mat2, mat1, mat2), cbind(mat3, mat2, mat1)) + +snn_graph <- graph_from_adjacency_matrix(adj_mat) +snn_graph <- as.undirected(snn_graph, mode = "each") + +test_that("run with reticulate in legacy mode with 3 clusters", { + skip_if_no_python() + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = TRUE, + resolution_parameter = 0.95) + expect_length(partition, 300) +}) + +test_that("run with igraph mode with 3 clusters", { + skip_if_no_python() + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = FALSE, + resolution_parameter = 0.95) + expect_length(partition, 300) +}) + +test_that("run consistent results bewteen igraph and reticulate legacy mode with 3 clusters", { + skip_if_no_python() + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = TRUE, + resolution_parameter = 0.95) + expect_length(partition, 300) + set.seed(42) + partition2 <- leiden(snn_graph, + partition_type = "ModularityVertexPartition", + seed = 42L, + legacy = FALSE, + resolution_parameter = 0.95) + expect_true(all(table(partition, partition2) %in% c(0, 100))) + set.seed(42) + partition <- leiden(snn_graph, + partition_type = "CPMVertexPartition", + seed = 42L, + legacy = TRUE, + resolution_parameter = 0.5) + expect_length(partition, 300) + set.seed(42) + partition2 <- leiden(snn_graph, + partition_type = "CPMVertexPartition", + seed = 42L, + legacy = FALSE, + resolution_parameter = 0.5) + expect_true(all(table(partition, partition2) %in% c(0, 100))) +}) + + +