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.
- 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.
- 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
- 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.
- 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.
- in_edges(node: str, keys: bool = False, data: bool = False) List [source]#
Get all incoming edges for a given node.
- 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
- out_edges(node: str, keys: bool = False, data: bool = False) List [source]#
Get all outgoing edges for a given node.
- static relabel_nodes(graph: Any, mapping: Dict) Any [source]#
Relabel identifiers for a series of nodes based on mappings.
- remove_edge(subject_node: str, object_node: str, edge_key: Optional[str] = None) Any [source]#
Remove a given edge from the graph.
- 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:
- Returns:
A dictionary corresponding to the updated edge 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.
- 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.
- 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.
- 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.
- 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:
graph (kgx.graph.base_graph.BaseGraph) – The graph to modify
attr_key (str) – The attribute key
- 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:
graph (kgx.graph.base_graph.BaseGraph) – The graph to modify
attr_key (str) – The attribute key
- 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.
- in_edges(node: str, keys: bool = False, data: bool = False) List [source]#
Get all incoming edges for a given node.
- 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
- out_edges(node: str, keys: bool = False, data: bool = False) List [source]#
Get all outgoing edges for a given node.
- 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.
- 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:
graph (kgx.graph.base_graph.BaseGraph) – The graph to modify
attributes (Dict) – A dictionary of node identifier to key-value pairs
- Return type:
Any
- static set_node_attributes(graph: BaseGraph, attributes: Dict) None [source]#
Set nodes attributes from a dictionary of key-values.
- Parameters:
graph (kgx.graph.base_graph.BaseGraph) – The graph to modify
attributes (Dict) – A dictionary of node identifier to key-value pairs
- 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:
- Returns:
A dictionary corresponding to the updated edge properties
- Return type:
Dict