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_iterators(): assert_array_equal( [sec.id for sec in SIMPLE.section(5).iter(IterType.upstream)], [5, 3]) assert_array_equal( [sec.id for sec in SIMPLE.section(0).iter(IterType.depth_first)], [0, 1, 2]) assert_array_equal( [sec.id for sec in SIMPLE.section(0).iter(IterType.breadth_first)], [0, 1, 2]) assert_array_equal([sec.id for sec in SIMPLE.iter()], [0, 1, 2, 3, 4, 5]) assert_array_equal([sec.id for sec in SIMPLE.iter(IterType.depth_first)], [0, 1, 2, 3, 4, 5]) assert_array_equal([sec.id for sec in SIMPLE.iter(IterType.breadth_first)], [0, 3, 1, 2, 4, 5]) neuron = Morphology(os.path.join(_path, "iterators.asc")) root = neuron.root_sections[0] assert_array_equal( [section.id for section in root.iter(IterType.depth_first)], [0, 1, 2, 3, 4, 5, 6]) assert_array_equal( [section.id for section in root.iter(IterType.breadth_first)], [0, 1, 4, 2, 3, 5, 6]) assert_array_equal( [section.id for section in neuron.iter(IterType.breadth_first)], [0, 7, 1, 4, 8, 9, 2, 3, 5, 6])
def test_mitochondria(): morpho = Morphology() morpho.soma.points = [[0, 0, 0], [1, 1, 1]] morpho.soma.diameters = [1, 1] morpho.append_root_section( PointLevel([[2, 2, 2], [3, 3, 3]], [4, 4], [5, 5]), SectionType.axon) mito = morpho.mitochondria first_mito_id = mito.append_root_section( MitochondriaPointLevel([0, 0], [0.5, 0.6], [10, 20])) first_child = first_mito_id.append_section( MitochondriaPointLevel([3, 4, 4, 5], [0.6, 0.7, 0.8, 0.9], [20, 30, 40, 50])) second_mito_id = mito.append_root_section( MitochondriaPointLevel([0, 1, 1, 2], [0.6, 0.7, 0.8, 0.9], [5, 6, 7, 8])) assert_equal(mito.is_root(first_mito_id), True) assert_equal(mito.children(first_mito_id), [first_child]) assert_equal(mito.parent(first_child), first_mito_id) assert_equal(mito.root_sections, [first_mito_id, second_mito_id]) assert_array_equal(first_child.diameters, [20, 30, 40, 50]) assert_array_equal(first_child.neurite_section_ids, [3, 4, 4, 5]) assert_array_equal( np.array(first_child.relative_path_lengths, dtype=np.float32), np.array([0.6, 0.7, 0.8, 0.9], dtype=np.float32))
def test_single_neurite(): m = Morphology() m.append_root_section(PointLevel([[1, 2, 3]], [2], [20]), SectionType.axon) assert_array_equal(m.root_sections[0].points, [[1, 2, 3]]) assert_equal(m.root_sections[0].diameters, [2]) assert_equal(m.root_sections[0].perimeters, [20]) m.root_sections[0].points = [[10, 20, 30]] assert_array_equal(m.root_sections[0].points, [[10, 20, 30]], 'Points array should have been mutated') m.root_sections[0].diameters = [7] assert_equal(m.root_sections[0].diameters, [7], 'Diameter array should have been mutated') m.root_sections[0].perimeters = [27] assert_equal(m.root_sections[0].perimeters, [27], 'Perimeter array should have been mutated')
def test_write_perimeter(): morpho = Morphology() morpho.soma.points = [[0, 0, 0]] morpho.soma.diameters = [2] dendrite = morpho.append_root_section( PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2], [5, 6]), SectionType.basal_dendrite) dendrite.append_section(PointLevel([[0, 5, 0], [-5, 5, 0]], [2, 3], [6, 7])) dendrite.append_section(PointLevel([[0, 5, 0], [6, 5, 0]], [2, 3], [6, 8])) with setup_tempdir('test_write_perimeter') as tmp_folder: h5_out = os.path.join(tmp_folder, "test_write.h5") morpho.write(h5_out) assert_array_equal( ImmutMorphology(h5_out).perimeters, [5., 6., 6., 7., 6., 8.]) # Cannot right a morph with perimeter data to ASC and SWC for ext in ['swc', 'asc']: out_path = os.path.join(tmp_folder, "test_write_perimeter." + ext) assert_raises(WriterError, morpho.write, out_path)
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 test_child_section(): m = Morphology() section = m.append_root_section(PointLevel([[1, 2, 3]], [2], [20]), SectionType.axon) ok_(section.is_root) section.append_section(PointLevel([[1, 2, 3], [4, 5, 6]], [2, 3], [20, 30])) children = m.root_sections[0].children assert_equal(len(children), 1) assert_array_equal(children[0].points, [[1, 2, 3], [4, 5, 6]]) assert_array_equal(children[0].diameters, [2, 3]) assert_array_equal(children[0].perimeters, [20, 30]) children[0].points = [[7, 8, 9], [10, 11, 12]] assert_array_equal(children[0].points, [[7, 8, 9], [10, 11, 12]])
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_connectivity(): cells = OrderedDict({ 'asc': Morphology(Path(DATA_DIR, "simple.asc")), 'swc': Morphology(Path(DATA_DIR, "simple.swc")), 'h5': Morphology(Path(DATA_DIR, "h5/v1/simple.h5")), }) for cell in cells: assert cells[cell].connectivity == {-1: [0, 3], 0: [1, 2], 3: [4, 5]}
def test_append_mutable_section(): morpho = Morphology() second_children_first_root = SIMPLE.root_sections[0].children[1] morpho.append_root_section(second_children_first_root) assert_equal(len(morpho.root_sections), 1) assert_array_equal(morpho.root_sections[0].points, second_children_first_root.points)
def test_write_soma_basic(): morpho = Morphology() morpho.soma.points = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] morpho.soma.diameters = [2, 3, 3] with setup_tempdir('test_write_soma_basic') as tmp_folder: morpho.write(os.path.join(tmp_folder, "test_write.swc")) morpho.write(Path(tmp_folder, "test_write_pathlib.swc")) assert_equal(len(os.listdir(tmp_folder)), 2)
def test_connectivity(): cells = OrderedDict({ 'asc': Morphology(os.path.join(_path, "simple.asc")), 'swc': Morphology(os.path.join(_path, "simple.swc")), 'h5': Morphology(os.path.join(_path, "h5/v1/simple.h5")), }) for cell in cells: assert_dict_equal(cells[cell].connectivity, {-1: [0, 3], 0: [1, 2], 3: [4, 5]})
def test_nested_single_child(): with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): n = Morphology(DATA_DIR / 'nested_single_children.asc') n.remove_unifurcations() assert_array_equal( n.root_sections[0].points, [[0., 0., 0.], [0., 0., 1.], [0., 0., 2.], [0., 0., 3.], [0., 0., 4.]]) assert_array_equal(n.root_sections[0].diameters, np.array([8, 7, 6, 5, 4], dtype=np.float32))
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_self_graft(): '''Grafting a neuron with its own neuron''' filename = DATA / 'neuron.asc' new_axon = graft.find_axon(ImmutMorphology(filename)) neuron = Morphology(filename) graft.graft_axon(neuron, new_axon) expected = Morphology(filename) assert not diff(expected, neuron)
def test_dendrites_mean_direction(): donor_neuron = Morphology(os.path.join(_path, 'simple3.asc')) assert_array_equal(graft._dendrites_mean_direction(donor_neuron), np.array([0.6666667, 4.6666665, 3.], dtype=np.float32)) no_dendrite = Morphology() no_dendrite.append_root_section(PointLevel([[0, 0, 0], [1, 1, 1]], [1, 1]), SectionType.axon) with assert_raises(MorphToolException) as obj: graft._dendrites_mean_direction(no_dendrite)
def test_self_graft(): '''Grafting a neuron with its own neuron''' filename = os.path.join(_path, 'neuron.asc') new_axon = graft.find_axon(ImmutMorphology(filename)) neuron = Morphology(filename) graft.graft_axon(neuron, new_axon) expected = Morphology(filename) ok_(not diff(expected, neuron))
def test_graft_axon_on_synthesized_cell(): np.random.seed(0) # donor neuron is empty assert_raises(NoAxonException, graft.graft_axon, SIMPLE, Morphology()) donor_neuron = Morphology(os.path.join(_path, 'simple3.asc')) synthesized_cell = Morphology(os.path.join(_path, 'synthesized_cell.asc')) graft.graft_axon(synthesized_cell, donor_neuron) axon = graft.find_axon(synthesized_cell) assert_array_almost_equal( axon.points, [[5.110419, 5.486378, 4.9647303], [5.110419, 1.486378, 4.9647303]])
def test_build_read_only(): m = Morphology() m.soma.points = [[-1, -2, -3]] m.soma.diameters = [-4] section = m.append_root_section(PointLevel([[1, 2, 3], [4, 5, 6]], [2, 2], [20, 20]), SectionType.axon) section.append_section(PointLevel([[4, 5, 6], [7, 8, 9]], [2, 3], [20, 30])) section.append_section(PointLevel([[4, 5, 6], [10, 11, 12]], [2, 2], [20, 20])) immutable_morphology = ImmutableMorphology(m) sections = list(immutable_morphology.iter()) assert_equal(len(sections), 3) assert_array_equal(immutable_morphology.soma.points, [[-1, - 2, -3]]) assert_array_equal(immutable_morphology.soma.diameters, [-4]) assert_array_equal(immutable_morphology.section(0).points, [[1, 2, 3], [4, 5, 6]]) assert_array_equal(immutable_morphology.section(0).diameters, [2, 2]) assert_array_equal(immutable_morphology.section(0).perimeters, [20, 20]) assert_equal(len(immutable_morphology.section(0).children), 2) child = immutable_morphology.section(0).children[0] assert_array_equal(child.points, [[4, 5, 6], [7, 8, 9]]) assert_array_equal(child.diameters, [2, 3]) assert_array_equal(child.perimeters, [20, 30]) same_child = immutable_morphology.section(1) assert_array_equal(same_child.points, [[4, 5, 6], [7, 8, 9]]) assert_array_equal(same_child.diameters, [2, 3]) assert_array_equal(same_child.perimeters, [20, 30])
def test_graft_axon_on_synthesized_cell(): np.random.seed(0) # donor neuron is empty with pytest.raises(NoAxonException): graft.graft_axon(SIMPLE, Morphology()) donor_neuron = Morphology(DATA / 'simple3.asc') synthesized_cell = Morphology(DATA / 'synthesized_cell.asc') graft.graft_axon(synthesized_cell, donor_neuron) axon = graft.find_axon(synthesized_cell) assert_array_almost_equal( axon.points, [[5.1272364, 5.4825425, 4.9689593], [5.1272364, 1.4825425, 4.9689593]])
def test_single_point_root_section(): m = Morphology() points = [] diameters = [] # Too hide the warning: appending empty section with captured_output(): with ostream_redirect(stdout=True, stderr=True): m.append_root_section(PointLevel(points, diameters), SectionType(2)) with setup_tempdir('test_single_point_root_section', no_cleanup=True) as tmp_folder: assert_raises( SectionBuilderError, m.write, os.path.join(tmp_folder, "h5/empty_vasculature.h5")) m = Morphology() points = [[1., 1., 1.]] diameters = [2.] m.append_root_section(PointLevel(points, diameters), SectionType(2)) with setup_tempdir('test_single_point_root_section', no_cleanup=True) as tmp_folder: assert_raises(SectionBuilderError, m.write, os.path.join(tmp_folder, "h5/empty_vasculature.h5"))
def test_for_morphio(): Neuron(Morphology()) neuron = Morphology() neuron.soma.points = [[0, 0, 0], [1, 1, 1], [2, 2, 2]] neuron.soma.diameters = [1, 1, 1] NeuroM_neuron = Neuron(neuron) assert_array_equal( NeuroM_neuron.soma.points, [[0., 0., 0., 0.5], [1., 1., 1., 0.5], [2., 2., 2., 0.5]]) NeuroM_neuron.soma.points = [[1, 1, 1, 1], [2, 2, 2, 2]] assert_array_equal(NeuroM_neuron.soma.points, [[1, 1, 1, 1], [2, 2, 2, 2]])
def test_mitochondria(): morpho = Morphology() morpho.soma.points = [[0, 0, 0], [1, 1, 1]] morpho.soma.diameters = [1, 1] neuronal_section_ids = [0, 0] relative_pathlengths = np.array([0.5, 0.6], dtype=np.float32) diameters = [10, 20] mito_id = morpho.mitochondria.append_root_section( MitochondriaPointLevel(neuronal_section_ids, relative_pathlengths, diameters)) mito_id.append_section( MitochondriaPointLevel([0, 0, 0, 0], [0.6, 0.7, 0.8, 0.9], [20, 30, 40, 50])) with setup_tempdir('test_mitochondria') as tmp_folder: morpho.write(os.path.join(tmp_folder, "test.h5")) with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): morpho.write(os.path.join(tmp_folder, "test.swc")) assert_string_equal( err.getvalue(), "Warning: this cell has mitochondria, they cannot be saved in " " ASC or SWC format. Please use H5 if you want to save them." ) with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): morpho.write(os.path.join(tmp_folder, "test.asc")) assert_string_equal( err.getvalue(), "Warning: this cell has mitochondria, they cannot be saved in " " ASC or SWC format. Please use H5 if you want to save them." ) mito = ImmutMorphology(os.path.join(tmp_folder, 'test.h5')).mitochondria assert_array_equal(mito.root_sections[0].diameters, diameters) assert_array_equal(mito.root_sections[0].neurite_section_ids, neuronal_section_ids) assert_array_equal(mito.root_sections[0].relative_path_lengths, relative_pathlengths) assert_equal(len(mito.root_sections), 1) mito = Morphology(os.path.join(tmp_folder, 'test.h5')).mitochondria assert_equal(len(mito.root_sections), 1) assert_equal(mito.root_sections[0].neurite_section_ids, neuronal_section_ids) assert_array_equal(mito.section(0).diameters, diameters) assert_array_equal( mito.section(0).neurite_section_ids, neuronal_section_ids)
def test_mut_copy_ctor(): simple = Morphology(os.path.join(_path, "simple.swc")) assert_equal([sec.id for sec in simple.iter()], [0, 1, 2, 3, 4, 5]) copy = Morphology(simple) copy.append_root_section( PointLevel([[1, 2, 3], [4, 5, 6]], [2, 2], [20, 20]), SectionType.axon) # test that first object has not been mutated assert_equal([sec.id for sec in simple.iter()], [0, 1, 2, 3, 4, 5]) assert_equal([sec.id for sec in copy.iter()], [0, 1, 2, 3, 4, 5, 6])
def test_graft(): m = Morphology(os.path.join(_path, 'simple2.swc')) new_axon = graft.find_axon(m) neuron = Morphology(os.path.join(_path, 'simple.swc')) graft.graft_axon(neuron, new_axon) grafted_axon = graft.find_axon(neuron) points = np.vstack([section.points for section in grafted_axon.iter()]) assert_array_equal( points, np.array([[0., 0., 0.], [1., 2., 0.], [1., 2., 0.], [3., 4.5, 0.], [1., 2., 0.], [-5., -4., 0.], [-5., -4., 1.]], dtype=np.float32))
def test_empty_neurite(): m = Morphology() with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): root = m.append_root_section(PointLevel(), SectionType.axon) assert_equal(err.getvalue().strip(), 'Warning: appending empty section with id: 0') assert_equal(len(m.root_sections), 1) assert_equal(m.root_sections[0].type, SectionType.axon) with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): root.append_section(PointLevel(), SectionType.axon) assert_equal(err.getvalue().strip(), 'Warning: appending empty section with id: 1')
def test_mitochondria_read(): '''Read a H5 file with a mitochondria''' morpho = Morphology(os.path.join(_path, "h5/v1/mitochondria.h5")) mito = morpho.mitochondria assert_equal(len(mito.root_sections), 2) mitochondria = mito.root_sections assert_array_equal(mitochondria[0].diameters, [10, 20]) assert_array_equal(mitochondria[0].relative_path_lengths, np.array([0.5, 0.6], dtype=np.float32)) assert_array_equal(mitochondria[0].neurite_section_ids, np.array([0., 0.], dtype=np.float32)) assert_equal(len(mito.children(mito.root_sections[0])), 1) assert_equal(mito.parent(mito.children(mitochondria[0])[0]), mitochondria[0]) assert_array_equal(mitochondria[1].diameters, [5, 6, 7, 8]) assert_array_equal(mitochondria[1].relative_path_lengths, np.array([0.6, 0.7, 0.8, 0.9], dtype=np.float32)) assert_array_equal(mitochondria[1].neurite_section_ids, np.array([0, 1, 1, 2], dtype=np.float32)) assert_equal(len(mito.children(mito.root_sections[1])), 0)
def test_mitochondria_read(): """Read a H5 file with a mitochondria""" morpho = Morphology(DATA_DIR / "h5/v1/mitochondria.h5") mito = morpho.mitochondria assert len(mito.root_sections) == 2 mitochondria = mito.root_sections assert_array_equal(mitochondria[0].diameters, [10, 20]) assert_array_equal(mitochondria[0].relative_path_lengths, np.array([0.5, 0.6], dtype=np.float32)) assert_array_equal(mitochondria[0].neurite_section_ids, np.array([0., 0.], dtype=np.float32)) assert len(mito.children(mito.root_sections[0])) == 1 assert (mito.parent(mito.children(mitochondria[0])[0]) == mitochondria[0]) assert_array_equal(mitochondria[1].diameters, [5, 6, 7, 8]) assert_array_equal(mitochondria[1].relative_path_lengths, np.array([0.6, 0.7, 0.8, 0.9], dtype=np.float32)) assert_array_equal(mitochondria[1].neurite_section_ids, np.array([0, 1, 1, 2], dtype=np.float32)) assert len(mito.children(mito.root_sections[1])) == 0
def convert(input_file, outputfile, recenter=False, nrn_order=False, single_point_soma=False): '''Run the appropriate converter Args: input_file(str): path to input file outputfile(str): path to output file recenter(bool): whether to recenter the morphology based on the center of gravity of the soma nrn_order(bool): whether to traverse the neuron in the NEURON fashion single_point_soma(bool):For SWC only ''' kwargs = {} if nrn_order: kwargs['options'] = Option.nrn_order neuron = Morphology(input_file, **kwargs) output_ext = Path(outputfile).suffix if single_point_soma and output_ext.lower() != '.swc': raise Exception('Single point soma is only applicable for swc output') if output_ext.lower() not in ('.swc', '.asc', '.h5', ): raise Exception('Output file format should be one swc, asc or h5') output_ext = output_ext[1:] # Remove the dot try: converter = {MorphologyVersion.MORPHOLOGY_VERSION_SWC_1: from_swc, MorphologyVersion.MORPHOLOGY_VERSION_ASC_1: from_h5_or_asc, MorphologyVersion.MORPHOLOGY_VERSION_H5_1: from_h5_or_asc, MorphologyVersion.MORPHOLOGY_VERSION_H5_1_1: from_h5_or_asc, MorphologyVersion.MORPHOLOGY_VERSION_H5_2: from_h5_or_asc }[neuron.version] except KeyError as e: raise Exception( 'No converter for morphology type: {}'.format(neuron.version)) from e logger.info('Original soma type: %s', neuron.soma_type) new = converter(neuron, output_ext) if single_point_soma: soma_to_single_point(new.soma) if recenter: transform.translate(new, -1 * new.soma.center) new.write(outputfile) try: # pylint: disable=import-outside-toplevel from morph_tool.neuron_surface import get_NEURON_surface logger.info('Soma surface as computed by NEURON:\n' 'before conversion: %s\n' 'after conversion: %s', get_NEURON_surface(input_file), get_NEURON_surface(outputfile)) except: # noqa pylint: disable=bare-except logger.info('Final NEURON soma surface check was skipped probably because BluePyOpt' ' or NEURON is not installed')
def test_lifetime_copy_single_section(): """Copying a single section from a destroyed morphology works because it does not use any topological information""" section = _get_section() # Proof that the morphology has really been destroyed with pytest.raises(RuntimeError): section.children morph = Morphology() morph.append_root_section(section) del section assert len(morph.root_sections) == 1 assert_array_equal( morph.root_sections[0].points, np.array([[0., 0., 0.], [0., 5., 0.]], dtype=np.float32))