networks.describe.py

class apparent.networks.describe.NetworkDescriber(G)

A class to compute and describe features for a given graph.

This class provides functionality to compute various node and edge features for NetworkX graphs. It supports standard network centrality measures as well as curvature-based edge features through the Scott package.

Parameters:

G (nx.Graph) – Input NetworkX graph to analyze.

G

Input graph with updated node and edge attributes after feature computation.

Type:

nx.Graph

features

Dictionary containing computed features with feature names as keys.

Type:

dict

Examples

>>> import networkx as nx
>>> from apparent.networks import NetworkDescriber
>>>
>>> # Create a sample graph
>>> G = nx.karate_club_graph()
>>>
>>> # Compute features
>>> describer = NetworkDescriber(G)
>>> describer.compute_features(
...     node_features=['degree', 'betweenness'],
...     edge_features=['forman_curvature']
... )
>>> print(f"Computed features: {list(describer.features.keys())}")
__init__(G)

Initialize the NetworkDescriber with a graph.

Parameters:

G (nx.Graph) – Input NetworkX graph to analyze. The graph will be modified in-place when features are computed.

Examples

>>> G = nx.karate_club_graph()
>>> describer = NetworkDescriber(G)
compute_node_features(node_features)

Compute specified node features for the graph and add them as attributes.

This method computes various centrality and structural measures for nodes and adds them as node attributes to the graph.

Parameters:

node_features (list) – List of node features to compute. Available options: - ‘degree’: Node degree (number of connections) - ‘clustering’: Local clustering coefficient - ‘betweenness’: Betweenness centrality - ‘closeness’: Closeness centrality - ‘pagerank’: PageRank centrality

Returns:

Features are added as node attributes to self.G and stored in self.features.

Return type:

None

Raises:

ValueError – If an unknown node feature is specified.

Examples

>>> describer = NetworkDescriber(G)
>>> describer.compute_node_features(['degree', 'betweenness'])
>>> print(G.nodes[0]['degree'])  # Access computed degree
compute_edge_features(edge_features, **kwargs)

Compute specified edge features for the graph and add them as attributes.

This method computes various edge-level features, including centrality measures and curvature-based features through the Scott package’s KILT implementation.

Parameters:
  • edge_features (list) –

    List of edge features to compute. Available options: - ‘edge_betweenness’: Edge betweenness centrality - Any curvature measure from scott.kilt.CURVATURE_MEASURES

    (e.g., ‘forman_curvature’, ‘ollivier_ricci_curvature’)

  • **kwargs (dict) – Additional arguments passed to KILT curvature measures.

Returns:

Features are added as edge attributes to self.G and stored in self.features.

Return type:

None

Raises:

ValueError – If an unknown edge feature is specified.

Examples

>>> describer = NetworkDescriber(G)
>>> describer.compute_edge_features(['edge_betweenness', 'forman_curvature'])
>>> print(G.edges[(0, 1)]['forman_curvature'])  # Access computed curvature
compute_features(node_features=None, edge_features=None, **kwargs)

Compute all specified features for the graph and add them as attributes.

This is a convenience method that calls both compute_node_features and compute_edge_features based on the provided feature lists.

Parameters:
  • node_features (list, optional) – List of node features to compute. If None, no node features are computed.

  • edge_features (list, optional) – List of edge features to compute. If None, no edge features are computed.

  • **kwargs (dict) – Additional arguments passed to KILT curvature measures.

Returns:

Features are added as attributes to self.G and stored in self.features.

Return type:

None

Examples

>>> describer = NetworkDescriber(G)
>>> describer.compute_features(
...     node_features=['degree', 'clustering'],
...     edge_features=['forman_curvature']
... )
get_features()

Retrieve computed features.

Returns:

Dictionary containing computed features with feature names as keys and feature values as values.

Return type:

dict

Examples

>>> describer = NetworkDescriber(G)
>>> describer.compute_features(node_features=['degree'])
>>> features = describer.get_features()
>>> print(features.keys())  # ['degree']