def readSkelFromFile(fname, iz, iy, ix, bsize, anisotropy): print("Reading from file: " + fname) with open(fname, 'r') as f: inp_swc = f.read() skel_read = PrecomputedSkeleton.from_swc(inp_swc) # revert order from x y z to z y x vertices_copy = skel_read.vertices.copy() skel_read.vertices[:, 0] = vertices_copy[:, 2] skel_read.vertices[:, 2] = vertices_copy[:, 0] x_offset = ix * bsize[OR_X] * anisotropy[OR_X] y_offset = iy * bsize[OR_Y] * anisotropy[OR_Y] z_offset = iz * bsize[OR_Z] * anisotropy[OR_Z] transform_ = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]) transform_[0, 3] = z_offset transform_[1, 3] = y_offset transform_[2, 3] = x_offset skel_read.transform = transform_ skel_read.apply_transform() skel_read.transform = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]) return skel_read
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 = PrecomputedSkeleton.from_swc(test_file) assert skel.vertices.shape[0] == 8 assert skel.edges.shape[0] == 7 skel_gt = PrecomputedSkeleton( 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 PrecomputedSkeleton.equivalent(skel, skel_gt)
def readSkelFromFile(fname, bz, by, bx, bsize, anisotropy): print("Reading from file: " + fname) with open(fname, 'r') as f: inp_swc = f.read() skel_read = PrecomputedSkeleton.from_swc(inp_swc) x_offset = bx*bsize[0]*anisotropy[0] y_offset = by*bsize[1]*anisotropy[1] z_offset = bz*bsize[2]*anisotropy[2] print("Block: " + str((bx,by,bz))) print("Offset: "+ str((x_offset,y_offset,z_offset))) skel_read.transform = np.array([[1,0,0,x_offset], [0,1,0,y_offset], [0,0,1,z_offset]]) print("transform: " + str(skel_read.transform)) # print("xmin, xmax: " + str(np.min(skel_read.vertices[:,0])) + ", " + str(np.max(skel_read.vertices[:,0]))) # print("ymin, ymax: " + str(np.min(skel_read.vertices[:,1])) + ", " + str(np.max(skel_read.vertices[:,1]))) # print("zmin, zmax: " + str(np.min(skel_read.vertices[:,2])) + ", " + str(np.max(skel_read.vertices[:,2]))) # print("----------------------------") # skel_read.apply_transform() # # print("xmin, xmax: " + str(np.min(skel_read.vertices[:,0])) + ", " + str(np.max(skel_read.vertices[:,0]))) # print("ymin, ymax: " + str(np.min(skel_read.vertices[:,1])) + ", " + str(np.max(skel_read.vertices[:,1]))) # print("zmin, zmax: " + str(np.min(skel_read.vertices[:,2])) + ", " + str(np.max(skel_read.vertices[:,2]))) skel_read.transform = np.array([[1,0,0,0], [0,1,0,0], [0,0,1,0]]) return skel_read