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 list_subfolders(inputfolder, subfolders=[]): ''' list folders, each containing layers of one stack ''' files = filelib.list_subfolders(inputfolder, subfolders=subfolders) files.sort() folders = [] for f in files: folders.append(filelib.return_path(f)) folders = np.unique(folders) return folders
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 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))