def annot_to_vtk(annot_file, vtk_file, output_vtk=''): """ Load a FreeSurfer .annot file and save as a VTK format file. Parameters ---------- annot_file : string name of FreeSurfer .annot file vtk_file : string name of VTK surface file output_vtk : string name of output VTK file, where each vertex is assigned the corresponding shape value Returns ------- labels : list integers (one label per vertex) output_vtk : string name of output VTK file, where each vertex is assigned the corresponding shape value Examples -------- >>> import os >>> from mindboggle.utils.io_free import annot_to_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> annot_file = os.path.join(path, 'arno', 'freesurfer', 'lh.aparc.annot') >>> vtk_file = os.path.join(path, 'arno', 'freesurfer', 'lh.pial.vtk') >>> output_vtk = '' >>> # >>> labels, output_vtk = annot_to_vtk(annot_file, vtk_file, output_vtk) >>> # >>> # View: >>> from mindboggle.utils.plots import plot_vtk >>> plot_vtk(output_vtk) """ import os import nibabel as nb from mindboggle.utils.io_vtk import rewrite_scalars labels, colortable, names = nb.freesurfer.read_annot(annot_file) if not output_vtk: output_vtk = os.path.join(os.getcwd(), os.path.basename(annot_file).strip('.annot') + '.vtk') rewrite_scalars(vtk_file, output_vtk, labels, 'Labels') if not os.path.exists(output_vtk): raise(IOError(output_vtk + " not found")) return labels, output_vtk
def curvature_to_vtk(surface_file, vtk_file, output_vtk): """ Convert FreeSurfer curvature, thickness, or convexity file to VTK format. Parameters ---------- surface_file : string name of FreeSurfer surface file vtk_file : string name of VTK surface file output_vtk : string name of output VTK file Returns ------- output_vtk : string name of output VTK file, where each vertex is assigned the corresponding shape value Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import curvature_to_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> surface_file = os.path.join(path, 'arno', 'freesurfer', 'lh.thickness') >>> vtk_file = os.path.join(path, 'arno', 'freesurfer', 'lh.pial.vtk') >>> output_vtk = '' >>> # >>> curvature_to_vtk(surface_file, vtk_file, output_vtk) >>> # >>> # View: >>> from mindboggle.utils.plots import plot_vtk >>> plot_vtk('lh.thickness.vtk') """ import os import nibabel as nb from mindboggle.utils.io_vtk import rewrite_scalars curvature_values = nb.freesurfer.read_morph_data(surface_file) scalar_names = os.path.basename(surface_file) if not output_vtk: output_vtk = os.path.join(os.getcwd(), os.path.basename(surface_file)+'.vtk') rewrite_scalars(vtk_file, output_vtk, curvature_values, scalar_names) if not os.path.exists(output_vtk): raise(IOError(output_vtk + " not found")) return output_vtk
def plot_vtk(vtk_file, mask_file='', masked_output=''): """ Use mayavi2 to visualize VTK surface mesh data. Inputs ------ vtk_file : string name of VTK surface mesh file Examples -------- >>> import os >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> vtk_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> mask_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> masked_output = '' >>> plot_vtk(vtk_file, mask_file, masked_output) """ import os # import subprocess # Filter mesh with the non -1 values from a second (same-size) mesh: if mask_file: from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars scalars, name = read_scalars(vtk_file) mask, name = read_scalars(mask_file) if not masked_output: masked_output = 'temp.vtk' rewrite_scalars(vtk_file, masked_output, scalars, 'masked', mask) cmd = ["mayavi2", "-d", masked_output, "-m", "Surface"] else: cmd = ["mayavi2", "-d", vtk_file, "-m", "Surface"] # Note: subprocess won't allow me to put the command in the background: # p = subprocess.Popen(cmd) # p.communicate() cmd = ' '.join(cmd) + ' &' print(cmd) os.system(cmd)
def plot_vtk(vtk_file, mask_file='', masked_output=''): """ Use mayavi2 to visualize VTK surface mesh data. Inputs ------ vtk_file : string name of VTK surface mesh file Examples -------- >>> import os >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> vtk_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> mask_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> masked_output = '' >>> plot_vtk(vtk_file, mask_file, masked_output) """ from mindboggle.utils.utils import execute # Filter mesh with the non -1 values from a second (same-size) mesh: if mask_file: from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars scalars, name = read_scalars(vtk_file) mask, name = read_scalars(mask_file) if not masked_output: masked_output = 'temp.vtk' rewrite_scalars(vtk_file, masked_output, scalars, 'masked', mask) cmd = ["mayavi2", "-d", masked_output, "-m", "Surface"] else: cmd = ["mayavi2", "-d", vtk_file, "-m", "Surface"] cmd.extend('&') execute(cmd, 'os')
def extract_borders_2nd_surface(labels_file, mask_file="", values_file=""): """ Extract borders (between labels) on a surface. Options: Mask out values; extract border values on a second surface. Parameters ---------- labels_file : string file name for surface mesh with labels mask_file : string file name for surface mesh with mask (>-1) values values_file : string file name for surface mesh with values to extract along borders Returns ------- border_file : string file name for surface mesh with label borders (-1 background values) border_values : numpy array values for all vertices (-1 for vertices not along label borders) Examples -------- >>> # Extract depth values along label borders in sulci (mask): >>> import os >>> from mindboggle.labels.labels import extract_borders_2nd_surface >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> labels_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT25.manual.vtk') >>> mask_file = os.path.join(path, 'arno', 'features', 'sulci.vtk') >>> values_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> # >>> border_file, border_values = extract_borders_2nd_surface(labels_file, mask_file, values_file) >>> # >>> plot_vtk(border_file) """ import os 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 # Load labeled surface file faces, foo1, foo2, foo3, npoints, labels, foo4, foo5 = read_vtk(labels_file, return_first=True, return_array=True) # Detect borders neighbor_lists = find_neighbors(faces, npoints) indices_borders, foo1, foo2 = extract_borders(range(npoints), labels, neighbor_lists) # Filter values with label borders border_values = -1 * np.ones(npoints) if values_file: values, name = read_scalars(values_file, return_first=True, return_array=True) border_values[indices_borders] = values[indices_borders] else: border_values[indices_borders] = 1 # Mask values (for mask >-1) if mask_file: mask_values, name = read_scalars(mask_file) else: mask_values = [] # Write out label boundary vtk file border_file = os.path.join(os.getcwd(), "borders_" + os.path.basename(labels_file)) rewrite_scalars(labels_file, border_file, border_values, "label_borders_in_mask", mask_values) if not os.path.exists(border_file): raise (IOError(border_file + " not found")) return border_file, border_values
def extract_subfolds(depth_file, folds, min_size=10, depth_factor=0.25, depth_ratio=0.1, tolerance=0.01, save_file=False): """ Use depth to segment folds into subfolds in a triangular surface mesh. Note :: The function extract_sulci() performs about the same whether folds or subfolds are used as input. The latter leads to some loss of small subfolds and possibly holes for small subfolds in the middle of other subfolds. Note about the watershed() function: The watershed() function performs individual seed growing from deep seeds, repeats segmentation from the resulting seeds until each seed's segment touches a boundary. The function segment() fills in the rest. Finally segments are joined if their seeds are too close to each other. Despite these precautions, the order of seed selection in segment() could possibly influence the resulting borders between adjoining segments. [The propagate() function is slower and insensitive to depth, but is not biased by seed order.] Parameters ---------- depth_file : string surface mesh file in VTK format with faces and depth scalar values folds : list of integers fold numbers for all vertices (-1 for non-fold vertices) min_size : integer minimum number of vertices for a subfold depth_factor : float watershed() depth_factor: 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 watershed() depth_ratio: the minimum fraction of depth for a neighboring shallower watershed catchment basin (otherwise merged with the deeper basin) tolerance : float watershed() tolerance: tolerance for detecting differences in depth between vertices save_file : Boolean save output VTK file? Returns ------- subfolds : list of integers fold numbers for all vertices (-1 for non-fold vertices) n_subfolds : int number of subfolds subfolds_file : string (if save_file) name of output VTK file with fold IDs (-1 for non-fold vertices) Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.mesh import find_neighbors_from_file >>> from mindboggle.features.folds import extract_subfolds >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> min_size = 10 >>> depth_factor = 0.5 >>> depth_ratio = 0.1 >>> tolerance = 0.01 >>> # >>> subfolds, n_subfolds, subfolds_file = extract_subfolds(depth_file, >>> folds, min_size, depth_factor, depth_ratio, tolerance, True) >>> # >>> # View: >>> rewrite_scalars(depth_file, 'subfolds.vtk', subfolds, 'subfolds', subfolds) >>> plot_surfaces('subfolds.vtk') """ import os import numpy as np from time import time from mindboggle.utils.io_vtk import rewrite_scalars, read_vtk from mindboggle.utils.mesh import find_neighbors from mindboggle.utils.segment import segment, propagate, watershed print("Segment folds into subfolds") 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) #------------------------------------------------------------------------- # Segment folds into "watershed basins" #------------------------------------------------------------------------- indices_folds = [i for i, x in enumerate(folds) if x != -1] subfolds, seed_indices = watershed(depths, points, indices_folds, neighbor_lists, min_size, depth_factor=0.25, depth_ratio=0.1, tolerance=0.01, regrow=True) # Print statement n_subfolds = len([x for x in np.unique(subfolds) if x != -1]) print(' ...Extracted {0} subfolds ({1:.2f} seconds)'.format( n_subfolds, time() - t0)) #------------------------------------------------------------------------- # Return subfolds, number of subfolds, file name #------------------------------------------------------------------------- if save_file: subfolds_file = os.path.join(os.getcwd(), 'subfolds.vtk') rewrite_scalars(depth_file, subfolds_file, subfolds, 'subfolds', subfolds) if not os.path.exists(subfolds_file): raise (IOError(subfolds_file + " not found")) else: subfolds_file = None return subfolds, n_subfolds, subfolds_file
def extract_subfolds(depth_file, folds, min_size=10, depth_factor=0.25, depth_ratio=0.1, tolerance=0.01, save_file=False): """ Use depth to segment folds into subfolds in a triangular surface mesh. Note :: The function extract_sulci() performs about the same whether folds or subfolds are used as input. The latter leads to some loss of small subfolds and possibly holes for small subfolds in the middle of other subfolds. Note about the watershed() function: The watershed() function performs individual seed growing from deep seeds, repeats segmentation from the resulting seeds until each seed's segment touches a boundary. The function segment() fills in the rest. Finally segments are joined if their seeds are too close to each other. Despite these precautions, the order of seed selection in segment() could possibly influence the resulting borders between adjoining segments. [The propagate() function is slower and insensitive to depth, but is not biased by seed order.] Parameters ---------- depth_file : string surface mesh file in VTK format with faces and depth scalar values folds : list of integers fold numbers for all vertices (-1 for non-fold vertices) min_size : integer minimum number of vertices for a subfold depth_factor : float watershed() depth_factor: 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 watershed() depth_ratio: the minimum fraction of depth for a neighboring shallower watershed catchment basin (otherwise merged with the deeper basin) tolerance : float watershed() tolerance: tolerance for detecting differences in depth between vertices save_file : Boolean save output VTK file? Returns ------- subfolds : list of integers fold numbers for all vertices (-1 for non-fold vertices) n_subfolds : int number of subfolds subfolds_file : string (if save_file) name of output VTK file with fold IDs (-1 for non-fold vertices) Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.mesh import find_neighbors_from_file >>> from mindboggle.features.folds import extract_subfolds >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> min_size = 10 >>> depth_factor = 0.5 >>> depth_ratio = 0.1 >>> tolerance = 0.01 >>> # >>> subfolds, n_subfolds, subfolds_file = extract_subfolds(depth_file, >>> folds, min_size, depth_factor, depth_ratio, tolerance, True) >>> # >>> # View: >>> rewrite_scalars(depth_file, 'subfolds.vtk', subfolds, 'subfolds', subfolds) >>> plot_vtk('subfolds.vtk') """ import os import numpy as np from time import time from mindboggle.utils.io_vtk import rewrite_scalars, read_vtk from mindboggle.utils.mesh import find_neighbors from mindboggle.utils.segment import segment, propagate, watershed print("Segment folds into subfolds") 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) #------------------------------------------------------------------------- # Segment folds into "watershed basins" #------------------------------------------------------------------------- indices_folds = [i for i,x in enumerate(folds) if x > -1] subfolds, seed_indices = watershed(depths, points, indices_folds, neighbor_lists, min_size, depth_factor=0.25, depth_ratio=0.1, tolerance=0.01, regrow=True) # Print statement n_subfolds = len([x for x in np.unique(subfolds) if x != -1]) print(' ...Extracted {0} subfolds ({1:.2f} seconds)'. format(n_subfolds, time() - t0)) #------------------------------------------------------------------------- # Return subfolds, number of subfolds, file name #------------------------------------------------------------------------- if save_file: subfolds_file = os.path.join(os.getcwd(), 'subfolds.vtk') rewrite_scalars(depth_file, subfolds_file, subfolds, 'subfolds', subfolds) if not os.path.exists(subfolds_file): raise(IOError(subfolds_file + " not found")) else: subfolds_file = None return subfolds, n_subfolds, subfolds_file
sulci_file = os.path.join(data_path, 'arno', 'features', 'sulci.vtk') faces, lines, indices, points, npoints, sulci, name, input_vtk = read_vtk(sulci_file) sulcus_ID = 1 sulcus_indices = [i for i,x in enumerate(sulci) if x == sulcus_ID] sulcus_faces = remove_faces(faces, sulcus_indices) sulcus_neighbor_lists = find_neighbors(sulcus_faces, len(points)) G=nx.Graph() G.add_nodes_from(sulcus_indices) for i, sulcus_neighbor_list in enumerate(sulcus_neighbor_lists): G.add_edges_from([[i,x] for x in sulcus_neighbor_list]) adjacency_matrix = nx.adjacency_matrix(G, nodelist=None, weight='weight') indices_to_connect = [0, len(sulcus_indices)-1] adjacency_matrix2, W, Path, Degree, TreeNbr = min_span_tree(adjacency_matrix, indices_to_connect) # Write results to vtk file and view: MST = np.zeros(len(points)) MST[W] = 1 rewrite_scalars(sulci_file, 'test_min_span_tree.vtk', MST, 'MST', MST) Terminal, Branching = [], [] for vtx in xrange(0,Num): if Degree[vtx] ==1: Terminal.append(vtx) elif Degree[vtx] > 2: Branching.append(vtx) Path, NodeColor = prune(Path, Degree, TreeNbr, Terminal, Branching, indices_to_connect, sulcus_indices) #endpoints = [i for i,x in enumerate(Degree) if x == 1]
def segment_fundi(fundus_per_fold, sulci=[], vtk_file='', save_file=False): """ Segment fundi by sulcus definitions. Parameters ---------- fundus_per_fold : list of integers fundus numbers for all vertices, labeled by fold (-1 for non-fundus vertices) sulci : numpy array or list of integers sulcus number for each vertex, used to filter and label fundi vtk_file : string (if save_file) VTK file with sulcus number for each vertex save_file : Boolean save output VTK file? Returns ------- fundus_per_sulcus : list of integers fundus numbers for all vertices, labeled by sulcus (-1 for non-fundus vertices) n_fundi : integer number of fundi fundus_per_sulcus_file : string (if save_file) output VTK file with fundus numbers (-1 for non-fundus vertices) Examples -------- >>> # Extract fundus from one or more sulci: >>> single_fold = True >>> import os >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.features.fundi import extract_fundi, segment_fundi >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> vtk_file = os.path.join(path, 'arno', 'features', 'sulci.vtk') >>> sulci, name = read_scalars(vtk_file, True, True) >>> curv_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, True, True) >>> if single_fold: >>> fold_number = 2 #11 >>> folds[folds != fold_number] = -1 >>> min_separation = 10 >>> erode_ratio = 0.10 >>> erode_min_size = 10 >>> save_file = True >>> fundus_per_fold, o1, o2 = extract_fundi(folds, curv_file, depth_file, min_separation, erode_ratio, erode_min_size, save_file) >>> o1, o2, fundus_per_sulcus_file = segment_fundi(fundus_per_fold, sulci, vtk_file, save_file) >>> # >>> # View: >>> plot_surfaces(fundus_per_sulcus_file) """ # Extract a skeleton to connect endpoints in a fold: import os import numpy as np from mindboggle.utils.io_vtk import rewrite_scalars if isinstance(sulci, list): sulci = np.array(sulci) #------------------------------------------------------------------------- # Create fundi by segmenting fold fundi with overlapping sulcus labels: #------------------------------------------------------------------------- indices = [i for i, x in enumerate(fundus_per_fold) if x != -1] if indices and np.size(sulci): fundus_per_sulcus = -1 * np.ones(len(sulci)) fundus_per_sulcus[indices] = sulci[indices] n_fundi = len([x for x in np.unique(fundus_per_sulcus) if x != -1]) else: fundus_per_sulcus = [] n_fundi = 0 if n_fundi == 1: sdum = 'sulcus fundus' else: sdum = 'sulcus fundi' print(' Segmented {0} {1}'.format(n_fundi, sdum)) #------------------------------------------------------------------------- # Return fundi, number of fundi, and file name: #------------------------------------------------------------------------- fundus_per_sulcus_file = None if n_fundi > 0: fundus_per_sulcus = [int(x) for x in fundus_per_sulcus] if save_file and os.path.exists(vtk_file): fundus_per_sulcus_file = os.path.join(os.getcwd(), 'fundus_per_sulcus.vtk') rewrite_scalars(vtk_file, fundus_per_sulcus_file, fundus_per_sulcus, 'fundus_per_sulcus', fundus_per_sulcus) if not os.path.exists(fundus_per_sulcus_file): raise (IOError(fundus_per_sulcus_file + " not found")) return fundus_per_sulcus, n_fundi, fundus_per_sulcus_file
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 rescale_by_label(input_vtk, labels_or_file, save_file=False, output_filestring='rescaled_scalars'): """ Rescale scalars for each label (such as depth values within each fold). Default is to normalize the scalar values of a VTK file by a percentile value in each vertex's surface mesh for each label. Parameters ---------- input_vtk : string name of VTK file with a scalar value for each vertex labels_or_file : list or string label number for each vertex or name of VTK file with index scalars save_file : Boolean save output VTK file? output_filestring : string (if save_file) name of output file Returns ------- rescaled_scalars : list of floats scalar values rescaled for each label, for label numbers not equal to -1 rescaled_scalars_file : string (if save_file) name of output VTK file with rescaled scalar values for each label Examples -------- >>> # Rescale depths by neighborhood within each label: >>> import os >>> from mindboggle.utils.mesh import rescale_by_label >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> input_vtk = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> labels_or_file = os.path.join(path, 'arno', 'features', 'subfolds.vtk') >>> save_file = True >>> output_filestring = 'rescaled_scalars' >>> # >>> rescaled_scalars, rescaled_scalars_file = rescale_by_label(input_vtk, >>> labels_or_file, save_file, output_filestring) >>> # >>> # View rescaled scalar values per fold: >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> # >>> rewrite_scalars(rescaled_scalars_file, rescaled_scalars_file, >>> rescaled_scalars, 'rescaled_depths', folds) >>> plot_surfaces(rescaled_scalars_file) """ import os import numpy as np from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars # Load scalars and vertex neighbor lists: scalars, name = read_scalars(input_vtk, True, True) print(" Rescaling scalar values within each label...") # Load label numbers: if isinstance(labels_or_file, str): labels, name = read_scalars(labels_or_file, True, True) elif isinstance(labels_or_file, list): labels = labels_or_file unique_labels = np.unique(labels) unique_labels = [x for x in unique_labels if x >= 0] # Loop through labels: for label in unique_labels: #print(" Rescaling scalar values within label {0} of {1} labels...".format( # int(label), len(unique_labels))) indices = [i for i, x in enumerate(labels) if x == label] if indices: # Rescale by the maximum label scalar value: scalars[indices] = scalars[indices] / np.max(scalars[indices]) rescaled_scalars = scalars.tolist() #------------------------------------------------------------------------- # Return rescaled scalars and file name #------------------------------------------------------------------------- if save_file: rescaled_scalars_file = os.path.join(os.getcwd(), output_filestring + '.vtk') rewrite_scalars(input_vtk, rescaled_scalars_file, rescaled_scalars, 'rescaled_scalars', labels) if not os.path.exists(rescaled_scalars_file): raise (IOError(rescaled_scalars_file + " not found")) else: rescaled_scalars_file = None return rescaled_scalars, rescaled_scalars_file
def compute_likelihood(trained_file, depth_file, curvature_file, folds, save_file=False): """ Compute likelihoods based on input values, folds, and estimated parameters. Compute likelihood values for a given VTK surface mesh file, after training on distributions of depth and curvature values from multiple files. Parameters ---------- trained_file : pickle compressed file contains the following dictionaries containing lists of floats (estimates of depth or curvature means, sigmas, and weights trained on fold vertices either on or off sulcus label borders) depth_border, curv_border, depth_nonborder, curv_nonborder depth_file : string VTK surface mesh file with depth values in [0,1] for all vertices curvature_file : string VTK surface mesh file with curvature values in [-1,1] for all vertices folds : list of integers fold number for all vertices (-1 for non-fold vertices) save_file : Boolean save output VTK file? Returns ------- likelihoods : list of floats likelihood values for all vertices (0 for non-fold vertices) likelihoods_file : string (if save_file) name of output VTK file with likelihood scalars (-1 for non-fold vertices) Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.shapes.likelihood import compute_likelihood >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> trained_file = os.path.join(path, 'atlases', 'depth_curv_border_nonborder_parameters.pkl') >>> #depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> curvature_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> save_file = True >>> # >>> compute_likelihood(trained_file, depth_file, curvature_file, folds, save_file) >>> # View: >>> plot_vtk('likelihoods.vtk', folds_file) """ import os import numpy as np from math import pi import cPickle as pickle from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars # Initialize variables: tiny = 0.000000001 L = np.zeros(len(folds)) probs_border = np.zeros(len(folds)) probs_nonborder = np.zeros(len(folds)) # Load estimated depth and curvature distribution parameters: depth_border, curv_border, depth_nonborder, curv_nonborder = pickle.load( open(trained_file, "r")) # Load depths, curvatures: depths, name = read_scalars(depth_file, True, True) curvatures, name = read_scalars(curvature_file, True, True) # Prep for below: n = 2 twopiexp = (2*pi)**(n/2) border_sigmas = depth_border['sigmas'] * curv_border['sigmas'] nonborder_sigmas = depth_nonborder['sigmas'] * curv_nonborder['sigmas'] norm_border = 1 / (twopiexp * border_sigmas + tiny) norm_nonborder = 1 / (twopiexp * nonborder_sigmas + tiny) I = [i for i,x in enumerate(folds) if x != -1] N = depth_border['sigmas'].shape[0] for j in range(N): # Border: expB = depth_border['weights'][j] * \ ((depths[I]-depth_border['means'][j])**2) / \ depth_border['sigmas'][j]**2 expB += curv_border['weights'][j] * \ ((curvatures[I]-curv_border['means'][j])**2) / \ curv_border['sigmas'][j]**2 expB = -expB / 2 probs_border[I] = probs_border[I] + norm_border[j] * np.exp(expB) # Non-border: expNB = depth_nonborder['weights'][j] * \ ((depths[I]-depth_nonborder['means'][j])**2) / \ depth_nonborder['sigmas'][j]**2 expNB += curv_nonborder['weights'][j] * \ ((curvatures[I]-curv_nonborder['means'][j])**2) / \ curv_nonborder['sigmas'][j]**2 expNB = -expNB / 2 probs_nonborder[I] = probs_nonborder[I] + norm_nonborder[j] * np.exp(expNB) likelihoods = probs_border / (probs_nonborder + probs_border + tiny) likelihoods.tolist() #------------------------------------------------------------------------- # Return likelihoods and output file name #------------------------------------------------------------------------- if save_file: likelihoods_file = os.path.join(os.getcwd(), 'likelihoods.vtk') rewrite_scalars(depth_file, likelihoods_file, likelihoods, 'likelihoods', likelihoods) if not os.path.exists(likelihoods_file): raise(IOError(likelihoods_file + " not found")) else: likelihoods_file = None return likelihoods, likelihoods_file
def rescale_by_label(input_vtk, labels_or_file, combine_all_labels=False, nedges=10, p=99, set_max_to_1=True, save_file=False, output_filestring='rescaled_scalars'): """ Rescale scalars for each label (such as depth values within each fold). Default is to normalize the scalar values of a VTK file by a percentile value in each vertex's surface mesh for each label. Parameters ---------- input_vtk : string name of VTK file with a scalar value for each vertex labels_or_file : list or string label number for each vertex or name of VTK file with index scalars combine_all_labels : Boolean combine all labels (scalars not equal to -1) as one label? nedges : integer (if norm_by_neighborhood) number or edges from vertex, defining the size of its neighborhood p : float in range of [0,100] (if norm_by_neighborhood) percentile used to rescale each scalar set_max_to_1 : Boolean set all rescaled values greater than 1 to 1.0? save_file : Boolean save output VTK file? output_filestring : string (if save_file) name of output file Returns ------- rescaled_scalars : list of floats scalar values rescaled for each label, for label numbers not equal to -1 rescaled_scalars_file : string (if save_file) name of output VTK file with rescaled scalar values for each label Examples -------- >>> # Rescale depths by neighborhood within each label: >>> import os >>> from mindboggle.shapes.measure import rescale_by_label >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> input_vtk = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> labels_or_file = os.path.join(path, 'arno', 'features', 'subfolds.vtk') >>> combine_all_labels = False >>> nedges = 10 >>> p = 99 >>> set_max_to_1 = True >>> save_file = True >>> output_filestring = 'rescaled_scalars' >>> # >>> rescaled_scalars, rescaled_scalars_file = rescale_by_label(input_vtk, >>> labels_or_file, combine_all_labels, nedges, p, >>> set_max_to_1, save_file, output_filestring) >>> # >>> # View rescaled scalar values per fold: >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> # >>> rewrite_scalars(rescaled_scalars_file, rescaled_scalars_file, >>> rescaled_scalars, 'rescaled_depths', folds) >>> plot_vtk(rescaled_scalars_file) """ 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 # Load scalars and vertex neighbor lists: scalars, name = read_scalars(input_vtk, True, True) print(" Rescaling scalar values within each label...") # Load label numbers: if isinstance(labels_or_file, str): labels, name = read_scalars(labels_or_file, True, True) elif isinstance(labels_or_file, list): labels = labels_or_file unique_labels = np.unique(labels) unique_labels = [x for x in unique_labels if x >= 0] # Loop through labels: for label in unique_labels: #print(" Rescaling scalar values within label {0} of {1} labels...".format( # int(label), len(unique_labels))) indices = [i for i,x in enumerate(labels) if x == label] if indices: # Rescale by the maximum label scalar value: scalars[indices] = scalars[indices] / np.max(scalars[indices]) rescaled_scalars = scalars.tolist() #--------------------------------------------------------------------------- # Return rescaled scalars and file name #--------------------------------------------------------------------------- if save_file: rescaled_scalars_file = os.path.join(os.getcwd(), output_filestring + '.vtk') rewrite_scalars(input_vtk, rescaled_scalars_file, rescaled_scalars, 'rescaled_scalars', labels) else: rescaled_scalars_file = None return rescaled_scalars, rescaled_scalars_file
def compute_likelihood(trained_file, depth_file, curvature_file, folds, save_file=False): """ Compute likelihoods based on input values, folds, and estimated parameters. Compute likelihood values for a given VTK surface mesh file, after training on distributions of depth and curvature values from multiple files. Parameters ---------- trained_file : pickle compressed file contains the following dictionaries containing lists of floats (estimates of depth or curvature means, sigmas, and weights trained on fold vertices either on or off sulcus label borders) depth_border, curv_border, depth_nonborder, curv_nonborder depth_file : string VTK surface mesh file with depth values in [0,1] for all vertices curvature_file : string VTK surface mesh file with curvature values in [-1,1] for all vertices folds : list of integers fold number for all vertices (-1 for non-fold vertices) save_file : Boolean save output VTK file? Returns ------- likelihoods : list of floats likelihood values for all vertices (0 for non-fold vertices) likelihoods_file : string (if save_file) name of output VTK file with likelihood scalars (-1 for non-fold vertices) Examples -------- >>> import os >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.shapes.likelihood import compute_likelihood >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> trained_file = os.path.join(path, 'atlases', 'depth_curv_border_nonborder_parameters.pkl') >>> #depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> curvature_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> save_file = True >>> # >>> compute_likelihood(trained_file, depth_file, curvature_file, folds, save_file) >>> # View: >>> plot_surfaces('likelihoods.vtk', folds_file) """ import os import numpy as np from math import pi import cPickle as pickle from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars # Initialize variables: tiny = 0.000000001 L = np.zeros(len(folds)) probs_border = np.zeros(len(folds)) probs_nonborder = np.zeros(len(folds)) # Load estimated depth and curvature distribution parameters: depth_border, curv_border, depth_nonborder, curv_nonborder = pickle.load( open(trained_file, "r")) # Load depths, curvatures: depths, name = read_scalars(depth_file, True, True) curvatures, name = read_scalars(curvature_file, True, True) # Prep for below: n = 2 twopiexp = (2 * pi)**(n / 2) border_sigmas = depth_border['sigmas'] * curv_border['sigmas'] nonborder_sigmas = depth_nonborder['sigmas'] * curv_nonborder['sigmas'] norm_border = 1 / (twopiexp * border_sigmas + tiny) norm_nonborder = 1 / (twopiexp * nonborder_sigmas + tiny) I = [i for i, x in enumerate(folds) if x != -1] N = depth_border['sigmas'].shape[0] for j in range(N): # Border: expB = depth_border['weights'][j] * \ ((depths[I]-depth_border['means'][j])**2) / \ depth_border['sigmas'][j]**2 expB += curv_border['weights'][j] * \ ((curvatures[I]-curv_border['means'][j])**2) / \ curv_border['sigmas'][j]**2 expB = -expB / 2 probs_border[I] = probs_border[I] + norm_border[j] * np.exp(expB) # Non-border: expNB = depth_nonborder['weights'][j] * \ ((depths[I]-depth_nonborder['means'][j])**2) / \ depth_nonborder['sigmas'][j]**2 expNB += curv_nonborder['weights'][j] * \ ((curvatures[I]-curv_nonborder['means'][j])**2) / \ curv_nonborder['sigmas'][j]**2 expNB = -expNB / 2 probs_nonborder[ I] = probs_nonborder[I] + norm_nonborder[j] * np.exp(expNB) likelihoods = probs_border / (probs_nonborder + probs_border + tiny) likelihoods = likelihoods.tolist() #------------------------------------------------------------------------- # Return likelihoods and output file name #------------------------------------------------------------------------- if save_file: likelihoods_file = os.path.join(os.getcwd(), 'likelihoods.vtk') rewrite_scalars(depth_file, likelihoods_file, likelihoods, 'likelihoods', likelihoods) if not os.path.exists(likelihoods_file): raise (IOError(likelihoods_file + " not found")) else: likelihoods_file = None return likelihoods, likelihoods_file
def segment_fundi(fundus_per_fold, sulci=[], vtk_file='', save_file=False): """ Segment fundi by sulcus definitions. Parameters ---------- fundus_per_fold : list of integers fundus numbers for all vertices, labeled by fold (-1 for non-fundus vertices) sulci : numpy array or list of integers sulcus number for each vertex, used to filter and label fundi vtk_file : string (if save_file) VTK file with sulcus number for each vertex save_file : Boolean save output VTK file? Returns ------- fundus_per_sulcus : list of integers fundus numbers for all vertices, labeled by sulcus (-1 for non-fundus vertices) n_fundi : integer number of fundi fundus_per_sulcus_file : string (if save_file) output VTK file with fundus numbers (-1 for non-fundus vertices) Examples -------- >>> # Extract fundus from one or more sulci: >>> single_fold = True >>> import os >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.features.fundi import extract_fundi, segment_fundi >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> vtk_file = os.path.join(path, 'arno', 'features', 'sulci.vtk') >>> sulci, name = read_scalars(vtk_file, True, True) >>> curv_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, True, True) >>> if single_fold: >>> fold_number = 2 #11 >>> folds[folds != fold_number] = -1 >>> min_separation = 10 >>> erode_ratio = 0.10 >>> erode_min_size = 10 >>> save_file = True >>> fundus_per_fold, o1, o2 = extract_fundi(folds, curv_file, depth_file, min_separation, erode_ratio, erode_min_size, save_file) >>> o1, o2, fundus_per_sulcus_file = segment_fundi(fundus_per_fold, sulci, vtk_file, save_file) >>> # >>> # View: >>> plot_surfaces(fundus_per_sulcus_file) """ # Extract a skeleton to connect endpoints in a fold: import os import numpy as np from mindboggle.utils.io_vtk import rewrite_scalars if isinstance(sulci, list): sulci = np.array(sulci) #------------------------------------------------------------------------- # Create fundi by segmenting fold fundi with overlapping sulcus labels: #------------------------------------------------------------------------- indices = [i for i,x in enumerate(fundus_per_fold) if x != -1] if indices and np.size(sulci): fundus_per_sulcus = -1 * np.ones(len(sulci)) fundus_per_sulcus[indices] = sulci[indices] n_fundi = len([x for x in np.unique(fundus_per_sulcus) if x != -1]) else: fundus_per_sulcus = [] n_fundi = 0 if n_fundi == 1: sdum = 'sulcus fundus' else: sdum = 'sulcus fundi' print(' Segmented {0} {1}'.format(n_fundi, sdum)) #------------------------------------------------------------------------- # Return fundi, number of fundi, and file name: #------------------------------------------------------------------------- fundus_per_sulcus_file = None if n_fundi > 0: fundus_per_sulcus = [int(x) for x in fundus_per_sulcus] if save_file and os.path.exists(vtk_file): fundus_per_sulcus_file = os.path.join(os.getcwd(), 'fundus_per_sulcus.vtk') rewrite_scalars(vtk_file, fundus_per_sulcus_file, fundus_per_sulcus, 'fundus_per_sulcus', fundus_per_sulcus) if not os.path.exists(fundus_per_sulcus_file): raise(IOError(fundus_per_sulcus_file + " not found")) return fundus_per_sulcus, n_fundi, fundus_per_sulcus_file
def extract_fundi(folds, curv_file, depth_file, min_separation=10, erode_ratio=0.1, erode_min_size=1, save_file=False): """ Extract fundi from folds. A fundus is a branching curve that runs along the deepest and most highly curved portions of a fold. Steps :: 1. Find fundus endpoints (outer anchors) with find_outer_anchors(). 2. Include inner anchor points. 3. Connect anchor points using connect_points_erosion(); inner anchors are removed if they result in endpoints. 4. Optionally smooth with smooth_skeleton(). Parameters ---------- folds : numpy array or list of integers fold number for each vertex curv_file : string surface mesh file in VTK format with mean curvature values depth_file : string surface mesh file in VTK format with rescaled depth values likelihoods : list of integers fundus likelihood value for each vertex min_separation : integer minimum number of edges between inner/outer anchor points erode_ratio : float fraction of indices to test for removal at each iteration in connect_points_erosion() save_file : Boolean save output VTK file? Returns ------- fundus_per_fold : list of integers fundus numbers for all vertices, labeled by fold (-1 for non-fundus vertices) n_fundi_in_folds : integer number of fundi fundus_per_fold_file : string (if save_file) output VTK file with fundus numbers (-1 for non-fundus vertices) Examples -------- >>> # Extract fundus from one or more folds: >>> single_fold = True >>> import os >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.features.fundi import extract_fundi >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> curv_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, True, True) >>> if single_fold: >>> fold_number = 2 #11 >>> folds[folds != fold_number] = -1 >>> min_separation = 10 >>> erode_ratio = 0.10 >>> erode_min_size = 10 >>> save_file = True >>> o1, o2, fundus_per_fold_file = extract_fundi(folds, curv_file, ... depth_file, min_separation, erode_ratio, erode_min_size, save_file) >>> # >>> # View: >>> plot_surfaces(fundi_file) """ # Extract a skeleton to connect endpoints in a fold: import os import numpy as np from time import time from mindboggle.utils.io_vtk import read_scalars, read_vtk, rewrite_scalars from mindboggle.utils.compute import median_abs_dev from mindboggle.utils.paths import find_max_values from mindboggle.utils.mesh import find_neighbors_from_file, find_complete_faces from mindboggle.utils.paths import find_outer_anchors, connect_points_erosion if isinstance(folds, list): folds = np.array(folds) # Load values, inner anchor threshold, and neighbors: faces, u1,u2, points, npoints, curvs, u3,u4 = read_vtk(curv_file, True,True) depths, name = read_scalars(depth_file, True, True) values = curvs * depths values0 = [x for x in values if x > 0] thr = np.median(values0) + 2 * median_abs_dev(values0) neighbor_lists = find_neighbors_from_file(curv_file) #------------------------------------------------------------------------- # Loop through folds: #------------------------------------------------------------------------- t1 = time() skeletons = [] unique_fold_IDs = [x for x in np.unique(folds) if x != -1] if len(unique_fold_IDs) == 1: print("Extract a fundus from 1 fold...") else: print("Extract a fundus from each of {0} folds...". format(len(unique_fold_IDs))) for fold_ID in unique_fold_IDs: indices_fold = [i for i,x in enumerate(folds) if x == fold_ID] if indices_fold: print(' Fold {0}:'.format(int(fold_ID))) #----------------------------------------------------------------- # Find outer anchor points on the boundary of the surface region, # to serve as fundus endpoints: #----------------------------------------------------------------- outer_anchors, tracks = find_outer_anchors(indices_fold, neighbor_lists, values, depths, min_separation) #----------------------------------------------------------------- # Find inner anchor points: #----------------------------------------------------------------- inner_anchors = find_max_values(points, values, min_separation, thr) #----------------------------------------------------------------- # Connect anchor points to create skeleton: #----------------------------------------------------------------- B = -1 * np.ones(npoints) B[indices_fold] = 1 skeleton = connect_points_erosion(B, neighbor_lists, outer_anchors, inner_anchors, values, erode_ratio, erode_min_size, save_steps=[], save_vtk='') if skeleton: skeletons.extend(skeleton) #----------------------------------------------------------------- # Remove fundus vertices if they complete triangle faces: #----------------------------------------------------------------- Iremove = find_complete_faces(skeletons, faces) if Iremove: skeletons = list(frozenset(skeletons).difference(Iremove)) indices = [x for x in skeletons if folds[x] != -1] fundus_per_fold = -1 * np.ones(npoints) fundus_per_fold[indices] = folds[indices] n_fundi_in_folds = len([x for x in np.unique(fundus_per_fold) if x != -1]) if n_fundi_in_folds == 1: sdum = 'fold fundus' else: sdum = 'fold fundi' print(' ...Extracted {0} {1}; {2} total ({3:.2f} seconds)'. format(n_fundi_in_folds, sdum, n_fundi_in_folds, time() - t1)) #------------------------------------------------------------------------- # Return fundi, number of fundi, and file name: #------------------------------------------------------------------------- if n_fundi_in_folds > 0: fundus_per_fold = [int(x) for x in fundus_per_fold] if save_file: fundus_per_fold_file = os.path.join(os.getcwd(), 'fundus_per_fold.vtk') rewrite_scalars(curv_file, fundus_per_fold_file, fundus_per_fold, 'fundi', folds) if not os.path.exists(fundus_per_fold_file): raise(IOError(fundus_per_fold_file + " not found")) else: fundus_per_fold_file = None return fundus_per_fold, n_fundi_in_folds, fundus_per_fold_file
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 rescale_by_neighborhood(input_vtk, indices=[], nedges=10, p=99, set_max_to_1=True, save_file=False, output_filestring='rescaled_scalars', background_value=-1): """ Rescale the scalar values of a VTK file by a percentile value in each vertex's surface mesh neighborhood. Parameters ---------- input_vtk : string name of VTK file with a scalar value for each vertex indices : list of integers (optional) indices of scalars to normalize nedges : integer number or edges from vertex, defining the size of its neighborhood p : float in range of [0,100] percentile used to normalize each scalar set_max_to_1 : Boolean set all rescaled values greater than 1 to 1.0? save_file : Boolean save output VTK file? output_filestring : string (if save_file) name of output file background_value : integer background value Returns ------- rescaled_scalars : list of floats rescaled scalar values rescaled_scalars_file : string (if save_file) name of output VTK file with rescaled scalar values Examples -------- >>> import os >>> from mindboggle.utils.mesh import rescale_by_neighborhood >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> input_vtk = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> indices = [] >>> nedges = 10 >>> p = 99 >>> set_max_to_1 = True >>> save_file = True >>> output_filestring = 'rescaled_scalars' >>> background_value = -1 >>> # >>> rescaled_scalars, rescaled_scalars_file = rescale_by_neighborhood(input_vtk, >>> indices, nedges, p, set_max_to_1, save_file, output_filestring, background_value) >>> # >>> # View rescaled scalar values per fold: >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> # >>> rewrite_scalars(rescaled_scalars_file, rescaled_scalars_file, >>> rescaled_scalars, 'rescaled_depths', folds) >>> plot_surfaces(rescaled_scalars_file) """ 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, find_neighborhood # Load scalars and vertex neighbor lists: scalars, name = read_scalars(input_vtk, True, True) if not indices: indices = [i for i, x in enumerate(scalars) if x != background_value] print(" Rescaling {0} scalar values by neighborhood...".format( len(indices))) neighbor_lists = find_neighbors_from_file(input_vtk) # Loop through vertices: rescaled_scalars = scalars.copy() for index in indices: # Determine the scalars in the vertex's neighborhood: neighborhood = find_neighborhood(neighbor_lists, [index], nedges) # Compute a high neighborhood percentile to normalize vertex's value: normalization_factor = np.percentile(scalars[neighborhood], p) rescaled_scalar = scalars[index] / normalization_factor rescaled_scalars[index] = rescaled_scalar # Make any rescaled value greater than 1 equal to 1: if set_max_to_1: rescaled_scalars[[x for x in indices if rescaled_scalars[x] > 1.0]] = 1 rescaled_scalars = rescaled_scalars.tolist() #------------------------------------------------------------------------- # Return rescaled scalars and file name #------------------------------------------------------------------------- if save_file: rescaled_scalars_file = os.path.join(os.getcwd(), output_filestring + '.vtk') rewrite_scalars(input_vtk, rescaled_scalars_file, rescaled_scalars, 'rescaled_scalars') if not os.path.exists(rescaled_scalars_file): raise (IOError(rescaled_scalars_file + " not found")) else: rescaled_scalars_file = None return rescaled_scalars, rescaled_scalars_file
def extract_fundi(folds, curv_file, depth_file, min_separation=10, erode_ratio=0.1, erode_min_size=1, save_file=False): """ Extract fundi from folds. A fundus is a branching curve that runs along the deepest and most highly curved portions of a fold. Steps :: 1. Find fundus endpoints (outer anchors) with find_outer_anchors(). 2. Include inner anchor points. 3. Connect anchor points using connect_points_erosion(); inner anchors are removed if they result in endpoints. Parameters ---------- folds : numpy array or list of integers fold number for each vertex curv_file : string surface mesh file in VTK format with mean curvature values depth_file : string surface mesh file in VTK format with rescaled depth values likelihoods : list of integers fundus likelihood value for each vertex min_separation : integer minimum number of edges between inner/outer anchor points erode_ratio : float fraction of indices to test for removal at each iteration in connect_points_erosion() save_file : Boolean save output VTK file? Returns ------- fundus_per_fold : list of integers fundus numbers for all vertices, labeled by fold (-1 for non-fundus vertices) n_fundi_in_folds : integer number of fundi fundus_per_fold_file : string (if save_file) output VTK file with fundus numbers (-1 for non-fundus vertices) Examples -------- >>> # Extract fundus from one or more folds: >>> single_fold = True >>> import os >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.features.fundi import extract_fundi >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> curv_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, True, True) >>> if single_fold: >>> fold_number = 2 #11 >>> folds[folds != fold_number] = -1 >>> min_separation = 10 >>> erode_ratio = 0.10 >>> erode_min_size = 10 >>> save_file = True >>> o1, o2, fundus_per_fold_file = extract_fundi(folds, curv_file, ... depth_file, min_separation, erode_ratio, erode_min_size, save_file) >>> # >>> # View: >>> plot_surfaces(fundi_file) """ # Extract a skeleton to connect endpoints in a fold: import os import numpy as np from time import time from mindboggle.utils.io_vtk import read_scalars, read_vtk, rewrite_scalars from mindboggle.utils.compute import median_abs_dev from mindboggle.utils.paths import find_max_values from mindboggle.utils.mesh import find_neighbors_from_file, find_complete_faces from mindboggle.utils.paths import find_outer_anchors, connect_points_erosion if isinstance(folds, list): folds = np.array(folds) # Load values, inner anchor threshold, and neighbors: faces, u1, u2, points, npoints, curvs, u3, u4 = read_vtk( curv_file, True, True) depths, name = read_scalars(depth_file, True, True) values = curvs * depths values0 = [x for x in values if x > 0] thr = np.median(values0) + 2 * median_abs_dev(values0) neighbor_lists = find_neighbors_from_file(curv_file) #------------------------------------------------------------------------- # Loop through folds: #------------------------------------------------------------------------- t1 = time() skeletons = [] unique_fold_IDs = [x for x in np.unique(folds) if x != -1] if len(unique_fold_IDs) == 1: print("Extract a fundus from 1 fold...") else: print("Extract a fundus from each of {0} folds...".format( len(unique_fold_IDs))) for fold_ID in unique_fold_IDs: indices_fold = [i for i, x in enumerate(folds) if x == fold_ID] if indices_fold: print(' Fold {0}:'.format(int(fold_ID))) #----------------------------------------------------------------- # Find outer anchor points on the boundary of the surface region, # to serve as fundus endpoints: #----------------------------------------------------------------- outer_anchors, tracks = find_outer_anchors(indices_fold, neighbor_lists, values, depths, min_separation) #----------------------------------------------------------------- # Find inner anchor points: #----------------------------------------------------------------- inner_anchors = find_max_values(points, values, min_separation, thr) #----------------------------------------------------------------- # Connect anchor points to create skeleton: #----------------------------------------------------------------- B = -1 * np.ones(npoints) B[indices_fold] = 1 skeleton = connect_points_erosion(B, neighbor_lists, outer_anchors, inner_anchors, values, erode_ratio, erode_min_size, save_steps=[], save_vtk='') if skeleton: skeletons.extend(skeleton) #----------------------------------------------------------------- # Remove fundus vertices if they complete triangle faces: #----------------------------------------------------------------- Iremove = find_complete_faces(skeletons, faces) if Iremove: skeletons = list(frozenset(skeletons).difference(Iremove)) indices = [x for x in skeletons if folds[x] != -1] fundus_per_fold = -1 * np.ones(npoints) fundus_per_fold[indices] = folds[indices] n_fundi_in_folds = len([x for x in np.unique(fundus_per_fold) if x != -1]) if n_fundi_in_folds == 1: sdum = 'fold fundus' else: sdum = 'fold fundi' print(' ...Extracted {0} {1}; {2} total ({3:.2f} seconds)'.format( n_fundi_in_folds, sdum, n_fundi_in_folds, time() - t1)) #------------------------------------------------------------------------- # Return fundi, number of fundi, and file name: #------------------------------------------------------------------------- if n_fundi_in_folds > 0: fundus_per_fold = [int(x) for x in fundus_per_fold] if save_file: fundus_per_fold_file = os.path.join(os.getcwd(), 'fundus_per_fold.vtk') rewrite_scalars(curv_file, fundus_per_fold_file, fundus_per_fold, 'fundi', folds) if not os.path.exists(fundus_per_fold_file): raise (IOError(fundus_per_fold_file + " not found")) else: fundus_per_fold_file = None return fundus_per_fold, n_fundi_in_folds, fundus_per_fold_file
def rescale_by_label(input_vtk, labels_or_file, save_file=False, output_filestring='rescaled_scalars'): """ Rescale scalars for each label (such as depth values within each fold). Default is to normalize the scalar values of a VTK file by a percentile value in each vertex's surface mesh for each label. Parameters ---------- input_vtk : string name of VTK file with a scalar value for each vertex labels_or_file : list or string label number for each vertex or name of VTK file with index scalars save_file : Boolean save output VTK file? output_filestring : string (if save_file) name of output file Returns ------- rescaled_scalars : list of floats scalar values rescaled for each label, for label numbers not equal to -1 rescaled_scalars_file : string (if save_file) name of output VTK file with rescaled scalar values for each label Examples -------- >>> # Rescale depths by neighborhood within each label: >>> import os >>> from mindboggle.utils.mesh import rescale_by_label >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> input_vtk = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> labels_or_file = os.path.join(path, 'arno', 'features', 'subfolds.vtk') >>> save_file = True >>> output_filestring = 'rescaled_scalars' >>> # >>> rescaled_scalars, rescaled_scalars_file = rescale_by_label(input_vtk, >>> labels_or_file, save_file, output_filestring) >>> # >>> # View rescaled scalar values per fold: >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> # >>> rewrite_scalars(rescaled_scalars_file, rescaled_scalars_file, >>> rescaled_scalars, 'rescaled_depths', folds) >>> plot_surfaces(rescaled_scalars_file) """ import os import numpy as np from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars # Load scalars and vertex neighbor lists: scalars, name = read_scalars(input_vtk, True, True) print(" Rescaling scalar values within each label...") # Load label numbers: if isinstance(labels_or_file, str): labels, name = read_scalars(labels_or_file, True, True) elif isinstance(labels_or_file, list): labels = labels_or_file unique_labels = np.unique(labels) unique_labels = [x for x in unique_labels if x >= 0] # Loop through labels: for label in unique_labels: #print(" Rescaling scalar values within label {0} of {1} labels...".format( # int(label), len(unique_labels))) indices = [i for i,x in enumerate(labels) if x == label] if indices: # Rescale by the maximum label scalar value: scalars[indices] = scalars[indices] / np.max(scalars[indices]) rescaled_scalars = scalars.tolist() #------------------------------------------------------------------------- # Return rescaled scalars and file name #------------------------------------------------------------------------- if save_file: rescaled_scalars_file = os.path.join(os.getcwd(), output_filestring + '.vtk') rewrite_scalars(input_vtk, rescaled_scalars_file, rescaled_scalars, 'rescaled_scalars', labels) if not os.path.exists(rescaled_scalars_file): raise(IOError(rescaled_scalars_file + " not found")) else: rescaled_scalars_file = None return rescaled_scalars, rescaled_scalars_file
def plot_mask_surface(vtk_file, mask_file='', nonmask_value=-1, masked_output='', remove_nonmask=False, program='vtkviewer', use_colormap=False, colormap_file=''): """ Use vtkviewer or mayavi2 to visualize VTK surface mesh data. If a mask_file is provided, a temporary masked file is saved, and it is this file that is viewed. If using vtkviewer, can optionally provide colormap file or set $COLORMAP environment variable. Parameters ---------- vtk_file : string name of VTK surface mesh file mask_file : string name of VTK surface mesh file to mask vtk_file vertices nonmask_value : integer nonmask (usually background) value masked_output : string temporary masked output file name remove_nonmask : Boolean remove vertices that are not in mask? (otherwise assign nonmask_value) program : string {'vtkviewer', 'mayavi2'} program to visualize VTK file use_colormap : Boolean use Paraview-style XML colormap file set by $COLORMAP env variable? colormap_file : string use colormap in given file if use_colormap==True? if empty and use_colormap==True, use file set by $COLORMAP environment variable Examples -------- >>> import os >>> from mindboggle.utils.plots import plot_mask_surface >>> path = os.environ['MINDBOGGLE_DATA'] >>> vtk_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT31.manual.vtk') >>> mask_file = os.path.join(path, 'test_one_label.vtk') >>> nonmask_value = 0 #-1 >>> masked_output = '' >>> remove_nonmask = True >>> program = 'vtkviewer' >>> use_colormap = True >>> colormap_file = '' #'/software/mindboggle_tools/colormap.xml' >>> plot_mask_surface(vtk_file, mask_file, nonmask_value, masked_output, remove_nonmask, program, use_colormap, colormap_file) """ import os import numpy as np from mindboggle.utils.mesh import remove_faces, reindex_faces_points from mindboggle.utils.utils import execute from mindboggle.utils.plots import plot_surfaces from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars, \ read_vtk, write_vtk #------------------------------------------------------------------------- # Filter mesh with non-background values from a second (same-size) mesh: #------------------------------------------------------------------------- if mask_file: mask, name = read_scalars(mask_file, True, True) if not masked_output: masked_output = os.path.join(os.getcwd(), 'temp.vtk') file_to_plot = masked_output #--------------------------------------------------------------------- # Remove nonmask-valued vertices: #--------------------------------------------------------------------- if remove_nonmask: #----------------------------------------------------------------- # Load VTK files: #----------------------------------------------------------------- faces, lines, indices, points, npoints, scalars, scalar_names, \ o1 = read_vtk(vtk_file, True, True) #----------------------------------------------------------------- # Find mask indices, remove nonmask faces, and reindex: #----------------------------------------------------------------- Imask = [i for i,x in enumerate(mask) if x != nonmask_value] mask_faces = remove_faces(faces, Imask) mask_faces, points, \ original_indices = reindex_faces_points(mask_faces, points) #----------------------------------------------------------------- # Write VTK file with scalar values: #----------------------------------------------------------------- if np.ndim(scalars) == 1: scalar_type = type(scalars[0]).__name__ elif np.ndim(scalars) == 2: scalar_type = type(scalars[0][0]).__name__ else: print("Undefined scalar type!") write_vtk(file_to_plot, points, [], [], mask_faces, scalars[original_indices].tolist(), scalar_names, scalar_type=scalar_type) else: scalars, name = read_scalars(vtk_file, True, True) scalars[mask == nonmask_value] = nonmask_value rewrite_scalars(vtk_file, file_to_plot, scalars) else: file_to_plot = vtk_file #------------------------------------------------------------------------- # Display with vtkviewer.py: #------------------------------------------------------------------------- if program == 'vtkviewer': plot_surfaces(file_to_plot, use_colormap=use_colormap, colormap_file=colormap_file) #------------------------------------------------------------------------- # Display with mayavi2: #------------------------------------------------------------------------- elif program == 'mayavi2': cmd = ["mayavi2", "-d", file_to_plot, "-m", "Surface", "&"] execute(cmd, 'os')
def rescale_by_neighborhood(input_vtk, indices=[], nedges=10, p=99, set_max_to_1=True, save_file=False, output_filestring='rescaled_scalars', background_value=-1): """ Rescale the scalar values of a VTK file by a percentile value in each vertex's surface mesh neighborhood. Parameters ---------- input_vtk : string name of VTK file with a scalar value for each vertex indices : list of integers (optional) indices of scalars to normalize nedges : integer number or edges from vertex, defining the size of its neighborhood p : float in range of [0,100] percentile used to normalize each scalar set_max_to_1 : Boolean set all rescaled values greater than 1 to 1.0? save_file : Boolean save output VTK file? output_filestring : string (if save_file) name of output file background_value : integer background value Returns ------- rescaled_scalars : list of floats rescaled scalar values rescaled_scalars_file : string (if save_file) name of output VTK file with rescaled scalar values Examples -------- >>> import os >>> from mindboggle.utils.mesh import rescale_by_neighborhood >>> from mindboggle.utils.io_vtk import read_scalars, rewrite_scalars >>> from mindboggle.utils.plots import plot_surfaces >>> path = os.environ['MINDBOGGLE_DATA'] >>> input_vtk = os.path.join(path, 'arno', 'shapes', 'lh.pial.travel_depth.vtk') >>> indices = [] >>> nedges = 10 >>> p = 99 >>> set_max_to_1 = True >>> save_file = True >>> output_filestring = 'rescaled_scalars' >>> background_value = -1 >>> # >>> rescaled_scalars, rescaled_scalars_file = rescale_by_neighborhood(input_vtk, >>> indices, nedges, p, set_max_to_1, save_file, output_filestring, background_value) >>> # >>> # View rescaled scalar values per fold: >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file) >>> # >>> rewrite_scalars(rescaled_scalars_file, rescaled_scalars_file, >>> rescaled_scalars, 'rescaled_depths', folds) >>> plot_surfaces(rescaled_scalars_file) """ 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, find_neighborhood # Load scalars and vertex neighbor lists: scalars, name = read_scalars(input_vtk, True, True) if not indices: indices = [i for i,x in enumerate(scalars) if x != background_value] print(" Rescaling {0} scalar values by neighborhood...".format(len(indices))) neighbor_lists = find_neighbors_from_file(input_vtk) # Loop through vertices: rescaled_scalars = scalars.copy() for index in indices: # Determine the scalars in the vertex's neighborhood: neighborhood = find_neighborhood(neighbor_lists, [index], nedges) # Compute a high neighborhood percentile to normalize vertex's value: normalization_factor = np.percentile(scalars[neighborhood], p) rescaled_scalar = scalars[index] / normalization_factor rescaled_scalars[index] = rescaled_scalar # Make any rescaled value greater than 1 equal to 1: if set_max_to_1: rescaled_scalars[[x for x in indices if rescaled_scalars[x] > 1.0]] = 1 rescaled_scalars = rescaled_scalars.tolist() #------------------------------------------------------------------------- # Return rescaled scalars and file name #------------------------------------------------------------------------- if save_file: rescaled_scalars_file = os.path.join(os.getcwd(), output_filestring + '.vtk') rewrite_scalars(input_vtk, rescaled_scalars_file, rescaled_scalars, 'rescaled_scalars') if not os.path.exists(rescaled_scalars_file): raise(IOError(rescaled_scalars_file + " not found")) else: rescaled_scalars_file = None return rescaled_scalars, rescaled_scalars_file
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 extract_fundi(folds, sulci, curv_file, depth_file, min_separation=10, erode_ratio=0.1, erode_min_size=1, save_file=False): """ Extract fundi from folds. A fundus is a branching curve that runs along the deepest and most highly curved portions of a sulcus fold. Steps :: 1. Find fundus endpoints (outer anchors) with find_outer_anchors(). 2. Include inner anchor points. 3. Connect anchor points using connect_points_erosion(). 4. Segment fundi by sulcus definitions. Possible postprocessing step: smooth with smooth_skeleton(). Parameters ---------- folds : list of integers fold number for each vertex curv_file : string surface mesh file in VTK format with mean curvature values depth_file : string surface mesh file in VTK format with rescaled depth values sulci : list of integers sulcus number for each vertex likelihoods : list of integers fundus likelihood value for each vertex min_separation : integer minimum number of edges between inner/outer anchor points erode_ratio : float fraction of indices to test for removal at each iteration in connect_points_erosion() save_file : Boolean save output VTK file? Returns ------- fundi : list of integers fundus numbers for all vertices (-1 for non-fundus vertices) n_fundi : integer number of fundi fundi_file : string (if save_file) name of output VTK file with fundus numbers (-1 for non-fundus vertices) Examples -------- >>> # Extract fundus from one or more folds: >>> single_fold = True >>> import os >>> from mindboggle.utils.io_vtk import read_scalars >>> from mindboggle.features.fundi import extract_fundi >>> from mindboggle.utils.plots import plot_vtk >>> path = os.environ['MINDBOGGLE_DATA'] >>> sulci_file = os.path.join(path, 'arno', 'features', 'sulci.vtk') >>> sulci, name = read_scalars(sulci_file, True, True) >>> curv_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.mean_curvature.vtk') >>> depth_file = os.path.join(path, 'arno', 'shapes', 'travel_depth_rescaled.vtk') >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk') >>> folds, name = read_scalars(folds_file, True, True) >>> if single_fold: >>> fold_number = 2 #11 >>> folds[folds != fold_number] = -1 >>> min_separation = 10 >>> erode_ratio = 0.10 >>> erode_min_size = 10 >>> save_file = True >>> fundi, n_fundi, fundi_file = extract_fundi(folds, sulci, curv_file, >>> depth_file, min_separation, erode_ratio, erode_min_size, save_file) >>> # >>> # View: >>> plot_vtk(fundi_file) """ # Extract a skeleton to connect endpoints in a fold: import os import numpy as np from time import time from mindboggle.utils.io_vtk import read_scalars, read_vtk, rewrite_scalars from mindboggle.utils.compute import median_abs_dev from mindboggle.utils.paths import find_max_values from mindboggle.utils.mesh import find_neighbors_from_file from mindboggle.utils.paths import find_outer_anchors, connect_points_erosion # Load values, threshold, and neighbors: u1,u2,u3, points, npoints, curvs, u4,u5 = read_vtk(curv_file, True,True) depths, name = read_scalars(depth_file, True, True) values = curvs * depths values0 = [x for x in values if x > 0] thr = np.median(values0) + 2 * median_abs_dev(values0) neighbor_lists = find_neighbors_from_file(curv_file) #------------------------------------------------------------------------- # Loop through folds: #------------------------------------------------------------------------- t1 = time() skeletons = [] unique_fold_IDs = [x for x in np.unique(folds) if x != -1] if len(unique_fold_IDs) == 1: print("Extract a fundus from 1 fold...") else: print("Extract a fundus from each of {0} folds...". format(len(unique_fold_IDs))) for fold_ID in unique_fold_IDs: indices_fold = [i for i,x in enumerate(folds) if x == fold_ID] if indices_fold: print(' Fold {0}:'.format(int(fold_ID))) #----------------------------------------------------------------- # Find outer anchor points on the boundary of the surface region, # to serve as fundus endpoints : #----------------------------------------------------------------- outer_anchors, tracks = find_outer_anchors(indices_fold, neighbor_lists, values, depths, min_separation) #----------------------------------------------------------------- # Find inner anchor points: #----------------------------------------------------------------- inner_anchors = find_max_values(points, values, min_separation, thr) #----------------------------------------------------------------- # Connect endpoints to create skeleton: #----------------------------------------------------------------- B = -1 * np.ones(npoints) B[indices_fold] = 1 skeleton = connect_points_erosion(B, neighbor_lists, outer_anchors, inner_anchors, values, erode_ratio, erode_min_size, save_steps=[], save_vtk='') if skeleton: skeletons.extend(skeleton) #------------------------------------------------------------------------- # Create fundi by segmenting skeletons with overlapping sulcus labels: #------------------------------------------------------------------------- fundi = -1 * np.ones(npoints) indices = [x for x in skeletons if sulci[x] != -1] fundi[indices] = sulci[indices] n_fundi = len([x for x in np.unique(fundi) if x != -1]) if n_fundi == 1: sdum = 'fundus' else: sdum = 'fundi' print(' ...Extracted {0} {1} ({2:.2f} seconds)'. format(n_fundi, sdum, time() - t1)) #------------------------------------------------------------------------- # Return fundi, number of fundi, and file name: #------------------------------------------------------------------------- fundi = fundi.tolist() if save_file: fundi_file = os.path.join(os.getcwd(), 'fundi.vtk') rewrite_scalars(curv_file, fundi_file, fundi, 'fundi', folds) else: fundi_file = None return fundi, n_fundi, fundi_file
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