def segment(folder, params): ''' Segment all layers in a folder ''' #create folders for the output filelib.make_folders([ params.inputfolder + '../segmented/masks/glomeruli/' + folder, params.inputfolder + '../segmented/masks/kidney/' + folder, params.inputfolder + '../log/' + folder ]) start = time.time() #list all files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() params.folder = folder #segment all layers in parallel boost.run_parallel(process=segment_layer, files=files, params=params, procname='Segmentation of glomeruli') # segment_layer(files[50], params) t = pd.Series({ 'Segmentation': time.time() - start, 'threads': params.max_threads }) t.to_csv(params.inputfolder + '../log/' + folder + 'Segmentation.csv', sep='\t')
def quantify(folder, params): ''' Quantify a stack ''' if not os.path.exists(params.inputfolder + '../statistics/' + folder[:-1] + '.csv'): #list files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() #create a folder for statistics filelib.make_folders([params.inputfolder + '../statistics/' + filelib.return_path(folder[:-1] + '.csv')]) #extract voxel size xsize, zsize = extract_zoom(folder) #compute volume of the kidney kidney_volume = 0 for i in range(len(files)): ind = np.load(params.inputfolder + '../segmented/masks/' + folder + files[i][:-4] + '.npy') kidney_volume = kidney_volume + len(ind[0]) stat = pd.DataFrame() stat['Kidney_volume'] = [kidney_volume*xsize**2*zsize] stat['Image_name'] = folder[:-1] stat.to_csv(params.inputfolder + '../statistics/' + folder[:-1] + '.csv', sep = '\t')
def segment(folder, params): ''' Segment all layers in a folder ''' #create folders for the output filelib.make_folders([params.inputfolder + '../segmented/outlines/' + folder, params.inputfolder + '../segmented/masks/' + folder]) #list all files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() ind = np.int_(np.arange(0, len(files), 10)) files = np.array(files)[ind] if not len(filelib.list_image_files(params.inputfolder + '../segmented/masks/' + folder)) == len(files): params.folder = folder #segment all layers in parallel boost.run_parallel(process = segment_layer, files = files, params = params, procname = 'Segmentation of glomeruli')
def quantify(folder, params): ''' Quantify a stack ''' start = time.time() #list files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() #create a folder for statistics filelib.make_folders([ params.inputfolder + '../statistics/' + filelib.return_path(folder[:-1] + '.csv') ]) #extract voxel size xsize, zsize = extract_zoom(folder) #compute volume of the kidney kidney_volume = 0 for i in range(len(files)): ind = np.load(params.inputfolder + '../segmented/masks/kidney/' + folder + files[i][:-4] + '.npy') kidney_volume = kidney_volume + len(ind[0]) #compute glomeruli characteristics maxvol = 4. / 3 * np.pi * float(params.maxrad)**3 #maximal allowed volume minvol = 4. / 3 * np.pi * float(params.minrad)**3 #minimal allowed volume print 'load data', len(files) ind0 = [] ind1 = [] ind2 = [] label = [] for i in range(len(files)): ind = np.load(params.inputfolder + '../segmented/labels/glomeruli/' + folder + files[i][:-4] + '.npy') label.append(ind[0]) ind1.append(ind[1]) ind2.append(ind[2]) ind0.append(np.ones_like(ind[1]) * i) ind0 = np.hstack(ind0) ind1 = np.hstack(ind1) ind2 = np.hstack(ind2) label = np.hstack(label) llist = np.unique(label) volumes = ndimage.sum((label > 0) * 1., label, llist) * xsize**2 * zsize ind = np.where((volumes < maxvol) & (volumes > minvol)) volumes = volumes[ind] llist = llist[ind] sizes = 2 * pow(3. / 4 * volumes / np.pi, 1. / 3) stat = pd.DataFrame({'Label': llist, 'Diameter': sizes, 'Volume': volumes}) stat['Image_name'] = folder[:-1] stat['Kidney_volume'] = kidney_volume * xsize**2 * zsize stat.to_csv(params.inputfolder + '../statistics/' + folder[:-1] + '.csv', sep='\t') t = pd.Series({'Quantification': time.time() - start}) t.to_csv(params.inputfolder + '../log/' + folder + 'Quantification.csv', sep='\t')
def label_cells(folder, params): ''' label connected objects in a stack ''' start = time.time() #list files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() #make output folder for labels filelib.make_folders( [params.inputfolder + '../segmented/labels/glomeruli/' + folder]) #initiate labelling num = 0 shape = tifffile.imread( params.inputfolder + folder + files[0][:-4] + '.tif').shape #get image dimensions from the first image mask = load_mask(params.inputfolder + '../segmented/masks/glomeruli/' + folder + files[0][:-4] + '.npy', shape) #load the first mask l0 = np.zeros_like(mask) labels = [] limsize = int(float(params.maxrad)) nfiles = list(np.array(files)) #label for i in range(len(files)): mask = load_mask( params.inputfolder + '../segmented/masks/glomeruli/' + folder + files[i][:-4] + '.npy', shape) l, n = ndimage.label(mask) mask = (mask > 0) * 1. labels.append(np.zeros_like(l)) for j in range(n): ind = np.where(l == j + 1) ls = np.unique(l0[ind]) ls = ls[np.where(ls > 0)] if len(ls) == 0: num += 1 labels[-1][ind] = num if len(ls) == 1: labels[-1][ind] = ls[0] if len(ls) > 1: labels[-1][ind] = ls[0] for k in range(1, len(ls)): for s in range(len(labels)): labels[s] = np.where(labels[s] == ls[k], ls[0], labels[s]) l0 = labels[-1] #if number of layers exceeds limit, save the first layer if len(labels) > limsize: mask = labels.pop(0) fname = nfiles.pop(0) ind = np.where(mask > 0) lb = mask[ind] np.save( params.inputfolder + '../segmented/labels/glomeruli/' + folder + fname[:-4] + '.npy', np.array([lb, ind[0], ind[1]])) #save remaining layers for i in range(len(labels)): mask = labels.pop(0) fname = nfiles.pop(0) ind = np.where(mask > 0) lb = mask[ind] np.save( params.inputfolder + '../segmented/labels/glomeruli/' + folder + fname[:-4] + '.npy', np.array([lb, ind[0], ind[1]])) t = pd.Series({'Labelling': time.time() - start}) t.to_csv(params.inputfolder + '../log/' + folder + 'Labelling.csv', sep='\t')
def quantify(folder, params): ''' Quantify a stack ''' start = time.time() #list files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() #create a folder for statistics filelib.make_folders([ params.inputfolder + '../statistics/' + filelib.return_path(folder[:-1] + '.csv') ]) #extract voxel size xsize, zsize = extract_zoom(folder) print("xsize: " + str(xsize) + ", zsize: " + str(zsize)) #compute volume of the kidney kidney_volume = 0 for i in range(len(files)): ind = np.load(params.inputfolder + '../segmented/masks/kidney/' + folder + files[i][:-4] + '.npy') kidney_volume = kidney_volume + len(ind[0]) #compute glomeruli characteristics maxvol = np.pi * float(params.maxrad)**2 #maximal allowed area minvol = np.pi * float(params.minrad)**2 #minimal allowed area print 'load data', len(files) ind0 = [] ind1 = [] ind2 = [] label = [] for i in range(len(files)): ind = np.load(params.inputfolder + '../segmented/labels/glomeruli/' + folder + files[i][:-4] + '.npy') label.append(ind[0]) ind1.append(ind[1]) ind2.append(ind[2]) ind0.append(np.ones_like(ind[1]) * i) ind0 = np.hstack(ind0) ind1 = np.hstack(ind1) ind2 = np.hstack(ind2) label = np.hstack(label) llist = np.unique(label) volumes = ndimage.sum( (label > 0) * 1., label, llist) * xsize * xsize # Modification: Calculate areas ind = np.where((volumes < maxvol) & (volumes > minvol)) volumes = volumes[ind] llist = llist[ind] sizes = 2 * np.sqrt(volumes / np.pi) stat = pd.DataFrame({'Label': llist, 'Diameter': sizes, 'Volume': volumes}) stat['Image_name'] = folder[:-1] stat['Kidney_volume'] = kidney_volume * xsize**2 stat.to_csv(params.inputfolder + '../statistics/' + folder[:-1] + '.csv', sep='\t') t = pd.Series({'Quantification': time.time() - start}) t.to_csv(params.inputfolder + '../log/' + folder + 'Quantification.csv', sep='\t') shape = tifffile.imread( params.inputfolder + folder + files[0][:-4] + '.tif').shape # get image dimensions from the first image for i in range(len(files)): ind = np.load(params.inputfolder + '../segmented/labels/glomeruli/' + folder + files[i][:-4] + '.npy') lbl = ind[0] mask = np.ones(len(lbl), np.bool) mask[not lbl in llist] = 0 lbl[mask] = 0 lbl = np.reshape(lbl, shape) skimage.io.imsave( params.inputfolder + '../segmented/labels/glomeruli/' + params.folder + files[i][:-4] + '.tif', lbl.astype(np.int))
def label_cells(folder, params): ''' label connected objects in a stack ''' start = time.time() #list files in the folder files = filelib.list_image_files(params.inputfolder + folder) files.sort() #make output folder for labels filelib.make_folders( [params.inputfolder + '../segmented/labels/glomeruli/' + folder]) #initiate labelling num = 0 shape = tifffile.imread( params.inputfolder + folder + files[0][:-4] + '.tif').shape #get image dimensions from the first image mask = load_mask(params.inputfolder + '../segmented/masks/glomeruli/' + folder + files[0][:-4] + '.npy', shape) #load the first mask l0 = np.zeros_like(mask) # Label for last layer labels = [] limsize = int(float(params.maxrad)) nfiles = list(np.array(files)) #label for i in range(len(files)): # Go through each image (in order) mask = load_mask( params.inputfolder + '../segmented/masks/glomeruli/' + folder + files[i][:-4] + '.npy', shape) l, n = ndimage.label(mask) # Label the image mask = (mask > 0) * 1. labels.append( np.zeros_like(l) ) # Create a new label image initialized with 0 and add it to the list for j in range(n): # For each label: ind = np.where( l == j + 1) ## Get all indices where the current label is (j + 1) ls = np.unique( l0[ind] ) # Get all labels in last layer where current label is (j + 1) ls = ls[np.where(ls > 0)] # Only consider non-background if len(ls) == 0: # If there is no connection, create a new label num += 1 labels[-1][ind] = num if len( ls ) == 1: # If there is a connection to 1 specific id, rename the current layer (labels[-1]) label labels[-1][ind] = ls[0] if len( ls ) > 1: # If there are multiple connections, synchronize them labels[-1][ind] = ls[ 0] # Assign current label from last label (for the first) for k in range( 1, len(ls) ): # Go through all last labels (except the first one) for s in range(len( labels)): # Go through the list of stored labels labels[s] = np.where( labels[s] == ls[k], ls[0], labels[s]) #Replace all ls[k] with ls[0] l0 = labels[-1] #if number of layers exceeds limit, save the first layer if len(labels) > limsize: mask = labels.pop(0) fname = nfiles.pop(0) ind = np.where(mask > 0) lb = mask[ind] np.save( params.inputfolder + '../segmented/labels/glomeruli/' + folder + fname[:-4] + '.npy', np.array([lb, ind[0], ind[1]])) #save remaining layers for i in range(len(labels)): mask = labels.pop(0) fname = nfiles.pop(0) ind = np.where(mask > 0) lb = mask[ind] np.save( params.inputfolder + '../segmented/labels/glomeruli/' + folder + fname[:-4] + '.npy', np.array([lb, ind[0], ind[1]])) t = pd.Series({'Labelling': time.time() - start}) t.to_csv(params.inputfolder + '../log/' + folder + 'Labelling.csv', sep='\t')