finds.recipes.graph

Graph network convenience wrappers

  • iterate over Graph edges and attributes:

for edge_tuple, attributes_dict in G.edges.items()

  • iterate over Graph nodes and attributes:

for node, attributes_dict in G.nodes.items()

  • neighbors of a Node:

nx.all_neighgors nx.neighbors list(g[1004]) G.degree[1004]

  • populate a graph:

G = nx.Graph(social_pairs) G = nx.DiGraph(social_pairs) G.add_nodes_from(vertices) G.add_edges_from(edges)

Copyright 2022-2024, Terence Lim

MIT License

finds.recipes.graph.community_detection(G: Graph, methods: List[str] = [], weight: str | None = None, resolution: float = 1.0, seed: float | None = None, verbose: int = 1) Dict[str, List[List]][source]

Run built-in community detection algorithms on an undirected graph

Parameters:
  • G – Undirected networkx Graph

  • methods – Algorithms to run, in {‘label’, ‘louvain’, ‘greedy’}

  • weight – Name of edge attribute for weights

  • resolution – If less than 1, favors large communities.

  • seed

Returns:

Dictionary, keyed by algorithm, of community lists

finds.recipes.graph.community_quality(G: Graph, communities: List[List], methods: List[str] = []) Dict[str, float | int][source]

Run built-in community performance metrics

Parameters:
  • G – Undirected networkx Graph

  • communties – Communities list of lists

  • methods – Metrics to compute, in {‘modularity’, ‘quality’}

Returns:

Dictionary, keyed by metric label, of metric values

finds.recipes.graph.graph_draw(G: Graph, num: int = 1, figsize: Tuple[float, float] = (10, 10), savefig: str = '', title: str = '', font_weight: str = 'bold', font_size: int = 8, font_family: str = 'helvetica', k: float = 2.0, pos: Callable | Dict = {}, center_name: Any = None, alpha: float = 0.5, arrowsize: float = 10.0, arrowstyle: str = '-|>', style: str = ':', width: float = 0.5, edge_color: str = 'r', nodelist: List = [], node_scale: float = 1.0, node_size: float | Dict | List = 20.0, node_color: str | Dict | List = '#1f78b4', labels: List | Dict = None, seed: int | None = None, **kwargs)[source]

Convenience wrapper over nx.draw_network

Parameters:
  • G – Directed or undirected graph to draw

  • savefig – JPG filename to save as

  • figsize – Figure size in inches

  • title – Text to display in top left of plot

  • font_weight – {‘normal’, bold’ ‘light’}

  • k – Extent of separatation between nodes

  • center_name – Node to place in center of plot

  • pos – Layout dict or callable, default uses nx.spring_layout()

  • arrowsize – Size of arrow head to draw edges

  • arrowstyle – Format of arrow to draw

  • width – Width of arrow

  • edge_color – Color of edge arrow

  • style – Edge style in {‘-’, ‘–’, ‘-.’, ‘:’}

  • nodelist – List of nodes to draw

  • node_scale – Relative scaling of nodes

  • node_size – List of node size, or a lookup dict

  • node_color – List of node color, or a lookup dict

  • labels – List of node labels, or a lookup dict

  • **kwargs – parameters passed on to networkx.draw

Returns:

Dict of pos with nodes as keys and positions as values

Other pos layouts:

  • pos = nx.circular_layout(G)

  • pos = nx.spectral_layout(G)

  • pos = nx.kamada_kawai_layout(G)

  • pos = nx.fruchterman_reingold_layout(G)

  • pos = nx.spring_layout(G)

finds.recipes.graph.graph_info(G, fast=True)[source]

Return summary of graph properties

Parameters:

fast – True to skip slow triadic census, clustering and distance measures

Returns:

Dict of property name and value

Run link prediction algorihms

Returns:

edge-and-score for all nonexistent edges in graph, by algorithm

finds.recipes.graph.nodes_centrality(G, weight='weight', cost=False, alpha=0.99)[source]

Return dict of vertex centrality measures

Parameters:
  • G – Graph may be directed or indirected, weighted or unweighted

  • weight – name of edge attribute for weights, Set to None for unweighted

  • cost – If True, then weights are costs; else weights are importances

Returns:

score (float)}

Return type:

Dict of {label (str)

Notes:

  • centrality: degree, eigenvector, closeness, betweenness

  • link_analysis: pagerank, hits

if weight is cost: ‘eigenvector’, ‘pagerank’, ‘hub’, ‘authority’ ignore weights

if weight is not cost: ‘betweenness’, ‘closeness’ ignore weights