Skip to content

Commit

Permalink
Add node_info.depth property
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel committed Jan 13, 2021
1 parent 4b708ca commit 9ee8833
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nxontology/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ def n_descendants(self) -> int:
"""Number of descendants of node in graph, including itself."""
return len(self.descendants)

@property # type: ignore [misc]
@cache_on_frozen
def depth(self) -> int:
"""Minimum shortest path distance from a root node to this node."""
depth = min(
nx.shortest_path_length(self.nxo.graph, root, self.node)
for root in self.ancestors & self.nxo.roots
)
assert isinstance(depth, int)
return depth

@property # type: ignore [misc]
@cache_on_frozen
def intrinsic_ic(self) -> float:
Expand Down
10 changes: 10 additions & 0 deletions nxontology/tests/ontology_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def test_nxontology_leaves(metal_nxo_frozen: NXOntology) -> None:
assert leaves == {"copper", "gold", "palladium", "platinum", "silver"}


def test_node_info_root(metal_nxo_frozen: NXOntology) -> None:
"""Test metal node_info. Metal is the only root node."""
info = metal_nxo_frozen.node_info("metal")
assert info.node == "metal"
assert info.n_descendants == metal_nxo_frozen.n_nodes
assert info.n_ancestors == 1
assert info.depth == 0


def test_node_info_gold(metal_nxo_frozen: NXOntology) -> None:
print(metal_nxo_frozen.graph.graph)
gold_info = metal_nxo_frozen.node_info("gold")
Expand All @@ -81,6 +90,7 @@ def test_node_info_gold(metal_nxo_frozen: NXOntology) -> None:
assert gold_info.url is None
assert gold_info.n_descendants == 1
assert gold_info.n_ancestors == 4
assert gold_info.depth == 2


def test_set_graph_attributes(metal_nxo: NXOntology) -> None:
Expand Down

0 comments on commit 9ee8833

Please sign in to comment.