@@ -394,135 +394,6 @@ set_chain <- function(graph, chains) {
394394 return (graph )
395395}
396396
397- # Description:
398- # find components, cliques, subgraphs in a community
399- decode_communities <- function (community_id ,
400- graph ,
401- edge_filter ,
402- node_filter ) {
403-
404- if (missing(edge_filter )) {
405- edge_filter <- NULL
406- }
407- if (missing(node_filter )) {
408- node_filter <- NULL
409- }
410-
411- apply_op <- function (vec , op , val ) {
412- ops <- list (" ==" = `==` ,
413- " !=" = `!=` ,
414- " <" = `<` ,
415- " >" = `>` ,
416- " <=" = `<=` ,
417- " >=" = `>=` )
418-
419- if (! op %in% names(ops )) {
420- stop(" Invalid operator, choose: '==', '!=', '<', '>', '<=', '>='" )
421- }
422-
423- return (ops [[op ]](vec , val ))
424- }
425-
426- if (any(vertex_attr_names(graph )== " community" )== FALSE ) {
427- stop(" no community ID as node attribute" )
428- } else {
429- graph <- subgraph(graph = graph ,
430- vids = V(graph )$ community == community_id )
431- }
432- if (length(graph )== 1 | length(E(graph )) == 0 ) {
433- warning(" community has only one vertex" )
434- }
435-
436- # consider edges
437- if (is.null(edge_filter )== FALSE ) {
438- # this is where the edge filter results will be kept
439- etm <- matrix (data = 0 ,
440- nrow = nrow(edge_filter ),
441- ncol = length(E(graph )))
442- for (i in seq_len(nrow(edge_filter ))) {
443- a_name <- edge_filter $ name [i ]
444- a_value <- edge_filter $ value [i ]
445- a_operation <- edge_filter $ operation [i ]
446-
447- j <- which(edge_attr_names(graph ) == a_name )
448- if (length(j ) != 0 ) {
449- v <- edge_attr(graph = graph , name = a_name )
450- etm [i ,] <- apply_op(vec = v , val = a_value , op = a_operation )
451- }
452- }
453- etm <- apply(X = etm , MARGIN = 2 , FUN = prod )
454- i <- which(etm == FALSE )
455- if (length(i ) != 0 ) {
456- graph <- delete_edges(graph = graph , edges = i )
457- }
458- }
459-
460- # now partition based on node-attributes
461- if (is.null(node_filter )== FALSE ) {
462- vs <- as_data_frame(x = graph , what = " vertices" )
463- V(graph )$ key <- apply(X = vs [, node_filter $ name , drop = FALSE ],
464- MARGIN = 1 , FUN = paste , collapse = ' |' )
465-
466- sgs <- lapply(
467- X = unique(V(graph )$ key ), g = graph ,
468- FUN = function (x , g ) {
469- # get a subgraph with shared node attributes
470- sg <- subgraph(graph = g , vids = which(V(g )$ key == x ))
471-
472- # find connected components
473- V(sg )$ components <- components(graph = sg )$ membership
474- V(sg )$ component_id <- paste0(V(sg )$ key , ' |' ,
475- V(sg )$ components )
476- return (disjoint_union(lapply(
477- X = unique(V(sg )$ component_id ), g = sg ,
478- FUN = function (x , g ) {
479- vids <- which(V(g )$ component_id == x )
480- return (subgraph(graph = g , vids = vids ))
481- })))
482- })
483- sgs <- disjoint_union(sgs )
484- V(sgs )$ component_id <- as.numeric(as.factor(V(sgs )$ component_id ))
485-
486- sgs_stat <- get_component_stats(x = sgs )
487-
488- vs <- as_data_frame(x = sgs , what = " vertices" )
489-
490- return (list (community_graph = sgs ,
491- component_stats = sgs_stat ,
492- node_summary = vs ))
493- }
494- else {
495- sgs <- lapply(
496- X = unique(V(graph )$ community ), g = graph ,
497- FUN = function (x , g ) {
498- # get a subgraph with shared node attributes
499- sg <- subgraph(graph = g , vids = which(V(g )$ community == x ))
500-
501- # find connected components
502- V(sg )$ components <- components(graph = sg )$ membership
503- V(sg )$ component_id <- paste0(V(sg )$ community , ' |' ,
504- V(sg )$ components )
505- return (disjoint_union(lapply(
506- X = unique(V(sg )$ component_id ), g = sg ,
507- FUN = function (x , g ) {
508- vids <- which(V(g )$ component_id == x )
509- return (subgraph(graph = g , vids = vids ))
510- })))
511- })
512- sgs <- disjoint_union(sgs )
513- V(sgs )$ component_id <- as.numeric(as.factor(V(sgs )$ component_id ))
514-
515- sgs_stat <- get_component_stats(x = sgs )
516-
517- vs <- as_data_frame(x = sgs , what = " vertices" )
518-
519- return (list (community_graph = sgs ,
520- component_stats = sgs_stat ,
521- node_summary = vs ))
522- }
523- }
524-
525-
526397get_component_stats <- function (x ) {
527398
528399 # what is he
@@ -574,4 +445,3 @@ get_component_stats <- function(x) {
574445
575446 return (rbind(stats_components , stats_singletons ))
576447}
577-
0 commit comments