def choices(tree):
    """
    Get the 'address' of each leaf node in terms of internal
    node choices
    """
    n = len(leaves(tree))
    addr = np.nan * np.ones((n, n-1))
    def _addresses(node, index, choices):
        # index is the index of the current internal node
        # choices is a list of (indice, 0/1) choices made
        if np.isscalar(node):
            for i, choice in choices:
                addr[node, i] = choice
            return index

        elif isinstance(node, tuple) and len(node) == 2:
            newindex = _addresses(node[0], index+1, choices + [(index, 0)])
            newindex = _addresses(node[1], newindex, choices + [(index, 1)])
            return newindex

        else:
            raise Exception("Not a tree!")

    _addresses(tree, 0, [])
    return addr
def choices ( tree ) : n = len ( leaves ( tree ) ) addr = np . nan * np . ones ( ( n , n - 1 ) ) def _addresses ( node , index , choices ) : if np . isscalar ( node ) : for i , choice in choices : addr [ node , i ] = choice return index elif isinstance ( node , tuple ) and len ( node ) == 2 : newindex = _addresses ( node [ 0 ] , index + 1 , choices + [ ( index , 0 ) ] ) newindex = _addresses ( node [ 1 ] , newindex , choices + [ ( index , 1 ) ] ) return newindex else : raise Exception ( "Not a tree!" ) _addresses ( tree , 0 , [ ] ) return addr
Get the 'address' of each leaf node in terms of internal
    node choices