Skip to content

PersistentGraph #

Bases: GraphView

A temporal graph that allows edges and nodes to be deleted.

__new__() #

Create and return a new object. See help(type) for accurate signature.

__reduce__() #

add_edge(timestamp, src, dst, properties=None, layer=None, secondary_index=None) #

Adds a new edge with the given source and destination nodes and properties to the graph.

Parameters:

Name Type Description Default
timestamp int

The timestamp of the edge.

required
src str | int

The id of the source node.

required
dst str | int

The id of the destination node.

required
properties PropInput

The properties of the edge, as a dict of string and properties

None
layer str

The layer of the edge.

None
secondary_index int

The optional integer which will be used as a secondary index

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

add_metadata(metadata) #

Adds metadata to the graph.

Parameters:

Name Type Description Default
metadata dict

The static properties of the graph.

required

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

add_node(timestamp, id, properties=None, node_type=None, secondary_index=None) #

Adds a new node with the given id and properties to the graph.

Parameters:

Name Type Description Default
timestamp TimeInput

The timestamp of the node.

required
id str | int

The id of the node.

required
properties PropInput

The properties of the node.

None
node_type (str, optional)

The optional string which will be used as a node type

required
secondary_index int

The optional integer which will be used as a secondary index

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

add_properties(timestamp, properties, secondary_index=None) #

Adds properties to the graph.

Parameters:

Name Type Description Default
timestamp TimeInput

The timestamp of the temporal property.

required
properties dict

The temporal properties of the graph.

required
secondary_index int

The optional integer which will be used as a secondary index

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

cache(path) #

Write PersistentGraph to cache file and initialise the cache.

Future updates are tracked. Use write_updates to persist them to the cache file. If the file already exists its contents are overwritten.

Parameters:

Name Type Description Default
path str

The path to the cache file

required

Returns:

Type Description
None

create_index() #

Create graph index

Returns:

Type Description
None

create_index_in_ram() #

Creates a graph index in memory (RAM).

This is primarily intended for use in tests and should not be used in production environments, as the index will not be persisted to disk.

Returns:

Type Description
None

create_index_in_ram_with_spec(py_spec) #

Creates a graph index in memory (RAM) with the provided index spec.

This is primarily intended for use in tests and should not be used in production environments, as the index will not be persisted to disk.

Parameters:

Name Type Description Default
py_spec Any

The specification for the in-memory index to be created.

required

Arguments: IndexSpec: The specification for the in-memory index to be created.

Returns:

Type Description
None

create_index_with_spec(py_spec) #

Create graph index with the provided index spec. Arguments: py_spec: - The specification for the in-memory index to be created.

Returns:

Type Description
None

create_node(timestamp, id, properties=None, node_type=None, secondary_index=None) #

Creates a new node with the given id and properties to the graph. It fails if the node already exists.

Parameters:

Name Type Description Default
timestamp TimeInput

The timestamp of the node.

required
id str | int

The id of the node.

required
properties PropInput

The properties of the node.

None
node_type (str, optional)

The optional string which will be used as a node type

required
secondary_index int

The optional integer which will be used as a secondary index

None

Returns:

Type Description
MutableNode

the newly created node.

Raises:

Type Description
GraphError

If the operation fails.

delete_edge(timestamp, src, dst, layer=None, secondary_index=None) #

Deletes an edge given the timestamp, src and dst nodes and layer (optional)

Parameters:

Name Type Description Default
timestamp int

The timestamp of the edge.

required
src str | int

The id of the source node.

required
dst str | int

The id of the destination node.

required
layer str

The layer of the edge.

None
secondary_index int

The optional integer which will be used as a secondary index.

None

Returns:

Type Description
MutableEdge

The deleted edge

Raises:

Type Description
GraphError

If the operation fails.

deserialise(bytes) staticmethod #

Load PersistentGraph from serialised bytes.

Parameters:

Name Type Description Default
bytes bytes

The serialised bytes to decode

required

Returns:

Type Description
PersistentGraph

edge(src, dst) #

Gets the edge with the specified source and destination nodes

Parameters:

Name Type Description Default
src str | int

the source node id

required
dst str | int

the destination node id

required

Returns:

Type Description
Optional[MutableEdge]

The edge with the specified source and destination nodes, or None if the edge does not exist

event_graph() #

Get event graph

Returns:

Type Description
Graph

the graph with event semantics applied

get_all_node_types() #

Returns all the node types in the graph.

Returns:

Type Description
list[str]

A list of node types

import_edge(edge, merge=False) #

Import a single edge into the graph.

This function takes an edge object and an optional boolean flag. If the flag is set to true, the function will merge the import of the edge even if it already exists in the graph.

Parameters:

Name Type Description Default
edge Edge

An edge object representing the edge to be imported.

required
merge bool

An optional boolean flag indicating whether to merge the import of the edge. Defaults to False.

False

Returns:

Type Description
Edge

The imported edge.

Raises:

Type Description
GraphError

If the operation fails.

import_edge_as(edge, new_id, merge=False) #

Import a single edge into the graph with new id.

This function takes a edge object, a new edge id and an optional boolean flag. If the flag is set to true, the function will merge the import of the edge even if it already exists in the graph.

Parameters:

Name Type Description Default
edge Edge

A edge object representing the edge to be imported.

required
new_id (tuple)

The ID of the new edge. It's a tuple of the source and destination node ids.

required
merge bool

An optional boolean flag indicating whether to merge the import of the edge. Defaults to False.

False

Returns:

Type Description
Edge

The imported edge.

Raises:

Type Description
GraphError

If the operation fails.

import_edges(edges, merge=False) #

Import multiple edges into the graph.

This function takes a vector of edge objects and an optional boolean flag. If the flag is set to true, the function will merge the import of the edges even if they already exist in the graph.

Parameters:

Name Type Description Default
edges List[Edge]

A vector of edge objects representing the edges to be imported.

required
merge bool

An optional boolean flag indicating whether to merge the import of the edges. Defaults to False.

False

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

import_edges_as(edges, new_ids, merge=False) #

Import multiple edges into the graph with new ids.

This function takes a vector of edge objects, a list of new edge ids and an optional boolean flag. If the flag is set to true, the function will merge the import of the edges even if they already exist in the graph.

Parameters:

Name Type Description Default
edges List[Edge]

A vector of edge objects representing the edges to be imported.

required
new_ids list[Tuple[GID, GID]]

The new edge ids

required
merge bool

An optional boolean flag indicating whether to merge the import of the edges. Defaults to False.

False

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

import_node(node, merge=False) #

Import a single node into the graph.

This function takes a node object and an optional boolean flag. If the flag is set to true, the function will merge the import of the node even if it already exists in the graph.

Parameters:

Name Type Description Default
node Node

A node object representing the node to be imported.

required
merge bool

An optional boolean flag indicating whether to merge the import of the node. Defaults to False.

False

Returns:

Type Description
Node

A Node object if the node was successfully imported, and an error otherwise.

Raises:

Type Description
GraphError

If the operation fails.

import_node_as(node, new_id, merge=False) #

Import a single node into the graph with new id.

This function takes a node object, a new node id and an optional boolean flag. If the flag is set to true, the function will merge the import of the node even if it already exists in the graph.

Parameters:

Name Type Description Default
node Node

A node object representing the node to be imported.

required
new_id str | int

The new node id.

required
merge bool

An optional boolean flag indicating whether to merge the import of the node. Defaults to False.

False

Returns:

Type Description
Node

A Node object if the node was successfully imported, and an error otherwise.

Raises:

Type Description
GraphError

If the operation fails.

import_nodes(nodes, merge=False) #

Import multiple nodes into the graph.

This function takes a vector of node objects and an optional boolean flag. If the flag is set to true, the function will merge the import of the nodes even if they already exist in the graph.

Parameters:

Name Type Description Default
nodes List[Node]

A vector of node objects representing the nodes to be imported.

required
merge bool

An optional boolean flag indicating whether to merge the import of the nodes. Defaults to False.

False

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

import_nodes_as(nodes, new_ids, merge=False) #

Import multiple nodes into the graph with new ids.

This function takes a vector of node objects, a list of new node ids and an optional boolean flag. If the flag is set to true, the function will merge the import of the nodes even if they already exist in the graph.

Parameters:

Name Type Description Default
nodes List[Node]

A vector of node objects representing the nodes to be imported.

required
new_ids List[str | int]

A list of node IDs to use for the imported nodes.

required
merge bool

An optional boolean flag indicating whether to merge the import of the nodes. Defaults to False.

False

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_cached(path) staticmethod #

Load PersistentGraph from a file and initialise it as a cache file.

Future updates are tracked. Use write_updates to persist them to the cache file.

Parameters:

Name Type Description Default
path str

The path to the cache file

required

Returns:

Type Description
PersistentGraph

the loaded graph with initialised cache

load_edge_deletions_from_pandas(df, time, src, dst, layer=None, layer_col=None) #

Load edges deletions from a Pandas DataFrame into the graph.

Parameters:

Name Type Description Default
df DataFrame

The Pandas DataFrame containing the edges.

required
time str

The column name for the update timestamps.

required
src str

The column name for the source node ids.

required
dst str

The column name for the destination node ids.

required
layer str

A value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

None
layer_col str

The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_edge_deletions_from_parquet(parquet_path, time, src, dst, layer=None, layer_col=None) #

Load edges deletions from a Parquet file into the graph.

Parameters:

Name Type Description Default
parquet_path str

Parquet file or directory of Parquet files path containing node information.

required
src str

The column name for the source node ids.

required
dst str

The column name for the destination node ids.

required
time str

The column name for the update timestamps.

required
layer str

A value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

None
layer_col str

The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_edge_props_from_pandas(df, src, dst, metadata=None, shared_metadata=None, layer=None, layer_col=None) #

Load edge properties from a Pandas DataFrame.

Parameters:

Name Type Description Default
df DataFrame

The Pandas DataFrame containing edge information.

required
src str

The column name for the source node.

required
dst str

The column name for the destination node.

required
metadata List[str]

List of edge metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every edge. Defaults to None.

None
layer str

The edge layer name. Defaults to None.

None
layer_col str

The edge layer col name in dataframe. Defaults to None.

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_edge_props_from_parquet(parquet_path, src, dst, metadata=None, shared_metadata=None, layer=None, layer_col=None) #

Load edge properties from parquet file

Parameters:

Name Type Description Default
parquet_path str

Parquet file or directory of Parquet files path containing edge information.

required
src str

The column name for the source node.

required
dst str

The column name for the destination node.

required
metadata List[str]

List of edge metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every edge. Defaults to None.

None
layer str

The edge layer name. Defaults to None.

None
layer_col str

The edge layer col name in dataframe. Defaults to None.

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_edges_from_pandas(df, time, src, dst, properties=None, metadata=None, shared_metadata=None, layer=None, layer_col=None) #

Load edges from a Pandas DataFrame into the graph.

Parameters:

Name Type Description Default
df DataFrame

The Pandas DataFrame containing the edges.

required
time str

The column name for the update timestamps.

required
src str

The column name for the source node ids.

required
dst str

The column name for the destination node ids.

required
properties List[str]

List of edge property column names. Defaults to None.

None
metadata List[str]

List of edge metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every edge. Defaults to None.

None
layer str

A value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

None
layer_col str

The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_edges_from_parquet(parquet_path, time, src, dst, properties=None, metadata=None, shared_metadata=None, layer=None, layer_col=None) #

Load edges from a Parquet file into the graph.

Parameters:

Name Type Description Default
parquet_path str

Parquet file or directory of Parquet files path containing edges

required
time str

The column name for the update timestamps.

required
src str

The column name for the source node ids.

required
dst str

The column name for the destination node ids.

required
properties List[str]

List of edge property column names. Defaults to None.

None
metadata List[str]

List of edge metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every edge. Defaults to None.

None
layer str

A value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

None
layer_col str

The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_from_file(path) staticmethod #

Load PersistentGraph from a file.

Parameters:

Name Type Description Default
path str

The path to the file.

required

Returns:

Type Description
PersistentGraph

load_node_props_from_pandas(df, id, node_type=None, node_type_col=None, metadata=None, shared_metadata=None) #

Load node properties from a Pandas DataFrame.

Parameters:

Name Type Description Default
df DataFrame

The Pandas DataFrame containing node information.

required
id str

The column name for the node IDs.

required
node_type str

A value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

None
node_type_col str

The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

None
metadata List[str]

List of node metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every node. Defaults to None.

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_node_props_from_parquet(parquet_path, id, node_type=None, node_type_col=None, metadata=None, shared_metadata=None) #

Load node properties from a parquet file.

Parameters:

Name Type Description Default
parquet_path str

Parquet file or directory of Parquet files path containing node information.

required
id str

The column name for the node IDs.

required
node_type str

A value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

None
node_type_col str

The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

None
metadata List[str]

List of node metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every node. Defaults to None.

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_nodes_from_pandas(df, time, id, node_type=None, node_type_col=None, properties=None, metadata=None, shared_metadata=None) #

Load nodes from a Pandas DataFrame into the graph.

Parameters:

Name Type Description Default
df DataFrame

The Pandas DataFrame containing the nodes.

required
time str

The column name for the timestamps.

required
id str

The column name for the node IDs.

required
node_type str

A value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

None
node_type_col str

The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

None
properties List[str]

List of node property column names. Defaults to None.

None
metadata List[str]

List of node metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every node. Defaults to None.

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

load_nodes_from_parquet(parquet_path, time, id, node_type=None, node_type_col=None, properties=None, metadata=None, shared_metadata=None) #

Load nodes from a Parquet file into the graph.

Parameters:

Name Type Description Default
parquet_path str

Parquet file or directory of Parquet files containing the nodes

required
time str

The column name for the timestamps.

required
id str

The column name for the node IDs.

required
node_type str

A value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

None
node_type_col str

The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

None
properties List[str]

List of node property column names. Defaults to None.

None
metadata List[str]

List of node metadata column names. Defaults to None.

None
shared_metadata PropInput

A dictionary of metadata properties that will be added to every node. Defaults to None.

None

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

node(id) #

Gets the node with the specified id

Parameters:

Name Type Description Default
id str | int

the node id

required

Returns:

Type Description
Optional[MutableNode]

The node with the specified id, or None if the node does not exist

persistent_graph() #

Get persistent graph

Returns:

Type Description
PersistentGraph

the graph with persistent semantics applied

save_to_file(path) #

Saves the PersistentGraph to the given path.

Parameters:

Name Type Description Default
path str

The path to the file.

required

Returns:

Type Description
None

save_to_zip(path) #

Saves the PersistentGraph to the given path.

Parameters:

Name Type Description Default
path str

The path to the file.

required

Returns: None:

serialise() #

Serialise PersistentGraph to bytes.

Returns:

Type Description
bytes

update_metadata(metadata) #

Updates metadata of the graph.

Parameters:

Name Type Description Default
metadata dict

The static properties of the graph.

required

Returns:

Type Description
None

This function does not return a value, if the operation is successful.

Raises:

Type Description
GraphError

If the operation fails.

write_updates() #

Persist the new updates by appending them to the cache file.

Returns:

Type Description
None