Undocumented
_construct_graph_from_dataframe
Generates a graph from one or two dataframes.
_construct_graph_from_dict_dict
Constructs a graph from a dict-of-dicts representation.
_construct_graph_from_dict_list
Constructs a graph from a list-of-dictionaries representation.
_construct_graph_from_list_dict
Constructs a graph from a dict-of-lists representation.
_construct_graph_from_tuple_list
Constructs a graph from a list-of-tuples representation.
_export_edge_dataframe
Export edges with attributes to pandas.DataFrame
_export_graph_to_dict_dict
Export graph to dictionary of dicts of edge attributes
_export_graph_to_dict_list
Export graph as two lists of dictionaries, for vertices and edges.
_export_graph_to_list_dict
Export graph to a dictionary of lists (or other sequences).
_export_graph_to_tuple_list
Export graph to a list of edge tuples
_export_vertex_dataframe
Export vertices with attributes to pandas.DataFrame
Generates a graph from one or two dataframes.
bool whether the graph is directedbool whether to interpret the first two columns of the edges argument as vertex ids (0-based integers) instead of vertex names. If this argument is set to True and the first two columns of edges are not integers, an error is thrown.the graph
Vertex names in either the edges or vertices arguments that are set to NaN (not a number) will be set to the string "NA". That might lead to unexpected behaviour: fill your NaNs with values before calling this function to mitigate.
Constructs a graph from a dict-of-dicts representation.
Each key can be an integer or a string and represent a vertex. Each value is a dict representing edges (outgoing if the graph is directed) from that vertex. Each dict key is an integer/string for a target vertex, such that an edge will be created between those two vertices. Integers are interpreted as vertex_ids from 0 (as used in igraph), strings are interpreted as vertex names, in which case vertices are given separate numeric ids. Each value is a dictionary of edge attributes for that edge.
Example:
>>> {'Alice': {'Bob': {'weight': 1.5}, 'David': {'weight': 2}}}
creates a graph with three vertices (Alice, Bob, and David) and two edges:
- Alice - Bob (with weight 1.5)
- Alice - David (with weight 2)
bool whether to create a directed graphstr UndocumentedConstructs 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.
bool whether the constructed graph will be directedstr the name of the distinguished key in the dicts in the vertex data source that contains the vertex names. Ignored if vertices is None.bool whether 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.the 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.
Constructs a graph from a dict-of-lists representation.
This function is used to construct a graph from a dictionary of lists. Other, non-list sequences (e.g. tuples) and lazy iterators are are accepted. For each key x, its corresponding value must be a sequence of multiple values y: the edge (x,y) will be created in the graph. x and y must be either one of:
- two integers: the vertices with those ids will be connected
- two strings: the vertices with those names will be connected
If names are used, the order of vertices is not guaranteed, and each vertex will be given the vertex_name_attr attribute.
bool whether to create a directed graphstr Undocumenteda Graph object
Example:
>>> mydict = {'apple': ['pear', 'peach'], 'pear': ['peach']} >>> g = Graph.ListDict(mydict)
# The graph has three vertices with names and three edges connecting # each pair.
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.
bool whether the constructed graph will be directedstr the name of the vertex attribute that will contain the vertex names.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
Export graph to dictionary of dicts of edge attributes
This function is the reverse of Graph.DictDict.
Example:
>>> g = Graph.Full(3) >>> g.es['name'] = ['first_edge', 'second', 'third'] >>> g.to_dict_dict() {0: {1: {'name': 'first_edge'}, 2: {'name': 'second'}}, 1: {2: {'name': 'third'}}}
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.Union[str, Sequence[str]] list of edge attributes to export. None (default) signified all attributes (unlike Graph.to_tuple_list). A string is acceptable to signify a single attribute and will be wrapped in a list internally.bool whether to skip, for each edge, attributes that have a value of None. This is useful if only some edges are expected to possess an attribute.str UndocumentedExport graph as two lists of dictionaries, for vertices and edges.
This function is the reverse of Graph.DictList.
Example:
>>> g = Graph([(0, 1), (1, 2)]) >>> g.vs["name"] = ["apple", "pear", "peach"] >>> g.es["name"] = ["first_edge", "second"]
>>> g.to_dict_list() ([{"name": "apple"}, {"name": "pear"}, {"name": "peach"}], [{"source": 0, "target": 1, "name": "first_edge"}, {"source" 0, "target": 2, name": "second"}])
>>> g.to_dict_list(use_vids=False) ([{"name": "apple"}, {"name": "pear"}, {"name": "peach"}], [{"source": "apple", "target": "pear", "name": "first_edge"}, {"source" "apple", "target": "peach", name": "second"}])
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.bool whether to skip, for each edge, attributes that have a value of None. This is useful if only some edges are expected to possess an attribute.str UndocumentedExport graph to a dictionary of lists (or other sequences).
This function is the reverse of Graph.ListDict.
Example:
>>> g = Graph.Full(3) >>> g.to_sequence_dict() -> {0: [1, 2], 1: [2]} >>> g.to_sequence_dict(sequence_constructor=tuple) -> {0: (1, 2), 1: (2,)} >>> g.vs['name'] = ['apple', 'pear', 'peach'] >>> g.to_sequence_dict(use_vids=False) {'apple': ['pear', 'peach'], 'pear': ['peach']}
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.callable constructor for the data structure to be used as values of the dictionary. The default (list) makes a dict of lists, with each list representing the neighbors of the vertex specified in the respective dictionary key.str UndocumentedExport graph to a list of edge tuples
This function is the reverse of Graph.TupleList.
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")]
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.Union[str, Sequence[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.str UndocumentedExport 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)