Graph

A temporal graph with event semantics.

Methods

MethodDescription
add_edgeAdds a new edge with the given source and destination nodes and properties to the graph.
add_metadataAdds static properties to the graph.
add_nodeAdds a new node with the given id and properties to the graph.
add_propertiesAdds properties to the graph.
cacheWrite Graph to cache file and initialise the cache.
create_indexCreate graph index
create_index_in_ramCreates a graph index in memory (RAM).
create_index_in_ram_with_specCreates a graph index in memory (RAM) with the provided index spec.
create_index_with_specCreate graph index with the provided index spec.
create_nodeCreates a new node with the given id and properties to the graph. It fails if the node already exists.
deserialiseLoad Graph from serialised bytes.
edgeGets the edge with the specified source and destination nodes
event_graphView graph with event semantics
from_parquetRead graph from parquet files
get_all_node_typesReturns all the node types in the graph.
import_edgeImport a single edge into the graph.
import_edge_asImport a single edge into the graph with new id.
import_edgesImport multiple edges into the graph.
import_edges_asImport multiple edges into the graph with new ids.
import_nodeImport a single node into the graph.
import_node_asImport a single node into the graph with new id.
import_nodesImport multiple nodes into the graph.
import_nodes_asImport multiple nodes into the graph with new ids.
largest_connected_componentGives the large connected component of a graph.
load_cachedLoad Graph from a file and initialise it as a cache file.
load_edge_metadataLoad edge metadata into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method),
load_edgesLoad edges into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method),
load_from_fileLoad Graph from a file.
load_node_metadataLoad node metadata into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method),
load_nodesLoad nodes into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method),
nodeGets the node with the specified id
persistent_graphView graph with persistent semantics
save_to_fileSaves the Graph to the given path.
save_to_zipSaves the Graph to the given path.
serialiseSerialise Graph to bytes.
to_parquetPersist graph to parquet files.
update_metadataUpdates static properties to the graph.
write_updatesPersist the new updates by appending them to the cache file.

Method Details

add_edge

Signature: add_edge(timestamp, src, dst, properties=None, layer=None, event_id=None)

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

Parameters

NameTypeDefaultDescription
timestampTimeInput-The timestamp of the edge.
srcstr | int-The id of the source node.
dststr | int-The id of the destination node.
propertiesPropInput, optionalNoneThe properties of the edge, as a dict of string and properties.
layerstr, optionalNoneThe layer of the edge.
event_idint, optionalNoneThe optional integer which will be used as an event id.

Returns

TypeDescription
MutableEdgeThe added edge.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

add_metadata

Signature: add_metadata(metadata)

Adds static properties to the graph.

Parameters

NameTypeDefaultDescription
metadataPropInput-The static properties of the graph.

Returns

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

Raises

ExceptionDescription
GraphErrorIf the operation fails.

add_node

Signature: add_node(timestamp, id, properties=None, node_type=None, event_id=None)

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

Parameters

NameTypeDefaultDescription
timestampTimeInput-The timestamp of the node.
idstr | int-The id of the node.
propertiesPropInput, optionalNoneThe properties of the node.
node_typestr, optionalNoneThe optional string which will be used as a node type.
event_idint, optionalNoneThe optional integer which will be used as an event id.

Returns

TypeDescription
MutableNodeThe added node.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

add_properties

Signature: add_properties(timestamp, properties, event_id=None)

Adds properties to the graph.

Parameters

NameTypeDefaultDescription
timestampTimeInput-The timestamp of the temporal property.
propertiesPropInput-The temporal properties of the graph.
event_idint, optionalNoneThe optional integer which will be used as an event id.

Returns

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

Raises

ExceptionDescription
GraphErrorIf the operation fails.

cache

Signature: cache(path)

Write Graph 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

NameTypeDefaultDescription
pathstr-The path to the cache file

Returns

TypeDescription
None

create_index

Create graph index

Returns

TypeDescription
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

TypeDescription
None

create_index_in_ram_with_spec

Signature: 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

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

Returns

TypeDescription
None

create_index_with_spec

Signature: create_index_with_spec(py_spec)

Create graph index with the provided index spec.

Parameters

NameTypeDefaultDescription
py_specAny-

Returns

TypeDescription
None

create_node

Signature: create_node(timestamp, id, properties=None, node_type=None, event_id=None)

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

Parameters

NameTypeDefaultDescription
timestampTimeInput-The timestamp of the node.
idstr | int-The id of the node.
propertiesPropInput, optionalNoneThe properties of the node.
node_typestr, optionalNoneThe optional string which will be used as a node type.
event_idint, optionalNoneThe optional integer which will be used as an event id.

Returns

TypeDescription
MutableNodeThe created node.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

deserialise

Signature: deserialise(bytes)

Load Graph from serialised bytes.

Parameters

NameTypeDefaultDescription
bytesbytes-The serialised bytes to decode

Returns

TypeDescription
Graph

edge

Signature: edge(src, dst)

Gets the edge with the specified source and destination nodes

Parameters

NameTypeDefaultDescription
srcstr | int-the source node id
dststr | int-the destination node id

Returns

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

event_graph

View graph with event semantics

Returns

TypeDescription
Graphthe graph with event semantics applied

from_parquet

Signature: from_parquet(graph_dir)

Read graph from parquet files

Parameters

NameTypeDefaultDescription
graph_dirstr | PathLike-the folder where the graph is stored as parquet

Returns

TypeDescription
Grapha view of the graph

get_all_node_types

Returns all the node types in the graph.

Returns

TypeDescription
list[str]the node types

import_edge

Signature: import_edge(edge, merge=False)

Import a single edge into the graph.

Parameters

NameTypeDefaultDescription
edgeEdge-A Edge object representing the edge to be imported.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if the imported edge already exists in the graph. If merge is True, the function merges the histories of the imported edge and the existing edge (in the graph).

Returns

TypeDescription
MutableEdgeAn Edge object if the edge was successfully imported.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_edge_as

Signature: import_edge_as(edge, new_id, merge=False)

Import a single edge into the graph with new id.

Parameters

NameTypeDefaultDescription
edgeEdge-A Edge object representing the edge to be imported.
new_idtuple-The ID of the new edge. It's a tuple of the source and destination node ids.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if the imported edge already exists in the graph. If merge is True, the function merges the histories of the imported edge and the existing edge (in the graph).

Returns

TypeDescription
EdgeAn Edge object if the edge was successfully imported.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_edges

Signature: import_edges(edges, merge=False)

Import multiple edges into the graph.

Parameters

NameTypeDefaultDescription
edgeslist[Edge]-A list of Edge objects representing the edges to be imported.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if any of the imported edges already exists in the graph. If merge is True, the function merges the histories of the imported edges and the existing edges (in the graph).

Returns

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

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_edges_as

Signature: import_edges_as(edges, new_ids, merge=False)

Import multiple edges into the graph with new ids.

Parameters

NameTypeDefaultDescription
edgeslist[Edge]-A list of Edge objects representing the edges to be imported.
new_idslist[tuple[int, int]]-The IDs of the new edges. It's a vector of tuples of the source and destination node ids.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if any of the imported edges already exists in the graph. If merge is True, the function merges the histories of the imported edges and the existing edges (in the graph).

Returns

TypeDescription
NoneThis function does not return a value if the operation is successful.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_node

Signature: import_node(node, merge=False)

Import a single node into the graph.

Parameters

NameTypeDefaultDescription
nodeNode-A Node object representing the node to be imported.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if the imported node already exists in the graph. If merge is True, the function merges the histories of the imported node and the existing node (in the graph).

Returns

TypeDescription
NodeA node object if the node was successfully imported.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_node_as

Signature: import_node_as(node, new_id, merge=False)

Import a single node into the graph with new id.

Parameters

NameTypeDefaultDescription
nodeNode-A Node object representing the node to be imported.
new_idstr | int-The new node id.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if the imported node already exists in the graph. If merge is True, the function merges the histories of the imported node and the existing node (in the graph).

Returns

TypeDescription
MutableNodeA node object if the node was successfully imported.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_nodes

Signature: import_nodes(nodes, merge=False)

Import multiple nodes into the graph.

Parameters

NameTypeDefaultDescription
nodeslist[Node]-A vector of Node objects representing the nodes to be imported.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is False, the function will return an error if any of the imported nodes already exists in the graph. If merge is True, the function merges the histories of the imported nodes and the existing nodes (in the graph).

Returns

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

Raises

ExceptionDescription
GraphErrorIf the operation fails.

import_nodes_as

Signature: import_nodes_as(nodes, new_ids, merge=False)

Import multiple nodes into the graph with new ids.

Parameters

NameTypeDefaultDescription
nodeslist[Node]-A vector of Node objects representing the nodes to be imported.
new_idsList | int]-A list of node IDs to use for the imported nodes.
mergebool, optionalFalseAn optional boolean flag. Defaults to False. If merge is True, the function will return an error if any of the imported nodes already exists in the graph. If merge is False, the function merges the histories of the imported nodes and the existing nodes (in the graph).

Returns

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

Raises

ExceptionDescription
GraphErrorIf the operation fails.

largest_connected_component

Gives the large connected component of a graph.

Example Usage:

g.largest_connected_component()

Returns

TypeDescription
GraphViewsub-graph of the graph g containing the largest connected component

load_cached

Signature: load_cached(path)

Load Graph 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

NameTypeDefaultDescription
pathstr-The path to the cache file

Returns

TypeDescription
Graphthe loaded graph with initialised cache

load_edge_metadata

Signature: load_edge_metadata(data, src, dst, metadata=None, shared_metadata=None, layer=None, layer_col=None, schema=None, csv_options=None)

Load edge metadata into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method), a path to a CSV or Parquet file, or a directory containing multiple CSV or Parquet files. The following are known to support the ArrowStreamExportable protocol: Pandas dataframes, FireDucks(.pandas) dataframes, Polars dataframes, Arrow tables, DuckDB (e.g. DuckDBPyRelation obtained from running an SQL query).

Parameters

NameTypeDefaultDescription
dataAny-The data source containing edge information.
srcstr-The column name for the source node.
dststr-The column name for the destination node.
metadatalist[str], optionalNoneList of edge metadata column names. Defaults to None.
shared_metadataPropInput, optionalNoneA dictionary of metadata properties that will be added to every edge. Defaults to None.
layerstr, optionalNoneThe edge layer name. Defaults to None.
layer_colstr, optionalNoneThe edge layer column name in a dataframe. Defaults to None.
schemalist | PropType | str]] | dict | PropType | str], optionalNoneA list of (column_name, column_type) tuples or dict of {"column_name": column_type} to cast columns to. Defaults to None.
csv_optionsdict | bool], optionalNoneA dictionary of CSV reading options such as delimiter, comment, escape, quote, and terminator characters, as well as allow_truncated_rows and has_header flags. Defaults to None.

Returns

TypeDescription
NoneThis function does not return a value if the operation is successful.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

load_edges

Signature: load_edges(data, time, src, dst, properties=None, metadata=None, shared_metadata=None, layer=None, layer_col=None, schema=None, csv_options=None)

Load edges into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method), a path to a CSV or Parquet file, or a directory containing multiple CSV or Parquet files. The following are known to support the ArrowStreamExportable protocol: Pandas dataframes, FireDucks(.pandas) dataframes, Polars dataframes, Arrow tables, DuckDB (e.g. DuckDBPyRelation obtained from running an SQL query).

Parameters

NameTypeDefaultDescription
dataAny-The data source containing the edges.
timestr-The column name for the update timestamps.
srcstr-The column name for the source node IDs.
dststr-The column name for the destination node IDs.
propertieslist[str], optionalNoneList of edge property column names. Defaults to None.
metadatalist[str], optionalNoneList of edge metadata column names. Defaults to None.
shared_metadataPropInput, optionalNoneA dictionary of metadata properties that will be added to every edge. Defaults to None.
layerstr, optionalNoneA value to use as the layer for all edges. Cannot be used in combination with layer_col. Defaults to None.
layer_colstr, optionalNoneThe edge layer column name in a dataframe. Cannot be used in combination with layer. Defaults to None.
schemalist | PropType | str]] | dict | PropType | str], optionalNoneA list of (column_name, column_type) tuples or dict of {"column_name": column_type} to cast columns to. Defaults to None.
csv_optionsdict | bool], optionalNoneA dictionary of CSV reading options such as delimiter, comment, escape, quote, and terminator characters, as well as allow_truncated_rows and has_header flags. Defaults to None.

Returns

TypeDescription
NoneThis function does not return a value if the operation is successful.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

load_from_file

Signature: load_from_file(path)

Load Graph from a file.

Parameters

NameTypeDefaultDescription
pathstr-The path to the file.

Returns

TypeDescription
Graph

load_node_metadata

Signature: load_node_metadata(data, id, node_type=None, node_type_col=None, metadata=None, shared_metadata=None, schema=None, csv_options=None)

Load node metadata into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method), a path to a CSV or Parquet file, or a directory containing multiple CSV or Parquet files. The following are known to support the ArrowStreamExportable protocol: Pandas dataframes, FireDucks(.pandas) dataframes, Polars dataframes, Arrow tables, DuckDB (e.g. DuckDBPyRelation obtained from running an SQL query).

Parameters

NameTypeDefaultDescription
dataAny-The data source containing node information.
idstr-The column name for the node IDs.
node_typestr, optionalNoneA value to use as the node type for all nodes. Cannot be used in combination with node_type_col. Defaults to None.
node_type_colstr, optionalNoneThe node type column name in a dataframe. Cannot be used in combination with node_type. Defaults to None.
metadatalist[str], optionalNoneList of node metadata column names. Defaults to None.
shared_metadataPropInput, optionalNoneA dictionary of metadata properties that will be added to every node. Defaults to None.
schemalist | PropType | str]] | dict | PropType | str], optionalNoneA list of (column_name, column_type) tuples or dict of {"column_name": column_type} to cast columns to. Defaults to None.
csv_optionsdict | bool], optionalNoneA dictionary of CSV reading options such as delimiter, comment, escape, quote, and terminator characters, as well as allow_truncated_rows and has_header flags. Defaults to None.

Returns

TypeDescription
NoneThis function does not return a value if the operation is successful.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

load_nodes

Signature: load_nodes(data, time, id, node_type=None, node_type_col=None, properties=None, metadata=None, shared_metadata=None, schema=None, csv_options=None)

Load nodes into the graph from any data source that supports the ArrowStreamExportable protocol (by providing an arrow_c_stream() method), a path to a CSV or Parquet file, or a directory containing multiple CSV or Parquet files. The following are known to support the ArrowStreamExportable protocol: Pandas dataframes, FireDucks(.pandas) dataframes, Polars dataframes, Arrow tables, DuckDB (e.g. DuckDBPyRelation obtained from running an SQL query).

Parameters

NameTypeDefaultDescription
dataAny-The data source containing the nodes.
timestr-The column name for the timestamps.
idstr-The column name for the node IDs.
node_typestr, optionalNoneA value to use as the node type for all nodes. Cannot be used in combination with node_type_col. Defaults to None.
node_type_colstr, optionalNoneThe node type column name in a dataframe. Cannot be used in combination with node_type. Defaults to None.
propertieslist[str], optionalNoneList of node property column names. Defaults to None.
metadatalist[str], optionalNoneList of node metadata column names. Defaults to None.
shared_metadataPropInput, optionalNoneA dictionary of metadata properties that will be added to every node. Defaults to None.
schemalist | PropType | str]] | dict | PropType | str], optionalNoneA list of (column_name, column_type) tuples or dict of {"column_name": column_type} to cast columns to. Defaults to None.
csv_optionsdict | bool], optionalNoneA dictionary of CSV reading options such as delimiter, comment, escape, quote, and terminator characters, as well as allow_truncated_rows and has_header flags. Defaults to None.

Returns

TypeDescription
NoneThis function does not return a value if the operation is successful.

Raises

ExceptionDescription
GraphErrorIf the operation fails.

node

Signature: node(id)

Gets the node with the specified id

Parameters

NameTypeDefaultDescription
idstr | int-the node id

Returns

TypeDescription
MutableNodeThe node object with the specified id, or None if the node does not exist

persistent_graph

View graph with persistent semantics

Returns

TypeDescription
PersistentGraphthe graph with persistent semantics applied

save_to_file

Signature: save_to_file(path)

Saves the Graph to the given path.

Parameters

NameTypeDefaultDescription
pathstr-The path to the file.

Returns

TypeDescription
None

save_to_zip

Signature: save_to_zip(path)

Saves the Graph to the given path.

Parameters

NameTypeDefaultDescription
pathstr-The path to the file.

Returns

TypeDescription
None

serialise

Serialise Graph to bytes.

Returns

TypeDescription
bytes

to_parquet

Signature: to_parquet(graph_dir)

Persist graph to parquet files.

Parameters

NameTypeDefaultDescription
graph_dirstr | PathLike-the folder where the graph will be persisted as parquet

Returns

TypeDescription
None

update_metadata

Signature: update_metadata(metadata)

Updates static properties to the graph.

Parameters

NameTypeDefaultDescription
metadataPropInput-The static properties of the graph.

Returns

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

Raises

ExceptionDescription
GraphErrorIf the operation fails.

write_updates

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

Returns

TypeDescription
None