hpotk.graph package

class hpotk.graph.OntologyGraph[source]

Bases: Generic[NODE]

A simple graph with one node type and one edge type.

The graph is generic over a node type which must extend TermId. The graph must not be empty, it must consist of at least one node.

Note

OntologyGraph provides iterators for traversals instead of sets, lists, etc. See Iterators vs. collections to learn why.

abstract property root: NODE

Get the root node of the ontology graph.

abstract get_children(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the children of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the children, False otherwise.

Raises:

ValueError – if source is not present in the graph.

abstract get_descendants(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the descendants of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the descendants, False otherwise.

Raises:

ValueError – if source is not present in the graph.

abstract get_parents(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the parents of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the parents, False otherwise.

Raises:

ValueError – if source is not present in the graph.

abstract get_ancestors(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the ancestors of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the ancestors, False otherwise.

Raises:

ValueError – if source is not present in the graph.

is_leaf(node: str | NODE | Identified) bool[source]

Test if the node is a leaf - a node with no children.

Returns:

True if the node is a leaf node or False otherwise.

Raises:

ValueError – if node is not present in the graph.

is_parent_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the subject sub is a parent of the object obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is a parent of the obj.

Raises:

ValueError – if obj is not present in the graph.

is_ancestor_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the subject sub is an ancestor of the object obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is an ancestor of the obj.

Raises:

ValueError – if obj is not present in the graph.

is_child_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the sub is a child of the obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is a child of the obj.

Raises:

ValueError – if obj is not present in the graph.

is_descendant_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the sub is a descendant of the obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is a descendant of the obj.

Raises:

ValueError – if obj is not present in the graph.

class hpotk.graph.IndexedOntologyGraph[source]

Bases: Generic[NODE], OntologyGraph[NODE]

IndexedOntologyGraph allows working with ontology graph node indices instead of the ontology graph nodes. Working in the index space is generally faster, when used to traverse the graph or to create term id unions, differences, etc…

Starting from a node index, IndexedOntologyGraph provides methods for getting indices of its children, descendants, parents, and ancestors. The node index can be obtained from node_to_idx(). Having an index, you can get the corresponding node using idx_to_node().

abstract property root_idx: int

Get the index of the root node of the ontology graph.

abstract get_children_idx(source: int) Sequence[int][source]

Get an iterator with the indices of the children of the source node.

Parameters:

source – an index of a node that represents the source node.

Raises:

ValueError – if source is not present in the graph.

abstract get_descendant_idx(source: int) Iterator[int][source]

Get an iterator with the indices of the descendants of the source node.

Parameters:

source – an index of a node that represents the source node.

Raises:

ValueError – if source is not present in the graph.

abstract get_parents_idx(source: int) Sequence[int][source]

Get an iterator with the indices of the parents of the source node.

Parameters:

source – an index of a node that represents the source node.

Raises:

ValueError – if source is not present in the graph.

abstract get_ancestor_idx(source: int) Iterator[int][source]

Get an iterator with the indices of the ancestors of the source node.

Parameters:

source – an index of a node that represents the source node.

Raises:

ValueError – if source is not present in the graph.

abstract idx_to_node(idx: int) NODE[source]

Map the index into the corresponding node.

Parameters:

idx – index to map to a node.

Returns:

the node corresponding to the index.

Raises:

ValueError – if idx does not correspond to any nodes of the ontology graph.

abstract node_to_idx(node: NODE) int | None[source]

Map the node into the corresponding node index.

Parameters:

node – node to retrieve an index for.

Returns:

the index corresponding to the node or None if the node is not in the graph.

property root: NODE

Get the root node of the ontology graph.

get_children(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the children of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the children, False otherwise.

Raises:

ValueError – if source is not present in the graph.

get_descendants(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the descendants of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the descendants, False otherwise.

Raises:

ValueError – if source is not present in the graph.

get_parents(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the parents of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the parents, False otherwise.

Raises:

ValueError – if source is not present in the graph.

get_ancestors(source: str | NODE | Identified, include_source: bool = False) Iterator[NODE][source]

Get an iterator with the ancestors of the source node.

Parameters:
  • source – a TermId, an item that has a TermId (Identified), or a curie str representing the source node.

  • include_sourceTrue if the source should be included among the ancestors, False otherwise.

Raises:

ValueError – if source is not present in the graph.

is_leaf(node: str | NODE | Identified) bool[source]

Test if the node is a leaf - a node with no children.

Returns:

True if the node is a leaf node or False otherwise.

Raises:

ValueError – if node is not present in the graph.

is_parent_of_idx(sub: int, obj: int) bool[source]

Return True if the subject sub is a parent of the object obj.

Parameters:
  • sub – index of a graph node.

  • obj – index of the other graph node.

Returns:

True if the sub is a parent of the obj.

Raises:

ValueError – if no such node exists for the obj index.

is_parent_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the subject sub is a parent of the object obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is a parent of the obj.

Raises:

ValueError – if obj is not present in the graph.

is_ancestor_of_idx(sub: int, obj: int) bool[source]

Return True if the subject sub is an ancestor of the object obj.

Parameters:
  • sub – index of a graph node.

  • obj – index of the other graph node.

Returns:

True if the sub is an ancestor of the obj.

Raises:

ValueError – if no such node exists for the obj index.

is_ancestor_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the subject sub is an ancestor of the object obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is an ancestor of the obj.

Raises:

ValueError – if obj is not present in the graph.

is_child_of_idx(sub: int, obj: int) bool[source]

Return True if the subject sub is a child of the object obj.

Parameters:
  • sub – index of a graph node.

  • obj – index of the other graph node.

Returns:

True if the sub is a child of the obj.

Raises:

ValueError – if no such node exists for the sub index.

is_child_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the sub is a child of the obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is a child of the obj.

Raises:

ValueError – if obj is not present in the graph.

is_descendant_of_idx(sub: int, obj: int) bool[source]

Return True if the subject sub is a descendant of the object obj.

Parameters:
  • sub – index of a graph node.

  • obj – index of the other graph node.

Returns:

True if the sub is a descendant of the obj.

Raises:

ValueError – if no such node exists for the sub index.

is_descendant_of(sub: str | NODE | Identified, obj: str | NODE | Identified) bool[source]

Return True if the sub is a descendant of the obj.

Parameters:
  • sub – a graph node.

  • obj – other graph node.

Returns:

True if the sub is a descendant of the obj.

Raises:

ValueError – if obj is not present in the graph.

class hpotk.graph.GraphFactory[source]

Bases: Generic[GRAPH]

Graph factory creates a graph from a list of TermId pairs.

abstract create_graph(edge_list: Sequence[Tuple[TermId, TermId]]) GRAPH[source]

Create graph from edge list.

Parameters:

edge_list – a sequence of directed edges - tuples where the first item is the source and the second item is the destination.

Returns:

the graph

class hpotk.graph.IncrementalCsrGraphFactory[source]

Bases: AbstractCsrGraphFactory

The CSR graph factory that builds the row, col and data in an incremental fashion.

class hpotk.graph.CsrIndexedGraphFactory[source]

Bases: GraphFactory[IndexedOntologyGraph]

CsrIndexedGraphFactory builds an IndexedOntologyGraph that is backed by two CSR arrays for storing the parent and child nodes of an ontology graph node.

create_graph(edge_list: Sequence[Tuple[TermId, TermId]]) GRAPH[source]

Create graph from edge list.

Parameters:

edge_list – a sequence of directed edges - tuples where the first item is the source and the second item is the destination.

Returns:

the graph

class hpotk.graph.GraphAware[source]

Bases: Generic[NODE]

A mixin class for entities that have an OntologyGraph.

abstract property graph: OntologyGraph[NODE]

Get the ontology graph.

Subpackages