scott.compare

The Comparator class handles comparisons between graphs or graph distributions.

Example of Comparator usage:

import networkx as nx
from scott import KILT, Comparator

graph_dist1 = [nx.erdos_reyni(10,0.4) for _ in range(40)]
graph_dist2 = [nx.erdos_reyni(20,0.6) for _ in range(50)]

compare = Compare(measure="forman_curvature")

dist = compare.fit_transform(graph_dist1,graph_dist2,metric="image")

print(f"Distance between distributions measured by Forman Filtration: {dist}")
class scott.compare.Comparator(measure='forman_curvature', weight=None, alpha=0.0, prob_fn=None, homology_dims: List[int] | None = [0, 1], extended_persistence: bool = False)[source]

Compare Graphs or Graph Distributions using Curvature Filtrations as in https://openreview.net/forum?id=Dt71xKyabn using KILT.

Initialization of object allows user to specify parameters to customize how curvature is calculated.

kilt

The KILT object facilitates computing discrete curvature values for graphs.

Type:

KILT

homology_dims

Dimensions of the homology groups to compute (e.g., [0, 1] for H_0 and H_1). Default is [0, 1], the usual choice for graphs.

Type:

List[int]

extended_persistence

If True, the extended persistence diagram is computed. Default is False.

Type:

bool

descriptor1

The summarizing topological descriptor for G1, which encodes persistent homology information.

Type:

One of {PersistenceLandscape (default), PersistenceImage}

descriptor2

The summarizing topological descriptor for G2. Must match type of descriptor1 in order to compute distance, which encodes persistent homology inf`ormation.

Type:

One of {PersistenceLandscape (default), PersistenceImage}

__init__(measure='forman_curvature', weight=None, alpha=0.0, prob_fn=None, homology_dims: List[int] | None = [0, 1], extended_persistence: bool = False) None[source]
Parameters:
  • measure (string, default= "forman_curvature") – The type of curvature measure to be calculated. See CURVATURE_MEASURES for the methods that KILT functionality currently supports.

  • alpha (float, default=0.0) – Only used if Olivier–Ricci curvature is calculated, with default 0.0. Provides laziness parameter for default probability measure. The measure is not compatible with a user-defined prob_fn. If such a function is set, alpha will be ignored.

  • weight (str or None, default=None) – Can be specified for (weighted) Forman–Ricci, Olivier–Ricci and Resistance curvature measures. Name of an edge attribute that is supposed to be used as an edge weight. If None, unweighted curvature is calculated. Notice that for Ollivier–Ricci curvature, if prob_fn is provided, this parameter will have no effect for the calculation of probability measures, but it will be used for the calculation of shortest-path distances.

  • prob_fn (callable or None, default=None) – Used only if Ollivier–Ricci curvature is calculated, with default None. If set, should refer to a function that calculate a probability measure for a given graph and a given node. This callable needs to satisfy the following signature: prob_fn(G, node, node_to_index) Here, G refers to the graph, node to the node whose measure is to be calculated, and node_to_index to the lookup map that maps a node identifier to a zero-based index. If prob_fn is set, providing alpha will not have an effect.

  • homology_dims (List[int]) – Dimensions of the homology groups to compute (e.g., [0, 1] for H_0 and H_1). Default is [0, 1].

  • extended_persistence (bool) – If True, the extended persistence diagram is computed. Default is False.

Return type:

None

fit(G1, G2, metric='landscape', **kwargs) None[source]

Initializes a Topological Distance object and computes the topological descriptors that will be compared. Stores these topological descriptors in the descriptor1 and descriptor2 attributes of the Comparator object.

Parameters:
  • G1 (nx.Graph or List[nx.Graph]) – The first graph or graph distribution for comparison.

  • G2 (nx.Graph or List[nx.Graph]) – The first graph or graph distribution for comparison.

  • metric (str, default="landscape") – One of: {“landscape”, “image”}. Indicates which topological descriptor to use for computing distance.

Return type:

None

transform() float[source]

Computes the numeric distance between topological descriptors, i.e. attributes self.descriptor1 and self.descriptor2. Can only be run after fit().

Returns:

The distance between the topological descriptors stored in the descriptor1 and descriptor2 attributes.

Return type:

float

fit_transform(G1, G2, metric='landscape', **kwargs) float[source]

Runs the fit() and transform() methods in succession. Returns a numeric distance between G1 and G2, computed according to the given metric.

Parameters:
  • G1 (nx.Graph or List[nx.Graph]) – The first graph or graph distribution for comparison.

  • G2 (nx.Graph or List[nx.Graph]) – The first graph or graph distribution for comparison.

  • metric (str, default="landscape") – One of: {“landscape”, “image”}. Indicates which topological descriptor to use for computing distance.

Returns:

The distance between G1 and G2.

Return type:

float

Example

>>> import networkx as nx
>>> from scott.compare import Comparator
>>> graph1 = nx.random_geometric_graph(100, 0.1)
>>> graph2 = nx.random_geometric_graph(100, 0.2)
>>> comp = Comparator(measure='ollivier_ricci_curvature')
>>> distance = comp.fit_transform(graph1, graph2, metric='image')
>>> print(distance)
>>> 9.274264343274613