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 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_write_basic(): 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]), SectionType.basal_dendrite) dendrite.append_section(PointLevel([[0, 5, 0], [-5, 5, 0]], [2, 3])) dendrite.append_section(PointLevel([[0, 5, 0], [6, 5, 0]], [2, 3])) axon = morpho.append_root_section( PointLevel([[0, 0, 0], [0, -4, 0]], [2, 2]), SectionType.axon) axon.append_section(PointLevel([[0, -4, 0], [6, -4, 0]], [2, 4])) axon = axon.append_section(PointLevel([[0, -4, 0], [-5, -4, 0]], [2, 4])) with setup_tempdir('test_write_basic') as tmp_folder: morpho.write(Path(tmp_folder, "test_write.asc")) morpho.write(Path(tmp_folder, "test_write.swc")) h5_out = Path(tmp_folder, "test_write.h5") morpho.write(h5_out) expected = [[0., 0., 0.], [0., 5., 0.], [0., 5., 0.], [-5., 5., 0.], [0., 5., 0.], [6., 5., 0.], [0., 0., 0.], [0., -4., 0.], [0., -4., 0.], [6., -4., 0.], [0., -4., 0.], [-5., -4., 0.]] assert_array_equal(ImmutMorphology(Path(tmp_folder, "test_write.asc")).points, expected) assert_array_equal(ImmutMorphology(Path(tmp_folder, "test_write.swc")).points, expected) h5_morph = ImmutMorphology(Path(tmp_folder, "test_write.h5")) assert_array_equal(h5_morph.points, expected) assert h5_morph.version == ('h5', 1, 2) import h5py with h5py.File(h5_out, 'r') as h5_file: assert '/perimeters' not in h5_file.keys()
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_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_load_neuron_from_other_neurons(): filename = SWC_PATH / 'simple.swc' expected_points = [[0., 0., 0., 1.], [0., 5., 0., 1.], [0., 5., 0., 1.], [-5., 5., 0., 0.], [0., 5., 0., 1.], [6., 5., 0., 0.], [0., 0., 0., 1.], [0., -4., 0., 1.], [0., -4., 0., 1.], [6., -4., 0., 0.], [0., -4., 0., 1.], [-5., -4., 0., 0.]] assert_array_equal( nm.load_neuron(nm.load_neuron(filename)).points, expected_points) assert_array_equal( nm.load_neuron(Morphology(filename)).points, expected_points) assert_array_equal( nm.load_neuron(ImmutMorphology(filename)).points, expected_points)
def test_contour_surface(): # # circle contour # assert_almost_equal(ImmutMorphology(os.path.join(_path, "circle_contour.asc")).soma.surface, # np.pi, decimal=2) # assert_almost_equal(Morphology(os.path.join(_path, "circle_contour.asc")).soma.surface, # np.pi, decimal=2) # # triangular contour # assert_almost_equal(ImmutMorphology(os.path.join(_path, "h5/v1/Neuron.h5")).soma.surface, # 0.01, decimal=5) # assert_almost_equal(Morphology(os.path.join(_path, "h5/v1/Neuron.h5")).soma.surface, # 0.01, decimal=5) # # single point ASC # assert_almost_equal(ImmutMorphology(os.path.join(_path, "simple.asc")).soma.surface, # 4 * np.pi, decimal=2) # assert_almost_equal(Morphology(os.path.join(_path, "simple.asc")).soma.surface, # 4 * np.pi, decimal=2) # single point SWC assert_almost_equal(ImmutMorphology(os.path.join( _path, "simple.swc")).soma.surface, 4 * np.pi, decimal=2) assert_almost_equal(Morphology(os.path.join(_path, "simple.swc")).soma.surface, 4 * np.pi, decimal=2) # single point H5 assert_almost_equal(ImmutMorphology(os.path.join( _path, "h5/v1/simple.h5")).soma.surface, 4 * np.pi, decimal=2) assert_almost_equal(Morphology(os.path.join( _path, "h5/v1/simple.h5")).soma.surface, 4 * np.pi, decimal=2) # SWC three points cylinder assert_almost_equal(ImmutMorphology( os.path.join(_path, "soma_three_points_cylinder.swc")).soma.surface, 4 * np.pi * 81, decimal=2) assert_almost_equal(Morphology( os.path.join(_path, "soma_three_points_cylinder.swc")).soma.surface, 4 * np.pi * 81, decimal=2) # SWC consecutive cylinders (3 cylinders along X) assert_almost_equal(ImmutMorphology( os.path.join(_path, "soma_cylinders.swc")).soma.surface, 2 * np.pi * 40 * 3, decimal=2) assert_almost_equal(Morphology(os.path.join( _path, "soma_cylinders.swc")).soma.surface, 2 * np.pi * 40 * 3, decimal=2) with pytest.raises(SomaError): _ = Morphology(os.path.join(_path, "no_soma.swc")).soma.surface assert_almost_equal(Morphology( os.path.join(_path, "soma_single_frustum.swc")).soma.surface, 1201.428, decimal=3) # SWC multiple frustums assert_almost_equal(ImmutMorphology( os.path.join(_path, "soma_multiple_frustums.swc")).soma.surface, 4164.610254415956, decimal=3) assert_almost_equal(Morphology( os.path.join(_path, "soma_multiple_frustums.swc")).soma.surface, 4164.610254415956, decimal=3) # SWC complex assert_almost_equal(ImmutMorphology(os.path.join( _path, "complexe.swc")).soma.surface, 13.980, decimal=2)