def test_segment_path_length(): leaves = [l for l in tr.ileaf(NEURON_TREE)] for l in leaves: nt.ok_(mtr.path_length(l) == 9) leaves = [l for l in tr.ileaf(SIMPLE_TREE)] for l in leaves: nt.ok_(mtr.path_length(l) == 8)
def branch_order(tree_section): '''Branching order of a tree section The branching order is defined as the depth of the tree section. Note: The first level has branch order 1. ''' node = tree_section[-1] bo = sum(1 for _ in tr.iforking_point(node, tr.iupstream)) return bo - 1 if tr.is_forking_point(node) else bo
def generate(self): '''Generate dendrogram ''' offsets = (0., 0.) n_previous = 0 # set recursion limit with respect to # the max number of nodes on the trees old_depth = sys.getrecursionlimit() max_depth = old_depth if old_depth > self._max_rec_depth else self._max_rec_depth # TODO: This should be fixed so we don't set sys.setrecursionlimit at all sys.setrecursionlimit(max_depth) if isinstance(self._obj, Neurite): max_diameter = _max_diameter(self._obj.root_node) dummy_section = Tree() dummy_section.add_child(self._obj.root_node) self._generate_dendro(dummy_section, (max_diameter, 0.), offsets) self._groups.append((0., self._n)) self._dims.append(self._max_dims) else: for neurite in self._obj.neurites: neurite = neurite.root_node max_diameter = _max_diameter(neurite) dummy_section = Tree() dummy_section.add_child(neurite) self._generate_dendro(dummy_section, (max_diameter, 0.), offsets) # store in trees the indices for the slice which corresponds # to the current neurite self._groups.append((n_previous, self._n)) # store the max dims per neurite for view positioning self._dims.append(self._max_dims) # reset the max dimensions for the next tree in line self._max_dims = [0., 0.] # keep track of the next tree start index in list n_previous = self._n # set it back to its initial value sys.setrecursionlimit(old_depth)
def compare_trees(tree1, tree2): ''' Comparison between all the nodes and their respective radii between two trees. Ids are do not have to be identical between the trees, and swapping is allowed Returns: False if the trees are not identical. True otherwise. ''' leaves1 = list(tr.ileaf(tree1)) leaves2 = list(tr.ileaf(tree2)) if len(leaves1) != len(leaves2): return False else: nleaves = len(leaves1) for leaf1, leaf2 in product(leaves1, leaves2): is_equal = True for node1, node2 in izip(val_iter(tr.iupstream(leaf1)), val_iter(tr.iupstream(leaf2))): if any(node1[0:5] != node2[0:5]): is_equal = False continue if is_equal: nleaves -= 1 return nleaves == 0
def partition(tree): '''Measures the distribution of sections to the children subtrees at each bifurcation point. Partition is defined as the max/min number of sections between the children subtrees of a bifurcation point. Returns: List of partition for each bifurcation point. ''' def partition_at_point(bif_point): '''Partition at each bif point.''' n = float(n_sections(bif_point.children[0])) m = float(n_sections(bif_point.children[1])) return max(n, m) / min(n, m) return [partition_at_point(i) for i in tr.ibifurcation_point(tree)]
def generate(self): '''Generate dendrogram ''' offsets = (0., 0.) n_previous = 0 # set recursion limit with respect to # the max number of nodes on the trees old_depth = sys.getrecursionlimit() max_depth = old_depth if old_depth > self._max_rec_depth else self._max_rec_depth # TODO: This should be fixed so we don't set sys.setrecursionlimit at all sys.setrecursionlimit(max_depth) if isinstance(self._obj, Neurite): max_diameter = _max_diameter(self._obj.root_node) dummy_section = Tree() dummy_section.add_child(self._obj.root_node) self._generate_dendro(dummy_section, (max_diameter, 0.), offsets) self._groups.append((0, self._n)) self._dims.append(self._max_dims) else: for neurite in self._obj.neurites: neurite = neurite.root_node max_diameter = _max_diameter(neurite) dummy_section = Tree() dummy_section.add_child(neurite) self._generate_dendro(dummy_section, (max_diameter, 0.), offsets) # store in trees the indices for the slice which corresponds # to the current neurite self._groups.append((n_previous, self._n)) # store the max dims per neurite for view positioning self._dims.append(self._max_dims) # reset the max dimensions for the next tree in line self._max_dims = [0., 0.] # keep track of the next tree start index in list n_previous = self._n # set it back to its initial value sys.setrecursionlimit(old_depth)
def _skip_0_length(p, c): '''Return the first child c with non-zero distance to parent p''' while np.all(p.value[:COLS.R] == c.value[:COLS.R])\ and not tr.is_leaf(c) and not tr.is_forking_point(c): c = c.children[0] return c
def n_terminations(tree): """ Return number of terminations in tree """ return sum(1 for _ in tr.ileaf(tree))
def n_bifurcations(tree): """ Return number of bifurcations in tree """ return sum(1 for _ in tr.ibifurcation_point(tree))