def test_multiple_soma(): with assert_raises(SomaError) as obj: Morphology(os.path.join(_path, 'multiple_soma.swc')) assert_substring( ''.join([ 'Multiple somata found: \n\n', os.path.join(_path, 'multiple_soma.swc:2:error\n\n\n'), os.path.join(_path, 'multiple_soma.swc:11:error') ]), strip_color_codes(str(obj.exception)))
def test_unsupported_section_type(): with tmp_swc_file('''1 1 0 4 0 3.0 -1 2 3 0 0 2 0.5 1 3 5 0 0 3 0.5 2 # <-- 5 is unsupported section type ''') as tmp_file: with assert_raises(RawDataError) as obj: Morphology(tmp_file.name) assert_substring('.swc:3:error', strip_color_codes(str(obj.exception))) assert_substring('Unsupported section type: 5', strip_color_codes(str(obj.exception)))
def test_single_children(): '''Single children are merged with their parent''' with tmp_asc_file(''' ((Dendrite) (3 -4 0 2) (3 -6 0 2) (3 -8 0 2) (3 -10 0 2) ( (3 -10 0 2) ; merged with parent section (0 -10 0 2) ; merged with parent section (-3 -10 0 2) ; merged with parent section ( (-5 -5 5 5) | (-6 -6 6 6) ) ) ) ''') as tmp_file: with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): n = Morphology(tmp_file.name) assert_substring('is the only child of section: 0 starting at:', err.getvalue().strip()) assert_substring('It will be merged with the parent section', err.getvalue().strip()) nt.assert_equal(len(n.soma.points), 0) nt.assert_equal(len(n.soma.points), 0) assert_equal(len(n.root_sections), 1) assert_array_equal(n.root_sections[0].points, np.array([[3, -4, 0], [3, -6, 0], [3, -8, 0], [3, -10, 0], [0, -10, 0], [-3, -10, 0]], dtype=np.float32)) assert_equal(len(n.root_sections[0].children), 2) assert_array_equal(n.root_sections[0].children[0].points, np.array([[-3, -10, 0], [-5, -5, 5]])) assert_array_equal(n.root_sections[0].children[1].points, np.array([[-3, -10, 0], [-6, -6, 6]]))
def test_non_C_nparray(): m = Morphology(os.path.join(_path, "simple.swc")) section = m.root_sections[0] points = np.array([[1, 2, 3], [4, 5, 6]]) section.points = points assert_array_equal(section.points, points) with assert_raises(MorphioError) as obj: section.points = points.T assert_substring("Wrong array shape. Expected: (X, 3), got: (3, 2)", str(obj.exception)) non_standard_stride = np.asfortranarray(points) section.points = non_standard_stride assert_array_equal(section.points, points)
def test_point_level(): a = PointLevel([[1, 2, 3]], [2], []) assert_equal(a.points, [[1, 2, 3]]) assert_equal(a.diameters, [2]) with assert_raises(SectionBuilderError) as obj: a = PointLevel([[1, 2, 3], [1, 2, 3]], [2], []) assert_substring( "Point vector have size: 2 while Diameter vector has size: 1", str(obj.exception)) with assert_raises(SectionBuilderError) as obj: a = PointLevel([[1, 2, 3], [1, 2, 3]], [2, 3], [4]) assert_substring( "Point vector have size: 2 while Perimeter vector has size: 1", str(obj.exception))
def test_empty_sibling(): '''The empty sibling will be removed and the single child will be merged with its parent''' with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): with tmp_asc_file('''((Dendrite) (3 -4 0 10) (3 -6 0 9) (3 -8 0 8) (3 -10 0 7) ( (3 -10 0 6) (0 -10 0 5) (-3 -10 0 4) | ; <-- empty sibling but still works ! ) ) ''') as tmp_file: n = Morphology(tmp_file.name) n.remove_unifurcations() assert_substring('is the only child of section: 0', err.getvalue().strip()) assert_substring('It will be merged with the parent section', err.getvalue().strip()) assert len(n.root_sections) == 1 assert_array_equal( n.root_sections[0].points, np.array([[3, -4, 0], [3, -6, 0], [3, -8, 0], [3, -10, 0], [0, -10, 0], [-3, -10, 0]], dtype=np.float32)) assert_array_equal(n.root_sections[0].diameters, np.array([10, 9, 8, 7, 5, 4], dtype=np.float32)) assert len(n.annotations) == 1 annotation = n.annotations[0] assert annotation.type == morphio.AnnotationType.single_child assert annotation.line_number == -1 assert_array_equal(annotation.points, [[3, -10, 0], [0, -10, 0], [-3, -10, 0]]) assert_array_equal(annotation.diameters, [6, 5, 4])
def test_empty_sibling(): '''The empty sibling will be removed and the single child will be merged with its parent''' with captured_output() as (_, err): with ostream_redirect(stdout=True, stderr=True): with tmp_asc_file('''((Dendrite) (3 -4 0 2) (3 -6 0 2) (3 -8 0 2) (3 -10 0 2) ( (3 -10 0 2) (0 -10 0 2) (-3 -10 0 2) | ; <-- empty sibling but still works ! ) ) ''') as tmp_file: n = Morphology(tmp_file.name) assert_substring('is the only child of section: 0 starting at:', err.getvalue().strip()) assert_substring('It will be merged with the parent section', err.getvalue().strip()) assert_equal(len(n.root_sections), 1) assert_array_equal(n.root_sections[0].points, np.array([[3, -4, 0], [3, -6, 0], [3, -8, 0], [3, -10, 0], [0, -10, 0], [-3, -10, 0]], dtype=np.float32)) assert_equal(len(n.annotations), 1) annotation = n.annotations[0] assert_equal(annotation.type, morphio.AnnotationType.single_child) assert_equal(annotation.line_number, 6) assert_array_equal(annotation.points, [[3, -10, 0], [0, -10, 0], [-3, -10, 0]]) assert_array_equal(annotation.diameters, [2, 2, 2])