topologic.partition package¶
-
topologic.partition.
induce_graph_by_communities
(graph: networkx.classes.graph.Graph, communities: Dict[Any, int], weight_attribute: str = 'weight') → networkx.classes.graph.Graph[source]¶ Creates a community graph with nodes from the communities dictionary and using the edges of the original graph to form edges between communities.
Weights are aggregated; you may need to normalize the resulting graph after calling this function.
Note: logs a warning if the size of the community dictionary is less than the size of the provided graph’s vertexset.
- Parameters
graph (networkx.Graph) – The original graph that contains the edges that will be used to formulate a new induced community graph
communities (dict[Any, int]) – The communities dictionary provides a mapping of original vertex ID to new community ID.
weight_attribute (str) – The weight attribute on the original graph’s edges to use when aggregating the weights of the induced community graph. Default is weight.
- Returns
The induced community graph.
- Return type
- Raises
ValueError – If the graph is None
ValueError – If the communities dictionary is None
-
topologic.partition.
louvain
(graph: networkx.classes.graph.Graph, partition: Optional[Dict[Any, int]] = None, weight_attribute: str = 'weight', resolution: float = 1.0, random_state: Any = None) → Dict[Any, int][source]¶ Compute the partition of the graph nodes which maximises the modularity (or try..) using the Louvain heuristics
This is the partition of highest modularity, i.e. the highest partition of the dendrogram generated by the Louvain algorithm.
This louvain function is a limited wrapper to the community.best_partition function in the python-louvain library written by Thomas Aynaud.
- Parameters
graph (networkx.Graph) – the networkx graph which is decomposed
partition (Optional[Dict[Any, int]]) – the algorithm will start using this partition of the nodes. It’s a dictionary where keys are their nodes and values the communities
weight_attribute (str) – the key in graph to use as weight. Default to ‘weight’
resolution (float) – Will change the size of the communities, default to 1. represents the time described in “Laplacian Dynamics and Multiscale Modular Structure in Networks”, R. Lambiotte, J.-C. Delvenne, M. Barahona
random_state (Any) – If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
- Returns
The partition, with communities numbered from 0 to number of communities
- Return type
Dict[Any, int]
- Raises
NetworkXError - If the graph is not Eulerian.
References 1. Blondel, V.D. et al. Fast unfolding of communities in large networks. J. Stat. Mech 10008, 1-12(2008).
-
topologic.partition.
modularity
(graph: networkx.classes.graph.Graph, partitions: Dict[Any, int], weight_attribute: str = 'weight', resolution: float = 1.0) → float[source]¶ Given an undirected graph and a dictionary of vertices to community ids, calculate the modularity.
See also: https://en.wikipedia.org/wiki/Modularity_(networks)
- Parameters
graph (nx.Graph) – An undirected graph
int] partitions (Dict[Any,) – A dictionary representing a community partitioning scheme with the keys being the vertex and the value being a community id. Within topologic, these community ids are required to be ints.
weight_attribute (str) – The edge data attribute on the graph that contains a float weight for the edge.
resolution (float) – The resolution to use when calculating the modularity.
- Returns
The modularity quality score for the given network and community partition schema.
- Raises
TypeError – If the graph is not a networkx Graph
ValueError – If the graph is unweighted
ValueError – If the graph is directed
-
topologic.partition.
modularity_components
(graph: networkx.classes.graph.Graph, partitions: Dict[Any, int], weight_attribute: str = 'weight', resolution: float = 1.0) → Dict[int, float][source]¶ Given an undirected, weighted graph and a community partition dictionary, calculates a modularity quantum for each community ID. The sum of these quanta is the modularity of the graph and partitions provided.
- Parameters
graph (nx.Graph) – An undirected graph
int] partitions (Dict[Any,) – A dictionary representing a community partitioning scheme with the keys being the vertex and the value being a community id. Within topologic, these community ids are required to be ints.
weight_attribute (str) – The edge data attribute on the graph that contains a float weight for the edge.
resolution (float) – The resolution to use when calculating the modularity.
- Returns
A dictionary of the community id to the modularity component of that community
- Return type
- Raises
TypeError – If the graph is not a networkx Graph
ValueError – If the graph is unweighted
ValueError – If the graph is directed
-
topologic.partition.
q_score
(partitioned_graph: topologic.partitioned_graph.PartitionedGraph, weight_column: str = 'weight') → float[source]¶ Deprecated: See modularity() for replacement.
Given a topologic PartitionedGraph, return the q score - or modularity of a graph.
See also: https://en.wikipedia.org/wiki/Modularity_(networks)
- Parameters
partitioned_graph (Optional[topologic.PartitionedGraph]) – Partitioned graph contains a dictionary of all the communities in a graph, optimized for best modularity. This partition structure is used when computing final q_score / modularity of graph.
weight_column (str) – weight column to use in computing modularity.
- Raises
UnweightedGraphError – if graph does not contain weight_column in edge attributes
KeyError – If the partition is not a partition of all graph nodes. This should not occur if PartitionedGraph is recently created and no changes occurred to the underlying networkx.Graph object.
ValueError – If the graph has no links.
TypeError – If partitioned_graph is not of type topologic.PartitionedGraph
- Returns
q_score, or modularity, of this graph using the provided partitioning scheme.
- Return type