Skip to content

Graph #

Bases: GraphView

A temporal graph with event semantics.

Parameters:

Name Type Description Default
num_shards int

The number of locks to use in the storage to allow for multithreaded updates.

required

__new__(num_shards=None) #

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 TimeInput

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
MutableEdge

The added edge.

Raises:

Type Description
GraphError

If the operation fails.

add_metadata(metadata) #

Adds static properties to the graph.

Parameters:

Name Type Description Default
metadata PropInput

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

The optional string which will be used as a node type

None
secondary_index int

The optional integer which will be used as a secondary index

None

Returns:

Type Description
MutableNode

The added node.

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 PropInput

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

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

Parameters:

Name Type Description Default
IndexSpec
  • The specification for the in-memory index to be created.
required

Returns:

Type Description
None

create_index_with_spec(py_spec) #

Create graph index with the provided index spec.

Parameters:

Name Type Description Default
py_spec Any
  • The specification for the in-memory index to be created.
required

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

The optional string which will be used as a node type

None
secondary_index int

The optional integer which will be used as a secondary index

None

Returns:

Type Description
MutableNode

The created node.

Raises:

Type Description
GraphError

If the operation fails.

deserialise(bytes) staticmethod #

Load Graph from serialised bytes.

Parameters:

Name Type Description Default
bytes bytes

The serialised bytes to decode

required

Returns:

Type Description
Graph

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
MutableEdge

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

event_graph() #

View graph with event semantics

Returns:

Type Description
Graph

the graph with event semantics applied

from_parquet(graph_dir) staticmethod #

Read graph from parquet files

Parameters:

Name Type Description Default
graph_dir str | PathLike

the folder where the graph is stored as parquet

required

Returns:

Type Description
Graph

a view of the graph

get_all_node_types() #

Returns all the node types in the graph.

Returns:

Type Description
List[str]

the node types

import_edge(edge, merge=False) #

Import a single edge into the graph.

Parameters:

Name Type Description Default
edge Edge

A Edge object representing the edge to be imported.

required
merge bool

An 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).

False

Returns:

Type Description
MutableEdge

An Edge object if the edge was successfully imported.

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.

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. 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).

False

Returns:

Type Description
Edge

An Edge object if the edge was successfully imported.

Raises:

Type Description
GraphError

If the operation fails.

import_edges(edges, merge=False) #

Import multiple edges into the graph.

Parameters:

Name Type Description Default
edges List[Edge]

A list of Edge objects representing the edges to be imported.

required
merge bool

An 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).

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.

Parameters:

Name Type Description Default
edges List[Edge]

A list of Edge objects representing the edges to be imported.

required
new_ids List[Tuple[int, int]]

The IDs of the new edges. It's a vector of tuples of the source and destination node ids.

required
merge bool

An 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).

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.

Parameters:

Name Type Description Default
node Node

A Node object representing the node to be imported.

required
merge bool

An 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).

False

Returns:

Type Description
Node

A node object if the node was successfully imported.

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.

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. 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).

False

Returns:

Type Description
MutableNode

A node object if the node was successfully imported.

Raises:

Type Description
GraphError

If the operation fails.

import_nodes(nodes, merge=False) #

Import multiple nodes into 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. 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).

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.

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. 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).

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.

largest_connected_component() #

Gives the large connected component of a graph.

Example Usage:#

g.largest_connected_component()

Returns:

Type Description
GraphView

sub-graph of the graph g containing the largest connected component

load_cached(path) staticmethod #

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:

Name Type Description Default
path str

The path to the cache file

required

Returns:

Type Description
Graph

the loaded graph with initialised cache

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 Graph from a file.

Parameters:

Name Type Description Default
path str

The path to the file.

required

Returns:

Type Description
Graph

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
MutableNode

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

persistent_graph() #

View graph with persistent semantics

Returns:

Type Description
PersistentGraph

the graph with persistent semantics applied

save_to_file(path) #

Saves the Graph 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 Graph to the given path.

Parameters:

Name Type Description Default
path str

The path to the file.

required

Returns: None:

serialise() #

Serialise Graph to bytes.

Returns:

Type Description
bytes

to_parquet(graph_dir) #

Persist graph to parquet files.

Parameters:

Name Type Description Default
graph_dir str | PathLike

the folder where the graph will be persisted as parquet

required

Returns:

Type Description
None

update_metadata(metadata) #

Updates static properties to the graph.

Parameters:

Name Type Description Default
metadata PropInput

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