DiGraph

Overview

class nx_arangodb.DiGraph(incoming_graph_data: Any = None, name: str | None = None, default_node_type: str | None = None, edge_type_key: str = '_edge_type', edge_type_func: Callable[[str, str], str] | None = None, edge_collections_attributes: set[str] | None = None, db: StandardDatabase | None = None, read_parallelism: int = 10, read_batch_size: int = 100000, write_batch_size: int = 50000, write_async: bool = False, symmetrize_edges: bool = False, use_arango_views: bool = False, overwrite_graph: bool = False, *args: Any, **kwargs: Any)[source]

Base class for directed graphs.

Subclasses nxadb.Graph and nx.DiGraph.

In order to connect to an ArangoDB instance, the following environment variables must be set:

  1. DATABASE_HOST

  2. DATABASE_USERNAME

  3. DATABASE_PASSWORD

  4. DATABASE_NAME

Furthermore, the name parameter is required to create a new graph or to connect to an existing graph in the database.

Example

>>> import os
>>> import networkx as nx
>>> import nx_arangodb as nxadb
>>>
>>> os.environ["DATABASE_HOST"] = "http://localhost:8529"
>>> os.environ["DATABASE_USERNAME"] = "root"
>>> os.environ["DATABASE_PASSWORD"] = "openSesame"
>>> os.environ["DATABASE_NAME"] = "_system"
>>>
>>> G = nxadb.DiGraph(name="MyGraph")
>>> ...

Parameters

incoming_graph_datainput graph (optional, default: None)

Data to initialize graph. If None (default) an empty graph is created. Must be used in conjunction with name if the user wants to persist the graph in ArangoDB. NOTE: It is recommended for incoming_graph_data to be a NetworkX graph due to faster loading times.

namestr (optional, default: None)

Name of the graph in the database. If the graph already exists, the user can pass the name of the graph to connect to it. If the graph does not exist, a General Graph will be created by passing the name. NOTE: Must be used in conjunction with incoming_graph_data if the user wants to persist the graph in ArangoDB.

default_node_typestr (optional, default: None)

Default node type for the graph. In ArangoDB terms, this is the default vertex collection. If the graph already exists, the user can omit this parameter and the default node type will be set to the first vertex collection in the graph. If the graph does not exist, the user can pass the default node type to create the default vertex collection.

edge_type_keystr (optional, default: “_edge_type”)

Key used to store the edge type when inserting edges into the graph. Useful for working with Heterogeneous Graphs.

edge_type_funcCallable[[str, str], str] (optional, default: None)

Function to determine the edge type between two nodes. If the graph already exists, the user can omit this parameter and the edge type function will be set based on the existing edge definitions. If the graph does not exist, the user can pass a function that determines the edge type between two nodes.

edge_collections_attributesset[str] (optional, default: None)

Set of edge attributes to fetch when executing a NetworkX algorithm. Useful if the user has edge weights or other edge attributes that they want to use in a NetworkX algorithm.

dbarango.database.StandardDatabase (optional, default: None)

ArangoDB database object. If the user has an existing python-arango connection to the database, they can pass the database object to the graph. If not provided, a database object will be created using the environment variables DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, and DATABASE_NAME.

read_parallelismint (optional, default: 10)

Number of parallel threads to use when reading data from ArangoDB. Used for fetching node and edge data from the database.

read_batch_sizeint (optional, default: 100000)

Number of documents to fetch in a single batch when reading data from ArangoDB. Used for fetching node and edge data from the database.

write_batch_sizeint (optional, default: 50000)

Number of documents to insert in a single batch when writing data to ArangoDB. Used for inserting node and edge data into the database if and only if incoming_graph_data is a NetworkX graph.

write_asyncbool (optional, default: False)

Whether to insert data into ArangoDB asynchronously. Used for inserting node and edge data into the database if and only if incoming_graph_data is a NetworkX graph.

symmetrize_edgesbool (optional, default: False)

Whether to symmetrize the edges in the graph when fetched from the database. Only applies to directed graphs, thereby converting them to undirected graphs.

use_arango_viewsbool (optional, default: False)

Whether to use experimental work-in-progress ArangoDB Views for the nodes, adjacency list, and edges. These views are designed to improve data processing performance by delegating CRUD operations to the database whenever possible. NOTE: This feature is experimental and may not work as expected.

overwrite_graphbool (optional, default: False)

Whether to overwrite the graph in the database if it already exists. If set to True, the graph collections will be dropped and recreated. Note that this operation is irreversible and will result in the loss of all data in the graph. NOTE: If set to True, Collection Indexes will also be lost.

args: positional arguments for nx.Graph

Additional arguments passed to nx.Graph.

kwargs: keyword arguments for nx.Graph

Additional arguments passed to nx.Graph.

query(query: str, bind_vars: dict[str, Any] = {}, **kwargs: Any) Cursor

Execute an AQL query on the graph.

Read more about AQL here: https://www.arangodb.com/docs/stable/aql/

Parameters

querystr

AQL query to execute.

bind_varsdict[str, Any] (optional, default: {})

Bind variables to pass to the query.

kwargsdict[str, Any]

Additional keyword arguments to pass to the query.

Returns

arango.cursor.Cursor

Cursor object containing the results of the query.

chat(prompt: str, verbose: bool = False, llm: BaseLanguageModel | None = None) str

Chat with the graph using an LLM. Use at your own risk.

Parameters

promptstr

Prompt to chat with the graph.

verbosebool (optional, default: False)

Whether to print the intermediate steps of the conversation.

llmlangchain_core.language_models.BaseLanguageModel (optional, default: None)

Language model to use for the conversation. If None, the default language model is ChatOpenAI with the GPT-4 model, which expects the OpenAI API key to be set in the environment variable OPENAI_API_KEY.

Returns

str

Response from the Language Model.

Methods

Adding and removing nodes and edges

DiGraph.__init__([incoming_graph_data, ...])

Initialize a graph with edges, name, or graph attributes.

DiGraph.add_node(node_for_adding, **attr)

Add a single node node_for_adding and update node attributes.

DiGraph.add_nodes_from(nodes_for_adding, **attr)

Add multiple nodes.

DiGraph.remove_node(n)

Remove node n.

DiGraph.remove_nodes_from(nodes)

Remove multiple nodes.

DiGraph.add_edge(u_of_edge, v_of_edge, **attr)

Add an edge between u and v.

DiGraph.add_edges_from(ebunch_to_add, **attr)

Add all the edges in ebunch_to_add.

DiGraph.add_weighted_edges_from(ebunch_to_add)

Add weighted edges in ebunch_to_add with specified weight attr

DiGraph.remove_edge(u, v)

Remove the edge between u and v.

DiGraph.remove_edges_from(ebunch)

Remove all edges specified in ebunch.

DiGraph.update([edges, nodes])

Update the graph using nodes/edges/graphs as input.

DiGraph.clear()

Remove all nodes and edges from the graph.

DiGraph.clear_edges()

Remove all edges from the graph without altering nodes.

Reporting nodes edges and neighbors

DiGraph.nodes

A NodeView of the Graph as G.nodes or G.nodes().

DiGraph.__iter__()

Iterate over the nodes.

DiGraph.has_node(n)

Returns True if the graph contains the node n.

DiGraph.__contains__(n)

Returns True if n is a node, False otherwise.

DiGraph.edges

An OutEdgeView of the DiGraph as G.edges or G.edges().

DiGraph.out_edges

An OutEdgeView of the DiGraph as G.edges or G.edges().

DiGraph.in_edges

A view of the in edges of the graph as G.in_edges or G.in_edges().

DiGraph.has_edge(u, v)

Returns True if the edge (u, v) is in the graph.

DiGraph.get_edge_data(u, v[, default])

Returns the attribute dictionary associated with edge (u, v).

DiGraph.neighbors(n)

Returns an iterator over successor nodes of n.

DiGraph.adj

Graph adjacency object holding the neighbors of each node.

DiGraph.__getitem__(n)

Returns a dict of neighbors of node n.

DiGraph.successors(n)

Returns an iterator over successor nodes of n.

DiGraph.succ

Graph adjacency object holding the successors of each node.

DiGraph.predecessors(n)

Returns an iterator over predecessor nodes of n.

DiGraph.pred

Graph adjacency object holding the predecessors of each node.

DiGraph.adjacency()

Returns an iterator over (node, adjacency dict) tuples for all nodes.

DiGraph.nbunch_iter([nbunch])

Returns an iterator over nodes contained in nbunch that are also in the graph.

Counting nodes edges and neighbors

DiGraph.order()

Returns the number of nodes in the graph.

DiGraph.number_of_nodes()

Returns the number of nodes in the graph.

DiGraph.__len__()

Returns the number of nodes in the graph.

DiGraph.degree

A DegreeView for the Graph as G.degree or G.degree().

DiGraph.in_degree

An InDegreeView for (node, in_degree) or in_degree for single node.

DiGraph.out_degree

An OutDegreeView for (node, out_degree)

DiGraph.size([weight])

Returns the number of edges or total of all edge weights.

DiGraph.number_of_edges([u, v])

Returns the number of edges between two nodes.

Making copies and subgraphs

DiGraph.copy([as_view])

Returns a copy of the graph.

DiGraph.to_undirected([reciprocal, as_view])

Returns an undirected representation of the digraph.

DiGraph.to_directed([as_view])

Returns a directed representation of the graph.

DiGraph.subgraph(nodes)

Returns a SubGraph view of the subgraph induced on nodes.

DiGraph.edge_subgraph(edges)

Returns the subgraph induced by the specified edges.

DiGraph.reverse([copy])

Returns the reverse of the graph.