def test_add_child(): t = Tree(0) t.add_child(Tree(11)) t.add_child(Tree(22)) nt.ok_(t.value == 0) nt.ok_(len(t.children) == 2) nt.ok_([i.value for i in t.children] == [11, 22])
def test_is_forking_point(): t = Tree(0) t.add_child(Tree(1)) t.add_child(Tree(2)) nt.ok_(is_forking_point(t)) t.add_child(Tree(3)) nt.ok_(is_forking_point(t))
def make_tree(rdw, root_id=ROOT_ID, post_action=None): '''Return a tree obtained from a raw data block The tree contains rows of raw data. Args: rdw: a RawDataWrapper object. root_id: ID of the root of the tree to be built. post_action: optional function to run on the built tree. ''' head_node = Tree(rdw.get_row(root_id)) children = [ head_node, ] while children: cur_node = children.pop() for c in rdw.get_children(cur_node.value[COLS.ID]): row = rdw.get_row(c) child = Tree(row) cur_node.add_child(child) children.append(child) if post_action is not None: post_action(head_node) return head_node
def test_is_bifurcation_point_false(): t = Tree(0) nt.ok_(not is_bifurcation_point(t)) t.add_child(Tree(1)) nt.ok_(not is_bifurcation_point(t)) t.add_child(Tree(2)) t.add_child(Tree(3)) nt.ok_(not is_bifurcation_point(t))
def test_deep_iteration(): root = t = Tree(0) for i in range(1, sys.getrecursionlimit() + 2): child = Tree(i) t.add_child(child) t = child list(ipreorder(root)) list(ipostorder(root)) list(iupstream(t))
def test_parent(): t = Tree(0) for i in xrange(10): t.add_child(Tree(i)) nt.ok_(len(t.children) == 10) for c in t.children: nt.ok_(c.parent is t)
def _make_tree(): '''This tree has 3 branching points''' p = [0.0, 0.0, 0.0, 1.0, 1, 1, 2] T = Tree(p) T1 = T.add_child(Tree([0.0, 1.0, 0.0, 2.0, 1, 1, 2])) T2 = T1.add_child(Tree([0.0, 2.0, 0.0, 3.0, 1, 1, 2])) T3 = T2.add_child(Tree([0.0, 4.0, 0.0, 4.0, 1, 1, 2])) T4 = T3.add_child(Tree([0.0, 5.0, 0.0, 5.0, 1, 1, 2])) T5 = T4.add_child(Tree([2.0, 5.0, 0.0, 6.0, 1, 1, 2])) T6 = T4.add_child(Tree([0.0, 5.0, 2.0, 7.0, 1, 1, 2])) return T
def _make_simple_tree(): p = [0.0, 0.0, 0.0, 1.0, 1, 1, 1] T = Tree(p) T1 = T.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 1])) T2 = T1.add_child(Tree([2.0, 2.0, 0.0, 1.0, 1, 1, 1])) T3 = T2.add_child(Tree([2.0, 6.0, 0.0, 1.0, 1, 1, 1])) T5 = T.add_child(Tree([0.0, 0.0, 2.0, 1.0, 1, 1, 1])) T6 = T5.add_child(Tree([2.0, 0.0, 2.0, 1.0, 1, 1, 1])) T7 = T6.add_child(Tree([6.0, 0.0, 2.0, 1.0, 1, 1, 1])) return T
def _create_root_soma_tree(neuron): ''' soma segment to represent the soma as a square of radius equal to the soma one ''' soma_radius = neuron.get_soma_radius() soma_node0 = Tree((0., 0., 0., soma_radius, 1.)) soma_node1 = Tree((soma_radius, 0., 0., soma_radius, 1.)) soma_node0.add_child(soma_node1) return soma_node0
def _make_neuron_tree(): p = [0.0, 0.0, 0.0, 1.0, 1, 1, 2] T = Tree(p) T1 = T.add_child(Tree([0.0, 1.0, 0.0, 1.0, 1, 1, 2])) T2 = T1.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 2])) T3 = T2.add_child(Tree([0.0, 4.0, 0.0, 2.0, 1, 1, 2])) T4 = T3.add_child(Tree([0.0, 5.0, 0.0, 2.0, 1, 1, 2])) T5 = T4.add_child(Tree([2.0, 5.0, 0.0, 1.0, 1, 1, 2])) T6 = T4.add_child(Tree([0.0, 5.0, 2.0, 1.0, 1, 1, 2])) T7 = T5.add_child(Tree([3.0, 5.0, 0.0, 0.75, 1, 1, 2])) T8 = T7.add_child(Tree([4.0, 5.0, 0.0, 0.75, 1, 1, 2])) T9 = T6.add_child(Tree([0.0, 5.0, 3.0, 0.75, 1, 1, 2])) T10 = T9.add_child(Tree([0.0, 6.0, 3.0, 0.75, 1, 1, 2])) return T
def test_trunk_elevation(): t = Tree((1, 0, 0, 2)) s = make_soma([[0, 0, 0, 4]]) nt.assert_equal(trunk_elevation(t, s), 0.0) t = Tree((0, 1, 0, 2)) nt.assert_equal(trunk_elevation(t, s), np.pi/2) t = Tree((0, -1, 0, 2)) nt.assert_equal(trunk_elevation(t, s), -np.pi/2) t = Tree((0, 0, 0, 2)) try: trunk_elevation(t, s) nt.ok_(False) except ValueError: nt.ok_(True)
def test_principal_direction_extent(): points = np.array([[-10., 0., 0.], [-9., 0., 0.], [9., 0., 0.], [10., 0., 0.]]) tree = Tree(np.array([points[0][0], points[0][1], points[0][2], 1., 0., 0.])) tree.add_child(Tree(np.array([points[1][0], points[1][1], points[1][2], 1., 0., 0.]))) tree.children[0].add_child(Tree(np.array([points[2][0], points[2][1], points[2][2], 1., 0., 0.]))) tree.children[0].add_child(Tree(np.array([points[3][0], points[3][1], points[3][2], 1., 0., 0.]))) extent = mtr.principal_direction_extent(tree) nt.assert_true(np.allclose(extent, [20., 0., 0.]))
def test_is_bifurcation_point_false(): t = Tree() nt.ok_(not t.is_bifurcation_point()) t.add_child(Tree()) nt.ok_(not t.is_bifurcation_point()) t.add_child(Tree()) t.add_child(Tree()) nt.ok_(not t.is_bifurcation_point())
def _create_root_soma_tree(neuron): ''' soma segment to represent the soma as a square of radius equal to the soma one ''' soma_radius = neuron.get_soma_radius() soma_center = neuron.soma.center soma_node0 = Tree((soma_center[0] - soma_radius, soma_center[1], soma_center[2], soma_radius, 1.)) soma_node1 = Tree( (soma_center[0], soma_center[1], soma_center[2], soma_radius, 1.)) soma_node0.add_child(soma_node1) return soma_node0
def _form_branching_tree(): p = [0.0, 0.0, 0.0, 1.0, 1, 1, 2] T = Tree(p) T1 = T.add_child(Tree([0.0, 1.0, 0.0, 1.0, 1, 1, 2])) T2 = T1.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 2])) T3 = T2.add_child(Tree([0.0, 4.0, 0.0, 2.0, 1, 1, 2])) T4 = T3.add_child(Tree([0.0, 5.0, 0.0, 2.0, 1, 1, 2])) T5 = T4.add_child(Tree([2.0, 5.0, 0.0, 1.0, 1, 1, 2])) T6 = T4.add_child(Tree([0.0, 5.0, 2.0, 1.0, 1, 1, 2])) T7 = T5.add_child(Tree([3.0, 5.0, 0.0, 0.75, 1, 1, 2])) T8 = T7.add_child(Tree([4.0, 5.0, 0.0, 0.75, 1, 1, 2])) T9 = T6.add_child(Tree([0.0, 5.0, 3.0, 0.75, 1, 1, 2])) T10 = T9.add_child(Tree([0.0, 6.0, 3.0, 0.75, 1, 1, 2])) T11 = T9.add_child(Tree([0.0, 6.0, 4.0, 0.75, 1, 1, 2])) T33 = T3.add_child(Tree([1.0, 5.0, 0.0, 2.0, 1, 1, 2])) T331 = T33.add_child(Tree([15.0, 15.0, 0.0, 2.0, 1, 1, 2])) return T
def test_is_forking_point(): t = Tree() t.add_child(Tree()) t.add_child(Tree()) nt.ok_(t.is_forking_point()) t.add_child(Tree()) nt.ok_(t.is_forking_point())
def _make_branching_tree(): '''This tree has 3 branching points''' p = [0.0, 0.0, 0.0, 1.0, 1, 1, 2] T = Tree(p) T1 = T.add_child(Tree([0.0, 1.0, 0.0, 1.0, 1, 1, 2])) T2 = T1.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 2])) T3 = T2.add_child(Tree([0.0, 4.0, 0.0, 2.0, 1, 1, 2])) T4 = T3.add_child(Tree([0.0, 5.0, 0.0, 2.0, 1, 1, 2])) T5 = T4.add_child(Tree([2.0, 5.0, 0.0, 1.0, 1, 1, 2])) T6 = T4.add_child(Tree([0.0, 5.0, 2.0, 1.0, 1, 1, 2])) T7 = T5.add_child(Tree([3.0, 5.0, 0.0, 0.75, 1, 1, 2])) T8 = T7.add_child(Tree([4.0, 5.0, 0.0, 0.75, 1, 1, 2])) T9 = T6.add_child(Tree([0.0, 5.0, 3.0, 0.75, 1, 1, 2])) T10 = T9.add_child(Tree([0.0, 6.0, 3.0, 0.75, 1, 1, 2])) T11 = T9.add_child(Tree([0.0, 6.0, 4.0, 0.75, 1, 1, 2])) T33 = T3.add_child(Tree([1.0, 5.0, 0.0, 2.0, 1, 1, 2])) T331 = T33.add_child(Tree([15.0, 15.0, 0.0, 2.0, 1, 1, 2])) return T
def test_trunk_azimuth(): t = Tree((0, 0, 0, 2)) s = make_soma([[0, 0, 1, 4]]) nt.assert_equal(trunk_azimuth(t, s), -np.pi/2) s = make_soma([[0, 0, -1, 4]]) nt.assert_equal(trunk_azimuth(t, s), np.pi/2) s = make_soma([[0, 0, 0, 4]]) nt.assert_equal(trunk_azimuth(t, s), 0.0) s = make_soma([[-1, 0, -1, 4]]) nt.assert_equal(trunk_azimuth(t, s), np.pi/4) s = make_soma([[-1, 0, 0, 4]]) nt.assert_equal(trunk_azimuth(t, s), 0.0) s = make_soma([[1, 0, 0, 4]]) nt.assert_equal(trunk_azimuth(t, s), np.pi)
def test_is_root_false(): t = Tree(0) t.add_child(Tree(1)) nt.ok_(not is_root(t.children[0]))
def test_is_leaf(): nt.ok_(is_leaf(Tree(0)))
def test_is_leaf_false(): t = Tree() t.add_child(Tree()) nt.ok_(not t.is_leaf())
from itertools import izip DATA_PATH = './test_data' SWC_PATH = os.path.join(DATA_PATH, 'swc/') data = load_data(SWC_PATH + 'Neuron.swc') neuron0 = make_neuron(data) tree0 = neuron0.neurites[0] tree_types = [TreeType.axon, TreeType.basal_dendrite, TreeType.basal_dendrite, TreeType.apical_dendrite] # Mock tree holding integers, not points MOCK_TREE = Tree(0) MOCK_TREE.add_child(Tree(11)) MOCK_TREE.add_child(Tree(12)) MOCK_TREE.children[0].add_child(Tree(111)) MOCK_TREE.children[0].add_child(Tree(112)) MOCK_TREE.children[1].add_child(Tree(121)) MOCK_TREE.children[1].add_child(Tree(122)) MOCK_TREE.children[1].children[0].add_child(Tree(1211)) MOCK_TREE.children[1].children[0].children[0].add_child(Tree(12111)) MOCK_TREE.children[1].children[0].children[0].add_child(Tree(12112)) T1111 = MOCK_TREE.children[0].children[0].add_child(Tree(1111)) T11111 = T1111.add_child(Tree(11111)) T11112 = T1111.add_child(Tree(11112)) T11113 = T1111.add_child(Tree(11113))
def test_str(): t = Tree('hello') nt.ok_(str(t))
def test_is_forking_point_false(): t = Tree(0) nt.ok_(not is_forking_point(t)) t.add_child(Tree(1)) nt.ok_(not is_forking_point(t))
def test_is_root_false(): t = Tree() t.add_child(Tree()) nt.ok_(not t.children[0].is_root())
def test_is_forking_point_false(): t = Tree() nt.ok_(not t.is_forking_point()) t.add_child(Tree()) nt.ok_(not t.is_forking_point())
[11, 22, 33, 44, 3, 1, 2], [11, 22, 33, 44, 4, 1, 3], [11, 22, 33, 44, 5, 1, 4], [11, 22, 33, 44, 6, 1, 5] ] INVALID_PTS_0 = [] INVALID_PTS_2 = [ [11, 22, 33, 44, 1, 1, -1], [11, 22, 33, 44, 2, 1, 1] ] TREE = Tree([0.0, 0.0, 0.0, 1.0, 1, 1, 2] ) T1 = TREE.add_child(Tree([0.0, 1.0, 0.0, 1.0, 1, 1, 2])) T2 = T1.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 2])) T3 = T2.add_child(Tree([0.0, 4.0, 0.0, 2.0, 1, 1, 2])) T4 = T3.add_child(Tree([0.0, 5.0, 0.0, 2.0, 1, 1, 2])) T5 = T4.add_child(Tree([2.0, 5.0, 0.0, 1.0, 1, 1, 2])) T6 = T4.add_child(Tree([0.0, 5.0, 2.0, 1.0, 1, 1, 2])) T7 = T5.add_child(Tree([3.0, 5.0, 0.0, 0.75, 1, 1, 2])) T8 = T7.add_child(Tree([4.0, 5.0, 0.0, 0.75, 1, 1, 2])) T9 = T6.add_child(Tree([0.0, 5.0, 3.0, 0.75, 1, 1, 2])) T10 = T9.add_child(Tree([0.0, 6.0, 3.0, 0.75, 1, 1, 2])) def test_make_SomaA(): soma = neuron.make_soma(SOMA_A_PTS) nt.ok_('SomaA' in str(soma)) nt.ok_(isinstance(soma, neuron.SomaA))
def test_is_bifurcation_point(): t = Tree(0) t.add_child(Tree(1)) t.add_child(Tree(2)) nt.ok_(is_bifurcation_point(t))
def test_is_root_true(): t = Tree() nt.ok_(Tree.is_root(t)) nt.ok_(t.is_root())
def test_is_bifurcation_point(): t = Tree() t.add_child(Tree()) t.add_child(Tree()) nt.ok_(t.is_bifurcation_point())
def test_is_leaf_false(): t = Tree(0) t.add_child(Tree(1)) nt.ok_(not is_leaf(t))
def test_trunk_length(): t = Tree((0, 0, 0, 42)) tt = t.add_child(Tree((10, 0, 0, 4))) tt.add_child(Tree((10, 15, 0, 4))) nt.assert_almost_equal(trunk_length(t), 25.0)
def test_is_leaf(): nt.ok_(Tree().is_leaf())
def test_trunk_direction(): t = Tree((1, 0, 0, 2)) s = make_soma([[0, 0, 0, 4]]) nt.ok_(np.allclose(trunk_direction(t, s), np.array([1, 0, 0])))
from neurom.core.tree import is_forking_point from neurom.core.tree import is_bifurcation_point from neurom.core.tree import ipreorder from neurom.core.tree import ipostorder from neurom.core.tree import iupstream from neurom.core.tree import isegment from neurom.core.tree import ileaf from neurom.core.tree import itriplet from neurom.core.tree import iforking_point from neurom.core.tree import ibifurcation_point from neurom.core.tree import isection from neurom.core.tree import val_iter from neurom.core.tree import i_branch_end_points from copy import deepcopy REF_TREE = Tree(0) REF_TREE.add_child(Tree(11)) REF_TREE.add_child(Tree(12)) REF_TREE.children[0].add_child(Tree(111)) REF_TREE.children[0].add_child(Tree(112)) REF_TREE.children[1].add_child(Tree(121)) REF_TREE.children[1].add_child(Tree(122)) REF_TREE.children[1].children[0].add_child(Tree(1211)) REF_TREE.children[1].children[0].children[0].add_child(Tree(12111)) REF_TREE.children[1].children[0].children[0].add_child(Tree(12112)) REF_TREE2 = deepcopy(REF_TREE) T1111 = REF_TREE2.children[0].children[0].add_child(Tree(1111)) T11111 = T1111.add_child(Tree(11111)) T11112 = T1111.add_child(Tree(11112)) T11113 = T1111.add_child(Tree(11113))
def test_instantiate_tree(): t = Tree('hello') nt.ok_(t.parent is None) nt.ok_(t.value == 'hello') nt.ok_(len(t.children) == 0)
def _form_simple_tree(): p = [0.0, 0.0, 0.0, 1.0, 1, 1, 1] T = Tree(p) T1 = T.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 1])) T2 = T1.add_child(Tree([0.0, 4.0, 0.0, 1.0, 1, 1, 1])) T3 = T2.add_child(Tree([0.0, 6.0, 0.0, 1.0, 1, 1, 1])) T4 = T3.add_child(Tree([0.0, 8.0, 0.0, 1.0, 1, 1, 1])) T5 = T.add_child(Tree([0.0, 0.0, 2.0, 1.0, 1, 1, 1])) T6 = T5.add_child(Tree([0.0, 0.0, 4.0, 1.0, 1, 1, 1])) T7 = T6.add_child(Tree([0.0, 0.0, 6.0, 1.0, 1, 1, 1])) T8 = T7.add_child(Tree([0.0, 0.0, 8.0, 1.0, 1, 1, 1])) return T
def test_is_root_true(): t = Tree(0) nt.ok_(is_root(t))
def test_trunk_radius(): t = Tree((0, 0, 0, 42)) t.add_child(Tree((1, 0, 0, 4))) nt.assert_equal(trunk_radius(t), 42.0)
def test_trunk_radius_length_point_tree(): t = Tree((0, 0, 0, 42)) nt.assert_equal(trunk_length(t), 0.0)
def _form_neuron_tree(): p = [0.0, 0.0, 0.0, 1.0, 1, 1, 2] T = Tree(p) T1 = T.add_child(Tree([0.0, 1.0, 0.0, 1.0, 1, 1, 2])) T2 = T1.add_child(Tree([0.0, 2.0, 0.0, 1.0, 1, 1, 2])) T3 = T2.add_child(Tree([0.0, 4.0, 0.0, 2.0, 1, 1, 2])) T4 = T3.add_child(Tree([0.0, 5.0, 0.0, 2.0, 1, 1, 2])) T5 = T4.add_child(Tree([2.0, 5.0, 0.0, 1.0, 1, 1, 2])) T6 = T4.add_child(Tree([0.0, 5.0, 2.0, 1.0, 1, 1, 2])) T7 = T5.add_child(Tree([3.0, 5.0, 0.0, 0.75, 1, 1, 2])) T8 = T7.add_child(Tree([4.0, 5.0, 0.0, 0.75, 1, 1, 2])) T9 = T6.add_child(Tree([0.0, 5.0, 3.0, 0.75, 1, 1, 2])) T10 = T9.add_child(Tree([0.0, 6.0, 3.0, 0.75, 1, 1, 2])) return T
def test_add_child(): t = Tree() ch11 = t.add_child(Tree()) ch22 = t.add_child(Tree()) nt.ok_(len(t.children) == 2) nt.ok_(t.children == [ch11, ch22])