Skip to content

Commit

Permalink
add support for bipartite graphs #5 (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKellyGenetics committed May 26, 2020
1 parent e61e458 commit 124c766
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 80 deletions.
13 changes: 12 additions & 1 deletion R/find_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ find_partition <- function(snn_graph, partition_type = c(
'CPMVertexPartition',
'MutableVertexPartition',
'SignificanceVertexPartition',
'SurpriseVertexPartition'
'SurpriseVertexPartition',
'CPMVertexPartition.Bipartite'
),
initial_membership = NULL,
weights = NULL,
Expand Down Expand Up @@ -67,6 +68,16 @@ n_iterations = 2L
initial_membership = initial_membership, weights = weights,
seed = seed, n_iterations = n_iterations, node_sizes = node_sizes
),
'CPMVertexPartition.Bipartite' =
optimiser <- leidenalg$Optimiser()
bipartite_layers <- leidenalg$CPMVertexPartition$Bipartite(snn_graph,
resolution_parameter_01 = 0.1,
resolution_parameter_0 = 0,
resolution_parameter_1 = 0,
degree_as_node_size = FALSE,
types = "type")
optimiser$optimise_partition_multiplex(r_to_py(bipartite_partition), layer_weights = r_to_py(c(1L, -1L, -1L)))
,
stop("please specify a partition type as a string out of those documented")
)
partition <- part$membership+1
Expand Down
93 changes: 14 additions & 79 deletions R/leiden.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ leiden <- function(object,
'CPMVertexPartition',
'MutableVertexPartition',
'SignificanceVertexPartition',
'SurpriseVertexPartition'
'SurpriseVertexPartition',
'CPMVertexPartition.Bipartite'
),
initial_membership = NULL,
weights = NULL,
Expand All @@ -102,7 +103,8 @@ leiden.matrix <- function(object,
'CPMVertexPartition',
'MutableVertexPartition',
'SignificanceVertexPartition',
'SurpriseVertexPartition'
'SurpriseVertexPartition',
'CPMVertexPartition.Bipartite'
),
initial_membership = NULL,
weights = NULL,
Expand Down Expand Up @@ -178,7 +180,8 @@ leiden.Matrix <- function(object,
'CPMVertexPartition',
'MutableVertexPartition',
'SignificanceVertexPartition',
'SurpriseVertexPartition'
'SurpriseVertexPartition',
'CPMVertexPartition.Bipartite'
),
initial_membership = NULL,
weights = NULL,
Expand Down Expand Up @@ -213,7 +216,7 @@ leiden.Matrix <- function(object,
##' @export
leiden.default <- leiden.matrix

##' @importFrom igraph V as_edgelist is.weighted is.named edge.attributes as_adjacency_matrix laplacian_matrix
##' @importFrom igraph V as_edgelist is.weighted is.named edge.attributes as_adjacency_matrix laplacian_matrix get.vertex.attribute is.bipartite
##' @export
leiden.igraph <- function(object,
partition_type = c(
Expand All @@ -223,7 +226,8 @@ leiden.igraph <- function(object,
'CPMVertexPartition',
'MutableVertexPartition',
'SignificanceVertexPartition',
'SurpriseVertexPartition'
'SurpriseVertexPartition',
'CPMVertexPartition.Bipartite'
),
initial_membership = NULL,
weights = NULL,
Expand Down Expand Up @@ -267,6 +271,11 @@ leiden.igraph <- function(object,
snn_graph$es$set_attribute_values('weight', weights)
}

if(!is.null(get.vertex.attribute(object, "type")) || is.bipartite(object)){
type <- as.integer(V(object)$type)
snn_graph$vs$set_attribute_values('type', type)
}

# from here is the same as method for matrix
# would be better to refactor to call from matrix methof

Expand Down Expand Up @@ -322,77 +331,3 @@ ig <- NULL
}
}
}

find_partition <- function(snn_graph, partition_type = c(
'RBConfigurationVertexPartition',
'ModularityVertexPartition',
'RBERVertexPartition',
'CPMVertexPartition',
'MutableVertexPartition',
'SignificanceVertexPartition',
'SurpriseVertexPartition'
),
initial_membership = NULL,
weights = NULL,
node_sizes = NULL,
resolution_parameter = 1,
seed = NULL,
n_iterations = 2L
) {
partition_type <- match.arg(partition_type)
if(!is.null(seed)) seed <- as.integer(seed)
if (is.integer(n_iterations)) n_iterations <- as.integer(n_iterations)
part <- switch(
EXPR = partition_type,
'RBConfigurationVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$RBConfigurationVertexPartition,
initial_membership = initial_membership, weights = weights,
seed = seed,
n_iterations = n_iterations,
resolution_parameter = resolution_parameter
),
'ModularityVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$ModularityVertexPartition,
initial_membership = initial_membership, weights = weights,
seed = seed, n_iterations = n_iterations
),
'RBERVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$RBERVertexPartition,
initial_membership = initial_membership, weights = weights,
seed = seed, n_iterations = n_iterations, node_sizes = node_sizes,
resolution_parameter = resolution_parameter
),
'CPMVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$CPMVertexPartition,
initial_membership = initial_membership, weights = weights,
seed = seed, n_iterations = n_iterations, node_sizes = node_sizes,
resolution_parameter = resolution_parameter
),
'MutableVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$MutableVertexPartition,
initial_membership = initial_membership,
seed = seed, n_iterations = n_iterations
),
'SignificanceVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$SignificanceVertexPartition,
initial_membership = initial_membership,
seed = seed, n_iterations = n_iterations, node_sizes = node_sizes,
resolution_parameter = resolution_parameter
),
'SurpriseVertexPartition' = leidenalg$find_partition(
snn_graph,
leidenalg$SurpriseVertexPartition,
initial_membership = initial_membership, weights = weights,
seed = seed, n_iterations = n_iterations, node_sizes = node_sizes
),
stop("please specify a partition type as a string out of those documented")
)
partition <- part$membership+1
partition
}

0 comments on commit 124c766

Please sign in to comment.