Skip to content

gnm.evaluation

evaluation

Network evaluation criteria for comparing generated networks to real networks.

This subpackage provides classes for quantifying the similarity or dissimilarity between synthetic (generated) networks and real (target) networks. These evaluation criteria can be used to:

  1. Assess the quality of generated networks.
  2. Guide optimization processes to find optimal model parameters.
  3. Compare different generative models parameters.

The module includes:

  • Base classes for defining novel evaluation criteria.
  • Criteria based on the Kolmogorov-Smirnov test comparing network property distributions.
  • Correlation-based criteria comparing spatial patterns of network properties.
  • Composite criteria combining multiple evaluation metrics.

Both binary (unweighted) and weighted network evaluations are supported.

Evaluation

gnm.evaluation.EvaluationCriterion(device=None)

Bases: ABC

Abstract base class for network evaluation criteria.

This class provides a framework for defining various criteria to evaluate the similarity between a synthetic (generated) network and a real (target) network. Each criterion computes a dissimilarity measure between the two networks based on specific network properties.

Subclasses must implement:

  1. _pre_call to perform validation checks on input matrices.
  2. _evaluate to compute the actual dissimilarity measure.
See Also

Parameters:

Name Type Description Default
device device

PyTorch device to use for computations. If None, uses CUDA if available, otherwise CPU.

None
Source code in src/gnm/evaluation/evaluation_base.py
39
40
41
42
43
44
45
46
def __init__(self, device: torch.device = None):
    r"""
    Args:
        device:
            PyTorch device to use for computations. If None, uses CUDA if available,
            otherwise CPU.
    """
    self.device = DEVICE if device is None else device

__str__()

Return a string representation of the criterion.

Source code in src/gnm/evaluation/evaluation_base.py
48
49
50
def __str__(self) -> str:
    r"""Return a string representation of the criterion."""
    return self.__class__.__name__

__call__(synthetic_matrices, real_matrices)

Compute the dissimilarity between synthetic and real networks.

This method validates the input matrices and then calls the _evaluate method to compute the actual dissimilarity measure.

Parameters:

Name Type Description Default
synthetic_matrices Float[Tensor, 'num_synthetic_networks num_nodes num_nodes']

Batch of adjacency/weight matrices of the synthetic networks with shape [num_synthetic_networks, num_nodes, num_nodes]

required
real_matrices Float[Tensor, 'num_real_networks num_nodes num_nodes']

Adjacency/weight matrices of the real networks with shape [num_real_networks, num_nodes, num_nodes]

required

Returns:

Type Description
Float[Tensor, 'num_synthetic_networks num_real_networks']

Tensor of dissimilarity values with shape [num_synthetic_networks, num_real_networks],

Float[Tensor, 'num_synthetic_networks num_real_networks']

where higher values indicate greater dissimilarity

Source code in src/gnm/evaluation/evaluation_base.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@jaxtyped(typechecker=typechecked)
def __call__(
    self,
    synthetic_matrices: Float[
        torch.Tensor, "num_synthetic_networks num_nodes num_nodes"
    ],
    real_matrices: Float[torch.Tensor, "num_real_networks num_nodes num_nodes"],
) -> Float[torch.Tensor, "num_synthetic_networks num_real_networks"]:
    r"""Compute the dissimilarity between synthetic and real networks.

    This method validates the input matrices and then calls the _evaluate method
    to compute the actual dissimilarity measure.

    Args:
        synthetic_matrices:
            Batch of adjacency/weight matrices of the synthetic networks with shape
            [num_synthetic_networks, num_nodes, num_nodes]
        real_matrices:
            Adjacency/weight matrices of the real networks with shape
            [num_real_networks, num_nodes, num_nodes]

    Returns:
        Tensor of dissimilarity values with shape [num_synthetic_networks, num_real_networks],
        where higher values indicate greater dissimilarity
    """
    self._pre_call(synthetic_matrices)
    self._pre_call(real_matrices)
    return self._evaluate(synthetic_matrices, real_matrices).to(DEVICE)

_pre_call(matrices) abstractmethod

Perform validation checks on input matrices.

This abstract method should be implemented by subclasses to ensure that input matrices meet the required criteria (e.g., binary values, symmetry).

Parameters:

Name Type Description Default
matrices Float[Tensor, 'num_network num_nodes num_nodes']

Adjacency/weight matrices with shape [num_networks, num_nodes, num_nodes]

required
Source code in src/gnm/evaluation/evaluation_base.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@abstractmethod
@jaxtyped(typechecker=typechecked)
def _pre_call(
    self, matrices: Float[torch.Tensor, "num_network num_nodes num_nodes"]
):
    r"""Perform validation checks on input matrices.

    This abstract method should be implemented by subclasses to ensure that input
    matrices meet the required criteria (*e.g.*, binary values, symmetry).

    Args:
        matrices:
            Adjacency/weight matrices with shape [num_networks, num_nodes, num_nodes]
    """
    pass

_evaluate(synthetic_matrices, real_matrices) abstractmethod

Compute the actual dissimilarity measure between networks.

This abstract method should be implemented by subclasses to define how to calculate the dissimilarity between synthetic and real networks.

Parameters:

Name Type Description Default
synthetic_matrices Float[Tensor, 'num_synthetic_networks num_nodes num_nodes']

Batch of adjacency/weight matrices of the synthetic networks with shape [num_synthetic_networks, num_nodes, num_nodes]

required
real_matrices Float[Tensor, 'num_real_networks num_nodes num_nodes']

Adjacency/weight matrices of the real networks with shape [num_real_networks, num_nodes, num_nodes]

required

Returns:

Type Description
Float[Tensor, 'num_synthetic_networks num_real_networks']

Tensor of dissimilarity values with shape [num_synthetic_networks, num_real_networks]

Source code in src/gnm/evaluation/evaluation_base.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@abstractmethod
@jaxtyped(typechecker=typechecked)
def _evaluate(
    self,
    synthetic_matrices: Float[
        torch.Tensor, "num_synthetic_networks num_nodes num_nodes"
    ],
    real_matrices: Float[torch.Tensor, "num_real_networks num_nodes num_nodes"],
) -> Float[torch.Tensor, "num_synthetic_networks num_real_networks"]:
    r"""Compute the actual dissimilarity measure between networks.

    This abstract method should be implemented by subclasses to define how
    to calculate the dissimilarity between synthetic and real networks.

    Args:
        synthetic_matrices:
            Batch of adjacency/weight matrices of the synthetic networks with shape
            [num_synthetic_networks, num_nodes, num_nodes]
        real_matrices:
            Adjacency/weight matrices of the real networks with shape
            [num_real_networks, num_nodes, num_nodes]

    Returns:
        Tensor of dissimilarity values with shape [num_synthetic_networks, num_real_networks]
    """
    pass

Criterion types

gnm.evaluation.BinaryEvaluationCriterion()

Bases: EvaluationCriterion, ABC

Base class for evaluation criteria specialized for binary networks.

This class extends EvaluationCriterion to specifically handle binary (unweighted) networks. It implements validation checks to ensure input matrices contain only binary values (0 or 1), are symmetric, and contain no self-connections.

Subclasses must implement the _evaluate method to compute the actual dissimilarity measure.

See Also

The initialisation method sets the accepts attribute to 'binary' to indicate that this criterion works with binary networks.

Source code in src/gnm/evaluation/evaluation_base.py
139
140
141
142
143
def __init__(self):
    r"""The initialisation method sets the accepts attribute to 'binary' to indicate that this criterion
    works with binary networks.
    """
    self.accepts = "binary"

_pre_call(matrices)

Perform validation checks on binary matrices.

Validates that the input matrices contain only binary values (0 or 1), are symmetric, and have no self-connections.

Parameters:

Name Type Description Default
matrices Float[Tensor, 'num_networks num_nodes num_nodes']

Binary adjacency matrices with shape [num_networks, num_nodes, num_nodes]

required
Source code in src/gnm/evaluation/evaluation_base.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
@jaxtyped(typechecker=typechecked)
def _pre_call(
    self, matrices: Float[torch.Tensor, "num_networks num_nodes num_nodes"]
):
    r"""Perform validation checks on binary matrices.

    Validates that the input matrices contain only binary values (0 or 1),
    are symmetric, and have no self-connections.

    Args:
        matrices:
            Binary adjacency matrices with shape [num_networks, num_nodes, num_nodes]
    """
    binary_checks(matrices)

gnm.evaluation.WeightedEvaluationCriterion()

Bases: EvaluationCriterion, ABC

Base class for evaluation criteria specialized for weighted networks.

This class extends EvaluationCriterion to specifically handle weighted networks. It implements validation checks to ensure input matrices contain only non-negative values which are symmetric and contain no self-connections.

Subclasses must implement the _evaluate to compute the actual dissimilarity measure.

See Also

The initialisation method sets the accepts attribute to 'weighted' to indicate that this criterion works with weighted networks.

Source code in src/gnm/evaluation/evaluation_base.py
175
176
177
178
179
def __init__(self):
    r"""The initialisation method sets the accepts attribute to 'weighted' to indicate that this criterion
    works with weighted networks.
    """
    self.accepts = "weighted"

_pre_call(matrices)

Perform validation checks on weighted matrices.

Validates that the input matrices contain only non-negative values, are symmetric, and have no self-connections.

Parameters:

Name Type Description Default
matrices Float[Tensor, 'num_networks num_nodes num_nodes']

Weighted adjacency matrices with shape [num_networks, num_nodes, num_nodes]

required
Source code in src/gnm/evaluation/evaluation_base.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@jaxtyped(typechecker=typechecked)
def _pre_call(
    self, matrices: Float[torch.Tensor, "num_networks num_nodes num_nodes"]
):
    r"""Perform validation checks on weighted matrices.

    Validates that the input matrices contain only non-negative values,
    are symmetric, and have no self-connections.

    Args:
        matrices:
            Weighted adjacency matrices with shape [num_networks, num_nodes, num_nodes]
    """
    weighted_checks(matrices)

gnm.evaluation.KSCriterion(device=None)

Bases: EvaluationCriterion, ABC

Base class for Kolmogorov-Smirnov (KS) distance based network evaluation.

This class implements network comparison using the KS test statistic between distributions of network properties (e.g., degree distribution, clustering coefficients). The KS statistic measures the maximum difference between two cumulative distribution functions, providing a measure of how different two distributions are.

Subclasses must implement the _get_graph_statistics method to define the specific network property to use in the KS test.

See Also
Source code in src/gnm/evaluation/evaluation_base.py
39
40
41
42
43
44
45
46
def __init__(self, device: torch.device = None):
    r"""
    Args:
        device:
            PyTorch device to use for computations. If None, uses CUDA if available,
            otherwise CPU.
    """
    self.device = DEVICE if device is None else device

_get_graph_statistics(matrices) abstractmethod

Compute network properties for KS comparison.

This abstract method should be implemented by subclasses to define which network property to extract for comparison (e.g., degrees, clustering coefficients).

Parameters:

Name Type Description Default
matrices Float[Tensor, 'num_networks num_nodes num_nodes']

Adjacency/weight matrices with shape [num_networks, num_nodes, num_nodes]

required

Returns:

Type Description
Float[Tensor, 'num_networks _']

Network property values with shape [num_networks, num_values]

Source code in src/gnm/evaluation/evaluation_base.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
@abstractmethod
@jaxtyped(typechecker=typechecked)
def _get_graph_statistics(
    self, matrices: Float[torch.Tensor, "num_networks num_nodes num_nodes"]
) -> Float[torch.Tensor, "num_networks _"]:
    r"""Compute network properties for KS comparison.

    This abstract method should be implemented by subclasses to define which
    network property to extract for comparison (*e.g.*, degrees, clustering coefficients).

    Args:
        matrices:
            Adjacency/weight matrices with shape [num_networks, num_nodes, num_nodes]

    Returns:
        Network property values with shape [num_networks, num_values]
    """
    pass

gnm.evaluation.CorrelationCriterion(smoothing_matrix)

Bases: EvaluationCriterion, ABC

Base class for correlation-based network evaluation criteria.

This class implements network comparison using correlation coefficients between spatial patterns of network properties (e.g., node degree, clustering coefficients). Higher correlation indicates greater similarity in the spatial organisation of network properties.

Subclasses must implement the _get_graph_statistics method to define the specific network property to use in the correlation calculation.

Parameters:

Name Type Description Default
smoothing_matrix Float[Tensor, 'num_nodes num_nodes']

Matrix used to spatially smooth the network property values, which can help account for registration errors or spatial uncertainty in brain networks.

required
See Also

Parameters:

Name Type Description Default
smoothing_matrix Float[Tensor, 'num_nodes num_nodes']

Matrix used to spatially smooth the network property values with shape [num_nodes, num_nodes]. This can help account for registration errors or spatial uncertainty in brain networks.

required
Source code in src/gnm/evaluation/evaluation_base.py
290
291
292
293
294
295
296
297
298
def __init__(self, smoothing_matrix: Float[torch.Tensor, "num_nodes num_nodes"]):
    r"""
    Args:
        smoothing_matrix:
            Matrix used to spatially smooth the network property values with shape
            [num_nodes, num_nodes]. This can help account for registration errors
            or spatial uncertainty in brain networks.
    """
    self.smoothing_matrix = smoothing_matrix

_get_graph_statistics(matrices) abstractmethod

Compute network properties for correlation comparison.

This abstract method should be implemented by subclasses to define which network property to extract for comparison (e.g., degrees, clustering coefficients).

Parameters:

Name Type Description Default
matrices Float[Tensor, 'num_networks num_nodes num_nodes']

Adjacency/weight matrices with shape [num_networks, num_nodes, num_nodes]

required

Returns:

Type Description
Float[Tensor, 'num_networks num_nodes']

Network property values with shape [num_networks, num_nodes]

Source code in src/gnm/evaluation/evaluation_base.py
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
@abstractmethod
@jaxtyped(typechecker=typechecked)
def _get_graph_statistics(
    self, matrices: Float[torch.Tensor, "num_networks num_nodes num_nodes"]
) -> Float[torch.Tensor, "num_networks num_nodes"]:
    r"""Compute network properties for correlation comparison.

    This abstract method should be implemented by subclasses to define which
    network property to extract for comparison (*e.g.*, degrees, clustering coefficients).

    Args:
        matrices:
            Adjacency/weight matrices with shape [num_networks, num_nodes, num_nodes]

    Returns:
        Network property values with shape [num_networks, num_nodes]
    """
    pass

Composite Criteria

gnm.evaluation.CompositeCriterion(criteria)

Bases: EvaluationCriterion, ABC

Base class for composite evaluation criteria combining multiple metrics.

This class allows combining multiple evaluation criteria into a single composite criterion. Subclasses define how to combine the individual criteria results (e.g., maximum, mean, weighted sum).

Parameters:

Name Type Description Default
criteria list[EvaluationCriterion]

List of EvaluationCriterion objects to combine

required
Notes

All criteria in the list must accept the same type of network (binary or weighted).

See Also

Parameters:

Name Type Description Default
criteria list[EvaluationCriterion]

List of EvaluationCriterion objects to combine. All criteria must accept the same type of network (binary or weighted).

required

Raises:

Type Description
AssertionError

If no criteria are provided or if they accept different network type

Source code in src/gnm/evaluation/evaluation_base.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def __init__(self, criteria: list[EvaluationCriterion]):
    r"""
    Args:
        criteria:
            List of EvaluationCriterion objects to combine. All criteria must
            accept the same type of network (binary or weighted).

    Raises:
        AssertionError: If no criteria are provided or if they accept different network type
    """
    assert len(criteria) > 0, "Must provide at least one criterion"
    self.criteria = criteria
    self.accepts = self.criteria[0].accepts
    assert all(
        criterion.accepts == self.accepts for criterion in self.criteria
    ), "All criteria must accept the same type of network"

gnm.evaluation.MaxCriteria(criteria)

Bases: CompositeCriterion

Combine multiple evaluation criteria by taking their maximum value.

This class enables the evaluation of networks using multiple criteria simultaneously, where the overall dissimilarity is determined by the worst-performing (maximum) criterion. This approach ensures that the synthetic network must match the real network well across all specified properties.

Examples:

>>> import torch
>>> from gnm.evaluation import DegreeKS, ClusteringKS, MaxCriteria
>>> from gnm.defaults import get_binary_network
>>> from gnm.utils import get_control
>>> # Create individual criteria
>>> degree_ks = DegreeKS()
>>> clustering_ks = ClusteringKS()
>>> # Combine them using MaxCriteria
>>> max_criterion = MaxCriteria([degree_ks, clustering_ks])
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate degree distribution dissimilarity
>>> dissimilarity = max_criterion(random_network, real_network)
See Also
Source code in src/gnm/evaluation/evaluation_base.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def __init__(self, criteria: list[EvaluationCriterion]):
    r"""
    Args:
        criteria:
            List of EvaluationCriterion objects to combine. All criteria must
            accept the same type of network (binary or weighted).

    Raises:
        AssertionError: If no criteria are provided or if they accept different network type
    """
    assert len(criteria) > 0, "Must provide at least one criterion"
    self.criteria = criteria
    self.accepts = self.criteria[0].accepts
    assert all(
        criterion.accepts == self.accepts for criterion in self.criteria
    ), "All criteria must accept the same type of network"

gnm.evaluation.MeanCriteria(criteria)

Bases: CompositeCriterion

Combine multiple evaluation criteria by taking their mean value.

This class enables the evaluation of networks using multiple criteria simultaneously, where the overall dissimilarity is determined by the average value of all criteria. This approach balances different aspects of network similarity without letting a single criterion dominate.

Examples:

>>> import torch
>>> from gnm.evaluation import DegreeKS, ClusteringKS, MeanCriteria
>>> from gnm.defaults import get_binary_network
>>> from gnm.utils import get_control
>>> # Create individual criteria
>>> degree_ks = DegreeKS()
>>> clustering_ks = ClusteringKS()
>>> # Combine them using MeanCriteria
>>> max_criterion = MeanCriteria([degree_ks, clustering_ks])
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate degree distribution dissimilarity
>>> dissimilarity = max_criterion(random_network, real_network)
See Also
Source code in src/gnm/evaluation/evaluation_base.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def __init__(self, criteria: list[EvaluationCriterion]):
    r"""
    Args:
        criteria:
            List of EvaluationCriterion objects to combine. All criteria must
            accept the same type of network (binary or weighted).

    Raises:
        AssertionError: If no criteria are provided or if they accept different network type
    """
    assert len(criteria) > 0, "Must provide at least one criterion"
    self.criteria = criteria
    self.accepts = self.criteria[0].accepts
    assert all(
        criterion.accepts == self.accepts for criterion in self.criteria
    ), "All criteria must accept the same type of network"

gnm.evaluation.WeightedSumCriteria(criteria, weights)

Bases: CompositeCriterion

Combine multiple evaluation criteria by taking their weighted sum.

This class enables the evaluation of networks using multiple criteria with different importance weights assigned to each criterion. By adjusting the weights, users can emphasize certain network properties over others in the evaluation.

Examples:

>>> import torch
>>> from gnm.evaluation import DegreeKS, ClusteringKS, WeightedSumCriteria
>>> from gnm.defaults import get_binary_network
>>> from gnm.utils import get_control
>>> # Create individual criteria
>>> degree_ks = DegreeKS()
>>> clustering_ks = ClusteringKS()
>>> # Combine them using WeightedSumCriteria (more weight on degree)
>>> weighted_criterion = WeightedSumCriteria(
...     [degree_ks, clustering_ks],
...     weights=[0.7, 0.3]
... )
>>> # Calculate dissimilarity between networks
>>> real_network = get_binary_network()  # Example real network
>>> random_network = get_control(real_network)  # Example random network
>>> dissimilarity = weighted_criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
criteria list[EvaluationCriterion]

List of evaluation criteria to combine.

required
weights list[float]

List of weights for each criterion. Should have the same length as criteria.

required
Source code in src/gnm/evaluation/composite_criteria.py
204
205
206
207
208
209
210
211
212
213
def __init__(self, criteria: list[EvaluationCriterion], weights: list[float]):
    r"""
    Args:
        criteria:
            List of evaluation criteria to combine.
        weights:
            List of weights for each criterion. Should have the same length as criteria.
    """
    self.weights = weights
    super().__init__(criteria)

Binary KS Criteria

gnm.evaluation.DegreeKS()

Bases: KSCriterion, BinaryEvaluationCriterion

Compare degree distributions between binary networks using KS statistic.

This criterion measures the dissimilarity between the degree distributions of synthetic and real networks using the Kolmogorov-Smirnov test. The degree of a node is the number of connections it has to other nodes.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network
>>> from gnm.evaluation import DegreeKS
>>> from gnm.utils import get_control
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate degree distribution dissimilarity
>>> criterion = DegreeKS()
>>> dissimilarity = criterion(random_network, real_network)
See Also
Source code in src/gnm/evaluation/binary_ks_criteria.py
46
47
48
def __init__(self):
    KSCriterion.__init__(self)
    BinaryEvaluationCriterion.__init__(self)

gnm.evaluation.ClusteringKS()

Bases: KSCriterion, BinaryEvaluationCriterion

Compare clustering coefficient distributions between binary networks using KS statistic.

This criterion measures the dissimilarity between the clustering coefficient distributions of synthetic and real networks using the Kolmogorov-Smirnov test. The clustering coefficient measures the degree to which nodes in a graph tend to cluster together.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network
>>> from gnm.evaluation import ClusteringKS
>>> from gnm.utils import get_control
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate clustering coefficient distribution dissimilarity
>>> criterion = ClusteringKS()
>>> dissimilarity = criterion(random_network, real_network)
See Also
Source code in src/gnm/evaluation/binary_ks_criteria.py
96
97
98
def __init__(self):
    KSCriterion.__init__(self)
    BinaryEvaluationCriterion.__init__(self)

gnm.evaluation.BetweennessKS()

Bases: KSCriterion, BinaryEvaluationCriterion

Compare betweenness centrality distributions between binary networks using KS statistic.

This criterion measures the dissimilarity between the betweenness centrality distributions of synthetic and real networks using the Kolmogorov-Smirnov test. Betweenness centrality quantifies the number of times a node acts as a bridge along the shortest path between two other nodes.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network
>>> from gnm.evaluation import BetweennessKS
>>> from gnm.utils import get_control
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate betweenness centrality distribution dissimilarity
>>> criterion = BetweennessKS()
>>> dissimilarity = criterion(random_network, real_network)
See Also
Source code in src/gnm/evaluation/binary_ks_criteria.py
146
147
148
def __init__(self):
    KSCriterion.__init__(self)
    BinaryEvaluationCriterion.__init__(self)

gnm.evaluation.EdgeLengthKS(distance_matrix)

Bases: BinaryEvaluationCriterion

Compare edge length distributions between binary networks using KS statistic.

This criterion measures the dissimilarity between the edge length distributions of synthetic and real networks using the Kolmogorov-Smirnov test. Edge length is determined by a distance matrix that defines the spatial distance between nodes.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network, get_distance_matrix
>>> from gnm.evaluation import EdgeLengthKS
>>> from gnm.utils import get_control
>>> # Load a default binary network and distance matrix
>>> real_network = get_binary_network()
>>> distance_matrix = get_distance_matrix()
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate edge length distribution dissimilarity
>>> criterion = EdgeLengthKS(distance_matrix)
>>> dissimilarity = criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
distance_matrix Float[Tensor, 'num_nodes num_nodes']

Matrix defining the spatial distance between each pair of nodes with shape [num_nodes, num_nodes]

required
Source code in src/gnm/evaluation/binary_ks_criteria.py
194
195
196
197
198
199
200
201
202
def __init__(self, distance_matrix: Float[torch.Tensor, "num_nodes num_nodes"]):
    r"""
    Args:
        distance_matrix:
            Matrix defining the spatial distance between each pair of nodes
            with shape [num_nodes, num_nodes]
    """
    BinaryEvaluationCriterion.__init__(self)
    self.distance_matrix = distance_matrix

Weighted KS Criteria

gnm.evaluation.WeightedNodeStrengthKS(normalise=True)

Bases: KSCriterion, WeightedEvaluationCriterion

Compare node strength distributions between weighted networks using KS statistic.

This criterion measures the dissimilarity between the node strength distributions of synthetic and real weighted networks using the Kolmogorov-Smirnov test. Node strength is the weighted equivalent of node degree - it is the sum of the weights of all edges connected to a node.

Examples:

>>> import torch
>>> from gnm.defaults import get_weighted_network
>>> from gnm.evaluation import WeightedNodeStrengthKS
>>> from gnm.utils import get_control
>>> # Load a default weighted network
>>> real_network = get_weighted_network()
>>> # Create a random network with the same weight distribution
>>> random_network = get_control(real_network)
>>> # Calculate node strength distribution dissimilarity
>>> criterion = WeightedNodeStrengthKS()
>>> dissimilarity = criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
normalise Optional[bool]

If True, normalise the weights of each network by its maximum weight before computing node strengths. This can be useful when comparing networks with different weight scales. Defaults to True.

True
Source code in src/gnm/evaluation/weighted_ks_criteria.py
47
48
49
50
51
52
53
54
55
56
57
58
@jaxtyped(typechecker=typechecked)
def __init__(self, normalise: Optional[bool] = True):
    r"""
    Args:
        normalise:
            If True, normalise the weights of each network by its maximum weight
            before computing node strengths. This can be useful when comparing
            networks with different weight scales. Defaults to True.
    """
    KSCriterion.__init__(self)
    WeightedEvaluationCriterion.__init__(self)
    self.normalise = normalise

gnm.evaluation.WeightedBetweennessKS(normalise=True)

Bases: KSCriterion, WeightedEvaluationCriterion

Compare weighted betweenness centrality distributions using KS statistic.

This criterion measures the dissimilarity between the weighted betweenness centrality distributions of synthetic and real networks using the Kolmogorov-Smirnov test.

We compute betweenness centrality using Brandes algorithm. Betweenness centrality for a node \(u\) in a weighted network is:

\[ c_B(u) = \sum_{v,w} \\frac{\sigma(v,w|u)}{\sigma(v,w)}, \]

where \(\sigma(v,w)\) is the number of shortest paths from \(v\) to \(w\), and \(\sigma(v,w|u)\) is the number of those that pass through \(u\). For weighted networks, path lengths are computed using the edge weights as distance.

Examples:

>>> import torch
>>> from gnm.defaults import get_weighted_network
>>> from gnm.evaluation import WeightedBetweennessKS
>>> from gnm.utils import get_control
>>> # Load a default weighted network
>>> real_network = get_weighted_network()
>>> # Create a random network with the same weight distribution
>>> random_network = get_control(real_network)
>>> # Calculate weighted betweenness centrality distribution dissimilarity
>>> criterion = WeightedBetweennessKS()
>>> dissimilarity = criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
normalise Optional[bool]

If True, normalise the weights of the network by the maximum weight in the network. Defaults to True.

True
Source code in src/gnm/evaluation/weighted_ks_criteria.py
117
118
119
120
121
122
123
124
125
@jaxtyped(typechecker=typechecked)
def __init__(self, normalise: Optional[bool] = True):
    """
    Args:
        normalise: If True, normalise the weights of the network by the maximum weight in the network. Defaults to True.
    """
    KSCriterion.__init__(self)
    WeightedEvaluationCriterion.__init__(self)
    self.normalise = normalise

gnm.evaluation.WeightedClusteringKS()

Bases: KSCriterion, WeightedEvaluationCriterion

Compare weighted clustering coefficient distributions using KS statistic.

This criterion measures the dissimilarity between the weighted clustering coefficient distributions of synthetic and real networks using the Kolmogorov-Smirnov test.

Implements the Onnela et al. (2005) definition of weighted clustering, which uses the geometric mean of triangle weights. For each node \(u\), the clustering coefficient is:

\[ c(u) = \\frac{1}{k_u(k_u-1)} \sum_{v,w} (\hat{w}_{uv} \\times \hat{w}_{uw} \\times \hat{w}_{vw})^{1/3}, \]

where \(k_u\) is the node strength of node \(u\), and \(\hat{w}_{uv}\) is the weight of the edge between nodes \(u\) and \(v\), after normalising by dividing by the maximum weight in the network.

Examples:

>>> import torch
>>> from gnm.defaults import get_weighted_network
>>> from gnm.evaluation import WeightedClusteringKS
>>> from gnm.utils import get_control
>>> # Load a default weighted network
>>> real_network = get_weighted_network()
>>> # Create a random network with the same weight distribution
>>> random_network = get_control(real_network)
>>> # Calculate weighted clustering coefficient distribution dissimilarity
>>> criterion = WeightedClusteringKS()
>>> dissimilarity = criterion(random_network, real_network)
See Also
Source code in src/gnm/evaluation/weighted_ks_criteria.py
191
192
193
def __init__(self):
    KSCriterion.__init__(self)
    WeightedEvaluationCriterion.__init__(self)

Binary Correlation Criteria

gnm.evaluation.DegreeCorrelation(smoothing_matrix)

Bases: CorrelationCriterion, BinaryEvaluationCriterion

Compare spatial patterns of node degrees between binary networks using correlation.

This criterion measures the similarity between the spatial patterns of node degrees in synthetic and real networks using Pearson correlation. Unlike KS statistics which compare distributions, correlation criteria assess whether the same nodes have similar relative values in both networks.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network, get_distance_matrix
>>> from gnm.evaluation import DegreeCorrelation
>>> from gnm.utils import get_control
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create smoothing matrix (here using identity for example)
>>> num_nodes = real_network.shape[-1]
>>> smoothing_matrix = torch.eye(num_nodes)
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate degree spatial correlation
>>> criterion = DegreeCorrelation(smoothing_matrix)
>>> dissimilarity = criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
smoothing_matrix Float[Tensor, 'num_nodes num_nodes']

Matrix used to spatially smooth the degree values, which can help account for registration errors or spatial uncertainty in brain networks. Shape [num_nodes, num_nodes].

required
Source code in src/gnm/evaluation/binary_corr_criteria.py
48
49
50
51
52
53
54
55
56
57
def __init__(self, smoothing_matrix: Float[torch.Tensor, "num_nodes num_nodes"]):
    r"""
    Args:
        smoothing_matrix:
            Matrix used to spatially smooth the degree values, which can
            help account for registration errors or spatial uncertainty in brain networks.
            Shape [num_nodes, num_nodes].
    """
    CorrelationCriterion.__init__(self, smoothing_matrix)
    BinaryEvaluationCriterion.__init__(self)

gnm.evaluation.ClusteringCorrelation(smoothing_matrix)

Bases: CorrelationCriterion, BinaryEvaluationCriterion

Compare spatial patterns of clustering coefficients using correlation.

This criterion measures the similarity between the spatial patterns of clustering coefficients in synthetic and real networks using Pearson correlation. The clustering coefficient measures the degree to which nodes in a graph tend to cluster together.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network, get_distance_matrix
>>> from gnm.evaluation import ClusteringCorrelation
>>> from gnm.utils import get_control
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create smoothing matrix (here using identity for example)
>>> num_nodes = real_network.shape[-1]
>>> smoothing_matrix = torch.eye(num_nodes)
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate clustering coefficient spatial correlation
>>> criterion = ClusteringCorrelation(smoothing_matrix)
>>> dissimilarity = criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
smoothing_matrix Float[Tensor, 'num_nodes num_nodes']

Matrix used to spatially smooth the clustering coefficient values, which can help account for registration errors or spatial uncertainty in brain networks. Shape [num_nodes, num_nodes].

required
Source code in src/gnm/evaluation/binary_corr_criteria.py
107
108
109
110
111
112
113
114
115
116
def __init__(self, smoothing_matrix: Float[torch.Tensor, "num_nodes num_nodes"]):
    r"""
    Args:
        smoothing_matrix:
            Matrix used to spatially smooth the clustering coefficient values, which can
            help account for registration errors or spatial uncertainty in brain networks.
            Shape [num_nodes, num_nodes].
    """
    CorrelationCriterion.__init__(self, smoothing_matrix)
    BinaryEvaluationCriterion.__init__(self)

gnm.evaluation.BetweennessCorrelation(smoothing_matrix)

Bases: CorrelationCriterion, BinaryEvaluationCriterion

Compare spatial patterns of betweenness centrality using correlation.

This criterion measures the similarity between the spatial patterns of betweenness centrality in synthetic and real networks using Pearson correlation. Betweenness centrality quantifies the number of times a node acts as a bridge along the shortest path between two other nodes.

Examples:

>>> import torch
>>> from gnm.defaults import get_binary_network, get_distance_matrix
>>> from gnm.evaluation import BetweennessCorrelation
>>> from gnm.utils import get_control
>>> # Load a default binary network
>>> real_network = get_binary_network()
>>> # Create smoothing matrix (here using identity for example)
>>> num_nodes = real_network.shape[-1]
>>> smoothing_matrix = torch.eye(num_nodes)
>>> # Create a random network
>>> random_network = get_control(real_network)
>>> # Calculate betweenness centrality spatial correlation
>>> criterion = BetweennessCorrelation(smoothing_matrix)
>>> dissimilarity = criterion(random_network, real_network)
See Also

Parameters:

Name Type Description Default
smoothing_matrix Float[Tensor, 'num_nodes num_nodes']

Matrix used to spatially smooth the betweenness centrality values, which can help account for registration errors or spatial uncertainty in brain networks. Shape [num_nodes, num_nodes].

required
Source code in src/gnm/evaluation/binary_corr_criteria.py
167
168
169
170
171
172
173
174
175
176
177
def __init__(self, smoothing_matrix: Float[torch.Tensor, "num_nodes num_nodes"]):
    r"""
    Args:
        smoothing_matrix:
            Matrix used to spatially smooth the betweenness centrality values, which can
            help account for registration errors or spatial uncertainty in brain networks.
            Shape [num_nodes, num_nodes].
    """

    CorrelationCriterion.__init__(self, smoothing_matrix)
    BinaryEvaluationCriterion.__init__(self)