utils package

Submodules

utils.plot module

A module for plotting graphs and charts.

class utils.plot.GraphDrawer(graph, title, save_path, seed=42)[source]

Bases: object

A class for visualizing network graphs with Ricci curvature and community structures.

draw_colorbar(curvature_values)[source]

Draws a colorbar for the Ricci curvature values.

Parameters:

curvature_values (list of float) – List of curvature values for edges.

draw_communities(clustering_label, nodes_cmap='tab20')[source]

Draws the communities (identified as the connected components) in subplots.

Parameters:
  • clustering_label (str) – Node attribute name for clustering (community) labels.

  • nodes_cmap (str, optional) – The colormap for nodes. Default is default_nodes_cmap.

draw_graph(clustering_label, nodes_cmap='tab20')[source]

Draws the graph with community coloring (from ground truth) and Ricci curvature visualization.

Parameters:
  • clustering_label (str) – Node attribute name for clustering (community) labels.

  • nodes_cmap (str, optional) – The colormap for nodes. Default is default_nodes_cmap.

plot_graph_histo(curvature='ricciCurvature')[source]

Plots histograms for Ricci curvature and edge weights.

Parameters:

curvature (str, optional) – The edge attribute name for Ricci curvature values. Default is “ricciCurvature”.

save_and_show(plot_axis=False)[source]

Saves the current plot to the specified directory and displays it.

Parameters:

plot_axis (bool, optional) – Whether to display the axis in the plot. Default is False.

utils.plot.edge_colors = [(0, 'blue'), (0.5, 'black'), (1, 'red')]

Custom colormap for edges based on Ricci curvature values. The colors represent different curvature ranges: - Blue: Negative curvature - Black: Zero curvature - Red: Positive curvature

utils.plot.plot_accuracy(maxw, cutoff_range, modularity, ari, save_path, good_cut=None)[source]

Plots the accuracy of the edge weight cutoff with respect to modularity and Adjusted Rand Index (ARI).

Parameters:
  • maxw (float) – Maximum edge weight for the x-axis limit.

  • cutoff_range (list or array of float) – Range of edge weight cutoff values.

  • modularity (list or array of float) – Modularity values corresponding to the cutoff range.

  • ari (list or array of float) – Adjusted Rand Index values corresponding to the cutoff range.

  • save_path (str) – Path to save the resulting plot image.

  • good_cut (float, optional) – Optional edge weight cutoff value that represents a “good” cut. If provided, a vertical line will be drawn at this value. Default is None.

utils.plot.plot_comp_histo(modularity_values, ari_values, save_path)[source]

Plot and compare modularity and Adjusted Rand Index (ARI) across different community detection methods.

This function creates two bar charts to visualize the performance of three community detection methods (Ricci Flow, Louvain, and Girvan-Newman) based on:

  • Modularity: Measures the strength of community structure in the network.

  • Adjusted Rand Index (ARI): Measures clustering accuracy compared to the ground truth.

The function saves the resulting comparison plot to the specified directory.

Parameters:
  • modularity_values (list[float]) – Modularity scores for Ricci Flow, Louvain, and Girvan-Newman.

  • ari_values (list[float]) – ARI scores for Ricci Flow, Louvain, and Girvan-Newman.

  • save_path (str) – Directory where the comparison plot will be saved.

utils.surgery module

utils.surgery.ARI(G, clustering, clustering_label)[source]

Compute the Adjust Rand Index (clustering accuracy) of “clustering” with “clustering_label” as ground truth.

Parameters:
  • G (networkx.Graph) – A graph with node attribute “clustering_label” as ground truth.

  • clustering (dict, list, or list of sets) – Predicted community clustering.

  • clustering_label (str) – Node attribute name for ground truth.

Returns:

Adjust Rand Index for predicted community.

Return type:

float

utils.surgery.check_accuracy(G_origin, weight='weight', clustering_label='community', eval_cut=False)[source]

Evaluate the clustering quality while cutting edges with a given weight using different thresholds.

This function iteratively removes edges based on a weight threshold (Ricci flow metric) and evaluates the clustering results using modularity and Adjusted Rand Index (ARI). If eval_cut is enabled, it also estimates a “good” cut threshold by detecting significant drops in modularity.

Parameters:
  • G_origin (networkx.Graph) – A graph with weight as the Ricci flow metric used for edge removal.

  • weight (str) – The edge weight attribute used as the Ricci flow metric. Defaults to "weight".

  • clustering_label (str) – Node attribute name for ground truth communities. Defaults to "community".

  • eval_cut (bool) – Whether to compute an estimated optimal cut threshold based on modularity drops. Defaults to False.

Returns:

A tuple containing: - maxw (float): Maximum edge weight value in the graph. - cutoff_range (numpy.ndarray): Array of tested cutoff values for edge removal. - modularity (list[float]): Modularity values at each cutoff. - ari (list[float]): ARI values at each cutoff. - (Optional) good_cut (float): Estimated best cutoff based on modularity drop (only if eval_cut=True). - (Optional) best_ari (float): Highest ARI value achieved (only if eval_cut=True). - (Optional) best_mod (float): Modularity value at best_ari (only if eval_cut=True).

Return type:

tuple - If eval_cut=False(float, numpy.ndarray, list[float], list[float]) - If eval_cut=True(float, numpy.ndarray, list[float], list[float], float, float, float)

utils.surgery.get_best_cut(G_origin, weight='weight', clustering_label='value')[source]

Determine the best edge removal cutoff threshold to maximize clustering accuracy (ARI).

This function iteratively removes edges based on a weight threshold (e.g., Ricci flow metric) and evaluates the resulting clustering accuracy using the Adjusted Rand Index (ARI). The best cutoff is selected as the one that yields the highest ARI.

Parameters:
  • G_origin (networkx.Graph) – A graph where edges have an attribute weight used as a removal criterion.

  • weight (str) – The edge weight attribute used as the removal metric. Defaults to "weight".

  • clustering_label (str) – Node attribute name for ground truth communities. Defaults to "value".

Returns:

The cutoff threshold that results in the highest ARI.

Return type:

float

utils.surgery.perform_surgery(G, weight='weight', clustering_label='community', cut=0)[source]

A simple surgery function that removes the edges with weight above a threshold.

Parameters:
  • G (networkx.Graph) – A graph with weight as the Ricci flow metric to cut.

  • weight (str) – The edge weight used as the Ricci flow metric. Defaults to “weight”.

  • cut (float or None) – Manually assigned cutoff point.

Module contents