Example #1
0
def test_leftsibling():
    """leftsibling."""
    dan = Node("Dan")
    jet = Node("Jet", parent=dan)
    jan = Node("Jan", parent=dan)
    joe = Node("Joe", parent=dan)
    eq_(leftsibling(dan), None)
    eq_(leftsibling(jet), None)
    eq_(leftsibling(jan), jet)
    eq_(leftsibling(joe), jan)
def test_get_previous_sibling():
    template = XMLTemplateParser("""
        <template name="root" sizing="auto">
            <area name="child0" size="4"></area>
            <area name="child1" size="4"></area>
            <area name="child2" size="4"></area>
            <area name="child3" size="4"></area>
            <area name="child4" size="4"></area>
        </template>
    """).parse()

    assert template.child1 == leftsibling(template.child2)
    assert None == leftsibling(template.child0)
    assert template.child3 == leftsibling(template.child4)
Example #3
0
    def build_linkage(self):
        # get a tuple of node at each level
        levels = []
        for group in LevelOrderGroupIter(self):
            levels.append(group)

        # just find how many nodes are leaves
        # this is necessary only because we need to add n to non-leaf clusters
        num_leaves = 0
        for node in PostOrderIter(self):
            if not node.children:
                num_leaves += 1

        link_count = 0
        node_index = 0
        linkages = []
        labels = []

        for g, group in enumerate(
                levels[::-1][:-1]):  # reversed and skip the last
            for i in range(len(group) // 2):
                # get partner nodes
                left_node = group[2 * i]
                right_node = group[2 * i + 1]
                # just double check that these are always partners
                assert leftsibling(right_node) == left_node

                # check if leaves, need to add some new fields to track for linkage
                if not left_node.children:
                    left_node._ind = node_index
                    left_node._n_clusters = 1
                    node_index += 1
                    labels.append(left_node.name)

                if not right_node.children:
                    right_node._ind = node_index
                    right_node._n_clusters = 1
                    node_index += 1
                    labels.append(right_node.name)

                # find the parent, count samples
                parent_node = left_node.parent
                n_clusters = left_node._n_clusters + right_node._n_clusters
                parent_node._n_clusters = n_clusters

                # assign an ind to this cluster for the dendrogram
                parent_node._ind = link_count + num_leaves
                link_count += 1

                distance = g + 1  # equal height for all links

                # add a row to the linkage matrix
                linkages.append(
                    [left_node._ind, right_node._ind, distance, n_clusters])

        labels = np.array(labels)
        linkages = np.array(linkages,
                            dtype=np.double)  # needs to be a double for scipy
        return (linkages, labels)
Example #4
0
    def getneighbour(self, left_right: str) -> ProofNode:
        """Zwraca prawego, lub lewego sąsiada danego węzła. 
        Równie dobrze można używać `anytree.util.rightsibling(self)` oraz `anytree.util.leftsibling(self)`

        :param left_right: 'L/Left' lub 'R/Right'
        :type left_right: str
        :raises ProofNodeError: Podano niepoprawny kierunek
        :return: Sąsiad
        :rtype: ProofNode
        """
        if not self.parent:
            return None
        if left_right.upper() in ('R', 'RIGHT'):
            return util.rightsibling(self)
        elif left_right.upper() in ('L', 'LEFT'):
            return util.leftsibling(self)
        else:
            raise ProofNodeError(f"'{left_right}' is not a valid direction")
Example #5
0
def is_right_argument(child_node):
    return(leftsibling(child_node) != None) 
Example #6
0
    def build_linkage(self, bic_distance=False):
        # get a tuple of node at each level
        levels = []
        for group in LevelOrderGroupIter(self):
            levels.append(group)

        # just find how many nodes are leaves
        # this is necessary only because we need to add n to non-leaf clusters
        num_leaves = 0
        for node in PostOrderIter(self):
            if not node.children:
                num_leaves += 1

        link_count = 0
        node_index = 0
        linkages = []
        labels = []

        for g, group in enumerate(levels[::-1][:-1]):  # reversed and skip the last
            for i in range(len(group) // 2):
                # get partner nodes
                left_node = group[2 * i]
                right_node = group[2 * i + 1]
                # just double check that these are always partners
                assert leftsibling(right_node) == left_node

                # check if leaves, need to add some new fields to track for linkage
                if not left_node.children:
                    left_node._ind = node_index
                    left_node._n_clusters = 1
                    node_index += 1
                    labels.append(left_node.name)

                if not right_node.children:
                    right_node._ind = node_index
                    right_node._n_clusters = 1
                    node_index += 1
                    labels.append(right_node.name)

                # find the parent, count samples
                parent_node = left_node.parent
                n_clusters = left_node._n_clusters + right_node._n_clusters
                parent_node._n_clusters = n_clusters

                # assign an ind to this cluster for the dendrogram
                parent_node._ind = link_count + num_leaves
                link_count += 1

                if not bic_distance:
                    distance = g + 1  # equal height for all links
                else:
                    raise NotImplementedError()
                    # tried to use BIC as linkage distance, but not monotonic.
                    # would need to sort somehow by BIC ratios, but this may not be
                    # possible while preserving splitting nature of the tree

                    # self.cum_dist_ += (left_node.cum_dist_ + right_node.cum_dist_) / 2
                    # self.cum_dist_ += parent_node.bic_ratio_ - 1
                    # distance = self.cum_dist_
                    distance = parent_node.bic_ratio_ - 1

                # add a row to the linkage matrix
                linkages.append([left_node._ind, right_node._ind, distance, n_clusters])

        labels = np.array(labels)
        linkages = np.array(linkages, dtype=np.double)  # needs to be a double for scipy
        return (linkages, labels)