python-igraph API reference

List of all classes, functions and methods in python-igraph

module documentation

Undocumented

Function _construct_graph_from_dict_list Constructs a graph from a list-of-dictionaries representation.
Function _construct_graph_from_tuple_list Constructs a graph from a list-of-tuples representation.
Function _construct_graph_from_list_dict Constructs a graph from a dict-of-lists representation.
Function _construct_graph_from_dict_dict Constructs a graph from a dict-of-dicts representation.
Function _construct_graph_from_dataframe Generates a graph from one or two dataframes.
Function _export_graph_to_dict_list Export graph as two lists of dictionaries, for vertices and edges.
Function _export_graph_to_tuple_list Export graph to a list of edge tuples
Function _export_graph_to_list_dict Export graph to a dictionary of lists (or other sequences).
Function _export_graph_to_dict_dict Export graph to dictionary of dicts of edge attributes
Function _export_vertex_dataframe Export vertices with attributes to pandas.DataFrame
Function _export_edge_dataframe Export edges with attributes to pandas.DataFrame
def _construct_graph_from_dict_list(cls, vertices, edges, directed=False, vertex_name_attr='name', edge_foreign_keys=('source', 'target'), iterative=False):

Constructs a graph from a list-of-dictionaries representation.

This function is useful when you have two lists of dictionaries, one for vertices and one for edges, each containing their attributes (e.g. name, weight). Of course, the edge dictionary must also contain two special keys that indicate the source and target vertices connected by that edge. Non-list iterables should work as long as they yield dictionaries or dict-like objects (they should have the 'items' and '__getitem__' methods). For instance, a database query result is likely to be fit as long as it's iterable and yields dict-like objects with every iteration.

Parametersverticesthe list of dictionaries for the vertices or None if there are no special attributes assigned to vertices and we should simply use the edge list of dicts to infer vertex names.
edgesthe list of dictionaries for the edges. Each dict must have at least the two keys specified by edge_foreign_keys to label the source and target vertices, while additional items will be treated as edge attributes.
directedwhether the constructed graph will be directed
vertex_name_attrthe name of the distinguished key in the dicts in the vertex data source that contains the vertex names. Ignored if vertices is None.
edge_foreign_keystuple specifying the attributes in each edge dictionary that contain the source (1st) and target (2nd) vertex names. These items of each dictionary are also added as edge_attributes.
iterativewhether to add the edges to the graph one by one, iteratively, or to build a large edge list first and use that to construct the graph. The latter approach is faster but it may not be suitable if your dataset is large. The default is to add the edges in a batch from an edge list.
Returnsthe graph that was constructed

Example:

>>> vertices = [{'name': 'apple'}, {'name': 'pear'}, {'name': 'peach'}]
>>> edges = [{'source': 'apple', 'target': 'pear', 'weight': 1.2},
...          {'source': 'apple', 'target': 'peach', 'weight': 0.9}]
>>> g = Graph.DictList(vertices, edges)

The graph has three vertices with names and two edges with weights.

def _construct_graph_from_tuple_list(cls, edges, directed=False, vertex_name_attr='name', edge_attrs=None, weights=False):

Constructs a graph from a list-of-tuples representation.

This representation assumes that the edges of the graph are encoded in a list of tuples (or lists). Each item in the list must have at least two elements, which specify the source and the target vertices of the edge. The remaining elements (if any) specify the edge attributes of that edge, where the names of the edge attributes originate from the edge_attrs list. The names of the vertices will be stored in the vertex attribute given by vertex_name_attr.

The default parameters of this function are suitable for creating unweighted graphs from lists where each item contains the source vertex and the target vertex. If you have a weighted graph, you can use items where the third item contains the weight of the edge by setting edge_attrs to "weight" or ["weight"]. If you have even more edge attributes, add them to the end of each item in the edges list and also specify the corresponding edge attribute names in edge_attrs as a list.

Parametersedgesthe data source for the edges. This must be a list where each item is a tuple (or list) containing at least two items: the name of the source and the target vertex. Note that names will be assigned to the name vertex attribute (or another vertex attribute if vertex_name_attr is specified), even if all the vertex names in the list are in fact numbers.
directedwhether the constructed graph will be directed
vertex_name_attrthe name of the vertex attribute that will contain the vertex names.
edge_attrsthe names of the edge attributes that are filled with the extra items in the edge list (starting from index 2, since the first two items are the source and target vertices). If None or an empty sequence, only the source and target vertices will be extracted and additional tuple items will be ignored. If a string, it is interpreted as a single edge attribute.
weightsalternative way to specify that the graph is weighted. If you set weights to true and edge_attrs is not given, it will be assumed that edge_attrs is ["weight"] and igraph will parse the third element from each item into an edge weight. If you set weights to a string, it will be assumed that edge_attrs contains that string only, and igraph will store the edge weights in that attribute.
Returnsthe graph that was constructed
def _construct_graph_from_list_dict(cls, edges, directed=False, vertex_name_attr='name'):
def _construct_graph_from_dict_dict(cls, edges, directed=False, vertex_name_attr='name'):
def _construct_graph_from_dataframe(cls, edges, directed=True, vertices=None, use_vids=True):
def _export_graph_to_dict_list(graph, use_vids=True, skip_none=False, vertex_name_attr='name'):
def _export_graph_to_tuple_list(graph, use_vids=True, edge_attrs=None, vertex_name_attr='name'):

Export graph to a list of edge tuples

This function is the reverse of Graph.TupleList.

ParametersgraphUndocumented
use_vidsUndocumented
edge_attrsUndocumented
vertex_name_attrUndocumented
use_vids (bool)whether to label vertices in the output data structure by their ids or their vertex_name_attr attribute. If use_vids=False but vertices lack a vertex_name_attr attribute, an AttributeError is raised.
edge_attrs (str or list of str)list of edge attributes to export in addition to source and target vertex, which are always the first two elements of each tuple. None (default) is equivalent to an empty list. A string is acceptable to signify a single attribute and will be wrapped in a list internally.
Returnsa list of tuples, each representing an edge of the graph.

Example:

>>> g = Graph.Full(3)
>>> g.vs["name"] = ["apple", "pear", "peach"]
>>> g.es["name"] = ["first_edge", "second", "third"]

# Get name of the edge >>> g.to_tuple_list(edge_attrs=["name"]) [(0, 1, "first_edge"), (0, 2, "second"), (1, 2, "third")]

# Use vertex names, no edge attributes >>> g.to_tuple_list(use_vids=False) [("apple", "pear"), ("apple", "peach"), ("pear", "peach")]

Unknown Field: vertex_name_attr(str)only used with use_vids=False to choose what vertex attribute to use to name your vertices in the output data structure.
def _export_graph_to_list_dict(graph, use_vids=True, sequence_constructor=list, vertex_name_attr='name'):
def _export_graph_to_dict_dict(graph, use_vids=True, edge_attrs=None, skip_none=False, vertex_name_attr='name'):
def _export_vertex_dataframe(graph):

Export vertices with attributes to pandas.DataFrame

If you want to use vertex names as index, you can do:

>>> from string import ascii_letters
>>> graph = Graph.GRG(25, 0.4)
>>> graph.vs["name"] = ascii_letters[:graph.vcount()]
>>> df = graph.get_vertex_dataframe()
>>> df.set_index('name', inplace=True)
Returnsa pandas.DataFrame representing vertices and their attributes. The index uses vertex IDs, from 0 to N - 1 where N is the number of vertices.
def _export_edge_dataframe(graph):

Export edges with attributes to pandas.DataFrame

If you want to use source and target vertex IDs as index, you can do:

>>> from string import ascii_letters
>>> graph = Graph.GRG(25, 0.4)
>>> graph.vs["name"] = ascii_letters[:graph.vcount()]
>>> df = graph.get_edge_dataframe()
>>> df.set_index(['source', 'target'], inplace=True)

The index will be a pandas.MultiIndex. You can use the `drop=False` option to keep the `source` and `target` columns.

If you want to use vertex names in the source and target columns:

>>> df = graph.get_edge_dataframe()
>>> df_vert = graph.get_vertex_dataframe()
>>> df['source'].replace(df_vert['name'], inplace=True)
>>> df['target'].replace(df_vert['name'], inplace=True)
>>> df_vert.set_index('name', inplace=True)  # Optional
Returnsa pandas.DataFrame representing edges and their attributes. The index uses edge IDs, from 0 to M - 1 where M is the number of edges. The first two columns of the dataframe represent the IDs of source and target vertices for each edge. These columns have names "source" and "target". If your edges have attributes with the same names, they will be present in the dataframe, but not in the first two columns.
API Documentation for python-igraph, generated by pydoctor 21.2.2 at 2021-10-18 16:23:19.