def test_read_duplicate_vertex_swc(): test_file = """ 1 0 -18.458370 23.227150 -84.035016 1.000000 -1 2 0 -18.159709 22.925778 -82.984344 1.000000 1 3 0 -17.861047 22.624407 -82.984344 1.000000 2 4 0 -17.562385 22.624407 -82.984344 1.000000 3 5 0 -16.965061 22.021663 -82.984344 1.000000 4 6 0 -16.965061 21.720292 -82.984344 1.000000 5 7 0 -16.069075 21.720292 -82.984344 1.000000 6 8 0 -16.069075 21.117548 -80.883000 1.000000 7 9 0 -15.770414 20.816176 -80.883000 1.000000 8 10 0 -15.770414 20.514805 -80.883000 1.000000 9 11 0 -15.770414 20.816176 -80.883000 1.000000 10 12 0 -16.069075 21.117548 -80.883000 1.000000 11 13 0 -16.069075 21.418920 -80.883000 1.000000 12 14 0 -16.069075 20.816176 -78.781655 1.000000 13 15 0 -15.471752 20.213433 -76.680311 1.000000 14 16 0 -15.471752 19.309318 -76.680311 1.000000 15 17 0 -15.471752 19.007946 -75.629639 1.000000 16 18 0 -15.173090 18.706574 -74.578966 1.000000 17 19 0 -14.874428 18.706574 -74.578966 1.000000 18 20 0 -14.575766 18.405202 -74.578966 1.000000 19 """ skel = Skeleton.from_swc(test_file) assert skel.vertices.shape[0] == 20 skel2 = Skeleton.from_swc(skel.to_swc()) assert skel2.vertices.shape[0] == 20 assert Skeleton.equivalent(skel, skel2)
def _swc2skeleton(self, swc_file, benchmarking=False, origin=None): """Converts swc file into Skeleton object Arguments: swc_file {str} -- path to SWC file Keyword Arguments: origin {numpy array with shape (3,1)} -- origin of coordinate frame in microns, (default: None assumes (0,0,0) origin) Returns: skel {cloudvolume.Skeleton} -- Skeleton object of given SWC file """ with open(swc_file, "r") as f: contents = f.read() # get every line that starts with a hashtag comments = [ i.split(" ") for i in contents.split("\n") if i.startswith("#") ] offset = np.array( [float(j) for i in comments for j in i[2:] if "OFFSET" in i]) color = [ float(j) for i in comments for j in i[2].split(",") if "COLOR" in i ] # set alpha to 0.0 so skeleton is opaque color.append(0.0) color = np.array(color, dtype="float32") skel = Skeleton.from_swc(contents) # physical units # space can be 'physical' or 'voxel' skel.space = "physical" # hard coding parsing the id from the filename idx = swc_file.find("G") if benchmarking == True: idx1 = swc_file.find("_", swc_file.find("_") + 1) # finding second occurence of "_" idx2 = swc_file.find(".") skel.id = swc_file[idx1 + 1:idx2] else: skel.id = int(swc_file[idx + 2:idx + 5]) # hard coding changing data type of vertex_types skel.extra_attributes[-1]["data_type"] = "float32" skel.extra_attributes.append({ "id": "vertex_color", "data_type": "float32", "num_components": 4 }) # add offset to vertices # and shift by origin skel.vertices += offset if origin is not None: skel.vertices -= origin # convert from microns to nanometers skel.vertices *= 1000 skel.vertex_color = np.zeros((skel.vertices.shape[0], 4), dtype="float32") skel.vertex_color[:, :] = color return skel
def view(filename): """Visualize a .swc or .npy file.""" basename, ext = os.path.splitext(filename) if ext == ".swc": with open(filename, "rt") as swc: skel = Skeleton.from_swc(swc.read()) skel.viewer() elif ext == ".npy": labels = np.load(filename) cloudvolume.view(labels, segmentation=True) else: print("kimimaro: {filename} was not a .swc or .npy file.")
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