Skip to content

Commit

Permalink
test igraph and legacy reticulate methods #1
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKellyGenetics committed Nov 5, 2020
1 parent 6f5af74 commit 1c40041
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/leiden.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -298,14 +300,16 @@ 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,
initial_membership = initial_membership,
n_iterations = n_iterations,
vertex_weights = NULL
))
partition <- as.numeric(partition)
} else {
#call python reticulate implementation

Expand Down
125 changes: 125 additions & 0 deletions tests/testthat/test_igraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})



0 comments on commit 1c40041

Please sign in to comment.