PersistentGraph#

class PersistentGraph#

Bases: GraphView

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

Methods:

add_constant_properties(properties)

Adds static properties to the graph.

add_edge(timestamp, src, dst[, properties, ...])

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

add_node(timestamp, id[, properties, ...])

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

add_properties(timestamp, properties[, ...])

Adds properties to the graph.

cache(path)

Write PersistentGraph to cache file and initialise the cache.

create_node(timestamp, id[, properties, ...])

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

delete_edge(timestamp, src, dst[, layer, ...])

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

deserialise(bytes)

Load PersistentGraph from serialised bytes.

edge(src, dst)

Gets the edge with the specified source and destination nodes

event_graph()

Get event graph

get_all_node_types()

Returns all the node types in the graph.

import_edge(edge[, merge])

Import a single edge into the graph.

import_edge_as(edge, new_id[, merge])

Import a single edge into the graph with new id.

import_edges(edges[, merge])

Import multiple edges into the graph.

import_edges_as(edges, new_ids[, merge])

Import multiple edges into the graph with new ids.

import_node(node[, merge])

Import a single node into the graph.

import_node_as(node, new_id[, merge])

Import a single node into the graph with new id.

import_nodes(nodes[, merge])

Import multiple nodes into the graph.

import_nodes_as(nodes, new_ids[, merge])

Import multiple nodes into the graph with new ids.

load_cached(path)

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

load_edge_deletions_from_pandas(df, time, ...)

Load edges deletions from a Pandas DataFrame into the graph.

load_edge_deletions_from_parquet(...[, ...])

Load edges deletions from a Parquet file into the graph.

load_edge_props_from_pandas(df, src, dst[, ...])

Load edge properties from a Pandas DataFrame.

load_edge_props_from_parquet(parquet_path, ...)

Load edge properties from parquet file

load_edges_from_pandas(df, time, src, dst[, ...])

Load edges from a Pandas DataFrame into the graph.

load_edges_from_parquet(parquet_path, time, ...)

Load edges from a Parquet file into the graph.

load_from_file(path)

Load PersistentGraph from a file.

load_node_props_from_pandas(df, id[, ...])

Load node properties from a Pandas DataFrame.

load_node_props_from_parquet(parquet_path, id)

Load node properties from a parquet file.

load_nodes_from_pandas(df, time, id[, ...])

Load nodes from a Pandas DataFrame into the graph.

load_nodes_from_parquet(parquet_path, time, id)

Load nodes from a Parquet file into the graph.

node(id)

Gets the node with the specified id

persistent_graph()

Get persistent graph

save_to_file(path)

Saves the PersistentGraph to the given path.

save_to_zip(path)

Saves the PersistentGraph to the given path.

serialise()

Serialise PersistentGraph to bytes.

update_constant_properties(properties)

Updates static properties to the graph.

write_updates()

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

add_constant_properties(properties)#

Adds static properties to the graph.

Parameters:

properties (dict) – The static properties of the graph.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

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:
  • timestamp (int) – The timestamp of the edge.

  • src (str | int) – The id of the source node.

  • dst (str | int) – The id of the destination node.

  • properties (PropInput, optional) – The properties of the edge, as a dict of string and properties

  • layer (str, optional) – The layer of the edge.

  • secondary_index (int, optional) – The optional integer which will be used as a secondary index

Returns:

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

Return type:

None

Raises:

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:
  • timestamp (TimeInput) – The timestamp of the node.

  • id (str | int) – The id of the node.

  • properties (PropInput, optional) – The properties of the node.

  • node_type (str, optional) – The optional string which will be used as a node type

  • secondary_index (int, optional) – The optional integer which will be used as a secondary index

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

add_properties(timestamp, properties, secondary_index=None)#

Adds properties to the graph.

Parameters:
  • timestamp (TimeInput) – The timestamp of the temporal property.

  • properties (dict) – The temporal properties of the graph.

  • secondary_index (int, optional) – The optional integer which will be used as a secondary index

Returns:

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

Return type:

None

Raises:

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:

path (str) – The path to the cache file

Return type:

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:
  • timestamp (TimeInput) – The timestamp of the node.

  • id (str | int) – The id of the node.

  • properties (PropInput, optional) – The properties of the node.

  • node_type (str, optional) – The optional string which will be used as a node type

  • secondary_index (int, optional) – The optional integer which will be used as a secondary index

Returns:

the newly created node.

Return type:

MutableNode

Raises:

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:
  • timestamp (int) – The timestamp of the edge.

  • src (str | int) – The id of the source node.

  • dst (str | int) – The id of the destination node.

  • layer (str, optional) – The layer of the edge.

  • secondary_index (int, optional) – The optional integer which will be used as a secondary index.

Returns:

The deleted edge

Return type:

MutableEdge

Raises:

GraphError – If the operation fails.

static deserialise(bytes)#

Load PersistentGraph from serialised bytes.

Parameters:

bytes (bytes) – The serialised bytes to decode

Return type:

PersistentGraph

edge(src, dst)#

Gets the edge with the specified source and destination nodes

Parameters:
  • src (str | int) – the source node id

  • dst (str | int) – the destination node id

Returns:

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

Return type:

Optional[MutableEdge]

event_graph()#

Get event graph

Returns:

the graph with event semantics applied

Return type:

Graph

get_all_node_types()#

Returns all the node types in the graph.

Returns:

A list of node types

Return type:

list[str]

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:
  • edge (Edge) – An edge object representing the edge to be imported.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the edge. Defaults to False.

Returns:

The imported edge.

Return type:

Edge

Raises:

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:
  • edge (Edge) – A edge object representing the edge to be imported.

  • new_id (tuple) – The ID of the new edge. It’s a tuple of the source and destination node ids.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the edge. Defaults to False.

Returns:

The imported edge.

Return type:

Edge

Raises:

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:
  • edges (List[Edge]) – A vector of edge objects representing the edges to be imported.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the edges. Defaults to False.

Returns:

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

Return type:

None

Raises:

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:
  • edges (List[Edge]) – A vector of edge objects representing the edges to be imported.

  • new_ids (list[Tuple[GID, GID]]) – The new edge ids

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the edges. Defaults to False.

Returns:

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

Return type:

None

Raises:

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:
  • node (Node) – A node object representing the node to be imported.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the node. Defaults to False.

Returns:

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

Return type:

Node

Raises:

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:
  • node (Node) – A node object representing the node to be imported.

  • new_id (str|int) – The new node id.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the node. Defaults to False.

Returns:

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

Return type:

Node

Raises:

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:
  • nodes (List[Node]) – A vector of node objects representing the nodes to be imported.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the nodes. Defaults to False.

Returns:

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

Return type:

None

Raises:

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:
  • nodes (List[Node]) – A vector of node objects representing the nodes to be imported.

  • new_ids (List[str|int]) – A list of node IDs to use for the imported nodes.

  • merge (bool) – An optional boolean flag indicating whether to merge the import of the nodes. Defaults to False.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

static load_cached(path)#

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:

path (str) – The path to the cache file

Returns:

the loaded graph with initialised cache

Return type:

PersistentGraph

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:
  • df (DataFrame) – The Pandas DataFrame containing the edges.

  • time (str) – The column name for the update timestamps.

  • src (str) – The column name for the source node ids.

  • dst (str) – The column name for the destination node ids.

  • layer (str, optional) – A constant value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

  • layer_col (str, optional) – The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

Returns:

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

Return type:

None

Raises:

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:
  • parquet_path (str) – Parquet file or directory of Parquet files path containing node information.

  • src (str) – The column name for the source node ids.

  • dst (str) – The column name for the destination node ids.

  • time (str) – The column name for the update timestamps.

  • layer (str, optional) – A constant value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

  • layer_col (str, optional) – The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_edge_props_from_pandas(df, src, dst, constant_properties=None, shared_constant_properties=None, layer=None, layer_col=None)#

Load edge properties from a Pandas DataFrame.

Parameters:
  • df (DataFrame) – The Pandas DataFrame containing edge information.

  • src (str) – The column name for the source node.

  • dst (str) – The column name for the destination node.

  • constant_properties (List[str], optional) – List of constant edge property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every edge. Defaults to None.

  • layer (str, optional) – The edge layer name. Defaults to None.

  • layer_col (str, optional) – The edge layer col name in dataframe. Defaults to None.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_edge_props_from_parquet(parquet_path, src, dst, constant_properties=None, shared_constant_properties=None, layer=None, layer_col=None)#

Load edge properties from parquet file

Parameters:
  • parquet_path (str) – Parquet file or directory of Parquet files path containing edge information.

  • src (str) – The column name for the source node.

  • dst (str) – The column name for the destination node.

  • constant_properties (List[str], optional) – List of constant edge property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every edge. Defaults to None.

  • layer (str, optional) – The edge layer name. Defaults to None.

  • layer_col (str, optional) – The edge layer col name in dataframe. Defaults to None.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_edges_from_pandas(df, time, src, dst, properties=None, constant_properties=None, shared_constant_properties=None, layer=None, layer_col=None)#

Load edges from a Pandas DataFrame into the graph.

Parameters:
  • df (DataFrame) – The Pandas DataFrame containing the edges.

  • time (str) – The column name for the update timestamps.

  • src (str) – The column name for the source node ids.

  • dst (str) – The column name for the destination node ids.

  • properties (List[str], optional) – List of edge property column names. Defaults to None.

  • constant_properties (List[str], optional) – List of constant edge property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every edge. Defaults to None.

  • layer (str, optional) – A constant value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

  • layer_col (str, optional) – The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_edges_from_parquet(parquet_path, time, src, dst, properties=None, constant_properties=None, shared_constant_properties=None, layer=None, layer_col=None)#

Load edges from a Parquet file into the graph.

Parameters:
  • parquet_path (str) – Parquet file or directory of Parquet files path containing edges

  • time (str) – The column name for the update timestamps.

  • src (str) – The column name for the source node ids.

  • dst (str) – The column name for the destination node ids.

  • properties (List[str], optional) – List of edge property column names. Defaults to None.

  • constant_properties (List[str], optional) – List of constant edge property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every edge. Defaults to None.

  • layer (str, optional) – A constant value to use as the layer for all edges. Defaults to None. (cannot be used in combination with layer_col)

  • layer_col (str, optional) – The edge layer col name in dataframe. Defaults to None. (cannot be used in combination with layer)

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

static load_from_file(path)#

Load PersistentGraph from a file.

Parameters:

path (str) – The path to the file.

Return type:

PersistentGraph

load_node_props_from_pandas(df, id, node_type=None, node_type_col=None, constant_properties=None, shared_constant_properties=None)#

Load node properties from a Pandas DataFrame.

Parameters:
  • df (DataFrame) – The Pandas DataFrame containing node information.

  • id (str) – The column name for the node IDs.

  • node_type (str, optional) – A constant value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

  • node_type_col (str, optional) – The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

  • constant_properties (List[str], optional) – List of constant node property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every node. Defaults to None.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_node_props_from_parquet(parquet_path, id, node_type=None, node_type_col=None, constant_properties=None, shared_constant_properties=None)#

Load node properties from a parquet file.

Parameters:
  • parquet_path (str) – Parquet file or directory of Parquet files path containing node information.

  • id (str) – The column name for the node IDs.

  • node_type (str, optional) – A constant value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

  • node_type_col (str, optional) – The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

  • constant_properties (List[str], optional) – List of constant node property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every node. Defaults to None.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_nodes_from_pandas(df, time, id, node_type=None, node_type_col=None, properties=None, constant_properties=None, shared_constant_properties=None)#

Load nodes from a Pandas DataFrame into the graph.

Parameters:
  • df (DataFrame) – The Pandas DataFrame containing the nodes.

  • time (str) – The column name for the timestamps.

  • id (str) – The column name for the node IDs.

  • node_type (str, optional) – A constant value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

  • node_type_col (str, optional) – The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

  • properties (List[str], optional) – List of node property column names. Defaults to None.

  • constant_properties (List[str], optional) – List of constant node property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every node. Defaults to None.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

load_nodes_from_parquet(parquet_path, time, id, node_type=None, node_type_col=None, properties=None, constant_properties=None, shared_constant_properties=None)#

Load nodes from a Parquet file into the graph.

Parameters:
  • parquet_path (str) – Parquet file or directory of Parquet files containing the nodes

  • time (str) – The column name for the timestamps.

  • id (str) – The column name for the node IDs.

  • node_type (str, optional) – A constant value to use as the node type for all nodes. Defaults to None. (cannot be used in combination with node_type_col)

  • node_type_col (str, optional) – The node type col name in dataframe. Defaults to None. (cannot be used in combination with node_type)

  • properties (List[str], optional) – List of node property column names. Defaults to None.

  • constant_properties (List[str], optional) – List of constant node property column names. Defaults to None.

  • shared_constant_properties (PropInput, optional) – A dictionary of constant properties that will be added to every node. Defaults to None.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

node(id)#

Gets the node with the specified id

Parameters:

id (str | int) – the node id

Returns:

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

Return type:

Optional[MutableNode]

persistent_graph()#

Get persistent graph

Returns:

the graph with persistent semantics applied

Return type:

PersistentGraph

save_to_file(path)#

Saves the PersistentGraph to the given path.

Parameters:

path (str) – The path to the file.

Return type:

None

save_to_zip(path)#

Saves the PersistentGraph to the given path.

Parameters:

path (str) – The path to the file.

Return type:

None

serialise()#

Serialise PersistentGraph to bytes.

Return type:

bytes

update_constant_properties(properties)#

Updates static properties to the graph.

Parameters:

properties (dict) – The static properties of the graph.

Returns:

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

Return type:

None

Raises:

GraphError – If the operation fails.

write_updates()#

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

Return type:

None