def watershed(depths, points, indices, neighbor_lists, min_size=1, depth_factor=0.25, depth_ratio=0.1, tolerance=0.01, regrow=True): """ Segment vertices of a surface mesh into contiguous "watershed basins" by seed growing from an iterative selection of the deepest vertices. Steps :: 1. Grow segments from an iterative selection of the deepest seeds. 2. Regrow segments from the resulting seeds, until each seed's segment touches a boundary. 3. Use the segment() function to fill in the rest. 4. Merge segments if their seeds are too close to each other or their depths are very different. Note :: Despite the above precautions, the order of seed selection in segment() could possibly influence the resulting borders between adjoining segments (vs. propagate(), which is slower and insensitive to depth, but is not biased by seed order). Parameters ---------- depths : numpy array of floats depth values for all vertices (default -1) points : list of lists of floats each element is a list of 3-D coordinates of a vertex on a surface mesh indices : list of integers indices to mesh vertices to be segmented min_size : index the minimum number of vertices in a basin neighbor_lists : list of lists of integers each list contains indices to neighboring vertices for each vertex depth_factor : float factor to determine whether to merge two neighboring watershed catchment basins -- they are merged if the Euclidean distance between their basin seeds is less than this fraction of the maximum Euclidean distance between points having minimum and maximum depths depth_ratio : float the minimum fraction of depth for a neighboring shallower watershed catchment basin (otherwise merged with the deeper basin) tolerance : float tolerance for detecting differences in depth between vertices regrow : Boolean regrow segments from watershed seeds? Returns ------- segments : list of integers region numbers for all vertices (default -1) seed_indices : list of integers list of indices to seed vertices Examples -------- >>> # Perform watershed segmentation on the deeper portions of a surface: >>> import os >>> import numpy as np >>> from mindboggle.utils.mesh import find_neighbors >>> from mindboggle.utils.plots import plot_vtk >>> from mindboggle.utils.segment import watershed, segment >>> from mindboggle.utils.io_vtk import read_vtk, read_scalars, rewrite_scalars >>> path = os.environ['MINDBOGGLE_DATA'] >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> faces, lines, indices, points, npoints, depths, name, input_vtk = read_vtk(depth_file, >>> return_first=True, return_array=True) >>> indices = np.where(depths > 0.01)[0] # high to speed up >>> neighbor_lists = find_neighbors(faces, npoints) >>> min_size = 50 >>> depth_factor = 0.25 >>> depth_ratio = 0.1 >>> tolerance = 0.01 >>> regrow = True >>> # >>> segments, seed_indices = watershed(depths, points, >>> indices, neighbor_lists, min_size, depth_factor, depth_ratio, >>> tolerance, regrow) >>> # >>> # Write results to vtk file and view: >>> rewrite_scalars(depth_file, 'watershed.vtk', >>> segments, 'segments', segments) >>> plot_vtk('watershed.vtk') >>> # View watershed seeds: >>> seeds = -1 * np.ones(len(depths)) >>> for i, s in enumerate(seed_indices): >>> seeds[s] = i >>> rewrite_scalars(depth_file, 'watershed_seeds.vtk', >>> seeds, 'seeds', seeds) >>> plot_vtk('watershed_seeds.vtk') """ import numpy as np from time import time from mindboggle.labels.labels import extract_borders from mindboggle.utils.segment import segment from mindboggle.utils.compute import point_distance # Make sure argument is a list if isinstance(indices, np.ndarray): indices.tolist() print('Segment {0} vertices by a surface watershed algorithm'. format(len(indices))) verbose = False merge = True t0 = time() tiny = 0.000001 use_depth_ratio = True #------------------------------------------------------------------------- # Find the borders of the given mesh vertices (indices): #------------------------------------------------------------------------- D = np.ones(len(depths)) D[indices] = 2 borders, foo1, foo2 = extract_borders(range(len(depths)), D, neighbor_lists, ignore_values=[], return_label_pairs=False) #------------------------------------------------------------------------- # Select deepest vertex as initial seed: #------------------------------------------------------------------------- index_deepest = indices[np.argmax(depths[indices])] seed_list = [index_deepest] basin_depths = [] original_indices = indices[:] #------------------------------------------------------------------------- # Loop until all vertices have been segmented. # This limits the number of possible seeds: #------------------------------------------------------------------------- segments = -1 * np.ones(len(depths)) seed_indices = [] seed_points = [] all_regions = [] region = [] counter = 0 terminate = False while not terminate: # Add seeds to region: region.extend(seed_list) all_regions.extend(seed_list) # Remove seeds from vertices to segment: indices = list(frozenset(indices).difference(seed_list)) if indices: # Identify neighbors of seeds: neighbors = [] [neighbors.extend(neighbor_lists[x]) for x in seed_list] # Select neighbors that have not been previously selected # and are among the vertices to segment: old_seed_list = seed_list[:] seed_list = list(frozenset(neighbors).intersection(indices)) seed_list = list(frozenset(seed_list).difference(all_regions)) # For each vertex, select neighbors that are shallower: seed_neighbors = [] for seed in old_seed_list: seed_neighbors.extend([x for x in neighbor_lists[seed] if depths[x] - tolerance <= depths[seed]]) seed_list = list(frozenset(seed_list).intersection(seed_neighbors)) else: seed_list = [] # If there are no seeds remaining: if not len(seed_list): # If there is at least min_size points, assign counter to # segmented region, store index, and increment counter: if len(region) >= min_size: segments[region] = counter seed_indices.append(index_deepest) seed_points.append(points[index_deepest]) counter += 1 # Compute basin depth (max - min): Imax = region[np.argmax(depths[region])] Imin = region[np.argmin(depths[region])] max_depth = point_distance(points[Imax], [points[Imin]])[0] basin_depths.append(max_depth) # If vertices left to segment, re-initialize parameters: if indices: # Initialize new region/basin: region = [] # Select deepest unsegmented vertex as new seed # if its rescaled depth is close to 1: index_deepest = indices[np.argmax(depths[indices])] seed_list = [index_deepest] # Termination criteria: if not len(indices): terminate = True # Display current number and size of region: if verbose: print(" {0} vertices remain".format(len(indices))) print(' ...Segmented {0} initial watershed regions ({1:.2f} seconds)'. format(counter, time() - t0)) #------------------------------------------------------------------------- # Regrow from (deep) watershed seeds, stopping at borders: #------------------------------------------------------------------------- if regrow: print(' Regrow segments from watershed seeds, stopping at borders') indices = original_indices[:] segments = -1 * np.ones(len(depths)) all_regions = [] for iseed, seed_index in enumerate(seed_indices): seed_list = [seed_index] region = [] terminate = False while not terminate: # Add seeds to region: region.extend(seed_list) all_regions.extend(seed_list) # Remove seeds from vertices to segment: indices = list(frozenset(indices).difference(seed_list)) if indices: # Identify neighbors of seeds: neighbors = [] [neighbors.extend(neighbor_lists[x]) for x in seed_list] # Select neighbors that have not been previously selected # and are among the vertices to segment: old_seed_list = seed_list[:] seed_list = list(frozenset(neighbors).intersection(indices)) seed_list = list(frozenset(seed_list).difference(all_regions)) # For each vertex, select neighbors that are shallower: seed_neighbors = [] for seed in old_seed_list: seed_neighbors.extend([x for x in neighbor_lists[seed] if depths[x] - tolerance <= depths[seed]]) seed_list = list(frozenset(seed_list).intersection(seed_neighbors)) # Remove seed list if it contains a border vertex: if seed_list: if list(frozenset(seed_list).intersection(borders)): seed_list = [] else: seed_list = [] # Terminate growth for this seed if the seed_list is empty: if not len(seed_list): terminate = True # If there is at least min_size points, store index: if len(region) >= min_size: segments[region] = iseed # Display current number and size of region: if verbose: print(" {0} vertices remain".format(len(indices))) #--------------------------------------------------------------------- # Continue growth until there are no more vertices to segment: #--------------------------------------------------------------------- # Note: As long as keep_seeding=False, the segment values in `segments` # are equal to the order of the `basin_depths` and `seed_points` below. seed_lists = [[i for i,x in enumerate(segments) if x==s] for s in np.unique(segments) if s!=-1] segments = segment(indices, neighbor_lists, min_region_size=1, seed_lists=seed_lists, keep_seeding=False, spread_within_labels=False, labels=[], label_lists=[], values=[], max_steps='', verbose=False) print(' ...Regrew {0} watershed regions from seeds ({1:.2f} seconds)'. format(iseed+1, time() - t0)) #------------------------------------------------------------------------- # Merge watershed catchment basins: #------------------------------------------------------------------------- if merge: # Extract segments pairs at borders between watershed basins: print(' Merge watershed catchment basins with deeper neighboring basins') if verbose: print(' Extract basin borders') foo1, foo2, pairs = extract_borders(original_indices, segments, neighbor_lists, ignore_values=[-1], return_label_pairs=True) # Sort basin depths (descending order) -- return segment indices: Isort = np.argsort(basin_depths).tolist() Isort.reverse() # Find neighboring basins to each of the sorted basins: if verbose: print(" Find neighboring basins") basin_pairs = [] for index in Isort: index_neighbors = [int(list(frozenset(x).difference([index]))[0]) for x in pairs if index in x] if index_neighbors: # Store neighbors whose depth is less than a fraction of the # basin's depth and farther away than half the basin's depth: if use_depth_ratio: index_neighbors = [[x, index] for x in index_neighbors if basin_depths[x] / (basin_depths[index]+tiny) < depth_ratio if point_distance(seed_points[x], [seed_points[index]])[0] > depth_factor * max([basin_depths[x], basin_depths[index]])] # Store neighbors farther away than half the basin's depth: else: index_neighbors = [[x, index] for x in index_neighbors if point_distance(seed_points[x], [seed_points[index]])[0] > depth_factor * max([basin_depths[x], basin_depths[index]])] if index_neighbors: basin_pairs.extend(index_neighbors) # Merge shallow watershed catchment basins: if basin_pairs: if verbose: print(' Merge basins with deeper neighboring basins') for basin_pair in basin_pairs: segments[np.where(segments == basin_pair[0])] = basin_pair[1] # Renumber segments so they are sequential: renumber_segments = segments.copy() segment_numbers = [int(x) for x in np.unique(segments) if x != -1] for i_segment, n_segment in enumerate(segment_numbers): segment = [i for i,x in enumerate(segments) if x == n_segment] renumber_segments[segment] = i_segment segments = renumber_segments # Print statement: print(' ...Merged segments to form {0} watershed regions ({1:.2f} seconds)'. format(i_segment + 1, time() - t0)) return segments.tolist(), seed_indices
def extract_folds(depth_file, min_fold_size=50, tiny_depth=0.001, save_file=False): """ Use depth to extract folds from a triangular surface mesh. Steps :: 1. Compute histogram of depth measures. 2. Define a depth threshold and find the deepest vertices. 3. Segment deep vertices as an initial set of folds. 4. Remove small folds. 5. Find and fill holes in the folds. 6. Renumber folds. Step 2 :: To extract an initial set of deep vertices from the surface mesh, we anticipate that there will be a rapidly decreasing distribution of low depth values (on the outer surface) with a long tail of higher depth values (in the folds), so we smooth the histogram's bin values, convolve to compute slopes, and find the depth value for the first bin with slope = 0. This is our threshold. Step 5 :: The folds could have holes in areas shallower than the depth threshold. Calling fill_holes() could accidentally include very shallow areas (in an annulus-shaped fold, for example), so we call fill_holes() with the argument exclude_range set close to zero to retain these areas. Parameters ---------- depth_file : string surface mesh file in VTK format with faces and depth scalar values min_fold_size : integer minimum fold size (number of vertices) tiny_depth : float largest non-zero depth value that will stop a hole from being filled save_file : Boolean save output VTK file? Returns ------- folds : list of integers fold numbers for all vertices (-1 for non-fold vertices) n_folds : int number of folds depth_threshold : float threshold defining the minimum depth for vertices to be in a fold bins : list of integers histogram bins: each is the number of vertices within a range of depth values bin_edges : list of floats histogram bin edge values defining the bin ranges of depth values folds_file : string (if save_file) name of output VTK file with fold IDs (-1 for non-fold vertices) Examples -------- >>> import os >>> import numpy as np >>> import pylab >>> from scipy.ndimage.filters import gaussian_filter1d >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.utils.mesh import find_neighbors_from_file >>> from mindboggle.utils.plots import plot_vtk >>> from mindboggle.features.folds import extract_folds >>> path = os.environ['MINDBOGGLE_DATA'] >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> neighbor_lists = find_neighbors_from_file(depth_file) >>> min_fold_size = 50 >>> tiny_depth = 0.001 >>> save_file = True >>> # >>> folds, n_folds, thr, bins, bin_edges, folds_file = extract_folds(depth_file, >>> min_fold_size, tiny_depth, save_file) >>> # >>> # View folds: >>> plot_vtk('folds.vtk') >>> # Plot histogram and depth threshold: >>> depths, name = read_scalars(depth_file) >>> nbins = np.round(len(depths) / 100.0) >>> a,b,c = pylab.hist(depths, bins=nbins) >>> pylab.plot(thr*np.ones((100,1)), np.linspace(0, max(bins), 100), 'r.') >>> pylab.show() >>> # Plot smoothed histogram: >>> bins_smooth = gaussian_filter1d(bins.tolist(), 5) >>> pylab.plot(range(len(bins)), bins, '.', range(len(bins)), bins_smooth,'-') >>> pylab.show() """ import os import sys import numpy as np from time import time from scipy.ndimage.filters import gaussian_filter1d from mindboggle.utils.io_vtk import rewrite_scalars, read_vtk from mindboggle.utils.mesh import find_neighbors from mindboggle.utils.morph import fill_holes from mindboggle.utils.segment import segment do_fill_holes = True print("Extract folds in surface mesh") t0 = time() #------------------------------------------------------------------------- # Load depth values for all vertices #------------------------------------------------------------------------- faces, lines, indices, points, npoints, depths, name, input_vtk = read_vtk(depth_file, return_first=True, return_array=True) #------------------------------------------------------------------------- # Find neighbors for each vertex #------------------------------------------------------------------------- neighbor_lists = find_neighbors(faces, npoints) #------------------------------------------------------------------------- # Compute histogram of depth measures #------------------------------------------------------------------------- min_vertices = 10000 if npoints > min_vertices: nbins = np.round(npoints / 100.0) else: sys.err(" Expecting at least {0} vertices to create depth histogram". format(min_vertices)) bins, bin_edges = np.histogram(depths, bins=nbins) #------------------------------------------------------------------------- # Anticipating that there will be a rapidly decreasing distribution # of low depth values (on the outer surface) with a long tail of higher # depth values (in the folds), smooth the bin values (Gaussian), convolve # to compute slopes, and find the depth for the first bin with slope = 0. #------------------------------------------------------------------------- bins_smooth = gaussian_filter1d(bins.tolist(), 5) window = [-1, 0, 1] bin_slopes = np.convolve(bins_smooth, window, mode='same') / (len(window) - 1) ibins0 = np.where(bin_slopes == 0)[0] if ibins0.shape: depth_threshold = bin_edges[ibins0[0]] else: depth_threshold = np.median(depths) #------------------------------------------------------------------------- # Find the deepest vertices #------------------------------------------------------------------------- indices_deep = [i for i,x in enumerate(depths) if x >= depth_threshold] if indices_deep: #--------------------------------------------------------------------- # Segment deep vertices as an initial set of folds #--------------------------------------------------------------------- print(" Segment vertices deeper than {0:.2f} as folds".format(depth_threshold)) t1 = time() folds = segment(indices_deep, neighbor_lists) # Slightly slower alternative -- fill boundaries: #regions = -1 * np.ones(len(points)) #regions[indices_deep] = 1 #folds = segment_by_filling_borders(regions, neighbor_lists) print(' ...Segmented folds ({0:.2f} seconds)'.format(time() - t1)) #--------------------------------------------------------------------- # Remove small folds #--------------------------------------------------------------------- if min_fold_size > 1: print(' Remove folds smaller than {0}'.format(min_fold_size)) unique_folds = [x for x in np.unique(folds) if x > -1] for nfold in unique_folds: indices_fold = [i for i,x in enumerate(folds) if x == nfold] if len(indices_fold) < min_fold_size: folds[indices_fold] = -1 #--------------------------------------------------------------------- # Find and fill holes in the folds # Note: Surfaces surrounded by folds can be mistaken for holes, # so exclude_range includes outer surface values close to zero. #--------------------------------------------------------------------- if do_fill_holes: print(" Find and fill holes in the folds") folds = fill_holes(folds, neighbor_lists, values=depths, exclude_range=[0, tiny_depth]) #--------------------------------------------------------------------- # Renumber folds so they are sequential #--------------------------------------------------------------------- renumber_folds = -1 * np.ones(len(folds)) fold_numbers = [int(x) for x in np.unique(folds) if x > -1] for i_fold, n_fold in enumerate(fold_numbers): fold = [i for i,x in enumerate(folds) if x == n_fold] renumber_folds[fold] = i_fold folds = renumber_folds n_folds = i_fold + 1 # Print statement print(' ...Extracted {0} folds ({1:.2f} seconds)'. format(n_folds, time() - t0)) else: print(' No deep vertices') #------------------------------------------------------------------------- # Return folds, number of folds, file name #------------------------------------------------------------------------- if save_file: folds_file = os.path.join(os.getcwd(), 'folds.vtk') rewrite_scalars(depth_file, folds_file, folds, 'folds', folds) if not os.path.exists(folds_file): raise(IOError(folds_file + " not found")) else: folds_file = None return folds.tolist(), n_folds, depth_threshold, bins, bin_edges, folds_file
def segment_by_filling_borders(regions, neighbor_lists): """ Fill borders (contours) on a surface mesh to segment vertices into contiguous regions. Steps :: 1. Extract region borders (assumed to be closed contours) 2. Segment borders into separate, contiguous borders 3. For each boundary 4. Find the neighbors to either side of the boundary 5. Segment the neighbors into exterior and interior sets of neighbors 6. Find the interior (smaller) sets of neighbors 7. Fill the contours formed by the interior neighbors Parameters ---------- regions : numpy array of integers region numbers for all vertices (default -1) neighbor_lists : list of lists of integers each list contains indices to neighboring vertices for each vertex Returns ------- segments : numpy array of integers region numbers for all vertices (default -1) Examples -------- >>> # Segment folds by extracting their borders and filling them in separately: >>> import os >>> import numpy as np >>> from mindboggle.utils.mesh import find_neighbors >>> from mindboggle.utils.segment import segment_by_filling_borders >>> from mindboggle.utils.io_vtk import read_vtk, rewrite_scalars >>> path = os.environ['MINDBOGGLE_DATA'] >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> faces, lines, indices, points, npoints, depths, name, input_vtk = read_vtk(depth_file, >>> return_first=True, return_array=True) >>> regions = -1 * np.ones(npoints) >>> regions[depths > 0.50] = 1 >>> neighbor_lists = find_neighbors(faces, npoints) >>> # >>> folds = segment_by_filling_borders(regions, neighbor_lists) >>> # >>> # Write results to vtk file and view: >>> rewrite_scalars(depth_file, 'segment_by_filling_borders.vtk', folds, 'folds', folds) >>> from mindboggle.utils.plots import plot_vtk >>> plot_vtk('segment_by_filling_borders.vtk') """ import numpy as np from mindboggle.labels.labels import extract_borders from mindboggle.utils.segment import segment include_boundary = False # Make sure arguments are numpy arrays if not isinstance(regions, np.ndarray): regions = np.array(regions) print('Segment vertices using region borders') # Extract region borders (assumed to be closed contours) print(' Extract region borders (assumed to be closed contours)') indices_borders, foo1, foo2 = extract_borders(range(len(regions)), regions, neighbor_lists) # Extract background indices_background = list(frozenset(range(len(regions))). difference(indices_borders)) # Segment borders into separate, contiguous borders print(' Segment borders into separate, contiguous borders') borders = segment(indices_borders, neighbor_lists, 1) # For each boundary unique_borders = [x for x in np.unique(borders) if x > -1] segments = -1 * np.ones(len(regions)) for boundary_number in unique_borders: print(' Boundary {0} of {1}:'.format(int(boundary_number), len(unique_borders))) border_indices = [i for i,x in enumerate(borders) if x == boundary_number] # Find the neighbors to either side of the boundary indices_neighbors = [] [indices_neighbors.extend(neighbor_lists[i]) for i in border_indices] #indices_neighbors2 = indices_neighbors[:] #[indices_neighbors2.extend(neighbor_lists[i]) for i in indices_neighbors] indices_neighbors = list(frozenset(indices_neighbors). difference(indices_borders)) # Segment the neighbors into exterior and interior sets of neighbors print(' Segment the neighbors into exterior and interior sets of neighbors') neighbors = segment(indices_neighbors, neighbor_lists, 1) # Find the interior (smaller) sets of neighbors print(' Find the interior (smaller) sets of neighbors') seed_lists = [] unique_neighbors = [x for x in np.unique(neighbors) if x > -1] max_neighbor = 0 max_len = 0 for ineighbor, neighbor in enumerate(unique_neighbors): indices_neighbor = [i for i,x in enumerate(neighbors) if x == neighbor] seed_lists.append(indices_neighbor) if len(indices_neighbor) > max_len: max_len = len(indices_neighbor) max_neighbor = ineighbor seed_lists = [x for i,x in enumerate(seed_lists) if i != max_neighbor] seed_list = [] [seed_list.extend(x) for x in seed_lists if len(x) > 2] # Fill the contours formed by the interior neighbors print(' Fill the contour formed by the interior neighbors') vertices_to_segment = list(frozenset(indices_background). difference(indices_borders)) segment_region = segment(vertices_to_segment, neighbor_lists, 1, [seed_list]) if include_boundary: segment_region[border_indices] = 1 segments[segment_region > -1] = boundary_number return segments
def segment_rings(region, seeds, neighbor_lists, step=1): """ Iteratively segment a region of surface mesh as concentric segments. Parameters ---------- region : list of integers indices of region vertices to segment (such as a fold) seeds : list of integers indices of seed vertices neighbor_lists : list of lists of integers indices to neighboring vertices for each vertex step : integer number of segmentation steps before assessing segments Returns ------- segments : list of lists of integers indices to vertices for each concentric segment Examples -------- >>> import os >>> import numpy as np >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.mesh import find_neighbors_from_file >>> from mindboggle.labels.labels import extract_borders >>> from mindboggle.utils.segment import segment_rings >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> values_file = os.path.join(path, 'arno', 'shapes', 'depth_rescaled.vtk') >>> values, name = read_scalars(values_file, True, True) >>> vtk_file = os.path.join(path, 'arno', 'freesurfer', 'lh.pial.vtk') >>> neighbor_lists = find_neighbors_from_file(vtk_file) >>> fold_file = os.path.join(path, 'arno', 'features', 'fold11.vtk') >>> fold, name = read_scalars(fold_file) >>> indices = [i for i,x in enumerate(fold) if x != -1] >>> # Initialize seeds with the boundary of thresholded indices: >>> use_threshold = True >>> if use_threshold: >>> # Threshold at the median depth or within maximum values in boundary: >>> threshold = np.median(values[indices]) #+ np.std(values[indices]) >>> indices_high = [x for x in indices if values[x] >= threshold] >>> # Make sure threshold is within the maximum values of the boundary: >>> B = np.ones(len(values)) >>> B[indices] = 2 >>> borders, foo1, foo2 = extract_borders(range(len(B)), B, neighbor_lists) >>> borders = [x for x in borders if values[x] != -1] >>> if list(frozenset(indices_high).intersection(borders)): >>> threshold = np.max(values[borders]) + np.std(values[borders]) >>> indices_high = [x for x in indices if values[x] >= threshold] >>> # Extract threshold boundary vertices as seeds: >>> B = -1 * np.ones(len(values)) >>> B[indices_high] = 2 >>> seeds, foo1, foo2 = extract_borders(range(len(values)), B, neighbor_lists) >>> # Or initialize P with the maximum value point: >>> else: >>> seeds = [indices[np.argmax(values[indices])]] >>> indices_high = [] >>> # >>> indices = list(frozenset(indices).difference(indices_high)) >>> indices = list(frozenset(indices).difference(seeds)) >>> segments = segment_rings(indices, seeds, neighbor_lists, step=1) >>> # >>> # View: >>> S = -1 * np.ones(len(values)) >>> for i, segment in enumerate(segments): >>> S[segment] = i >>> rewrite_scalars(vtk_file, 'segment_rings.vtk', S, 'segment_rings', fold) >>> plot_vtk('segment_rings.vtk') >>> # Store: >>> #import pickle >>> #output_file = os.path.join(path, 'tests', 'segments_fold11.pkl') >>> #pickle.dump(segments, open(output_file, "wb" )) """ from mindboggle.utils.segment import segment segments = [] while seeds: # Segment step-wise starting from seeds and through the region: seeds_plus_new = segment(region, neighbor_lists, min_region_size=1, seed_lists=[seeds], keep_seeding=False, spread_within_labels=False, labels=[], label_lists=[], values=[], max_steps=step) seeds_plus_new = [i for i,x in enumerate(seeds_plus_new) if x != -1] # Store the new segment after removing the previous segment: region = list(frozenset(region).difference(seeds)) seeds = list(frozenset(seeds_plus_new).difference(seeds)) if seeds: # Add the new segment and remove it from the region: segments.append(seeds) region = list(frozenset(region).difference(seeds)) return segments
def extract_sulci(labels_file, folds_or_file, hemi, sulcus_label_pair_lists, unique_sulcus_label_pairs, min_boundary=1, sulcus_names=[]): """ Identify sulci from folds in a brain surface according to a labeling protocol that includes a list of label pairs defining each sulcus. A fold is a group of connected, deep vertices. Steps for each fold :: 1. Remove fold if it has fewer than two labels. 2. Remove fold if its labels do not contain a sulcus label pair. 3. Find vertices with labels that are in only one of the fold's label boundary pairs. Assign the vertices the sulcus with the label pair if they are connected to the label boundary for that pair. 4. If there are remaining vertices, segment into sets of vertices connected to label boundaries, and assign a unique ID to each segment. Parameters ---------- labels_file : string file name for surface mesh VTK containing labels for all vertices folds_or_file : list or string fold number for each vertex or name of VTK file containing folds scalars hemi : string hemisphere ('lh' or 'rh') sulcus_label_pair_lists : list of two lists of multiple lists of integer pairs list containing left and right lists, each with multiple lists of integer pairs corresponding to label boundaries / sulcus / fundus unique_sulcus_label_pairs : list of unique pairs of integers unique label pairs min_boundary : integer minimum number of vertices for a sulcus label boundary segment sulcus_names : list of strings names of sulci Returns ------- sulci : list of integers sulcus numbers for all vertices (-1 for non-sulcus vertices) n_sulci : integers number of sulci sulci_file : string name of output VTK file with sulcus numbers (-1 for non-sulcus vertices) Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.labels.protocol import dkt_protocol >>> from mindboggle.features.sulci import extract_sulci >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> # Load labels, folds, neighbor lists, and sulcus names and label pairs >>> labels_file = os.path.join(path, 'arno', 'labels', 'relabeled_lh.DKTatlas40.gcs.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds_or_file, name = read_scalars(folds_file) >>> protocol = 'DKT31' >>> hemi = 'lh' >>> sulcus_names, sulcus_label_pair_lists, unique_sulcus_label_pairs, ... label_names, label_numbers, cortex_names, cortex_numbers, ... noncortex_names, noncortex_numbers = dkt_protocol(protocol) >>> min_boundary = 10 >>> # >>> sulci, n_sulci, sulci_file = extract_sulci(labels_file, folds_or_file, >>> hemi, sulcus_label_pair_lists, unique_sulcus_label_pairs, >>> min_boundary, sulcus_names) >>> # View: >>> plot_vtk('sulci.vtk') """ import os from time import time import numpy as np from mindboggle.utils.io_vtk import read_scalars, read_vtk, rewrite_scalars from mindboggle.utils.mesh import find_neighbors from mindboggle.labels.labels import extract_borders from mindboggle.utils.segment import propagate, segment # Load fold numbers if folds_or_file is a string if isinstance(folds_or_file, str): folds, name = read_scalars(folds_or_file) elif isinstance(folds_or_file, list): folds = folds_or_file if hemi == 'lh': sulcus_label_pair_lists = sulcus_label_pair_lists[0] elif hemi == 'rh': sulcus_label_pair_lists = sulcus_label_pair_lists[1] else: print("Warning: hemisphere not properly specified ('lh' or 'rh').") # Load points, faces, and neighbors faces, foo1, foo2, points, npoints, labels, foo3, foo4 = read_vtk(labels_file) neighbor_lists = find_neighbors(faces, npoints) # Array of sulcus IDs for fold vertices, initialized as -1. # Since we do not touch gyral vertices and vertices whose labels # are not in the label list, or vertices having only one label, # their sulcus IDs will remain -1. sulci = -1 * np.ones(npoints) #------------------------------------------------------------------------- # Loop through folds #------------------------------------------------------------------------- fold_numbers = [int(x) for x in np.unique(folds) if x > -1] n_folds = len(fold_numbers) print("Extract sulci from {0} folds...".format(n_folds)) t0 = time() for n_fold in fold_numbers: fold = [i for i,x in enumerate(folds) if x == n_fold] len_fold = len(fold) # List the labels in this fold (greater than zero) fold_labels = [labels[x] for x in fold] unique_fold_labels = [int(x) for x in np.unique(fold_labels) if x > 0] #--------------------------------------------------------------------- # NO MATCH -- fold has fewer than two labels #--------------------------------------------------------------------- if len(unique_fold_labels) < 2: # Ignore: sulci already initialized with -1 values if not unique_fold_labels: print(" Fold {0} ({1} vertices): NO MATCH -- fold has no labels". format(n_fold, len_fold)) else: print(" Fold {0} ({1} vertices): " "NO MATCH -- fold has only one label ({2})". format(n_fold, len_fold, unique_fold_labels[0])) # Ignore: sulci already initialized with -1 values else: # Find all label boundary pairs within the fold indices_fold_pairs, fold_pairs, unique_fold_pairs = extract_borders( fold, labels, neighbor_lists, ignore_values=[], return_label_pairs=True) # Find fold label pairs in the protocol (pairs are already sorted) fold_pairs_in_protocol = [x for x in unique_fold_pairs if x in unique_sulcus_label_pairs] if unique_fold_labels: print(" Fold {0} labels: {1} ({2} vertices)".format(n_fold, ', '.join([str(x) for x in unique_fold_labels]), len_fold)) #----------------------------------------------------------------- # NO MATCH -- fold has no sulcus label pair #----------------------------------------------------------------- if not fold_pairs_in_protocol: print(" Fold {0}: NO MATCH -- fold has no sulcus label pair". format(n_fold, len_fold)) #----------------------------------------------------------------- # Possible matches #----------------------------------------------------------------- else: print(" Fold {0} label pairs in protocol: {1}".format(n_fold, ', '.join([str(x) for x in fold_pairs_in_protocol]))) # Labels in the protocol (includes repeats across label pairs) labels_in_pairs = [x for lst in fold_pairs_in_protocol for x in lst] # Labels that appear in one or more than one sulcus label boundary unique_labels = [] nonunique_labels = [] for label in np.unique(labels_in_pairs): if len([x for x in labels_in_pairs if x == label]) == 1: unique_labels.append(label) else: nonunique_labels.append(label) #------------------------------------------------------------- # Vertices whose labels are in only one sulcus label pair #------------------------------------------------------------- # Find vertices with a label that is in only one of the fold's # label pairs (the other label in the pair can exist # in other pairs). Assign the vertices the sulcus with the label # pair if they are connected to the label boundary for that pair. #------------------------------------------------------------- if len(unique_labels): for pair in fold_pairs_in_protocol: # If one or both labels in label pair is/are unique unique_labels_in_pair = [x for x in pair if x in unique_labels] n_unique = len(unique_labels_in_pair) if n_unique: ID = [i for i,x in enumerate(sulcus_label_pair_lists) if pair in x][0] # Construct seeds from label boundary vertices # (fold_pairs and pair already sorted) indices_pair = [x for i,x in enumerate(indices_fold_pairs) if fold_pairs[i] == pair] # Identify vertices with unique label(s) in pair indices_unique_labels = [fold[i] for i,x in enumerate(fold_labels) if x in unique_sulcus_label_pairs] # Propagate from seeds to labels in label pair sulci2 = segment(indices_unique_labels, neighbor_lists, min_region_size=1, seed_lists=[indices_pair], keep_seeding=False, spread_within_labels=True, labels=labels) sulci[sulci2 > -1] = ID # Print statement if n_unique == 1: ps1 = '1 label' else: ps1 = 'Both labels' if len(sulcus_names): ps2 = sulcus_names[ID] else: ps2 = '' print(" {0} unique to one fold pair: {1} {2}". format(ps1, ps2, unique_labels_in_pair)) #------------------------------------------------------------- # Vertex labels shared by multiple label pairs #------------------------------------------------------------- # Propagate labels from label borders to vertices with labels # that are shared by multiple label pairs in the fold. #------------------------------------------------------------- if len(nonunique_labels): # For each label shared by different label pairs for label in nonunique_labels: # Print statement print(" Propagate sulcus label borders with label {0}". format(int(label))) # Construct seeds from label boundary vertices seeds = -1 * np.ones(len(points)) for ID, label_pair_list in enumerate(sulcus_label_pair_lists): label_pairs = [x for x in label_pair_list if label in x] for label_pair in label_pairs: indices_pair = [x for i,x in enumerate(indices_fold_pairs) if np.sort(fold_pairs[i]).tolist() == label_pair] if indices_pair: # Do not include short boundary segments if min_boundary > 1: indices_pair2 = [] seeds2 = segment(indices_pair, neighbor_lists) for seed2 in range(int(max(seeds2))+1): iseed2 = [i for i,x in enumerate(seeds2) if x == seed2] if len(iseed2) >= min_boundary: indices_pair2.extend(iseed2) else: if len(iseed2) == 1: print(" Remove assignment " "of ID {0} from 1 vertex". format(seed2)) else: print(" Remove assignment " "of ID {0} from {1} vertices". format(seed2, len(iseed2))) indices_pair = indices_pair2 # Assign sulcus IDs to seeds seeds[indices_pair] = ID # Identify vertices with the label label_array = -1 * np.ones(len(points)) indices_label = [fold[i] for i,x in enumerate(fold_labels) if x == label] if len(indices_label): label_array[indices_label] = 1 # Propagate from seeds to vertices with label #indices_seeds = [] #for seed in range(int(max(seeds))+1): # indices_seeds.append([i for i,x in enumerate(seeds) # if x == seed]) #sulci2 = segment(indices_label, neighbor_lists, # 50, indices_seeds, False, True, labels) sulci2 = propagate(points, faces, label_array, seeds, sulci, max_iters=10000, tol=0.001, sigma=5) sulci[sulci2 > -1] = sulci2[sulci2 > -1] #------------------------------------------------------------------------- # Print out assigned sulci #------------------------------------------------------------------------- sulcus_numbers = [int(x) for x in np.unique(sulci) if x > -1] n_sulci = len(sulcus_numbers) print("Extracted {0} sulci from {1} folds ({2:.1f}s):". format(n_sulci, n_folds, time()-t0)) if len(sulcus_names): for sulcus_number in sulcus_numbers: print(" {0}: {1}".format(sulcus_number, sulcus_names[sulcus_number])) else: print(" " + ", ".join([str(x) for x in sulcus_numbers])) #------------------------------------------------------------------------- # Print out unresolved sulci #------------------------------------------------------------------------- unresolved = [i for i in range(len(sulcus_label_pair_lists)) if i not in sulcus_numbers] if len(unresolved) == 1: print("The following sulcus is unaccounted for:") else: print("The following {0} sulci are unaccounted for:".format(len(unresolved))) if len(sulcus_names): for sulcus_number in unresolved: print(" {0}: {1}".format(sulcus_number, sulcus_names[sulcus_number])) else: print(" " + ", ".join([str(x) for x in unresolved])) #------------------------------------------------------------------------- # Return sulci, number of sulci, and file name #------------------------------------------------------------------------- sulci_file = os.path.join(os.getcwd(), 'sulci.vtk') rewrite_scalars(labels_file, sulci_file, sulci, 'sulci', sulci) sulci.tolist() return sulci, n_sulci, sulci_file
def select_largest(points, faces, exclude_labels=[-1], areas=None, reindex=True): """ Select the largest segment (connected set of indices) in a mesh. In case a surface patch is fragmented, we select the largest fragment, remove extraneous triangular faces, and reindex indices. Parameters ---------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh exclude_labels : list of integers background values to exclude areas : numpy array or list of floats (or None) surface area scalar values for all vertices reindex : Boolean reindex indices in faces? Returns ------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh Examples -------- >>> # Spectrum for one label (artificial composite), two fragments: >>> import os >>> import numpy as np >>> from mindboggle.utils.io_vtk import read_scalars, read_vtk, write_vtk >>> from mindboggle.utils.mesh import remove_faces >>> from mindboggle.utils.segment import select_largest >>> path = os.environ['MINDBOGGLE_DATA'] >>> label_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT31.manual.vtk') >>> area_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.area.vtk') >>> exclude_labels = [-1] >>> faces, lines, indices, points, u1, labels, u2,u3 = read_vtk(label_file, >>> return_first=True, return_array=True) >>> I19 = [i for i,x in enumerate(labels) if x==19] # pars orbitalis >>> I22 = [i for i,x in enumerate(labels) if x==22] # postcentral >>> I19.extend(I22) >>> faces = remove_faces(faces, I19) >>> areas, u1 = read_scalars(area_file, True, True) >>> reindex = True >>> # >>> points, faces = select_largest(points, faces, exclude_labels, areas, >>> reindex) >>> # View: >>> from mindboggle.utils.plots import plot_vtk >>> scalars = np.zeros(np.shape(labels)) >>> scalars[I19] = 1 >>> vtk_file = 'test_two_labels.vtk' >>> write_vtk(vtk_file, points, indices, lines, faces, >>> scalars, scalar_names='scalars') >>> plot_vtk(vtk_file) """ import numpy as np from mindboggle.utils.mesh import find_neighbors, remove_faces, \ reindex_faces_points from mindboggle.utils.segment import segment # Areas: use_area = False if isinstance(areas, np.ndarray) and np.shape(areas): use_area = True elif isinstance(areas, list) and len(areas): areas = np.array(areas) use_area = True # Check to see if there are enough points: min_npoints = 4 npoints = len(points) if npoints < min_npoints or len(faces) < min_npoints: print("The input size {0} ({1} faces) should be much larger " "than {2}". format(npoints, len(faces), min_npoints)) return None else: #--------------------------------------------------------------------- # Segment the indices into connected sets of indices: #--------------------------------------------------------------------- # Construct neighbor lists: neighbor_lists = find_neighbors(faces, npoints) # Determine the indices: indices = [x for sublst in faces for x in sublst] # Segment: segments = segment(indices, neighbor_lists, min_region_size=1, seed_lists=[], keep_seeding=False, spread_within_labels=False, labels=[], label_lists=[], values=[], max_steps='', verbose=False) #--------------------------------------------------------------------- # Select the largest segment (connected set of indices): #--------------------------------------------------------------------- unique_segments = [x for x in np.unique(segments) if x not in exclude_labels] if len(unique_segments) > 1: select_indices = [] max_segment_area = 0 for segment_number in unique_segments: segment_indices = [i for i,x in enumerate(segments) if x == segment_number] if use_area: segment_area = np.sum(areas[segment_indices]) else: segment_area = len(segment_indices) if segment_area > max_segment_area: select_indices = segment_indices max_segment_area = len(select_indices) print('Maximum size of {0} segments: {1} vertices'. format(len(unique_segments), len(select_indices))) #----------------------------------------------------------------- # Extract points and renumber faces for the selected indices: #----------------------------------------------------------------- faces = remove_faces(faces, select_indices) else: select_indices = indices # Alert if the number of indices is small: if len(select_indices) < min_npoints: print("The input size {0} is too small.".format(len(select_indices))) return None elif faces: #----------------------------------------------------------------- # Reindex indices in faces: #----------------------------------------------------------------- if reindex: faces, points = reindex_faces_points(faces, points) return points, faces else: points = np.array(points) points = points[select_indices].tolist() return points, faces else: return None
def fill_holes(regions, neighbor_lists, values=[], exclude_range=[], background_value=-1): """ Fill holes in regions on a surface mesh by using region boundaries. NOTE: assumes one set of connected vertices per region Steps :: 1. Segment region vertex neighbors into connected vertices (region boundaries). 2. Remove the largest region boundary, presumably the outer contour of the region, leaving smaller boundaries, presumably the contours of holes within the region. 3. Call label_holes() to fill holes with surrounding region numbers. Parameters ---------- regions : numpy array of integers region numbers for all vertices neighbor_lists : list of lists of integers each list contains indices to neighboring vertices for each vertex values : list of integers values for vertices, for use in determining which holes to remove exclude_range : list of two floats hole is not filled if it contains values within this range (prevents cases where surface connected by folds mistaken for holes) background_value : integer background value Returns ------- regions : numpy array of integers region numbers for all vertices Examples -------- >>> import os >>> import numpy as np >>> from mindboggle.utils.mesh import find_neighbors, remove_faces >>> from mindboggle.utils.morph import fill_holes >>> from mindboggle.utils.io_vtk import read_scalars, read_vtk, write_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> # >>> background_value = -1 >>> # Select one fold >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, return_first=True, return_array=True) >>> vtk_file = os.path.join(path, 'arno', 'freesurfer', 'lh.pial.vtk') >>> faces, lines, indices, points, npoints, scalars, name, input_vtk = read_vtk(vtk_file, >>> return_first=True, return_array=True) >>> neighbor_lists = find_neighbors(faces, npoints) >>> n_fold = np.unique(folds)[1] >>> folds[folds != n_fold] = background_value >>> # >>> # Make two holes in fold (background value and excluded values) >>> # Hole 1: >>> # Find a vertex whose removal (with its neighbors) would create a hole >>> I = np.where(folds==n_fold)[0] >>> for index1 in I: >>> N1 = neighbor_lists[index1] >>> stop = True >>> for n in N1: >>> if any(folds[neighbor_lists[n]] == background_value): >>> stop = False >>> break >>> else: >>> for f in neighbor_lists[n]: >>> if any(folds[neighbor_lists[f]] == background_value): >>> stop = False >>> break >>> if stop: >>> break >>> folds[index1] = background_value >>> folds[N1] = background_value >>> # Hole 2: >>> I = np.where(folds==n_fold)[0] >>> for index2 in I: >>> N2 = neighbor_lists[index2] >>> stop = True >>> for n in N2: >>> if any(folds[neighbor_lists[n]] == background_value): >>> stop = False >>> break >>> else: >>> for f in neighbor_lists[n]: >>> if any(folds[neighbor_lists[f]] == background_value): >>> stop = False >>> break >>> if stop: >>> break >>> folds[index2] = background_value >>> folds[N2] = background_value >>> values = np.zeros(len(folds)) >>> values[index2] = 100 >>> values[N2] = 200 >>> # >>> # Write holes to vtk file and view: >>> holes = folds.copy() >>> holes[index1] = 10 >>> holes[N1] = 20 >>> holes[index2] = 30 >>> holes[N2] = 40 >>> indices = [i for i,x in enumerate(holes) if x != background_value] >>> write_vtk('holes.vtk', points, indices, lines, >>> remove_faces(faces, indices), [holes.tolist()], ['holes'], 'int') >>> from mindboggle.utils.plots import plot_surfaces >>> plot_surfaces('holes.vtk') >>> # >>> # Fill Hole 1 but not Hole 2: >>> # (because values has an excluded value in the hole) >>> regions = np.copy(folds) >>> exclude_range = [99,101], >>> regions = fill_holes(regions, neighbor_lists, values, exclude_range, background_value) >>> # >>> # Write results to vtk file and view: >>> indices = [i for i,x in enumerate(regions) if x != background_value] >>> write_vtk('fill_holes.vtk', points, indices, lines, >>> remove_faces(faces, indices), regions.tolist(), 'regions', 'int') >>> from mindboggle.utils.plots import plot_surfaces >>> plot_surfaces('fill_holes.vtk') """ import numpy as np from mindboggle.utils.segment import segment # Make sure argument is a numpy array if not isinstance(regions, np.ndarray): regions = np.array(regions) def label_holes(holes, regions, neighbor_lists): """ Fill holes in regions on a surface mesh. Parameters ---------- holes : list or array of integers hole numbers for all vertices regions : numpy array of integers region numbers for all vertices neighbor_lists : list of lists of integers each list contains indices to neighboring vertices for each vertex Returns ------- regions : numpy array of integers region numbers for all vertices """ import numpy as np # Make sure argument is a numpy array if not isinstance(regions, np.ndarray): regions = np.array(regions) # Identify the vertices for each hole hole_numbers = [x for x in np.unique(holes) if x != background_value] for n_hole in hole_numbers: I = [i for i,x in enumerate(holes) if x == n_hole] # Identify neighbors to these vertices N=[]; [N.extend(neighbor_lists[i]) for i in I] if N: # Assign the hole the maximum region ID number of its neighbors regions[I] = max([regions[x] for x in N]) return regions #------------------------------------------------------------------------- # Find boundaries to holes #------------------------------------------------------------------------- hole_boundaries = background_value * np.ones(len(regions)) # Identify vertices for each region region_numbers = [x for x in np.unique(regions) if x != background_value] count = 0 for n_region in region_numbers: region_indices = np.where(regions == n_region)[0] # Identify neighbors to these vertices and their neighbors N = [] [N.extend(neighbor_lists[x]) for x in region_indices] N = list(frozenset(N).difference(region_indices)) N2 = [] [N2.extend(neighbor_lists[x]) for x in N] N.extend(N2) N = list(frozenset(N).difference(region_indices)) if N: # Segment the neighbors into connected vertices (region boundaries) boundaries = segment(N, neighbor_lists) # Remove the largest region boundary, presumably the # outer contour of the region, leaving smaller boundaries, # presumably the contours of holes within the region boundary_numbers = [x for x in np.unique(boundaries) if x != background_value] max_size = 0 max_number = 0 for n_boundary in boundary_numbers: border_indices = np.where(boundaries == n_boundary)[0] if len(border_indices) > max_size: max_size = len(border_indices) max_number = n_boundary boundaries[boundaries == max_number] = background_value boundary_numbers = [x for x in boundary_numbers if x != max_number] # Add remaining boundaries to holes array for n_boundary in boundary_numbers: indices = [i for i,x in enumerate(boundaries) if x == n_boundary] hole_boundaries[indices] = count count += 1 #------------------------------------------------------------------------- # Fill holes #------------------------------------------------------------------------- # If there are any holes if count > 0: hole_numbers = [x for x in np.unique(hole_boundaries) if x != background_value] background = [i for i,x in enumerate(regions) if x == background_value] # Grow seeds from hole boundaries to fill holes for n_hole in hole_numbers: seed_list = np.where(hole_boundaries == n_hole)[0].tolist() seed_lists = [list(frozenset(background).intersection(seed_list))] hole = segment(background, neighbor_lists, 1, seed_lists) # Label the vertices for each hole by surrounding region number # if hole does not include values within exclude_range: if len(exclude_range) == 2: Ihole = np.where(hole != background_value)[0] #if not len(frozenset(values[Ihole]).intersection(exclude_range)): if not [x for x in values[Ihole] if x > exclude_range[0] if x < exclude_range[1]]: regions = label_holes(hole, regions, neighbor_lists) else: regions = label_holes(hole, regions, neighbor_lists) return regions
def extract_sulci(labels_file, folds_or_file, hemi, min_boundary=1, sulcus_names=[]): """ Identify sulci from folds in a brain surface according to a labeling protocol that includes a list of label pairs defining each sulcus. A fold is a group of connected, deep vertices. Steps for each fold :: 1. Remove fold if it has fewer than two labels. 2. Remove fold if its labels do not contain a sulcus label pair. 3. Find vertices with labels that are in only one of the fold's label boundary pairs. Assign the vertices the sulcus with the label pair if they are connected to the label boundary for that pair. 4. If there are remaining vertices, segment into sets of vertices connected to label boundaries, and assign a unique ID to each set. Parameters ---------- labels_file : string file name for surface mesh VTK containing labels for all vertices folds_or_file : list or string fold number for each vertex / name of VTK file containing fold scalars hemi : string hemisphere abbreviation in {'lh', 'rh'} for sulcus labels min_boundary : integer minimum number of vertices for a sulcus label boundary segment sulcus_names : list of strings names of sulci Returns ------- sulci : list of integers sulcus numbers for all vertices (-1 for non-sulcus vertices) n_sulci : integers number of sulci sulci_file : string output VTK file with sulcus numbers (-1 for non-sulcus vertices) Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.features.sulci import extract_sulci >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> # Load labels, folds, neighbor lists, and sulcus names and label pairs >>> labels_file = os.path.join(path, 'arno', 'labels', 'relabeled_lh.DKTatlas40.gcs.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds_or_file, name = read_scalars(folds_file) >>> hemi = 'lh' >>> min_boundary = 10 >>> sulcus_names = [] >>> # >>> sulci, n_sulci, sulci_file = extract_sulci(labels_file, folds_or_file, hemi, min_boundary, sulcus_names) >>> # View: >>> plot_surfaces('sulci.vtk') """ import os from time import time import numpy as np from mindboggle.utils.io_vtk import read_scalars, read_vtk, rewrite_scalars from mindboggle.utils.mesh import find_neighbors from mindboggle.utils.segment import extract_borders, propagate, segment from mindboggle.LABELS import DKTprotocol # Load fold numbers if folds_or_file is a string: if isinstance(folds_or_file, str): folds, name = read_scalars(folds_or_file) elif isinstance(folds_or_file, list): folds = folds_or_file dkt = DKTprotocol() if hemi == 'lh': pair_lists = dkt.left_sulcus_label_pair_lists elif hemi == 'rh': pair_lists = dkt.right_sulcus_label_pair_lists else: print("Warning: hemisphere not properly specified ('lh' or 'rh').") # Load points, faces, and neighbors: faces, o1, o2, points, npoints, labels, o3, o4 = read_vtk(labels_file) neighbor_lists = find_neighbors(faces, npoints) # Array of sulcus IDs for fold vertices, initialized as -1. # Since we do not touch gyral vertices and vertices whose labels # are not in the label list, or vertices having only one label, # their sulcus IDs will remain -1: sulci = -1 * np.ones(npoints) #------------------------------------------------------------------------- # Loop through folds #------------------------------------------------------------------------- fold_numbers = [int(x) for x in np.unique(folds) if x != -1] n_folds = len(fold_numbers) print("Extract sulci from {0} folds...".format(n_folds)) t0 = time() for n_fold in fold_numbers: fold = [i for i, x in enumerate(folds) if x == n_fold] len_fold = len(fold) # List the labels in this fold: fold_labels = [labels[x] for x in fold] unique_fold_labels = [ int(x) for x in np.unique(fold_labels) if x != -1 ] #--------------------------------------------------------------------- # NO MATCH -- fold has fewer than two labels #--------------------------------------------------------------------- if len(unique_fold_labels) < 2: # Ignore: sulci already initialized with -1 values: if not unique_fold_labels: print(" Fold {0} ({1} vertices): " "NO MATCH -- fold has no labels".format( n_fold, len_fold)) else: print(" Fold {0} ({1} vertices): " "NO MATCH -- fold has only one label ({2})".format( n_fold, len_fold, unique_fold_labels[0])) # Ignore: sulci already initialized with -1 values else: # Find all label boundary pairs within the fold: indices_fold_pairs, fold_pairs, unique_fold_pairs = \ extract_borders(fold, labels, neighbor_lists, ignore_values=[], return_label_pairs=True) # Find fold label pairs in the protocol (pairs are already sorted): fold_pairs_in_protocol = [ x for x in unique_fold_pairs if x in dkt.unique_sulcus_label_pairs ] if unique_fold_labels: print(" Fold {0} labels: {1} ({2} vertices)".format( n_fold, ', '.join([str(x) for x in unique_fold_labels]), len_fold)) #----------------------------------------------------------------- # NO MATCH -- fold has no sulcus label pair #----------------------------------------------------------------- if not fold_pairs_in_protocol: print(" Fold {0}: NO MATCH -- fold has no sulcus label pair". format(n_fold, len_fold)) #----------------------------------------------------------------- # Possible matches #----------------------------------------------------------------- else: print(" Fold {0} label pairs in protocol: {1}".format( n_fold, ', '.join([str(x) for x in fold_pairs_in_protocol]))) # Labels in the protocol (includes repeats across label pairs): labels_in_pairs = [ x for lst in fold_pairs_in_protocol for x in lst ] # Labels that appear in one or more sulcus label boundary: unique_labels = [] nonunique_labels = [] for label in np.unique(labels_in_pairs): if len([x for x in labels_in_pairs if x == label]) == 1: unique_labels.append(label) else: nonunique_labels.append(label) #------------------------------------------------------------- # Vertices whose labels are in only one sulcus label pair #------------------------------------------------------------- # Find vertices with a label that is in only one of the fold's # label pairs (the other label in the pair can exist in other # pairs). Assign the vertices the sulcus with the label pair # if they are connected to the label boundary for that pair. #------------------------------------------------------------- if unique_labels: for pair in fold_pairs_in_protocol: # If one or both labels in label pair is/are unique: unique_labels_in_pair = [ x for x in pair if x in unique_labels ] n_unique = len(unique_labels_in_pair) if n_unique: ID = None for i, pair_list in enumerate(pair_lists): if not isinstance(pair_list, list): pair_list = [pair_list] if pair in pair_list: ID = i break if ID: # Seeds from label boundary vertices # (fold_pairs and pair already sorted): indices_pair = [ x for i, x in enumerate(indices_fold_pairs) if fold_pairs[i] == pair ] # Vertices with unique label(s) in pair: indices_unique_labels = [ fold[i] for i, x in enumerate(fold_labels) if x in dkt.unique_sulcus_label_pairs ] # Propagate from seeds to labels in label pair: sulci2 = segment(indices_unique_labels, neighbor_lists, min_region_size=1, seed_lists=[indices_pair], keep_seeding=False, spread_within_labels=True, labels=labels) sulci[sulci2 != -1] = ID # Print statement: if n_unique == 1: ps1 = '1 label' else: ps1 = 'Both labels' if len(sulcus_names): ps2 = sulcus_names[ID] else: ps2 = '' print(" {0} unique to one fold pair: " "{1} {2}".format(ps1, ps2, unique_labels_in_pair)) #------------------------------------------------------------- # Vertex labels shared by multiple label pairs #------------------------------------------------------------- # Propagate labels from label borders to vertices with labels # that are shared by multiple label pairs in the fold. #------------------------------------------------------------- if len(nonunique_labels): # For each label shared by different label pairs: for label in nonunique_labels: # Print statement: print(" Propagate sulcus borders with label {0}". format(int(label))) # Construct seeds from label boundary vertices: seeds = -1 * np.ones(len(points)) for ID, pair_list in enumerate(pair_lists): if not isinstance(pair_list, list): pair_list = [pair_list] label_pairs = [x for x in pair_list if label in x] for label_pair in label_pairs: indices_pair = [ x for i, x in enumerate(indices_fold_pairs) if np.sort(fold_pairs[i]).tolist() == label_pair ] if indices_pair: # Do not include short boundary segments: if min_boundary > 1: indices_pair2 = [] seeds2 = segment( indices_pair, neighbor_lists) useeds2 = [ x for x in np.unique(seeds2) if x != -1 ] for seed2 in useeds2: iseed2 = [ i for i, x in enumerate(seeds2) if x == seed2 ] if len(iseed2) >= min_boundary: indices_pair2.extend(iseed2) else: if len(iseed2) == 1: print(" Remove " "assignment " "of ID {0} from " "1 vertex".format( seed2)) else: print( " Remove " "assignment " "of ID {0} from " "{1} vertices".format( seed2, len(iseed2))) indices_pair = indices_pair2 # Assign sulcus IDs to seeds: seeds[indices_pair] = ID # Identify vertices with the label: label_array = -1 * np.ones(len(points)) indices_label = [ fold[i] for i, x in enumerate(fold_labels) if x == label ] if len(indices_label): label_array[indices_label] = 1 # Propagate from seeds to vertices with label: #indices_seeds = [] #for seed in range(int(max(seeds))+1): # indices_seeds.append([i for i,x # in enumerate(seeds) # if x == seed]) #sulci2 = segment(indices_label, neighbor_lists, # 50, indices_seeds, False, True, # labels) sulci2 = propagate(points, faces, label_array, seeds, sulci, max_iters=10000, tol=0.001, sigma=5) sulci[sulci2 != -1] = sulci2[sulci2 != -1] #------------------------------------------------------------------------- # Print out assigned sulci #------------------------------------------------------------------------- sulcus_numbers = [int(x) for x in np.unique(sulci) if x != -1] # if not np.isnan(x)] n_sulci = len(sulcus_numbers) print("Extracted {0} sulci from {1} folds ({2:.1f}s):".format( n_sulci, n_folds, time() - t0)) if sulcus_names: for sulcus_number in sulcus_numbers: print(" {0}: {1}".format(sulcus_number, sulcus_names[sulcus_number])) elif sulcus_numbers: print(" " + ", ".join([str(x) for x in sulcus_numbers])) #------------------------------------------------------------------------- # Print out unresolved sulci #------------------------------------------------------------------------- unresolved = [i for i in range(len(pair_lists)) if i not in sulcus_numbers] if len(unresolved) == 1: print("The following sulcus is unaccounted for:") else: print("The following {0} sulci are unaccounted for:".format( len(unresolved))) if sulcus_names: for sulcus_number in unresolved: print(" {0}: {1}".format(sulcus_number, sulcus_names[sulcus_number])) else: print(" " + ", ".join([str(x) for x in unresolved])) #------------------------------------------------------------------------- # Return sulci, number of sulci, and file name #------------------------------------------------------------------------- sulci = [int(x) for x in sulci] sulci_file = os.path.join(os.getcwd(), 'sulci.vtk') rewrite_scalars(labels_file, sulci_file, sulci, 'sulci', sulci) if not os.path.exists(sulci_file): raise (IOError(sulci_file + " not found")) return sulci, n_sulci, sulci_file
def fill_holes(regions, neighbor_lists, values=[], exclude_range=[], background_value=-1): """ Fill holes in regions on a surface mesh by using region boundaries. NOTE: assumes one set of connected vertices per region Steps :: 1. Segment region vertex neighbors into connected vertices (region boundaries). 2. Remove the largest region boundary, presumably the outer contour of the region, leaving smaller boundaries, presumably the contours of holes within the region. 3. Call label_holes() to fill holes with surrounding region numbers. Parameters ---------- regions : numpy array of integers region numbers for all vertices neighbor_lists : list of lists of integers each list contains indices to neighboring vertices for each vertex values : list of integers values for vertices, for use in determining which holes to remove exclude_range : list of two floats hole is not filled if it contains values within this range (prevents cases where surface connected by folds mistaken for holes) background_value : integer background value Returns ------- regions : numpy array of integers region numbers for all vertices Examples -------- >>> import os >>> import numpy as np >>> from mindboggle.utils.mesh import find_neighbors, remove_faces >>> from mindboggle.utils.morph import fill_holes >>> from mindboggle.utils.io_vtk import read_scalars, read_vtk, write_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> # >>> background_value = -1 >>> # Select one fold >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, return_first=True, return_array=True) >>> vtk_file = os.path.join(path, 'arno', 'freesurfer', 'lh.pial.vtk') >>> faces, lines, indices, points, npoints, scalars, name, input_vtk = read_vtk(vtk_file, >>> return_first=True, return_array=True) >>> neighbor_lists = find_neighbors(faces, npoints) >>> n_fold = np.unique(folds)[1] >>> folds[folds != n_fold] = background_value >>> # >>> # Make two holes in fold (background value and excluded values) >>> # Hole 1: >>> # Find a vertex whose removal (with its neighbors) would create a hole >>> I = np.where(folds==n_fold)[0] >>> for index1 in I: >>> N1 = neighbor_lists[index1] >>> stop = True >>> for n in N1: >>> if any(folds[neighbor_lists[n]] == background_value): >>> stop = False >>> break >>> else: >>> for f in neighbor_lists[n]: >>> if any(folds[neighbor_lists[f]] == background_value): >>> stop = False >>> break >>> if stop: >>> break >>> folds[index1] = background_value >>> folds[N1] = background_value >>> # Hole 2: >>> I = np.where(folds==n_fold)[0] >>> for index2 in I: >>> N2 = neighbor_lists[index2] >>> stop = True >>> for n in N2: >>> if any(folds[neighbor_lists[n]] == background_value): >>> stop = False >>> break >>> else: >>> for f in neighbor_lists[n]: >>> if any(folds[neighbor_lists[f]] == background_value): >>> stop = False >>> break >>> if stop: >>> break >>> folds[index2] = background_value >>> folds[N2] = background_value >>> values = np.zeros(len(folds)) >>> values[index2] = 100 >>> values[N2] = 200 >>> # >>> # Write holes to vtk file and view: >>> holes = folds.copy() >>> holes[index1] = 10 >>> holes[N1] = 20 >>> holes[index2] = 30 >>> holes[N2] = 40 >>> indices = [i for i,x in enumerate(holes) if x != background_value] >>> write_vtk('holes.vtk', points, indices, lines, >>> remove_faces(faces, indices), [holes.tolist()], ['holes'], 'int') >>> from mindboggle.utils.plots import plot_surfaces >>> plot_surfaces('holes.vtk') >>> # >>> # Fill Hole 1 but not Hole 2: >>> # (because values has an excluded value in the hole) >>> regions = np.copy(folds) >>> exclude_range = [99,101], >>> regions = fill_holes(regions, neighbor_lists, values, exclude_range, background_value) >>> # >>> # Write results to vtk file and view: >>> indices = [i for i,x in enumerate(regions) if x != background_value] >>> write_vtk('fill_holes.vtk', points, indices, lines, >>> remove_faces(faces, indices), regions.tolist(), 'regions', 'int') >>> from mindboggle.utils.plots import plot_surfaces >>> plot_surfaces('fill_holes.vtk') """ import numpy as np from mindboggle.utils.segment import segment # Make sure argument is a numpy array if not isinstance(regions, np.ndarray): regions = np.array(regions) def label_holes(holes, regions, neighbor_lists): """ Fill holes in regions on a surface mesh. Parameters ---------- holes : list or array of integers hole numbers for all vertices regions : numpy array of integers region numbers for all vertices neighbor_lists : list of lists of integers each list contains indices to neighboring vertices for each vertex Returns ------- regions : numpy array of integers region numbers for all vertices """ import numpy as np # Make sure argument is a numpy array if not isinstance(regions, np.ndarray): regions = np.array(regions) # Identify the vertices for each hole hole_numbers = [x for x in np.unique(holes) if x != background_value] for n_hole in hole_numbers: I = [i for i, x in enumerate(holes) if x == n_hole] # Identify neighbors to these vertices N = [] [N.extend(neighbor_lists[i]) for i in I] if N: # Assign the hole the maximum region ID number of its neighbors regions[I] = max([regions[x] for x in N]) return regions #------------------------------------------------------------------------- # Find boundaries to holes #------------------------------------------------------------------------- hole_boundaries = background_value * np.ones(len(regions)) # Identify vertices for each region region_numbers = [x for x in np.unique(regions) if x != background_value] count = 0 for n_region in region_numbers: region_indices = np.where(regions == n_region)[0] # Identify neighbors to these vertices and their neighbors N = [] [N.extend(neighbor_lists[x]) for x in region_indices] N = list(frozenset(N).difference(region_indices)) N2 = [] [N2.extend(neighbor_lists[x]) for x in N] N.extend(N2) N = list(frozenset(N).difference(region_indices)) if N: # Segment the neighbors into connected vertices (region boundaries) boundaries = segment(N, neighbor_lists) # Remove the largest region boundary, presumably the # outer contour of the region, leaving smaller boundaries, # presumably the contours of holes within the region boundary_numbers = [ x for x in np.unique(boundaries) if x != background_value ] max_size = 0 max_number = 0 for n_boundary in boundary_numbers: border_indices = np.where(boundaries == n_boundary)[0] if len(border_indices) > max_size: max_size = len(border_indices) max_number = n_boundary boundaries[boundaries == max_number] = background_value boundary_numbers = [x for x in boundary_numbers if x != max_number] # Add remaining boundaries to holes array for n_boundary in boundary_numbers: indices = [ i for i, x in enumerate(boundaries) if x == n_boundary ] hole_boundaries[indices] = count count += 1 #------------------------------------------------------------------------- # Fill holes #------------------------------------------------------------------------- # If there are any holes if count > 0: hole_numbers = [ x for x in np.unique(hole_boundaries) if x != background_value ] background = [ i for i, x in enumerate(regions) if x == background_value ] # Grow seeds from hole boundaries to fill holes for n_hole in hole_numbers: seed_list = np.where(hole_boundaries == n_hole)[0].tolist() seed_lists = [list(frozenset(background).intersection(seed_list))] hole = segment(background, neighbor_lists, 1, seed_lists) # Label the vertices for each hole by surrounding region number # if hole does not include values within exclude_range: if len(exclude_range) == 2: Ihole = np.where(hole != background_value)[0] #if not len(frozenset(values[Ihole]).intersection(exclude_range)): if not [ x for x in values[Ihole] if x > exclude_range[0] if x < exclude_range[1] ]: regions = label_holes(hole, regions, neighbor_lists) else: regions = label_holes(hole, regions, neighbor_lists) return regions
def spectrum_of_largest(points, faces, n_eigenvalues=6, exclude_labels=[-1], normalization=None, areas=None): """ Compute Laplace-Beltrami spectrum on largest connected segment. In case a surface patch is fragmented, we select the largest fragment, remove extraneous triangular faces, and reindex indices. Parameters ---------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh n_eigenvalues : integer number of eigenvalues to be computed (the length of the spectrum) exclude_labels : list of integers background values to exclude normalization : string the method used to normalize eigenvalues ('area' or None) if "area", use area of the 2D structure as in Reuter et al. 2006 areas : numpy array or list of floats (or None) surface area scalar values for all vertices Returns ------- spectrum : list first n_eigenvalues eigenvalues for Laplace-Beltrami spectrum Examples -------- >>> # Spectrum for one label (artificial composite), two fragments: >>> import os >>> import numpy as np >>> from mindboggle.utils.io_vtk import read_scalars, read_vtk, write_vtk >>> from mindboggle.utils.mesh import remove_faces >>> from mindboggle.shapes.laplace_beltrami import spectrum_of_largest >>> path = os.environ['MINDBOGGLE_DATA'] >>> label_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT25.manual.vtk') >>> area_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.area.vtk') >>> n_eigenvalues = 6 >>> exclude_labels = [0] #[-1] >>> normalization = None >>> faces, lines, indices, points, foo1, labels, foo2, foo3 = read_vtk(label_file, >>> return_first=True, return_array=True) >>> I2 = [i for i,x in enumerate(labels) if x==2] # cingulate >>> I22 = [i for i,x in enumerate(labels) if x==22] # postcentral >>> I2.extend(I22) >>> faces = remove_faces(faces, I2) >>> areas, u1 = read_scalars(area_file, True, True) >>> # >>> spectrum_of_largest(points, faces, n_eigenvalues, exclude_labels, >>> normalization, areas) >>> # >>> # View: >>> from mindboggle.utils.plots import plot_vtk >>> scalars = np.zeros(np.shape(labels)) >>> scalars[I2] = 1 >>> vtk_file = 'test_two_labels.vtk' >>> write_vtk(vtk_file, points, indices, lines, faces, >>> scalars, scalar_names='scalars') >>> plot_vtk(vtk_file) Load "Labels" scalars from lh.labels.DKT25.manual.vtk Reduced 290134 to 29728 triangular faces Load "scalars" scalars from lh.pial.area.vtk 2 segments Reduced 29728 to 14498 triangular faces Compute linear FEM Laplace-Beltrami spectrum [-8.764053090852845e-18, 0.00028121452203987146, 0.0010941205613292243, 0.0017301461686759188, 0.0034244633555606295, 0.004280982704174599] """ from scipy.sparse.linalg import eigsh, lobpcg import numpy as np from mindboggle.utils.mesh import find_neighbors, remove_faces, \ reindex_faces_points from mindboggle.utils.segment import segment from mindboggle.shapes.laplace_beltrami import fem_laplacian # Areas: use_area = False if isinstance(areas, np.ndarray) and np.shape(areas): use_area = True elif isinstance(areas, list) and len(areas): areas = np.array(areas) use_area = True # Check to see if there are enough points: min_npoints = n_eigenvalues npoints = len(points) if npoints < min_npoints or len(faces) < min_npoints: print("The input size {0} ({1} faces) should be much larger " "than n_eigenvalues {2}". format(npoints, len(faces), n_eigenvalues)) return None else: #--------------------------------------------------------------------- # Segment the indices into connected sets of indices: #--------------------------------------------------------------------- # Construct neighbor lists: neighbor_lists = find_neighbors(faces, npoints) # Determine the indices: indices = [x for sublst in faces for x in sublst] # Segment: segments = segment(indices, neighbor_lists, min_region_size=1, seed_lists=[], keep_seeding=False, spread_within_labels=False, labels=[], label_lists=[], values=[], max_steps='', verbose=False) #--------------------------------------------------------------------- # Select the largest segment (connected set of indices): #--------------------------------------------------------------------- unique_segments = [x for x in np.unique(segments) if x not in exclude_labels] if len(unique_segments) > 1: select_indices = [] max_segment_area = 0 for segment_number in unique_segments: segment_indices = [i for i,x in enumerate(segments) if x == segment_number] if use_area: segment_area = np.sum(areas[segment_indices]) else: segment_area = len(segment_indices) if segment_area > max_segment_area: select_indices = segment_indices max_segment_area = len(select_indices) print('Maximum size of {0} segments: {1} vertices'. format(len(unique_segments), len(select_indices))) #----------------------------------------------------------------- # Extract points and renumber faces for the selected indices: #----------------------------------------------------------------- faces = remove_faces(faces, select_indices) else: select_indices = indices # Alert if the number of indices is small: if len(select_indices) < min_npoints: print("The input size {0} is too small.".format(len(select_indices))) return None elif faces: #----------------------------------------------------------------- # Reindex indices in faces: #----------------------------------------------------------------- faces, points = reindex_faces_points(faces, points) #----------------------------------------------------------------- # Compute spectrum: #----------------------------------------------------------------- spectrum = fem_laplacian(points, faces, n_eigenvalues, normalization) return spectrum else: return None
def extract_folds(depth_file, min_fold_size=50, tiny_depth=0.001, save_file=False): """ Use depth to extract folds from a triangular surface mesh. Steps :: 1. Compute histogram of depth measures. 2. Define a depth threshold and find the deepest vertices. 3. Segment deep vertices as an initial set of folds. 4. Remove small folds. 5. Find and fill holes in the folds. 6. Renumber folds. Step 2 :: To extract an initial set of deep vertices from the surface mesh, we anticipate that there will be a rapidly decreasing distribution of low depth values (on the outer surface) with a long tail of higher depth values (in the folds), so we smooth the histogram's bin values, convolve to compute slopes, and find the depth value for the first bin with slope = 0. This is our threshold. Step 5 :: The folds could have holes in areas shallower than the depth threshold. Calling fill_holes() could accidentally include very shallow areas (in an annulus-shaped fold, for example), so we call fill_holes() with the argument exclude_range set close to zero to retain these areas. Parameters ---------- depth_file : string surface mesh file in VTK format with faces and depth scalar values min_fold_size : integer minimum fold size (number of vertices) tiny_depth : float largest non-zero depth value that will stop a hole from being filled save_file : Boolean save output VTK file? Returns ------- folds : list of integers fold numbers for all vertices (-1 for non-fold vertices) n_folds : int number of folds depth_threshold : float threshold defining the minimum depth for vertices to be in a fold bins : list of integers histogram bins: each is the number of vertices within a range of depth values bin_edges : list of floats histogram bin edge values defining the bin ranges of depth values folds_file : string (if save_file) name of output VTK file with fold IDs (-1 for non-fold vertices) Examples -------- >>> import os >>> import numpy as np >>> import pylab >>> from scipy.ndimage.filters import gaussian_filter1d >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.utils.mesh import find_neighbors_from_file >>> from mindboggle.utils.plots import plot_surfaces >>> from mindboggle.features.folds import extract_folds >>> path = os.environ['MINDBOGGLE_DATA'] >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> neighbor_lists = find_neighbors_from_file(depth_file) >>> min_fold_size = 50 >>> tiny_depth = 0.001 >>> save_file = True >>> # >>> folds, n_folds, thr, bins, bin_edges, folds_file = extract_folds(depth_file, >>> min_fold_size, tiny_depth, save_file) >>> # >>> # View folds: >>> plot_surfaces('folds.vtk') >>> # Plot histogram and depth threshold: >>> depths, name = read_scalars(depth_file) >>> nbins = np.round(len(depths) / 100.0) >>> a,b,c = pylab.hist(depths, bins=nbins) >>> pylab.plot(thr*np.ones((100,1)), np.linspace(0, max(bins), 100), 'r.') >>> pylab.show() >>> # Plot smoothed histogram: >>> bins_smooth = gaussian_filter1d(bins.tolist(), 5) >>> pylab.plot(range(len(bins)), bins, '.', range(len(bins)), bins_smooth,'-') >>> pylab.show() """ import os import sys import numpy as np from time import time from scipy.ndimage.filters import gaussian_filter1d from mindboggle.utils.io_vtk import rewrite_scalars, read_vtk from mindboggle.utils.mesh import find_neighbors from mindboggle.utils.morph import fill_holes from mindboggle.utils.segment import segment do_fill_holes = True print("Extract folds in surface mesh") t0 = time() #------------------------------------------------------------------------- # Load depth values for all vertices #------------------------------------------------------------------------- faces, lines, indices, points, npoints, depths, name, input_vtk = read_vtk( depth_file, return_first=True, return_array=True) #------------------------------------------------------------------------- # Find neighbors for each vertex #------------------------------------------------------------------------- neighbor_lists = find_neighbors(faces, npoints) #------------------------------------------------------------------------- # Compute histogram of depth measures #------------------------------------------------------------------------- min_vertices = 10000 if npoints > min_vertices: nbins = np.round(npoints / 100.0) else: sys.err(" Expecting at least {0} vertices to create depth histogram". format(min_vertices)) bins, bin_edges = np.histogram(depths, bins=nbins) #------------------------------------------------------------------------- # Anticipating that there will be a rapidly decreasing distribution # of low depth values (on the outer surface) with a long tail of higher # depth values (in the folds), smooth the bin values (Gaussian), convolve # to compute slopes, and find the depth for the first bin with slope = 0. #------------------------------------------------------------------------- bins_smooth = gaussian_filter1d(bins.tolist(), 5) window = [-1, 0, 1] bin_slopes = np.convolve(bins_smooth, window, mode='same') / (len(window) - 1) ibins0 = np.where(bin_slopes == 0)[0] if ibins0.shape: depth_threshold = bin_edges[ibins0[0]] else: depth_threshold = np.median(depths) #------------------------------------------------------------------------- # Find the deepest vertices #------------------------------------------------------------------------- indices_deep = [i for i, x in enumerate(depths) if x >= depth_threshold] if indices_deep: #--------------------------------------------------------------------- # Segment deep vertices as an initial set of folds #--------------------------------------------------------------------- print(" Segment vertices deeper than {0:.2f} as folds".format( depth_threshold)) t1 = time() folds = segment(indices_deep, neighbor_lists) # Slightly slower alternative -- fill boundaries: #regions = -1 * np.ones(len(points)) #regions[indices_deep] = 1 #folds = segment_by_filling_borders(regions, neighbor_lists) print(' ...Segmented folds ({0:.2f} seconds)'.format(time() - t1)) #--------------------------------------------------------------------- # Remove small folds #--------------------------------------------------------------------- if min_fold_size > 1: print(' Remove folds smaller than {0}'.format(min_fold_size)) unique_folds = [x for x in np.unique(folds) if x != -1] for nfold in unique_folds: indices_fold = [i for i, x in enumerate(folds) if x == nfold] if len(indices_fold) < min_fold_size: folds[indices_fold] = -1 #--------------------------------------------------------------------- # Find and fill holes in the folds # Note: Surfaces surrounded by folds can be mistaken for holes, # so exclude_range includes outer surface values close to zero. #--------------------------------------------------------------------- if do_fill_holes: print(" Find and fill holes in the folds") folds = fill_holes(folds, neighbor_lists, values=depths, exclude_range=[0, tiny_depth]) #--------------------------------------------------------------------- # Renumber folds so they are sequential #--------------------------------------------------------------------- renumber_folds = -1 * np.ones(len(folds)) fold_numbers = [int(x) for x in np.unique(folds) if x != -1] for i_fold, n_fold in enumerate(fold_numbers): fold = [i for i, x in enumerate(folds) if x == n_fold] renumber_folds[fold] = i_fold folds = renumber_folds n_folds = i_fold + 1 # Print statement print(' ...Extracted {0} folds ({1:.2f} seconds)'.format( n_folds, time() - t0)) else: print(' No deep vertices') folds = [int(x) for x in folds] #------------------------------------------------------------------------- # Return folds, number of folds, file name #------------------------------------------------------------------------- if save_file: folds_file = os.path.join(os.getcwd(), 'folds.vtk') rewrite_scalars(depth_file, folds_file, folds, 'folds', folds) if not os.path.exists(folds_file): raise (IOError(folds_file + " not found")) else: folds_file = None return folds, n_folds, depth_threshold, bins, bin_edges, folds_file