def test_consolidate(): skel = Skeleton( vertices=np.array([ (0, 0, 0), (1, 0, 0), (2, 0, 0), (0, 0, 0), (2, 1, 0), (2, 2, 0), (2, 2, 1), (2, 2, 2), ], dtype=np.float32), edges=np.array([ [0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], ], dtype=np.uint32), radii=np.array([0, 1, 2, 3, 4, 5, 6, 7], dtype=np.float32), vertex_types=np.array([0, 1, 2, 3, 4, 5, 6, 7], dtype=np.uint8), ) correct_skel = Skeleton( vertices=np.array([ (0, 0, 0), (1, 0, 0), (2, 0, 0), (2, 1, 0), (2, 2, 0), (2, 2, 1), (2, 2, 2), ], dtype=np.float32), edges=np.array([ [0, 1], [0, 2], [0, 3], [1, 2], [3, 4], [4, 5], [5, 6], ], dtype=np.uint32), radii=np.array([0, 1, 2, 4, 5, 6, 7], dtype=np.float32), vertex_types=np.array([0, 1, 2, 4, 5, 6, 7], dtype=np.uint8), ) consolidated = skel.consolidate() assert np.all(consolidated.vertices == correct_skel.vertices) assert np.all(consolidated.edges == correct_skel.edges) assert np.all(consolidated.radii == correct_skel.radii) assert np.all(consolidated.vertex_types == correct_skel.vertex_types)
def test_read_swc(): # From http://research.mssm.edu/cnic/swc.html test_file = """# ORIGINAL_SOURCE NeuronStudio 0.8.80 # CREATURE # REGION # FIELD/LAYER # TYPE # CONTRIBUTOR # REFERENCE # RAW # EXTRAS # SOMA_AREA # SHINKAGE_CORRECTION 1.0 1.0 1.0 # VERSION_NUMBER 1.0 # VERSION_DATE 2007-07-24 # SCALE 1.0 1.0 1.0 1 1 14.566132 34.873772 7.857000 0.717830 -1 2 0 16.022520 33.760513 7.047000 0.463378 1 3 5 17.542000 32.604973 6.885001 0.638007 2 4 0 19.163984 32.022469 5.913000 0.602284 3 5 0 20.448090 30.822802 4.860000 0.436025 4 6 6 21.897903 28.881084 3.402000 0.471886 5 7 0 18.461960 30.289471 8.586000 0.447463 3 8 6 19.420759 28.730757 9.558000 0.496217 7""" skel = Skeleton.from_swc(test_file) assert skel.vertices.shape[0] == 8 assert skel.edges.shape[0] == 7 skel_gt = Skeleton( vertices=[[14.566132, 34.873772, 7.857000], [16.022520, 33.760513, 7.047000], [17.542000, 32.604973, 6.885001], [19.163984, 32.022469, 5.913000], [20.448090, 30.822802, 4.860000], [21.897903, 28.881084, 3.402000], [18.461960, 30.289471, 8.586000], [19.420759, 28.730757, 9.558000]], edges=[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (2, 6), (7, 6)], radii=[ 0.717830, 0.463378, 0.638007, 0.602284, 0.436025, 0.471886, 0.447463, 0.496217 ], vertex_types=[1, 0, 5, 0, 0, 6, 0, 6], ) assert Skeleton.equivalent(skel, skel_gt) skel = Skeleton.from_swc(skel.to_swc()) assert np.all(np.abs(skel.vertices - skel_gt.vertices) < 0.00001) # sorts edges skel = skel.consolidate() skel_gt = skel_gt.consolidate() assert np.all(skel.edges == skel_gt.edges) assert np.all(np.abs(skel.radii - skel_gt.radii) < 0.00001) Nv = skel.vertices.shape[0] Ne = skel.edges.shape[0] for _ in range(10): skel = Skeleton.from_swc(skel.to_swc()) assert skel.vertices.shape[0] == Nv assert skel.edges.shape[0] == Ne