def nonzero_segment_lengths(neuron, threshold=0.0): '''Check presence of neuron segments with length not above threshold Arguments: neuron: Neuron object whose segments will be tested threshold: value above which a segment length is considered to be non-zero Return: list of (first_id, second_id) of zero length segments ''' l = [[s for s in val_iter(isegment(t)) if segment_length(s) <= threshold] for t in neuron.neurites] return [(i[0][COLS.ID], i[1][COLS.ID]) for i in chain(*l)]
def segment_centre_of_mass(seg): '''Calculate and return centre of mass of a segment. Calculated as centre of mass of conical frustum''' h = morphmath.segment_length(seg) r0 = seg[0][COLS.R] r1 = seg[1][COLS.R] num = r0 * r0 + 2 * r0 * r1 + 3 * r1 * r1 denom = 4 * (r0 * r0 + r0 * r1 + r1 * r1) centre_of_mass_z_loc = num / denom return seg[0][0:3] + (centre_of_mass_z_loc / h) * (seg[1][0:3] - seg[0][0:3])
def _generate_dendro(current_node, lines, colors, n, max_dims, spacing, offsets, show_diameters=True): '''Recursive function for dendrogram line computations ''' start_x = _spacingx(current_node, max_dims, offsets, spacing) radii = [0., 0.] # store the parent radius in order to construct polygonal segments # isntead of simple line segments radii[0] = current_node.value[3] if show_diameters else 0. for child in current_node.children: # segment length length = segment_length(list(val_iter((current_node, child)))) # extract the radius of the child node. Need both radius for # realistic segment representation radii[1] = child.value[3] if show_diameters else 0. # number of leaves in child terminations = n_terminations(child) # horizontal spacing with respect to the number of # terminations new_offsets = (start_x + spacing[0] * terminations / 2., offsets[1] + spacing[1] * 2. + length) # vertical segment lines[n[0]] = _vertical_segment(offsets, new_offsets, spacing, radii) # assign segment id to color array colors[n[0]] = child.value[4] n[0] += 1 if offsets[1] + spacing[1] * 2 + length > max_dims[1]: max_dims[1] = offsets[1] + spacing[1] * 2. + length # recursive call to self. _generate_dendro(child, lines, colors, n, max_dims, spacing, new_offsets, show_diameters=show_diameters) # update the starting position for the next child start_x += terminations * spacing[0] # write the horizontal lines only for bifurcations, where the are actual horizontal lines # and not zero ones if offsets[0] != new_offsets[0]: # horizontal segment lines[n[0]] = _horizontal_segment(offsets, new_offsets, spacing, 0.) colors[n[0]] = current_node.value[4] n[0] += 1
def nonzero_segment_lengths(neuron, threshold=0.0): '''Check presence of neuron segments with length not above threshold Arguments: neuron: Neuron object whose segments will be tested threshold: value above which a segment length is considered to be non-zero Return: list of (first_id, second_id) of zero length segments ''' l = [[s for s in val_iter(isegment(t)) if segment_length(s) <= threshold] for t in neuron.neurites] return [(i[0][COLS.ID], i[1][COLS.ID]) for i in chain(*l)]
def segment_centre_of_mass(seg): '''Calculate and return centre of mass of a segment. Calculated as centre of mass of conical frustum''' h = morphmath.segment_length(seg) r0 = seg[0][COLS.R] r1 = seg[1][COLS.R] num = r0 * r0 + 2 * r0 * r1 + 3 * r1 * r1 denom = 4 * (r0 * r0 + r0 * r1 + r1 * r1) centre_of_mass_z_loc = num / denom return seg[0][0:3] + (centre_of_mass_z_loc / h) * (seg[1][0:3] - seg[0][0:3])
def _generate_dendro(self, current_node, spacing, offsets): '''Recursive function for dendrogram line computations ''' max_dims = self._max_dims start_x = _spacingx(current_node, max_dims, offsets[0], spacing[0]) radii = [0., 0.] # store the parent radius in order to construct polygonal segments # isntead of simple line segments radii[0] = current_node.value[COLS.R] if self._show_diameters else 0. for child in current_node.children: # segment length ln = segment_length((current_node.value, child.value)) # extract the radius of the child node. Need both radius for # realistic segment representation radii[1] = child.value[COLS.R] if self._show_diameters else 0. # number of leaves in child terminations = n_terminations(child) # horizontal spacing with respect to the number of # terminations new_offsets = _update_offsets(start_x, spacing, terminations, offsets, ln) # create and store vertical segment self._rectangles[self._n] = _vertical_segment(offsets, new_offsets, spacing, radii) # assign segment id to color array # colors[n[0]] = child.value[4] self._n += 1 if offsets[1] + spacing[1] * 2 + ln > max_dims[1]: max_dims[1] = offsets[1] + spacing[1] * 2. + ln self._max_dims = max_dims # recursive call to self. self._generate_dendro(child, spacing, new_offsets) # update the starting position for the next child start_x += terminations * spacing[0] # write the horizontal lines only for bifurcations, where the are actual horizontal # lines and not zero ones if offsets[0] != new_offsets[0]: # horizontal segment. Thickness is either 0 if show_diameters is false # or 1. if show_diameters is true self._rectangles[self._n] = _horizontal_segment(offsets, new_offsets, spacing, 0.) self._n += 1
def test_segment_length(): nt.ok_(segment_length(((0,0,0), (0,0,42))) == 42) nt.ok_(segment_length(((0,0,0), (0,42,0))) == 42) nt.ok_(segment_length(((0,0,0), (42,0,0))) == 42)
def _generate_dendro(current_node, lines, colors, n, max_dims, spacing, offsets, show_diameters=True): '''Recursive function for dendrogram line computations ''' start_x = _spacingx(current_node, max_dims, offsets, spacing) radii = [0., 0.] # store the parent radius in order to construct polygonal segments # isntead of simple line segments radii[0] = current_node.value[3] if show_diameters else 0. for child in current_node.children: # segment length length = segment_length(list(val_iter((current_node, child)))) # extract the radius of the child node. Need both radius for # realistic segment representation radii[1] = child.value[3] if show_diameters else 0. # number of leaves in child terminations = n_terminations(child) # horizontal spacing with respect to the number of # terminations new_offsets = (start_x + spacing[0] * terminations / 2., offsets[1] + spacing[1] * 2. + length) # vertical segment lines[n[0]] = _vertical_segment(offsets, new_offsets, spacing, radii) # assign segment id to color array colors[n[0]] = child.value[4] n[0] += 1 if offsets[1] + spacing[1] * 2 + length > max_dims[1]: max_dims[1] = offsets[1] + spacing[1] * 2. + length # recursive call to self. _generate_dendro(child, lines, colors, n, max_dims, spacing, new_offsets, show_diameters=show_diameters) # update the starting position for the next child start_x += terminations * spacing[0] # write the horizontal lines only for bifurcations, where the are actual horizontal lines # and not zero ones if offsets[0] != new_offsets[0]: # horizontal segment lines[n[0]] = _horizontal_segment(offsets, new_offsets, spacing, 0.) colors[n[0]] = current_node.value[4] n[0] += 1