Knowledge Graphs in Memory#

KGX makes use of an in-memory labelled property graph for representing a Knowledge Graph.

To support a wide variety of graph libraries, KGX has a Graph API which abstracts over the underlying graph store.

Should you want to add support for a new graph store,

  • create a new class that extends kgx.graph.base_graph.BaseGraph.

  • modify the graph_store variable in kgx/config.yml.

kgx.graph.base_graph.BaseGraph#

BaseGraph is the base Graph API that can be used to abstract over any graph, as long as the graph is capable of successfully representing a property graph.

class kgx.graph.base_graph.BaseGraph[source]#

Bases: object

BaseGraph that is a wrapper and provides methods to interact with a graph store.

All implementations should extend this BaseGraph class and implement all the defined methods.

add_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None, **kwargs: Any) Any[source]#

Add an edge to the graph.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

  • kwargs (Any) – Any additional edge properties

Return type:

Any

add_edge_attribute(subject_node: str, object_node: str, edge_key: Optional[str], attr_key: str, attr_value: Any) Any[source]#

Add an attribute to a given edge.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

  • attr_key (str) – The attribute key

  • attr_value (Any) – The attribute value

Return type:

Any

add_node(node: str, **kwargs: Any) Any[source]#

Add a node to the graph.

Parameters:
  • node (str) – Node identifier

  • **kwargs (Any) – Any additional node properties

add_node_attribute(node: str, key: str, value: Any) Any[source]#

Add an attribute to a given node.

Parameters:
  • node (str) – The node identifier

  • key (str) – The key for an attribute

  • value (Any) – The value corresponding to the key

Return type:

Any

clear() None[source]#

Remove all the nodes and edges in the graph.

degree()[source]#

Get the degree of all the nodes in a graph.

edges(keys: bool = False, data: bool = True) Dict[source]#

Get all edges in a graph.

Parameters:
  • keys (bool) – Whether or not to include edge keys

  • data (bool) – Whether or not to fetch node properties

Returns:

A dictionary of edges

Return type:

Dict

edges_iter() Generator[source]#

Get an iterable to traverse through all the edges in a graph.

Returns:

A generator for edges

Return type:

Generator

get_edge(subject_node: str, object_node: str, edge_key: Optional[str]) Dict[source]#

Get an edge and its properties.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

Returns:

The edge dictionary

Return type:

Dict

static get_edge_attributes(graph: Any, attr_key: str) Any[source]#

Get all edges that have a value for the given attribute attr_key.

Parameters:
  • graph (Any) – The graph to modify

  • attr_key (str) – The attribute key

Return type:

Any

get_node(node: str) Dict[source]#

Get a node and its properties.

Parameters:

node (str) – The node identifier

Returns:

The node dictionary

Return type:

Dict

static get_node_attributes(graph: Any, attr_key: str) Any[source]#

Get all nodes that have a value for the given attribute attr_key.

Parameters:
  • graph (Any) – The graph to modify

  • attr_key (str) – The attribute key

Return type:

Any

has_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None) bool[source]#

Check whether a given edge exists in the graph.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

Returns:

Whether or not the given edge exists

Return type:

bool

has_node(node: str) bool[source]#

Check whether a given node exists in the graph.

Parameters:

node (str) – The node identifier

Returns:

Whether or not the given node exists

Return type:

bool

in_edges(node: str, keys: bool = False, data: bool = False) List[source]#

Get all incoming edges for a given node.

Parameters:
  • node (str) – The node identifier

  • keys (bool) – Whether or not to include edge keys

  • data (bool) – Whether or not to fetch node properties

Returns:

A list of edges

Return type:

List

nodes(data: bool = True) Dict[source]#

Get all nodes in a graph.

Parameters:

data (bool) – Whether or not to fetch node properties

Returns:

A dictionary of nodes

Return type:

Dict

nodes_iter() Generator[source]#

Get an iterable to traverse through all the nodes in a graph.

Returns:

A generator for nodes

Return type:

Generator

number_of_edges() int[source]#

Returns the number of edges in a graph.

Return type:

int

number_of_nodes() int[source]#

Returns the number of nodes in a graph.

Return type:

int

out_edges(node: str, keys: bool = False, data: bool = False) List[source]#

Get all outgoing edges for a given node.

Parameters:
  • node (str) – The node identifier

  • keys (bool) – Whether or not to include edge keys

  • data (bool) – Whether or not to fetch node properties

Returns:

A list of edges

Return type:

List

static relabel_nodes(graph: Any, mapping: Dict) Any[source]#

Relabel identifiers for a series of nodes based on mappings.

Parameters:
  • graph (Any) – The graph to modify

  • mapping (Dict[str, str]) – A dictionary of mapping where the key is the old identifier and the value is the new identifier.

Return type:

Any

remove_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None) Any[source]#

Remove a given edge from the graph.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

Return type:

Any

remove_node(node: str) Any[source]#

Remove a given node from the graph.

Parameters:

node (str) – The node identifier

Return type:

Any

static set_edge_attributes(graph: Any, attributes: Dict) Any[source]#

Set nodes attributes from a dictionary of key-values.

Parameters:
  • graph (Any) – The graph to modify

  • attributes (Dict) – A dictionary of node identifier to key-value pairs

Return type:

Any

static set_node_attributes(graph: Any, attributes: Dict) Any[source]#

Set nodes attributes from a dictionary of key-values.

Parameters:
  • graph (Any) – The graph to modify

  • attributes (Dict) – A dictionary of node identifier to key-value pairs

Return type:

Any

update_edge_attribute(subject_node: str, object_node: str, edge_key: Optional[str], attr_key: str, attr_value: Any) Dict[source]#

Update an attribute of a given edge.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

  • attr_key (str) – The attribute key

  • attr_value (Any) – The attribute value

Returns:

A dictionary corresponding to the updated edge properties

Return type:

Dict

update_node_attribute(node, key: str, value: Any) Dict[source]#

Update an attribute of a given node.

Parameters:
  • node (str) – The node identifier

  • key (str) – The key for an attribute

  • value (Any) – The value corresponding to the key

Returns:

A dictionary corresponding to the updated node properties

Return type:

Dict

kgx.graph.nx_graph.NxGraph#

NxGraph is basically an abstraction on top of networkx.MultiDiGraph.

The NxGraph subclasses kgx.graph.base_graph.BaseGraph and implements all the methods defined in BaseGraph.

class kgx.graph.nx_graph.NxGraph[source]#

Bases: BaseGraph

NxGraph is a wrapper that provides methods to interact with a networkx.MultiDiGraph.

NxGraph extends kgx.graph.base_graph.BaseGraph and implements all the methods from BaseGraph.

add_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None, **kwargs: Any) None[source]#

Add an edge to the graph.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

  • kwargs (Any) – Any additional edge properties

add_edge_attribute(subject_node: str, object_node: str, edge_key: Optional[str], attr_key: str, attr_value: Any) None[source]#

Add an attribute to a given edge.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

  • attr_key (str) – The attribute key

  • attr_value (Any) – The attribute value

add_node(node: str, **kwargs: Any) None[source]#

Add a node to the graph.

Parameters:
  • node (str) – Node identifier

  • **kwargs (Any) – Any additional node properties

add_node_attribute(node: str, attr_key: str, attr_value: Any) None[source]#

Add an attribute to a given node.

Parameters:
  • node (str) – The node identifier

  • attr_key (str) – The key for an attribute

  • attr_value (Any) – The value corresponding to the key

clear() None[source]#

Remove all the nodes and edges in the graph.

degree()[source]#

Get the degree of all the nodes in a graph.

edges(keys: bool = False, data: bool = True) Dict[source]#

Get all edges in a graph.

Parameters:
  • keys (bool) – Whether or not to include edge keys

  • data (bool) – Whether or not to fetch node properties

Returns:

A dictionary of edges

Return type:

Dict

edges_iter() Generator[source]#

Get an iterable to traverse through all the edges in a graph.

Returns:

A generator for edges where each element is a 4-tuple that contains (subject, object, edge_key, edge_data)

Return type:

Generator

get_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None) Dict[source]#

Get an edge and its properties.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

Returns:

The edge dictionary

Return type:

Dict

static get_edge_attributes(graph: BaseGraph, attr_key: str) Dict[source]#

Get all edges that have a value for the given attribute attr_key.

Parameters:
Returns:

A dictionary where edges are the keys and the values are the attribute values for attr_key

Return type:

Dict

get_node(node: str) Dict[source]#

Get a node and its properties.

Parameters:

node (str) – The node identifier

Returns:

The node dictionary

Return type:

Dict

static get_node_attributes(graph: BaseGraph, attr_key: str) Dict[source]#

Get all nodes that have a value for the given attribute attr_key.

Parameters:
Returns:

A dictionary where nodes are the keys and the values are the attribute values for key

Return type:

Dict

has_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None) bool[source]#

Check whether a given edge exists in the graph.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

Returns:

Whether or not the given edge exists

Return type:

bool

has_node(node: str) bool[source]#

Check whether a given node exists in the graph.

Parameters:

node (str) – The node identifier

Returns:

Whether or not the given node exists

Return type:

bool

in_edges(node: str, keys: bool = False, data: bool = False) List[source]#

Get all incoming edges for a given node.

Parameters:
  • node (str) – The node identifier

  • keys (bool) – Whether or not to include edge keys

  • data (bool) – Whether or not to fetch node properties

Returns:

A list of edges

Return type:

List

nodes(data: bool = True) Dict[source]#

Get all nodes in a graph.

Parameters:

data (bool) – Whether or not to fetch node properties

Returns:

A dictionary of nodes

Return type:

Dict

nodes_iter() Generator[source]#

Get an iterable to traverse through all the nodes in a graph.

Returns:

A generator for nodes where each element is a Tuple that contains (node_id, node_data)

Return type:

Generator

number_of_edges() int[source]#

Returns the number of edges in a graph.

Return type:

int

number_of_nodes() int[source]#

Returns the number of nodes in a graph.

Return type:

int

out_edges(node: str, keys: bool = False, data: bool = False) List[source]#

Get all outgoing edges for a given node.

Parameters:
  • node (str) – The node identifier

  • keys (bool) – Whether or not to include edge keys

  • data (bool) – Whether or not to fetch node properties

Returns:

A list of edges

Return type:

List

static relabel_nodes(graph: BaseGraph, mapping: Dict) None[source]#

Relabel identifiers for a series of nodes based on mappings.

Parameters:
  • graph (kgx.graph.base_graph.BaseGraph) – The graph to modify

  • mapping (Dict) – A dictionary of mapping where the key is the old identifier and the value is the new identifier.

remove_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None) None[source]#

Remove a given edge from the graph.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

remove_node(node: str) None[source]#

Remove a given node from the graph.

Parameters:

node (str) – The node identifier

static set_edge_attributes(graph: BaseGraph, attributes: Dict) None[source]#

Set nodes attributes from a dictionary of key-values.

Parameters:
Return type:

Any

static set_node_attributes(graph: BaseGraph, attributes: Dict) None[source]#

Set nodes attributes from a dictionary of key-values.

Parameters:
update_edge_attribute(subject_node: str, object_node: str, edge_key: Optional[str], attr_key: str, attr_value: Any, preserve: bool = False) Dict[source]#

Update an attribute of a given edge.

Parameters:
  • subject_node (str) – The subject (source) node

  • object_node (str) – The object (target) node

  • edge_key (Optional[str]) – The edge key

  • attr_key (str) – The attribute key

  • attr_value (Any) – The attribute value

  • preserve (bool) – Whether or not to preserve existing values for the given attr_key

Returns:

A dictionary corresponding to the updated edge properties

Return type:

Dict

update_node_attribute(node: str, attr_key: str, attr_value: Any, preserve: bool = False) Dict[source]#

Update an attribute of a given node.

Parameters:
  • node (str) – The node identifier

  • attr_key (str) – The key for an attribute

  • attr_value (Any) – The value corresponding to the key

  • preserve (bool) – Whether or not to preserve existing values for the given attr_key

Returns:

A dictionary corresponding to the updated node properties

Return type:

Dict