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_sanitize(): with TemporaryDirectory('test-sanitize') as tmp_folder: output_path = Path(tmp_folder, 'sanitized.asc') sanitize(Path(PATH, 'simple-with-duplicates.asc'), output_path) neuron = Morphology(output_path) assert_equal(len(neuron.root_sections), 1) assert_array_equal( neuron.section(0).points, [[0., 0., 0.], [1., 1., 0.], [2., 0., 0.], [3., 0., 0.]]) for input_morph, expected_exception in [ ('no-soma.asc', '{} has an invalid or no soma'.format(Path(PATH, 'no-soma.asc'))), ('neurite-with-multiple-types.swc', ('{} has a neurite whose type changes along the way\n' 'Child section (id: 5) has a different type (SectionType.basal_dendrite) ' 'than its parent (id: 3) (type: SectionType.axon)').format( Path(PATH, 'neurite-with-multiple-types.swc'))) ]: with assert_raises(CorruptedMorphology) as cm: sanitize(PATH / input_morph, Path(tmp_folder, 'output.asc')) assert_equal(str(cm.exception), expected_exception) out_path = Path(tmp_folder, 'output.asc') sanitize(PATH / 'negative-diameters.asc', out_path) assert_equal(next(Morphology(out_path).iter()).diameters, [2, 2, 0, 2])
def test_more_iter(): '''This used to fail at commit f74ce1f56de805ebeb27584051bbbb3a65cd1213''' m = Morphology(os.path.join(_path, 'simple.asc')) sections = list(m.iter()) assert_array_equal([s1.id for s1 in sections], [0, 1, 2, 3, 4, 5]) sections = list(m.iter(IterType.breadth_first)) assert_array_equal([s.id for s in sections], [0, 3, 1, 2, 4, 5]) sections = list(m.section(2).iter(IterType.upstream)) assert_array_equal([s.id for s in sections], [2, 0])
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_sanitize(): with TemporaryDirectory('test-sanitize') as tmp_folder: output_path = Path(tmp_folder, 'sanitized.asc') sanitize(Path(PATH, 'simple-with-duplicates.asc'), output_path) neuron = Morphology(output_path) assert_equal(len(neuron.root_sections), 1) assert_array_equal( neuron.section(0).points, [[0., 0., 0.], [1., 1., 0.], [2., 0., 0.], [3., 0., 0.]]) assert_raises(CorruptedMorphology, sanitize, Path(PATH, 'no-soma.asc'), Path(tmp_folder, 'no-soma.asc')) assert_raises(CorruptedMorphology, sanitize, Path(PATH, 'negative-diameters.asc'), Path(tmp_folder, 'negative-diameter.asc'))