-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from YosefLab/add-depth
add depth
- Loading branch information
Showing
7 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .setup_tree import add_depth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
|
||
import networkx as nx | ||
import treedata as td | ||
|
||
from pycea.utils import get_keyed_node_data, get_root, get_trees | ||
|
||
|
||
def _add_depth(tree, depth_key): | ||
"""Adds a depth attribute to the nodes of a tree.""" | ||
root = get_root(tree) | ||
depths = nx.single_source_shortest_path_length(tree, root) | ||
nx.set_node_attributes(tree, depths, depth_key) | ||
|
||
|
||
def add_depth( | ||
tdata: td.TreeData, depth_key: str = "depth", tree: str | Sequence[str] | None = None, copy: bool = False | ||
): | ||
"""Adds a depth attribute to the nodes of a tree. | ||
Parameters | ||
---------- | ||
tdata | ||
TreeData object. | ||
depth_key | ||
Node attribute key to store the depth. | ||
tree | ||
The `obst` key or keys of the trees to use. If `None`, all trees are used. | ||
copy | ||
If True, returns a pd.DataFrame node depths. | ||
""" | ||
tree_keys = tree | ||
trees = get_trees(tdata, tree_keys) | ||
for _, tree in trees.items(): | ||
_add_depth(tree, depth_key) | ||
if copy: | ||
return get_keyed_node_data(tdata, depth_key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import networkx as nx | ||
import pandas as pd | ||
import pytest | ||
import treedata as td | ||
|
||
from pycea.pp.setup_tree import add_depth | ||
|
||
|
||
@pytest.fixture | ||
def tdata(): | ||
tree1 = nx.DiGraph([("root", "A"), ("root", "B"), ("B", "C"), ("B", "D")]) | ||
tree2 = nx.DiGraph([("root", "E"), ("root", "F")]) | ||
tdata = td.TreeData(obs=pd.DataFrame(index=["A", "C", "D", "E", "F"]), obst={"tree1": tree1, "tree2": tree2}) | ||
yield tdata | ||
|
||
|
||
def test_add_depth(tdata): | ||
depths = add_depth(tdata, depth_key="depth", copy=True) | ||
assert depths.loc[("tree1", "root"), "depth"] == 0 | ||
assert depths.loc[("tree1", "C"), "depth"] == 2 | ||
assert tdata.obst["tree1"].nodes["root"]["depth"] == 0 | ||
assert tdata.obst["tree1"].nodes["C"]["depth"] == 2 | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main(["-v", __file__]) |