topologic.embedding.metric package

topologic.embedding.metric.calculate_internal_external_densities(graph: networkx.classes.graph.Graph, partitions: Dict[Any, Any], weight_attribute: str = 'weight') → Tuple[Dict[Any, List[float]], Dict[Any, List[float]]][source]

Calculates the internal and external densities given a graph and a node membership dictionary. Density is defined by ‘How to Make the Team: Social Networks vs. Demography as Criteria for Designing Effective Teams’ as being the mean strength of tie between members of the set. In other words, density is the normalized average of edge weights by node.

For a given node, the density is the sum of all edge weights divided by the maximum edge weight for that node.

For internal density, only the edge’s whose target node is in the same membership group will be summed. Similarly, for external density, only the edge’s whose target node is not in the same membership group will be summed.

See also: Reagans, R., Zuckerman, E., & McEvily, B. (2004). How to Make the Team: Social Networks vs. Demography as Criteria for Designing Effective Teams. Administrative Science Quarterly, 49(1), 101–133.

  • graph – A weighted graph that the internal density will be calculated over

  • int] partitions (Dict[any,) – A dictionary for the graph with each key being a node id and each value is the membership for that node id. Often this will be a partition dictionary calculated from topologic.louvain.best_partition

  • weight_attribute (str) – The key to the weight column on the graph’s edges


A tuple of two dictionaries. The first is the internal density and the second is the external density

Return type

Tuple[Dict[Any, List[float]], Dict[Any, List[float]]]

topologic.embedding.metric.mean_average_precision(graph: networkx.classes.graph.Graph, embedding_container: topologic.embedding.embedding_container.EmbeddingContainer, distance_metric: str = 'euclidean') → float[source]

Mean Average Precision (mAP)

A fidelity measure to evaluate the quality of embedding generated with respect to the original unweighted Graph.

Higher mAP value corresponds to a better quality embedding.

  • G (networkx.Graph) – The unweighted graph for which the embedding is generated

  • embedding_container (EmbeddingContainer) – The embedding container generated for the graph for which the mean average precision will be calculated

  • distance_metric (str) – The distance metric to be used to find shortest path between nodes in the graph and embedding space. Default value for this param is ‘euclidean’, but all distance metrics available to the scipy.spatial.distance.cdist function are valid.


The mean average precision (mAP <= 1) representing the quality of the embedding

Return type


topologic.embedding.metric.procrustes_error(target_matrix: numpy.ndarray, matrix_to_rotate: numpy.ndarray) → Tuple[numpy.ndarray, numpy.ndarray][source]

Procrustes rotation rotates a matrix to maximum similarity with a target matrix minimizing sum of squared differences. Procrustes rotation is typically used in comparison of ordination results. It is particularly useful in comparing alternative solutions in multidimensional scaling.

For more information:

  • target_matrix (numpy.ndarray) – A matrix representing an embedding

  • matrix_to_rotate (numpy.ndarray) – A matrix representing an embedding which will be rotated


The error which is the difference between the two matrices and the transformation matrix