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_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_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_no_soma(): morpho = Morphology() dendrite = morpho.append_root_section( PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2]), SectionType.basal_dendrite) dendrite = morpho.append_root_section( PointLevel([[0, 1, 0], [0, 7, 0]], [2, 2]), SectionType.basal_dendrite) with setup_tempdir('test_write_no_soma') as tmp_folder: for ext in ['asc', 'h5', 'swc']: with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): outfile = os.path.join(tmp_folder, 'tmp.' + ext) morpho.write(outfile) assert_equal(err.getvalue().strip(), 'Warning: writing file without a soma') read = Morphology(outfile) assert_equal(len(read.soma.points), 0) assert_equal(len(read.root_sections), 2) assert_array_equal(read.root_sections[0].points, [[0, 0, 0], [0, 5, 0]]) assert_array_equal(read.root_sections[1].points, [[0, 1, 0], [0, 7, 0]])
def test_sanitize(): m = Morphology() section = m.append_root_section( PointLevel([[1, 0, 0], [2, 0, 0]], [2, 2], [20, 20]), SectionType.axon) section.append_section(PointLevel([[2, 0, 0], [3, 0, 0]], [2, 2], [20, 20])) with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): m.sanitize() assert_equal(len(list(m.iter())), 1) assert_equal( err.getvalue().strip(), 'Warning: section 1 is the only child of section: 0\nIt will be merged ' 'with the parent section') # Checking that sanitize() issues a warning on missing duplicate m = Morphology() section = m.append_root_section( PointLevel([[1, 0, 0], [2, 0, 0]], [2, 2], [20, 20]), SectionType.axon) section.append_section(PointLevel([[2, 0, 0], [3, 0, 0]], [2, 2], [20, 20])) with captured_output() as (_, err): with ostream_redirect(): section.append_section( PointLevel([[2, 1, 0], [2, 0, 0]], [2, 2], [20, 20])) with captured_output() as (_, err): with ostream_redirect(): m.sanitize() assert_equal( err.getvalue().strip(), 'Warning: while appending section: 2 to parent: 0\nThe section first point should be parent section last point: \n : X Y Z Diameter\nparent last point :[2.000000, 0.000000, 0.000000, 2.000000]\nchild first point :[2.000000, 1.000000, 0.000000, 2.000000]' )
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_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_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_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))
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_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_write_merge_only_child_asc_h5(): '''The root section has only one child When writing, children should *not* be merged with their parent section. Note: See `test_write_merge_only_child_swc` for the SWC case. ''' morpho = Morphology() morpho.soma.points = [[0, 0, 0]] morpho.soma.diameters = [2] root = morpho.append_root_section( PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2]), SectionType.basal_dendrite) child = root.append_section(PointLevel([[0, 5, 0], [0, 6, 0]], [2, 3])) with setup_tempdir('test_write_merge_only_child') as tmp_folder: for extension in ['asc', 'h5']: with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): filename = Path(tmp_folder, 'test.{}'.format(extension)) morpho.write(filename) read = Morphology(filename) root = read.root_sections[0] assert_array_equal(root.points, [[0, 0, 0], [0, 5, 0]]) assert len(root.children) == 1
def test_duplicate_different_diameter(): '''Test that starting a child section with a different diamete work as expected''' morpho = Morphology() morpho.soma.points = [[0, 0, 0], [1, 1, 1]] morpho.soma.diameters = [1, 1] section = morpho.append_root_section( PointLevel([[2, 2, 2], [3, 3, 3]], [4, 4]), SectionType.axon, ) section.append_section(PointLevel([[3, 3, 3], [4, 4, 4]], [10, 12])) section.append_section(PointLevel([[3, 3, 3], [5, 5, 5]], [11, 12])) with setup_tempdir('test_write_duplicate_different_diameter', no_cleanup=True) as tmp_folder: for ext in ['asc', 'h5', 'swc']: with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): outfile = os.path.join(tmp_folder, 'tmp.' + ext) morpho.write(outfile) read = Morphology(outfile) assert_equal(len(read.root_sections[0].children), 2) child1, child2 = read.root_sections[0].children assert_array_equal(child1.points, [[3, 3, 3], [4, 4, 4]]) assert_array_equal(child2.points, [[3, 3, 3], [5, 5, 5]]) assert_array_equal(child1.diameters, [10, 12]) assert_array_equal(child2.diameters, [11, 12])
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_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_write_merge_only_child(): '''The root section has only one child The child should be merged with its parent section Special care must be given for the potential duplicate point o / / son 1 root child / o--------o----------o< \ \ son 2 \ o ''' morpho = Morphology() morpho.soma.points = [[0, 0, 0]] morpho.soma.diameters = [2] root = morpho.append_root_section( PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2]), SectionType.basal_dendrite) child = root.append_section(PointLevel([[0, 5, 0], [0, 6, 0]], [2, 3])) son1 = child.append_section(PointLevel([[0, 6, 0], [0, 7, 0]], [2, 3])) son2 = child.append_section(PointLevel([[0, 6, 0], [4, 5, 6]], [3, 3])) with setup_tempdir('test_write_merge_only_child') as tmp_folder: for extension in ['swc', 'asc', 'h5']: with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): filename = os.path.join(tmp_folder, 'test.{}'.format(extension)) morpho.write(filename) assert_equal(err.getvalue().strip(), 'Warning: section 1 is the only child of section: 0\nIt will be merged with the parent section') read = Morphology(filename) root = read.root_sections[0] assert_array_equal(root.points, [[0, 0, 0], [0, 5, 0], [0, 6, 0]]) assert_equal(len(root.children), 2) assert_array_equal(root.children[0].points, [[0, 6, 0], [0, 7, 0]]) assert_array_equal(root.children[1].points, [[0, 6, 0], [4, 5, 6]])
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_write_merge_only_child_swc(): '''Attempts to write a morphology with unifurcations with SWC should result in an exception. ''' morpho = Morphology() morpho.soma.points = [[0, 0, 0]] morpho.soma.diameters = [2] root = morpho.append_root_section( PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2]), SectionType.basal_dendrite) child = root.append_section(PointLevel([[0, 5, 0], [0, 6, 0]], [2, 3])) with pytest.raises(WriterError, match=r"Section 0 has a single child section\. " r"Single child section are not allowed when writing to SWC format\. " r"Please sanitize the morphology first\."): morpho.write('/tmp/bla.swc') # the path does not need to exists since it will fail before
def test_append_no_duplicate(): m = Morphology() section = m.append_root_section( PointLevel([[1, 2, 3], [4, 5, 6]], [2, 2], [20, 20]), SectionType.axon) assert_equal(section.id, 0) with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): section.append_section( PointLevel([[400, 5, 6], [7, 8, 9]], [2, 3], [20, 30])) assert_equal( err.getvalue().strip(), 'Warning: while appending section: 1 to parent: 0\n' 'The section first point should be parent section last point: \n' ' : X Y Z Diameter\n' 'parent last point :[4.000000, 5.000000, 6.000000, 2.000000]\n' 'child first point :[400.000000, 5.000000, 6.000000, 2.000000]' )