def test_interval_lengths(): assert_array_almost_equal(mm.interval_lengths([[0, 0, 0], [1, 1, 0], [2, 11, 0]]), [1.414214, 10.049876]) assert_array_almost_equal(mm.interval_lengths([[0, 0, 0], [1, 1, 0], [2, 11, 0]], prepend_zero=True), [0, 1.414214, 10.049876])
def _sec_taper_rate(sec): """Taper rate from fit along a section.""" path_distances = np.cumsum( interval_lengths(sec.points, prepend_zero=True)) return np.polynomial.polynomial.polyfit(path_distances, 2 * sec.points[:, COLS.R], 1)[1]
def __init__(self, neurom_section): """Dendrogram for NeuroM section tree. Args: neurom_section (Neurite|Neuron|Section): tree to build dendrogram for. """ if isinstance(neurom_section, Neuron): self.neurite_type = NeuriteType.soma self.height = 1 self.width = 1 self.coords = self.get_coords( np.array([0, self.height]), np.array([.5 * self.width, .5 * self.width])) self.children = [ Dendrogram(neurite.root_node) for neurite in neurom_section.neurites ] else: if isinstance(neurom_section, Neurite): neurom_section = neurom_section.root_node lengths = interval_lengths(neurom_section.points, prepend_zero=True) radii = neurom_section.points[:, COLS.R] self.neurite_type = neurom_section.type self.height = np.sum(lengths) self.width = 2 * np.max(radii) self.coords = Dendrogram.get_coords(lengths, radii) self.children = [ Dendrogram(child) for child in neurom_section.children ]
def segment_lengths(section): '''Returns the list of segment lengths within the section''' return interval_lengths(section.points)
def _unravel_section_path_length(sec, window_half_length, soma, legacy_behavior): '''Unravel a section using path length as window_half_length''' unraveled_points = sec.points n_points = len(sec.points) if legacy_behavior and sec.is_root and len(soma.points) > 1: unraveled_points = np.vstack((soma.points[0], unraveled_points)) # ensure the first point is the parent's last point if not sec.is_root: unraveled_points[0] = sec.parent.points[-1] for window_center in range(1, n_points): # find how many segement on the left and right to be close to window_half_length intervals = interval_lengths(sec.points) _l_left = np.cumsum(intervals[window_center::-1]) _l_right = np.cumsum(intervals[min(n_points - 2, window_center):]) window_start = np.argmin(abs(_l_left - window_half_length)) window_end = np.argmin(abs(_l_right - window_half_length)) # if we are near the first point of the section we increase window from the right/left # the 0.9 is only to not do that to often _left_length = _l_left[window_start] if _left_length < 0.9 * window_half_length: window_end = np.argmin( abs(_l_right - (2 * window_half_length - _left_length))) _right_length = _l_right[window_end] if _right_length < 0.9 * window_half_length: window_start = np.argmin( abs(_l_left - (2 * window_half_length - _right_length))) # center bounds to windows center window_start = window_center - window_start window_end = window_center + window_end # if windows as 0 width, extend by one segment on each side if possible if window_start == window_end: window_start = max(0, window_start - 1) window_end = min(n_points, window_end + 1) direction = _get_principal_direction( sec.points[window_start:window_end]) original_segment = sec.points[window_center] - sec.points[window_center - 1] # make it span length the same as the original segment within the window direction *= np.linalg.norm(original_segment) # point it in the same direction as the window window_direction = sec.points[window_end - 1] - sec.points[window_start] scalar_product = np.dot(window_direction, direction) direction *= np.sign(scalar_product or 1.) # update the unravel points unraveled_points[window_center] = unraveled_points[window_center - 1] + direction sec.points = unraveled_points if legacy_behavior and sec.is_root and len(soma.points) > 1: sec.diameters = np.hstack((soma.diameters[0], sec.diameters))
def _sec_taper_rate(sec): '''Taper rate from fit along a section''' distances = interval_lengths(sec.points, prepend_zero=True) return np.polynomial.polynomial.polyfit(distances, 2 * sec.points[:, COLS.R], 1)[1]
def segment_lengths(section, prepend_zero=False): """Returns the list of segment lengths within the section.""" return interval_lengths(section.points, prepend_zero=prepend_zero)