def map_segments(neurite, fun): '''map a function to the segments in a tree''' if isinstance(neurite, Section): neurite = Neurite(neurite) return [s for ss in neurite.iter_sections() for s in fst.sectionfunc.map_segments(fun, ss)]
def test_trunk_origin_elevations(): class Mock(object): pass n0 = Mock() n1 = Mock() s = make_soma([[0, 0, 0, 4]]) t0 = Section(((1, 0, 0, 2), (2, 1, 1, 2))) t0.type = NeuriteType.basal_dendrite t1 = Section(((0, 1, 0, 2), (1, 2, 1, 2))) t1.type = NeuriteType.basal_dendrite n0.neurites = [Neurite(t0), Neurite(t1)] n0.soma = s t2 = Section(((0, -1, 0, 2), (-1, -2, -1, 2))) t2.type = NeuriteType.basal_dendrite n1.neurites = [Neurite(t2)] n1.soma = s pop = Population([n0, n1]) nt.eq_(list(_nf.trunk_origin_elevations(pop)), [0.0, np.pi/2., -np.pi/2.]) nt.eq_( list(_nf.trunk_origin_elevations(pop, neurite_type=NeuriteType.basal_dendrite)), [0.0, np.pi/2., -np.pi/2.]) nt.eq_(len(_nf.trunk_origin_elevations(pop, neurite_type=NeuriteType.axon)), 0) nt.eq_(len(_nf.trunk_origin_elevations(pop, neurite_type=NeuriteType.apical_dendrite)), 0)
def test_neurite_type(): root_node = Section(POINTS0, section_type=nm.AXON) nrt = Neurite(root_node) nt.eq_(nrt.type, nm.AXON) root_node = Section(POINTS0, section_type=nm.BASAL_DENDRITE) nrt = Neurite(root_node) nt.eq_(nrt.type, nm.BASAL_DENDRITE)
def map_segments(neurite, fun): '''map a function to the segments in a tree''' if is_new_style(neurite): if isinstance(neurite, Section): neurite = Neurite(neurite) return [s for ss in neurite.iter_sections() for s in secfun.map_segments(fun, ss)] else: fun = seg.segment_function(as_tree=False)(fun) return list(iter_neurites(neurite, fun))
def test_neurite_type(): root_node = Section(POINTS0, section_type=nm.AXON) nrt = Neurite(root_node) nt.eq_(nrt.type, nm.AXON) root_node = Section(POINTS0, section_type=nm.BASAL_DENDRITE) nrt = Neurite(root_node) nt.eq_(nrt.type, nm.BASAL_DENDRITE) # https://github.com/BlueBrain/NeuroM/issues/697 nt.assert_equal(np.array([nm.AXON, nm.BASAL_DENDRITE]).dtype, np.object)
def make_neurites(rdw): """Build neurite trees from a raw data wrapper.""" post_action = _NEURITE_ACTION[rdw.fmt] trunks = rdw.neurite_root_section_ids() if not trunks: return [], [] # One pass over sections to build nodes nodes = tuple(Section(section_id=i, points=rdw.data_block[sec.ids], section_type=_TREE_TYPES[sec.ntype]) for i, sec in enumerate(rdw.sections)) # One pass over nodes to connect children to parents for i, node in enumerate(nodes): parent_id = rdw.sections[i].pid parent_type = nodes[parent_id].type # only connect neurites if parent_id != ROOT_ID and parent_type != NeuriteType.soma: nodes[parent_id].add_child(node) neurites = tuple(Neurite(nodes[i]) for i in trunks) if post_action is not None: for n in neurites: post_action(n.root_node) return neurites, nodes
def _generate_neurite(mode): def new_radius(prev_radius, mode): if mode == 0: return prev_radius / 2. elif mode == 1: return prev_radius else: return prev_radius * 2. def fake_tree(init_radius, mode): radius = init_radius sec = [] for _ in range(5): sec.append([0, 0, 0, radius, 0, 0]) radius = new_radius(radius, mode) return Section(np.array(sec)) radius = 1. tree = fake_tree(radius, mode) tree.add_child(fake_tree(tree.points[-1][COLS.R], mode)) tree.add_child(fake_tree(tree.points[-1][COLS.R], mode)) tree.type = NeuriteType.undefined return Neurite(tree)
def _generate_back_track_tree(n, dev): tree = Section( np.array([[0., 0., 0., 0.2, 1., 0., 0.], [0., 1., 0., 0.15, 1., 0., 0.], [0., 2., 0., 0.14, 1., 0., 0.]])) ch0 = Section( np.array([[0., 2., 0., 0.14, 1., 0., 0.], [1., 3., 0., 0.15, 1., 0., 0.], [2., 4., 0., 0.11, 1., 0., 0.]])) ch1 = Section( np.array([[0., 2., 0., 0.14, 1., 0., 0.], [1., -3., 0., 0.15, 1., 0., 0.], [2., -4., 0., 0.12, 1., 0., 0.], [dev[0], dev[1], dev[2], 0.11, 1., 0., 0.], [3., -5., 0., 0.1, 1., 0., 0.], [4., -6., 0., 0.1, 1., 0., 0.]])) tree.add_child(ch0) tree.add_child(ch1) tree.children[1].points[3] += tree.children[n].points[1] tree.type = NeuriteType.undefined return Neurite(tree)
def __init__(self, obj, show_diameters=True): '''Create dendrogram ''' # flag for diameters self._show_diameters = show_diameters # input object, tree, or neuron self._obj = deepcopy(Neurite(obj) if isinstance(obj, Tree) else obj) # counter/index for the storage of the rectangles. # it is updated recursively self._n = 0 # the maximum lengths in x and y that is occupied # by a neurite. It is updated recursively. self._max_dims = [0., 0.] # stores indices that refer to the _rectangles array # for each neurite self._groups = [] # dims store the max dimensions for each neurite # essential for the displacement in the plotting self._dims = [] # initialize the number of rectangles self._rectangles = np.zeros([_n_rectangles(self._obj), 4, 2]) # determine the maximum recursion depth for the given object # which depends on the tree with the maximum number of nodes self._max_rec_depth = _max_recursion_depth(self._obj)
def test_n_leaves(): nt.assert_equal(_nf.n_leaves(Neurite(s0)), 5) nt.assert_equal(_nf.n_leaves(Neurite(s1)), 3) nt.assert_equal(_nf.n_leaves(Neurite(s2)), 1) nt.assert_equal(_nf.n_leaves(Neurite(s3)), 1) nt.assert_equal(_nf.n_leaves(Neurite(s4)), 2) nt.assert_equal(_nf.n_leaves(Neurite(s5)), 1) nt.assert_equal(_nf.n_leaves(Neurite(s6)), 1) nt.assert_equal(_nf.n_leaves(Neurite(s7)), 1)
def test_n_forking_points(): nt.assert_equal(_nf.n_forking_points(Neurite(s0)), 3) nt.assert_equal(_nf.n_forking_points(Neurite(s1)), 2) nt.assert_equal(_nf.n_forking_points(Neurite(s2)), 0) nt.assert_equal(_nf.n_forking_points(Neurite(s3)), 0) nt.assert_equal(_nf.n_forking_points(Neurite(s4)), 1) nt.assert_equal(_nf.n_forking_points(Neurite(s5)), 0) nt.assert_equal(_nf.n_forking_points(Neurite(s6)), 0) nt.assert_equal(_nf.n_forking_points(Neurite(s7)), 0)
def test_n_bifurcation_points(): nt.assert_equal(_nf.n_bifurcation_points(Neurite(s0)), 2) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s1)), 2) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s2)), 0) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s3)), 0) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s4)), 1) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s5)), 0) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s6)), 0) nt.assert_equal(_nf.n_bifurcation_points(Neurite(s7)), 0)
def test_one_point_branch(): test_section = Neurite(Section(points=np.array([[1., 1., 1., 0.5, 2, 1, 0]]))) for diameter_scale, linewidth in it.product((1.0, None), (0.0, 1.2)): with get_fig_2d() as (fig, ax): view.plot_tree(ax, test_section, diameter_scale=diameter_scale, linewidth=linewidth) with get_fig_3d() as (fig, ax): view.plot_tree3d(ax, test_section, diameter_scale=diameter_scale, linewidth=linewidth)
def _genetate_tree_non_monotonic_section_boundary(): tree = Section( np.array([[0, 0, 0, 1.0, 0, 0], [0, 0, 0, 0.75, 0, 0], [0, 0, 0, 0.5, 0, 0], [0, 0, 0, 0.25, 0, 0]])) ch0 = Section( np.array([[0, 0, 0, 0.375, 0, 0], [0, 0, 0, 0.125, 0, 0], [0, 0, 0, 0.0625, 0, 0]])) tree.add_child(ch0) tree.type = NeuriteType.undefined return Neurite(tree)
def test_neurite_hash(): nrt = Neurite(ROOT_NODE) nt.eq_(hash(nrt), hash((nrt.type, nrt.root_node)))
def test_str(): nrt = Neurite(ROOT_NODE) nt.ok_('Neurite' in str(nrt))
def test_neurite_volume(): nrt = Neurite(ROOT_NODE) volume = math.pi * RADIUS * RADIUS * REF_LEN nt.assert_almost_equal(nrt.volume, volume)
def test_neurite_area(): nrt = Neurite(ROOT_NODE) area = 2 * math.pi * RADIUS * REF_LEN nt.assert_almost_equal(nrt.area, area)
def test_neurite_length(): nrt = Neurite(ROOT_NODE) nt.assert_almost_equal(nrt.length, REF_LEN)
def test_init(): nrt = Neurite(ROOT_NODE) nt.eq_(nrt.type, nm.NeuriteType.undefined) nt.eq_(len(nrt.points), 13)