MultiDiGraph
Overview
- class nx_arangodb.MultiDiGraph(incoming_graph_data: Any = None, multigraph_input: bool | None = 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]
A directed graph class that can store multiedges.
Subclasses
nxadb.MultiGraph,nxadb.Digraph, andnx.MultiGraph.In order to connect to an ArangoDB instance, the following environment variables must be set:
DATABASE_HOSTDATABASE_USERNAMEDATABASE_PASSWORDDATABASE_NAME
Furthermore, the
nameparameter 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.
- multigraph_inputbool or None (default None)
Note: Only used when incoming_graph_data is a dict. If True, incoming_graph_data is assumed to be a dict-of-dict-of-dict-of-dict structure keyed by node to neighbor to edge keys to edge data for multi-edges. A NetworkXError is raised if this is not the case. If False,
to_networkx_graph()is used to try to determine the dict’s graph data structure as either a dict-of-dict-of-dict keyed by node to neighbor to edge data, or a dict-of-iterable keyed by node to neighbors. If None, the treatment for True is tried, but if it fails, the treatment for False is tried.- 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
|
Initialize a graph with edges, name, or graph attributes. |
|
Add a single node node_for_adding and update node attributes. |
Add multiple nodes. |
|
Remove node n. |
|
Remove multiple nodes. |
|
|
Add an edge between u and v. |
|
Add all the edges in ebunch_to_add. |
|
Add weighted edges in ebunch_to_add with specified weight attr |
Returns an unused key for edges between nodes u and v. |
|
|
Remove an edge between u and v. |
|
Remove all edges specified in ebunch. |
|
Update the graph using nodes/edges/graphs as input. |
Remove all nodes and edges from the graph. |
|
Remove all edges from the graph without altering nodes. |
Reporting nodes edges and neighbors
A NodeView of the Graph as G.nodes or G.nodes(). |
|
Iterate over the nodes. |
|
Returns True if the graph contains the node n. |
|
Returns True if n is a node, False otherwise. |
|
An OutMultiEdgeView of the Graph as G.edges or G.edges(). |
|
An OutMultiEdgeView of the Graph as G.edges or G.edges(). |
|
A view of the in edges of the graph as G.in_edges or G.in_edges(). |
|
|
Returns True if the graph has an edge between nodes u and v. |
|
Returns the attribute dictionary associated with edge (u, v, key). |
Returns an iterator over successor nodes of n. |
|
Graph adjacency object holding the neighbors of each node. |
|
Returns a dict of neighbors of node n. |
|
Returns an iterator over successor nodes of n. |
|
Graph adjacency object holding the successors of each node. |
|
Returns an iterator over predecessor nodes of n. |
|
Graph adjacency object holding the predecessors of each node. |
|
Returns an iterator over (node, adjacency dict) tuples for all nodes. |
|
|
Returns an iterator over nodes contained in nbunch that are also in the graph. |
Counting nodes edges and neighbors
Returns the number of nodes in the graph. |
|
Returns the number of nodes in the graph. |
|
Returns the number of nodes in the graph. |
|
A DegreeView for the Graph as G.degree or G.degree(). |
|
A DegreeView for (node, in_degree) or in_degree for single node. |
|
Returns an iterator for (node, out-degree) or out-degree for single node. |
|
|
Returns the number of edges or total of all edge weights. |
|
Returns the number of edges between two nodes. |
Making copies and subgraphs
|
Returns a copy of the graph. |
|
Returns an undirected representation of the digraph. |
|
Returns a directed representation of the graph. |
|
Returns a SubGraph view of the subgraph induced on nodes. |
|
Returns the subgraph induced by the specified edges. |
|
Returns the reverse of the graph. |