-
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 #7 from YosefLab/distance
Distance
- Loading branch information
Showing
27 changed files
with
1,573 additions
and
155 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
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
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from .ancestral_states import ancestral_states | ||
from .clades import clades | ||
from .distance import compare_distance, distance | ||
from .sort import sort | ||
from .ancestral_states import ancestral_states | ||
from .tree_distance import tree_distance | ||
from .tree_neighbors import tree_neighbors |
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,63 @@ | ||
from collections.abc import Callable | ||
from typing import Literal | ||
|
||
import numpy as np | ||
import treedata as td | ||
|
||
_MetricFn = Callable[[np.ndarray, np.ndarray], float] | ||
|
||
_Metric = Literal[ | ||
"braycurtis", | ||
"canberra", | ||
"chebyshev", | ||
"cityblock", | ||
"cosine", | ||
"correlation", | ||
"dice", | ||
"euclidean", | ||
"hamming", | ||
"jaccard", | ||
"kulsinski", | ||
"l1", | ||
"l2", | ||
"mahalanobis", | ||
"minkowski", | ||
"manhattan", | ||
"rogerstanimoto", | ||
"russellrao", | ||
"seuclidean", | ||
"sokalmichener", | ||
"sokalsneath", | ||
"sqeuclidean", | ||
"yule", | ||
] | ||
|
||
|
||
def _lca_distance(tree, depth_key, node1, node2, lca): | ||
"""Compute the lca distance between two nodes in a tree.""" | ||
if node1 == node2: | ||
return tree.nodes[node1][depth_key] | ||
else: | ||
return tree.nodes[lca][depth_key] | ||
|
||
|
||
def _path_distance(tree, depth_key, node1, node2, lca): | ||
"""Compute the path distance between two nodes in a tree.""" | ||
if node1 == node2: | ||
return 0 | ||
else: | ||
return abs(tree.nodes[node1][depth_key] + tree.nodes[node2][depth_key] - 2 * tree.nodes[lca][depth_key]) | ||
|
||
|
||
_TreeMetricFn = Callable[[td.TreeData, str, str, str, str], np.ndarray] | ||
|
||
_TreeMetric = Literal["lca", "path"] | ||
|
||
|
||
def _get_tree_metric(metric: str) -> _TreeMetricFn: | ||
if metric == "lca": | ||
return _lca_distance | ||
elif metric == "path": | ||
return _path_distance | ||
else: | ||
raise ValueError(f"Unknown metric: {metric}. Valid metrics are 'lca' and 'path'.") |
Oops, something went wrong.