graph

knowledge.graph

Graph-based representation of the ontology.

This module contains functions for representing ontology-based structures as graphs, and various utilities for manipulating and visualizing these graphs.

Functions:

Functions

Name Description
edge_exists Check if an edge with the given type exists between source and target in a MultiDiGraph.
get_color_mapping Map nodes of a graph to distinct colors based on a node attribute.
nx2mermaid Convert a NetworkX graph to a Mermaid representation.
onto2graph Convert an ontology into a NetworkX directed graph.
owl2nx_digraph Convert an ontology into a NetworkX directed graph.

edge_exists

knowledge.graph.edge_exists(G, source, target, edge_type)

Check if an edge with the given type exists between source and target in a MultiDiGraph.

Parameters

Name Type Description Default
G nx.MultiDiGraph The graph. required
source hashable Source node. required
target hashable Target node. required
edge_type str Type attribute of the edge to check. required

Returns

Name Type Description
bool bool True if such an edge exists, False otherwise.

get_color_mapping

knowledge.graph.get_color_mapping(g, by='type')

Map nodes of a graph to distinct colors based on a node attribute.

Parameters:

g : nx.Graph The input graph. by : str, optional Node attribute to be used for color mapping. Default is “type”.

Returns:

dict A dictionary mapping each node to a color index.

nx2mermaid

knowledge.graph.nx2mermaid(G, id_as_label=False)

Convert a NetworkX graph to a Mermaid representation.

Parameters:

G : nx.Graph NetworkX graph to be converted. id_as_label : bool, optional Use identifier as label in the Mermaid graph. Default is False.

Returns:

str The Mermaid representation of the graph.

onto2graph

knowledge.graph.onto2graph(
    onto='default',
    add_object_properties=True,
    storid=False,
    object2string=True,
)

Convert an ontology into a NetworkX directed graph.

The function generates a directed graph (DiGraph) where:

  • Nodes represent ontology classes.
  • Node attributes contain annotation properties of the classes.
  • Edges represent relationships between the classes, either hierarchical (is_a) or based on object properties.

Note

The function assumes that there’s a utility function get_class_properties(c) available which retrieves properties of a given ontology class in a predefined format, especially the “annotation_properties” and “object_properties”.

Warning

The function omits the “Thing” class and its properties to avoid redundant information.

Returns

Name Type Description
nx.MultiDiGraph nx.MultiDiGraph: A directed multigraph representation of the ontology with
nx.MultiDiGraph nodes representing ontology classes and edges representing relationships.

Examples

>>> G = owl2nx_digraph()
>>> print(G.nodes(data=True))
[('ClassA', {'ID': 'id123', 'label': 'A'}), ...]
>>> print(G.edges(data=True))
[('ClassA', 'ClassB', {'type': 'is_a'}), ...]

Raises

Name Type Description
KeyError If expected properties are not found in the ontology class.
TypeError If the ontology structure differs from the expected format.

owl2nx_digraph

knowledge.graph.owl2nx_digraph(
    onto='default',
    add_object_properties=True,
    object2string=True,
)

Convert an ontology into a NetworkX directed graph.

The function generates a directed graph (DiGraph) where:

  • Nodes represent ontology classes.
  • Node attributes contain annotation properties of the classes.
  • Edges represent relationships between the classes, either hierarchical (is_a) or based on object properties.

Note

The function assumes that there’s a utility function get_class_properties(c) available which retrieves properties of a given ontology class in a predefined format, especially the “annotation_properties” and “object_properties”.

Warning

The function omits the “Thing” class and its properties to avoid redundant information.

Returns

Name Type Description
nx.MultiDiGraph networkx.DiGraph: A directed graph representation of the ontology
nx.MultiDiGraph with nodes representing ontology classes and edges representing
nx.MultiDiGraph relationships.

Examples

>>> G = owl2nx_digraph()
>>> print(G.nodes(data=True))
[('ClassA', {'ID': 'id123', 'label': 'A'}), ...]
>>> print(G.edges(data=True))
[('ClassA', 'ClassB', {'type': 'is_a'}), ...]

Raises

Name Type Description
KeyError If expected properties are not found in the ontology class.
TypeError If the ontology structure differs from the expected format.