def _construct_branch_map(self, ix, interval, form, horizontal_spacing,
                              sort):

        ## get children
        children = _np.array(self.nodes[ix].children)
        n_child = len(children)

        ## if there's no children, just one segment at the interval mean
        if n_child == 0:
            xpos = _np.mean(interval)
            segments = {}
            segmap = [ix]
            splits = {}
            splitmap = []

            if form == 'density':
                segments[ix] = (
                    ([xpos, self.nodes[ix].start_level],
                     [xpos, self.nodes[ix].end_level]))
            else:
                segments[ix] = (
                    ([xpos, self.nodes[ix].start_mass],
                     [xpos, self.nodes[ix].end_mass]))

        ## else, construct child branches then figure out parent's
        ## position
        else:
            parent_range = interval[1] - interval[0]
            segments = {}
            segmap = [ix]
            splits = {}
            splitmap = []

            census = _np.array([len(self.nodes[x].members) for x in children],
                               dtype=_np.float)
            weights = census / sum(census)

            if sort is True:
                seniority = _np.argsort(weights)[::-1]
                children = children[seniority]
                weights = weights[seniority]

            ## get relative branch intervals
            if horizontal_spacing == 'uniform':
                child_intervals = _np.cumsum(weights)
                child_intervals = _np.insert(child_intervals, 0, 0.0)

            elif horizontal_spacing == 'proportional':
                child_intervals = _np.linspace(0.0, 1.0, n_child + 1)

            else:
                raise ValueError("'horizontal_spacing' argument not " +
                                 "understood. 'horizontal_spacing' " +
                                 "must be either 'uniform' or 'proportional'.")

            ## loop over the children
            for j, child in enumerate(children):

                ## translate local interval to absolute interval
                branch_interval = (
                    interval[0] + child_intervals[j] * parent_range,
                    interval[0] + child_intervals[j + 1] * parent_range)

                ## recurse on the child
                branch = self._construct_branch_map(child, branch_interval,
                                                    form, horizontal_spacing,
                                                    sort)
                branch_segs, branch_splits, branch_segmap, \
                    branch_splitmap = branch

                segmap += branch_segmap
                splitmap += branch_splitmap
                splits = dict(splits.items() + branch_splits.items())
                segments = dict(segments.items() + branch_segs.items())

            ## find the middle of the children's x-position and make vertical
            #  segment ix
            children_xpos = _np.array([segments[k][0][0] for k in children])
            xpos = _np.mean(children_xpos)

            ## add horizontal segments to the list
            for child in children:
                splitmap.append(child)
                child_xpos = segments[child][0][0]

                if form == 'density':
                    splits[child] = (
                        [xpos, self.nodes[ix].end_level],
                        [child_xpos, self.nodes[ix].end_level])
                else:
                    splits[child] = (
                        [xpos, self.nodes[ix].end_mass],
                        [child_xpos, self.nodes[ix].end_mass])

            ## add vertical segment for current node
            if form == 'density':
                segments[ix] = (
                    ([xpos, self.nodes[ix].start_level],
                     [xpos, self.nodes[ix].end_level]))
            else:
                segments[ix] = (
                    ([xpos, self.nodes[ix].start_mass],
                     [xpos, self.nodes[ix].end_mass]))

        return segments, splits, segmap, splitmap
Map level set tree nodes to locations in a plot canvas. Finds the plot
dataset/ETHPy150Open CoAxLab/DeBaCl/debacl/level_set_tree.py/LevelSetTree._construct_branch_map