(linkage)
return T[root]
Return a Tree representation of a clustering encoded in a linkage matrix.

    .. seealso:: scipy.cluster.hierarchy.linkage
Return a Tree representation of a clustering encoded in a linkage matrix.
Return a Tree representation of a clustering encoded in a linkage matrix .
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)),
                         ())
            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]
    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]
def tree_from_linkage ( 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 ) ) , ( ) ) 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 ] 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 ]