igraph#
igraph is a popular graph library that PyGraphistry can interface with. This allows you to leverage igraph’s layout algorithms, and optionally, algorithmic enrichments. It is CPU-based and can generally handle small/medium-sized graphs.
- graphistry.plugins.igraph.compute_igraph(self, alg, out_col=None, directed=None, use_vids=False, params={}, stringify_rich_types=True)#
Enrich or replace graph using igraph methods
- Parameters:
alg (str) – Name of an igraph.Graph method like pagerank
out_col (Optional[str]) – For algorithms that generate a node attribute column, out_col is the desired output column name. When None, use the algorithm’s name. (default None)
directed (Optional[bool]) – During the to_igraph conversion, whether to be directed. If None, try directed and then undirected. (default None)
use_vids (bool) – During the to_igraph conversion, whether to interpret IDs as igraph vertex IDs (non-negative integers) or arbitrary values (False, default)
params (dict) – Any named parameters to pass to the underlying igraph method
stringify_rich_types (bool) – When rich types like igraph.Graph are returned, which may be problematic for downstream rendering, coerce them to strings
self (Plottable)
- Returns:
Plotter
- Return type:
Plotter
- Example: Pagerank
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['c','c','e','e']}) g = graphistry.edges(edges, 's', 'd').materialize_nodes() g2 = g.compute_igraph('pagerank') assert 'pagerank' in g2._nodes.columns
- Example: Pagerank with custom name
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['c','c','e','e']}) g = graphistry.edges(edges, 's', 'd').materialize_nodes() g2 = g.compute_igraph('pagerank', out_col='my_pr') assert 'my_pr' in g2._nodes.columns
- Example: Pagerank on an undirected
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['c','c','e','e']}) g = graphistry.edges(edges, 's', 'd').materialize_nodes() g2 = g.compute_igraph('pagerank', directed=False) assert 'pagerank' in g2._nodes.columns
- Example: Pagerank with custom parameters
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['c','c','e','e']}) g = graphistry.edges(edges, 's', 'd').materialize_nodes() g2 = g.compute_igraph('pagerank', params={'damping': 0.85}) assert 'pagerank' in g2._nodes.columns
- Example: Personalized Pagerank
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['c','c','e','e']}) g = graphistry.edges(edges, 's', 'd').materialize_nodes() g2 = g.compute_igraph('personalized_pagerank') assert 'personalized_pagerank' in g2._nodes.columns
- graphistry.plugins.igraph.from_igraph(self, ig, node_attributes=None, edge_attributes=None, load_nodes=True, load_edges=True, merge_if_existing=True)#
Convert igraph object into Plotter
If base g has _node, _source, _destination definitions, use them
When merge_if_existing with preexisting nodes/edges df and shapes match ig, combine attributes
For merge_if_existing to work with edges, must set g._edge and have corresponding edge index attribute in igraph.Graph
- Parameters:
ig (igraph) – Source igraph object
node_attributes (Optional[List[str]]) – Subset of node attributes to load; None means all (default)
edge_attributes (Optional[List[str]]) – Subset of edge attributes to load; None means all (default)
load_nodes (bool) – Whether to load nodes dataframe (default True)
load_edges (bool) – Whether to load edges dataframe (default True)
merge_if_existing (bool) – Whether to merge with existing node/edge dataframes (default True)
merge_if_existing – bool
- Returns:
Plotter
- Return type:
- Example: Convert from igraph, including all node/edge properties
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a', 'b', 'c', 'd'], 'd': ['b', 'c', 'd', 'e'], 'v': [101, 102, 103, 104]}) g = graphistry.edges(edges, 's', 'd').materialize_nodes().get_degrees() assert 'degree' in g._nodes.columns g2 = g.from_igraph(g.to_igraph()) assert len(g2._nodes.columns) == len(g._nodes.columns)
- Example: Enrich from igraph, but only load in 1 node attribute
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a', 'b', 'c', 'd'], 'd': ['b', 'c', 'd', 'e'], 'v': [101, 102, 103, 104]}) g = graphistry.edges(edges, 's', 'd').materialize_nodes().get_degree() assert 'degree' in g._nodes ig = g.to_igraph(include_nodes=False) assert 'degree' not in ig.vs ig.vs['pagerank'] = ig.pagerank() g2 = g.from_igraph(ig, load_edges=False, node_attributes=[g._node, 'pagerank']) assert 'pagerank' in g2._nodes asssert 'degree' in g2._nodes
- graphistry.plugins.igraph.layout_igraph(self, layout, directed=None, use_vids=False, bind_position=True, x_out_col='x', y_out_col='y', play=0, params={})#
Compute graph layout using igraph algorithm. For a list of layouts, see layout_algs or igraph documentation.
- Parameters:
layout (str) – Name of an igraph.Graph.layout method like sugiyama
directed (Optional[bool]) – During the to_igraph conversion, whether to be directed. If None, try directed and then undirected. (default None)
use_vids (bool) – Whether to use igraph vertex ids (non-negative integers) or arbitary node ids (False, default)
bind_position (bool) – Whether to call bind(point_x=, point_y=) (default True)
x_out_col (str) – Attribute to write x position to. (default ‘x’)
y_out_col (str) – Attribute to write x position to. (default ‘y’)
play (Optional[str]) – If defined, set settings(url_params={‘play’: play}). (default 0)
params (dict) – Any named parameters to pass to the underlying igraph method
self (Plottable)
- Returns:
Plotter
- Return type:
Plotter
- Example: Sugiyama layout
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['b','c','d','e']}) g = graphistry.edges(edges, 's', 'd') g2 = g.layout_igraph('sugiyama') assert 'x' in g2._nodes g2.plot()
- Example: Change which column names are generated
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['b','c','d','e']}) g = graphistry.edges(edges, 's', 'd') g2 = g.layout_igraph('sugiyama', x_out_col='my_x', y_out_col='my_y') assert 'my_x' in g2._nodes assert g2._point_x == 'my_x' g2.plot()
- Example: Pass parameters to layout methods - Sort nodes by degree
import graphistry, pandas as pd edges = pd.DataFrame({'s': ['a','b','c','d'], 'd': ['b','c','d','e']}) g = graphistry.edges(edges, 's', 'd') g2 = g.get_degrees() assert 'degree' in g._nodes.columns g3 = g.layout_igraph('sugiyama', params={'layers': 'degree'}) g3.plot()
- graphistry.plugins.igraph.to_igraph(self, directed=True, include_nodes=True, node_attributes=None, edge_attributes=None, use_vids=False)#
Convert current item to igraph Graph . See examples in from_igraph.
- Parameters:
directed (bool) – Whether to create a directed graph (default True)
include_nodes (bool) – Whether to ingest the nodes table, if it exists (default True)
node_attributes (Optional[List[str]]) – Which node attributes to load, None means all (default None)
edge_attributes (Optional[List[str]]) – Which edge attributes to load, None means all (default None)
use_vids (bool) – Whether to interpret IDs as igraph vertex IDs, which must be non-negative integers (default False)
self (Plottable)
Constants
- graphistry.plugins.igraph.compute_algs: List[str] = ['articulation_points', 'authority_score', 'betweenness', 'bibcoupling', 'harmonic_centrality', 'closeness', 'clusters', 'cocitation', 'community_edge_betweenness', 'community_fastgreedy', 'community_infomap', 'community_label_propagation', 'community_leading_eigenvector', 'community_leiden', 'community_multilevel', 'community_optimal_modularity', 'community_spinglass', 'community_walktrap', 'constraint', 'coreness', 'gomory_hu_tree', 'harmonic_centrality', 'hub_score', 'eccentricity', 'eigenvector_centrality', 'k_core', 'pagerank', 'personalized_pagerank', 'spanning_tree']#
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
- graphistry.plugins.igraph.layout_algs: List[str] = ['auto', 'automatic', 'bipartite', 'circle', 'circular', 'dh', 'davidson_harel', 'drl', 'drl_3d', 'fr', 'fruchterman_reingold', 'fr_3d', 'fr3d', 'fruchterman_reingold_3d', 'grid', 'grid_3d', 'graphopt', 'kk', 'kamada_kawai', 'kk_3d', 'kk3d', 'kamada_kawai_3d', 'lgl', 'large', 'large_graph', 'mds', 'random', 'random_3d', 'rt', 'tree', 'reingold_tilford', 'rt_circular', 'reingold_tilford_circular', 'sphere', 'spherical', 'circle_3d', 'circular_3d', 'star', 'sugiyama']#
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.