nwtools¶
consensus¶
This module provides different functionalities related to the consensus of multiple runs of community detection. The algorithms for community detection are provided by the leidenalg package.
-
nwtools.consensus.consensus_partition(g, initial_partition=None, partition_type=<class 'leidenalg.VertexPartition.ModularityVertexPartition'>, weights=None, nr_partitions=100, threshold=0, max_nr_iterations=5, singleton_clusters=False, verbose=False)¶ Partitions grap based on consensus clustering. Reference: A. Lancichinetti, S. Fortunato, Consensus clustering in complex networks, Scientific Reports 2(1), 336 (2012). DOI 10.1038/srep00336
Parameters: - g (igraph.Graph) – graph
- initial_partition – precalculated list of partitions (optional)
- partition_type (leidenalg.VertexPartition) – subtype of community detection algorithm
- weights (str) – weight attribute in graph
- nr_partitions (int) – number of partitions
- threshold (float) – threshold for consensus clustering algorithm
- max_nr_iterations (int) – maximum number of iterations in consensus clustering
- singleton_clusters (bool) – Whether to allow clusters of only one node
- verbose (bool) – If true, print details on progress
Returns: consensus matrix of first iteration, consensus clustering membership of nodes
Return type: (np.array, list[int])
-
nwtools.consensus.get_consensus_matrix(partitions, nr_nodes)¶ Get the consensus matrix of a list of partitions.
Parameters: - partitions – iterable of igraph.clustering.VertexClustering
- nr_nodes – number of nodes in the graph
Returns: consensus matrix of shape (nr_nodes, nr_nodes)
Return type: numpy.array
-
nwtools.consensus.get_edge_consistency(graph, consensus_matrix)¶ Defines consensus and consistency scores for edges in the graph
Parameters: - graph (igraph.Graph) – igraph Graph
- consensus_matrix (np.array) – the consensus matrix
Returns: the input graph, with edge attributes ‘consensus’ and ‘consistency’
Return type: igraph.Graph
-
nwtools.consensus.get_initial_partitions(g, partition_type=<class 'leidenalg.VertexPartition.ModularityVertexPartition'>, weights=None, nr_partitions=100)¶ Find initial set of partitions using a community detection method
Parameters: - g (igraph.Graph) – graph
- partition_type – subtype of leidenalg.VertexPartition, implementing a community detection algorithm
- weights – weight attribute in graph
- nr_partitions – number of partitions
Returns: list of partitions
-
nwtools.consensus.get_nmi_matrix(memberships, average_method='geometric')¶ Calculate full matrix of NMI scores of all combinations of partitions
Parameters: - memberships (list[np.array]) – list of numpy arrays with node memberships
- average_method (str) – see sklearn.metrics.cluster.normalized_mutual_info_score
Returns: matrix of shape (nr_partitions, nr_partitions) with NMI scores
Return type: numpy.array
-
nwtools.consensus.get_nmi_scores(consensus_membership, all_memberships, average_method='geometric')¶ Calculate the NMI score between a consensus partitioning and a list of partitionings, and between all individual partitions of the list.
Parameters: - consensus_membership (igraph.clustering.VertexClustering) – consensus clustering
- all_memberships – iterable of igraph.clustering.VertexClustering
- average_method (str) – see sklearn.metrics.cluster.normalized_mutual_info_score
Returns: (nmi_consensus, nmi_all) - NMI scores
Return type: (list, np.array)
-
nwtools.consensus.get_unique_partitions(partitions, average_method='geometric')¶ Find identical partitions in a list of partitionings.
Parameters: - partitions – iterable of igraph.clustering.VertexClustering
- average_method (str) – see sklearn.metrics.cluster.normalized_mutual_info_score
Returns: (nmi_all, unique_partitions, unique_part_counts)
Return type: (np.array, list[igraph.clustering.VertexClustering], list[int])
-
nwtools.consensus.modularity_contribution(graph, membership, weight=None)¶ Calculates the gain, penalty and contribution of individual nodes on the modularity score, with respect to a clustering Based on: [1] R. de Santiago and L. C. Lamb, “On the role of degree influence in suboptimal modularity maximization,” in 2016 IEEE Congress on Evolutionary Computation (CEC), Vancouver, BC, Canada, 2016, pp. 4618–4625.
Parameters: - graph (igraph.Graph) – igraph Graph
- membership (list) – list of cluster membership
Returns: (gain, penalty, contribution)
Return type: (np.arrary, np.array, np.array)
communities¶
-
nwtools.communities.citation_distance_matrix(graph)¶ Parameters: graph – networkx graph Returns: distance matrix, node labels
-
nwtools.communities.compare_communities(partition1, partition2, graph)¶ Calculates different similarity measures between two partitions of a graph :param dict partition1: first partition :param dict partition2: second partition :param networkx.Graph graph: networkx graph
Returns: dict with similarity measures Return type: dict
-
nwtools.communities.partition_statistics(partition, graph, weight=None)¶ Calculates statistics for each partition of a network. Treats the network as undirected.
Parameters: - partition (dict) – assignment into communities: {node: community}
- graph (networkx.Graph) – networkx graph
- weight (str) – optional weight attribute
Returns: statistics
Return type: dict(int, dict(str, list))