Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"Return branches from `tree` in optimal order"
if tree.is_leaf:
return ()
elif is_swapped(tree):
return tree.branches
else:
return tuple(reversed(tree.branches))
# Create a new tree structure with optimally swapped branches.
T = {}
counter = count(0)
for tree in postorder(root, branches=swaped_branches):
if tree.is_leaf:
# we need to 're-enumerate' the leaves
i = next(counter)
T[tree] = Tree(tree.value._replace(range=(i, i + 1)), ())
else:
left, right = T[tree.left], T[tree.right]
if left.value.first > right.value.first:
right, left = left, right
assert left.value.first < right.value.last
assert left.value.last == right.value.first
T[tree] = Tree(
tree.value._replace(range=(left.value.first, right.value.last)),
(left, right),
)
return T[root]
def tree_from_linkage(linkage):
"""
Return a Tree representation of a clustering encoded in a linkage matrix.
.. seealso:: scipy.cluster.hierarchy.linkage
"""
scipy.cluster.hierarchy.is_valid_linkage(linkage, throw=True, name="linkage")
T = {}
N, _ = linkage.shape
N = N + 1
order = []
for i, (c1, c2, d, _) in enumerate(linkage):
if c1 < N:
left = Tree(
SingletonData(
range=(len(order), len(order) + 1), height=0.0, index=int(c1)
),
(),
)
order.append(c1)
else:
left = T[c1]
if c2 < N:
right = Tree(
SingletonData(
range=(len(order), len(order) + 1), height=0.0, index=int(c2)
),
(),
)
N = N + 1
order = []
for i, (c1, c2, d, _) in enumerate(linkage):
if c1 < N:
left = Tree(
SingletonData(
range=(len(order), len(order) + 1), height=0.0, index=int(c1)
),
(),
)
order.append(c1)
else:
left = T[c1]
if c2 < N:
right = Tree(
SingletonData(
range=(len(order), len(order) + 1), height=0.0, index=int(c2)
),
(),
)
order.append(c2)
else:
right = T[c2]
t = Tree(
ClusterData(range=(left.value.first, right.value.last), height=d),
(left, right),
)
T[N + i] = t
root = T[N + N - 2]
else:
right = T[c2]
t = Tree(
ClusterData(range=(left.value.first, right.value.last), height=d),
(left, right),
)
T[N + i] = t
root = T[N + N - 2]
T = {}
leaf_idx = 0
for node in postorder(root):
if node.is_leaf:
T[node] = Tree(node.value._replace(range=(leaf_idx, leaf_idx + 1)), ())
leaf_idx += 1
else:
left, right = T[node.left].value, T[node.right].value
assert left.first < right.first
t = Tree(
node.value._replace(range=(left.range[0], right.range[1])),
tuple(T[ch] for ch in node.branches),
)
assert t.value.range[0] <= t.value.range[-1]
assert left.first == t.value.first and right.last == t.value.last
assert t.value.first < right.first
assert t.value.last > left.last
T[node] = t
return T[root]
)
T[N + i] = t
root = T[N + N - 2]
T = {}
leaf_idx = 0
for node in postorder(root):
if node.is_leaf:
T[node] = Tree(node.value._replace(range=(leaf_idx, leaf_idx + 1)), ())
leaf_idx += 1
else:
left, right = T[node.left].value, T[node.right].value
assert left.first < right.first
t = Tree(
node.value._replace(range=(left.range[0], right.range[1])),
tuple(T[ch] for ch in node.branches),
)
assert t.value.range[0] <= t.value.range[-1]
assert left.first == t.value.first and right.last == t.value.last
assert t.value.first < right.first
assert t.value.last > left.last
T[node] = t
return T[root]