def fix_non_zero_segments(neuron, zero_length=_ZERO_LENGTH): '''Return a neuron with zero length segments removed Sections composed of a single zero length segment are deleted, where zero is parametrized by zero_length Args: neuron (str|pathlib.Path|morphio.Morphology|morphio.mut.Morphology): input neuron zero_length (float): smallest length of a segment Returns: a fixed morphio.mut.Morphology ''' neuron = Morphology(neuron) to_be_deleted = list() for section in neuron.iter(): points = section.points distances = np.linalg.norm(np.diff(points, axis=0), axis=1) distances[distances < zero_length] = 0 indices = np.append(0, np.nonzero(distances)[0] + 1) if len(indices) != len(points): section.points = section.points[indices] section.diameters = section.diameters[indices] if len(indices) < 2: to_be_deleted.append(section) for section in to_be_deleted: neuron.delete_section(section) return neuron
def fix_non_zero_segments(neuron): '''Return a neuron with zero length segments removed Sections composed of a single zero length segment are deleted Args: neuron (str|pathlib.Path|morphio.Morphology|morphio.mut.Morphology): input neuron Returns: a fixed morphio.mut.Morphology ''' neuron = Morphology(neuron) to_be_deleted = list() for section in neuron.iter(): points = section.points distances = np.linalg.norm(np.diff(points, axis=0), axis=1) indices = np.append(0, np.nonzero(distances)[0] + 1) if len(indices) != len(points): section.points = section.points[indices] section.diameters = section.diameters[indices] if len(indices) < 2: to_be_deleted.append(section) for section in to_be_deleted: neuron.delete_section(section) return neuron
def test_remove_rootsection_in_loop(): morpho = Morphology(DATA_DIR / 'single_point_root.asc') assert len(morpho.root_sections) == 1 for root in morpho.root_sections: if len(root.points) == 1: morpho.delete_section(root, False) assert len(morpho.root_sections) == 2
def test_remove_rootsection(): morpho = Morphology(DATA_DIR / 'single_point_root.asc') assert len(morpho.root_sections) == 1 to_remove = [] for root in morpho.root_sections: if len(root.points) == 1: to_remove.append(root) for root in to_remove: morpho.delete_section(root, False) assert len(morpho.root_sections) == 2
def test_equality(): set_ignored_warning([Warning.wrong_duplicate, Warning.only_child], True) filename = joinp(DATA, 'simple2.asc') neuron_ref = Morphology(filename) ok_(not diff(neuron_ref, neuron_ref)) ok_(not diff(neuron_ref, filename)) ok_(not diff(neuron_ref, neuron_ref.as_immutable())) def mundane_section(neuron): '''Not a root section, not a leaf section''' return neuron.root_sections[0].children[0] a = Morphology(joinp(DATA, 'simple2.asc')) mundane_section(a).type = SectionType.apical_dendrite result = diff(neuron_ref, a) ok_(result) assert_equal(result.info, 'Section(id=1, points=[(0 5 0),..., (-6 5 0)]) and Section(id=1, points=[(0 5 0),..., (-6 5 0)]) have different section types') a = Morphology(joinp(DATA, 'simple2.asc')) mundane_section(a).points = [[0,0,0], [0,0,0], [0,0,1]] result = diff(neuron_ref, a) ok_(result) assert_equal(result.info, '\n'.join(['Attributes Section.points of:', 'Section(id=1, points=[(0 5 0),..., (-6 5 0)])', 'Section(id=1, points=[(0 0 0),..., (0 0 1)])', 'have the same shape but different values'])) a = Morphology(joinp(DATA, 'simple2.asc')) mundane_section(a).diameters = [0,0,0] result = diff(neuron_ref, a) ok_(result) assert_equal(result.info, '\n'.join(['Attributes Section.diameters of:', 'Section(id=1, points=[(0 5 0),..., (-6 5 0)])', 'Section(id=1, points=[(0 5 0),..., (-6 5 0)])', 'have the same shape but different values'])) a = Morphology(joinp(DATA, 'simple2.asc')) for section in a.iter(): section.perimeters = [1] * len(section.points) result = diff(neuron_ref, a) ok_(result) assert_equal(result.info, '\n'.join(['Attributes Section.perimeters of:', 'Section(id=0, points=[(0 0 0),..., (0 5 0)])', 'Section(id=0, points=[(0 0 0),..., (0 5 0)])', 'have different shapes: (0,) vs (2,)'])) a = Morphology(joinp(DATA, 'simple2.asc')) mundane_section(a).append_section(PointLevel([[-6, 5, 0], [4, 5, 6]], [2, 3])) result = diff(neuron_ref, a) ok_(result) assert_equal(result.info, 'Section(id=1, points=[(0 5 0),..., (-6 5 0)]) and Section(id=1, points=[(0 5 0),..., (-6 5 0)]) have different a different number of children') a = Morphology(joinp(DATA, 'simple2.asc')) a.delete_section(a.root_sections[0]) result = diff(neuron_ref, a) ok_(result) assert_equal(result.info, 'Both morphologies have different a different number of root sections') result = diff(joinp(DATA, 'single_child.asc'), joinp(DATA, 'not_single_child.asc')) ok_(not result) set_ignored_warning([Warning.wrong_duplicate, Warning.only_child], False)
def test_equality(): set_ignored_warning([Warning.wrong_duplicate, Warning.only_child], True) filename = DATA / 'simple2.asc' neuron_ref = Morphology(filename) assert not diff(neuron_ref, neuron_ref) assert not diff(neuron_ref, filename) assert not diff(neuron_ref, neuron_ref.as_immutable()) def mundane_section(neuron): '''Not a root section, not a leaf section''' return neuron.root_sections[0].children[0] a = Morphology(DATA / 'simple2.asc') mundane_section(a).type = SectionType.apical_dendrite result = diff(neuron_ref, a) assert result assert result.info == 'Section(id=1, points=[(0 5 0),..., (-6 5 0)]) and Section(id=1, points=[(0 5 0),..., (-6 5 0)]) have different section types' a = Morphology(DATA / 'simple2.asc') mundane_section(a).points = [[0,0,0], [0,0,0], [0,0,1]] result = diff(neuron_ref, a) assert result assert (result.info == '\n'.join(['Attributes Section.points of:', 'Section(id=1, points=[(0 5 0),..., (-6 5 0)])', 'Section(id=1, points=[(0 0 0),..., (0 0 1)])', 'have the same shape but different values', 'Vector points differs at index 0: [0. 5. 0.] != [0. 0. 0.]'])) a = Morphology(DATA / 'simple2.asc') mundane_section(a).diameters = [0,0,0] result = diff(neuron_ref, a) assert result assert (result.info == '\n'.join(['Attributes Section.diameters of:', 'Section(id=1, points=[(0 5 0),..., (-6 5 0)])', 'Section(id=1, points=[(0 5 0),..., (-6 5 0)])', 'have the same shape but different values', 'Vector diameters differs at index 0: 3.0 != 0.0'])) a = Morphology(DATA / 'simple2.asc') for section in a.iter(): section.perimeters = [1] * len(section.points) result = diff(neuron_ref, a) assert result assert (result.info == '\n'.join(['Attributes Section.perimeters of:', 'Section(id=0, points=[(0 0 0),..., (0 5 0)])', 'Section(id=0, points=[(0 0 0),..., (0 5 0)])', 'have different shapes: (0,) vs (2,)'])) a = Morphology(DATA / 'simple2.asc') mundane_section(a).append_section(PointLevel([[-6, 5, 0], [4, 5, 6]], [2, 3])) result = diff(neuron_ref, a) assert result assert result.info == 'Section(id=1, points=[(0 5 0),..., (-6 5 0)]) and Section(id=1, points=[(0 5 0),..., (-6 5 0)]) have a different number of children' a = Morphology(DATA / 'simple2.asc') a.delete_section(a.root_sections[0]) result = diff(neuron_ref, a) assert result assert result.info == 'Both morphologies have a different number of root sections' result = diff(DATA / 'single_child.asc', DATA / 'not_single_child.asc') if parse_version(get_distribution("morphio").version) < parse_version("3"): assert not result else: assert result set_ignored_warning([Warning.wrong_duplicate, Warning.only_child], False)