Community Algorithms

Community-detection helpers and wrappers.

asunder.base.algorithms.community._import_sknetwork()

Internal helper for importing sknetwork modules.

Returns:

Clustering modules and helper functions from sknetwork.

Return type:

tuple of modules and methods

asunder.base.algorithms.community._import_igraph()

Internal helper for importing igraph.

Returns:

igraph module

Return type:

module

asunder.base.algorithms.community._import_leidenalg()

Internal helper for importing leidenalg.

Returns:

leidenalg module

Return type:

module

asunder.base.algorithms.community.labels_to_probabilities(A, labels, p=1)

Convert hard labels into row-normalized membership probabilities.

Parameters:
  • A (ndarray of float, shape (N, N)) – Graph adjacency/weight matrix.

  • labels (ndarray of int, shape (N,)) – [Predicted] community labels for each node in a given graph.

  • p (int) – Order of the norm.

Returns:

Normalized matrix with K community assignment confidence scores for each node.

Return type:

ndarray of float, shape (N, K)

asunder.base.algorithms.community.probability_to_integer_labels(probabilities, method='threshold', threshold=0.8, verbose=False)

Heuristic map from probability / soft memberships values to integer labels using a configurable rule. It is used to sunder a core-like community from every other node based on the observation that such core-like nodes have low membership scores across every community, given their central role. These nodes are core-like and not exactly core nodes because they do not exhibit the typical dense connection one expects from core nodes. They, in fact, are not adjacent to one another.

Parameters:
  • probabilities (ndarray of float, shape (N,K) or (N,)) – 2D probabilities reflect the confidence that each node n belongs to community k. 1D probabilities reflect the confidence that a node is in one of two groups, typically a core and a periphery group.

  • method (str) – One of “threshold,” “gaussian_mixture,” and “DBSCAN.” Determines whether clustering algorithms are required to process probabilities or if thresholding is sufficient.

  • threshold (float) – Value (between 0 and 1) below which a node is assumed to be in the core-like group.

  • verbose (bool) – Controls the verbosity of the output. Default is False.

Returns:

Integer community assignment of the nodes reflecting a bipartition.

Return type:

ndarray of int, shape (N,)

asunder.base.algorithms.community.best_girvan_newman_partition(G, max_levels=10)

Search Girvan-Newman levels and return the best modularity partition.

Parameters:
  • G (nx.Graph) – Input NetworkX graph.

  • max_levels (int) – Maximum number of levels to check in the Girvan-Newman search process.

Returns:

  • best_communities (tuple[list[int or str]] or None) – Iterable with communities reflecting the best modularity found during the search process.

  • best_mod (float) – Best modularity obtained during the search process.

asunder.base.algorithms.community.run_modularity(modified_A, algo='louvain', package='networkx', seed=42, resolution=1, verbose=False, refine=False, refine_params=None)

Run modularity-style community detection and return (partition, score).

Parameters:
  • modified_A (ndarray of float, shape (N, N)) – Augmented adjacency / weight matrix reflecting the original adjacency / weight matrix with dual-modified weights. Negative weights are not allowed. The original adjacency / weight matrix can also be parsed.

  • algo (str) – Algorithm to be used for modularity based community detection.

  • package (str) – Python package to be used for modularity based community detection.

  • seed (int | None) – Random seed value.

  • resolution (int or float) – Resolution parameter (gamma) used in modularity based methods.

  • verbose (bool) – Controls the verbosity of the output. Default is False.

  • refine (bool) – Enables/disables refinement procedure.

  • refine_params (dict[str, Any]) – Refinement parameters, typically including the refinement function and any associated arguments.

Returns:

  • zii (ndarray of int, shape (N, N)) – 2D graph partititon.

  • metric (float) – Modularity score of zii computed using the provided adjacency / weight matrix.

asunder.base.algorithms.community.run_lpa(modified_A, refine=True)

Run label propagation clustering and return (partition, modularity).

Parameters:
  • modified_A (ndarray of float, shape (N, N)) – Augmented adjacency / weight matrix reflecting the original adjacency / weight matrix with dual-modified weights. Negative weights are not allowed. The original adjacency / weight matrix can also be parsed.

  • refine (bool) – Enables/disables refinement procedure.

Returns:

  • zii (ndarray of int, shape (N, N)) – 2D graph partititon.

  • float – Modularity score of zii computed using the provided adjacency / weight matrix.

asunder.base.algorithms.community.run_igraph_spinglass(modified_A)

Run igraph spinglass community detection and return a partition matrix.

Parameters:

modified_A (ndarray of float, shape (N, N)) – Augmented adjacency / weight matrix reflecting the original adjacency / weight matrix with dual-modified weights. Negative weights are not allowed. The original adjacency / weight matrix can also be parsed.

Returns:

2D graph partititon.

Return type:

ndarray of int, shape (N, N)

asunder.base.algorithms.community.run_igraph(modified_A, algo='infomap', resolution=1)

Run selected igraph community algorithm and return (partition, score).

Parameters:
  • modified_A (ndarray of float, shape (N, N)) – Augmented adjacency / weight matrix reflecting the original adjacency / weight matrix with dual-modified weights. Negative weights are not allowed. The original adjacency / weight matrix can also be parsed.

  • algo (str) – Algorithm to be used for modularity based community detection.

  • resolution (int or float) – Resolution parameter (gamma) used in computing the modularity metric.

Returns:

  • zii (ndarray of int, shape (N, N)) – 2D graph partititon.

  • metric (float) – Modularity score of zii computed using the provided adjacency / weight matrix.

asunder.base.algorithms.community.run_signed_louvain(modified_A, seed=42)

Run signed Louvain on positive/negative layers and return (partition, score).

Parameters:
  • modified_A (ndarray of float, shape (N, N)) – Augmented adjacency / weight matrix reflecting the original adjacency / weight matrix with dual-modified weights. Negative weights are not allowed. The original adjacency / weight matrix can also be parsed.

  • seed (int or None) – Random seed value

Returns:

  • zii (ndarray of int, shape (N, N)) – 2D graph partititon.

  • float – Modularity score of zii computed using the provided adjacency / weight matrix.