def spectrum_of_largest(points, faces, spectrum_size=10, exclude_labels=[-1], normalization="areaindex", areas=None, verbose=False): """ Compute Laplace-Beltrami spectrum on largest connected segment. In case a surface patch is fragmented, we select the largest fragment, remove extraneous triangular faces, and reindex indices. Parameters ---------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh spectrum_size : integer number of eigenvalues to be computed (the length of the spectrum) exclude_labels : list of integers background values to exclude normalization : string the method used to normalize eigenvalues if None, no normalization is used if "area", use area of the 2D structure as in Reuter et al. 2006 if "index", divide eigenvalue by index to account for linear trend if "areaindex", do both (default) areas : numpy array or list of floats (or None) surface area scalar values for all vertices verbose : bool print statements? Returns ------- spectrum : list first spectrum_size eigenvalues for Laplace-Beltrami spectrum Examples -------- >>> # Spectrum for left postcentral + pars triangularis pial surfaces: >>> import numpy as np >>> from mindboggle.mio.vtks import read_scalars, read_vtk, write_vtk >>> from mindboggle.guts.mesh import keep_faces, reindex_faces_points >>> from mindboggle.shapes.laplace_beltrami import spectrum_of_largest >>> from mindboggle.mio.fetch_data import prep_tests >>> urls, fetch_data = prep_tests() >>> label_file = fetch_data(urls['left_freesurfer_labels'], '', '.vtk') >>> area_file = fetch_data(urls['left_area'], '', '.vtk') >>> spectrum_size = 6 >>> exclude_labels = [-1] >>> normalization = None >>> points, indices, lines, faces, labels, f1, npoints, f2 = read_vtk(label_file, ... return_first=True, return_array=True) >>> I20 = [i for i,x in enumerate(labels) if x==1020] # pars triangularis >>> I22 = [i for i,x in enumerate(labels) if x==1022] # postcentral >>> I22.extend(I20) >>> faces = keep_faces(faces, I22) >>> faces, points, o1 = reindex_faces_points(faces, points) >>> areas, u1 = read_scalars(area_file, True, True) >>> verbose = False >>> spectrum = spectrum_of_largest(points, faces, spectrum_size, ... exclude_labels, normalization, areas, verbose) >>> [np.float("{0:.{1}f}".format(x, 5)) for x in spectrum[1::]] [0.00057, 0.00189, 0.00432, 0.00691, 0.00775] View both segments (skip test): >>> from mindboggle.mio.plots import plot_surfaces >>> scalars = np.zeros(np.shape(labels)) >>> scalars[I22] = 1 >>> vtk_file = 'test_two_labels.vtk' >>> write_vtk(vtk_file, points, indices, lines, faces, ... scalars, scalar_names='scalars', scalar_type='int') >>> plot_surfaces(vtk_file) # doctest: +SKIP """ import numpy as np #from scipy.sparse.linalg import eigsh, lobpcg from mindboggle.guts.segment import select_largest from mindboggle.shapes.laplace_beltrami import fem_laplacian if isinstance(areas, list): areas = np.array(areas) # Check to see if there are enough points: min_points_faces = spectrum_size npoints = len(points) if npoints < min_points_faces or len(faces) < min_points_faces: raise IOError("The input size {0} ({1} faces) should be much larger " "than spectrum_size ({2})".format( npoints, len(faces), spectrum_size)) return None else: # -------------------------------------------------------------------- # Select the largest segment (connected set of indices): # -------------------------------------------------------------------- points, faces = select_largest(points, faces, exclude_labels, areas, reindex=True) # Alert if the number of indices is small: if len(points) < min_points_faces: raise IOError("The input size {0} is too small.".format( len(points))) return None elif faces: # ---------------------------------------------------------------- # Compute spectrum: # ---------------------------------------------------------------- spectrum = fem_laplacian(points, faces, spectrum_size, normalization, verbose) return spectrum else: return None
def spectrum_of_largest(points, faces, spectrum_size=10, exclude_labels=[-1], normalization=None, areas=None, verbose=False): """ Compute Laplace-Beltrami spectrum on largest connected segment. In case a surface patch is fragmented, we select the largest fragment, remove extraneous triangular faces, and reindex indices. Parameters ---------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh spectrum_size : integer number of eigenvalues to be computed (the length of the spectrum) exclude_labels : list of integers background values to exclude normalization : string the method used to normalize eigenvalues ('area' or None) if "area", use area of the 2D structure as in Reuter et al. 2006 areas : numpy array or list of floats (or None) surface area scalar values for all vertices verbose : bool print statements? Returns ------- spectrum : list first spectrum_size eigenvalues for Laplace-Beltrami spectrum Examples -------- >>> # Spectrum for left postcentral + pars triangularis pial surfaces: >>> import numpy as np >>> from mindboggle.mio.vtks import read_scalars, read_vtk, write_vtk >>> from mindboggle.guts.mesh import keep_faces, reindex_faces_points >>> from mindboggle.shapes.laplace_beltrami import spectrum_of_largest >>> from mindboggle.mio.fetch_data import prep_tests >>> urls, fetch_data = prep_tests() >>> label_file = fetch_data(urls['left_freesurfer_labels'], '', '.vtk') >>> area_file = fetch_data(urls['left_area'], '', '.vtk') >>> spectrum_size = 6 >>> exclude_labels = [-1] >>> normalization = None >>> points, indices, lines, faces, labels, f1, npoints, f2 = read_vtk(label_file, ... return_first=True, return_array=True) >>> I20 = [i for i,x in enumerate(labels) if x==1020] # pars triangularis >>> I22 = [i for i,x in enumerate(labels) if x==1022] # postcentral >>> I22.extend(I20) >>> faces = keep_faces(faces, I22) >>> faces, points, o1 = reindex_faces_points(faces, points) >>> areas, u1 = read_scalars(area_file, True, True) >>> verbose = False >>> spectrum = spectrum_of_largest(points, faces, spectrum_size, ... exclude_labels, normalization, areas, verbose) >>> print(np.array_str(np.array(spectrum[1::]), ... precision=5, suppress_small=True)) [ 0.00057 0.00189 0.00432 0.00691 0.00775] View both segments (skip test): >>> from mindboggle.mio.plots import plot_surfaces >>> scalars = np.zeros(np.shape(labels)) >>> scalars[I22] = 1 >>> vtk_file = 'test_two_labels.vtk' >>> write_vtk(vtk_file, points, indices, lines, faces, ... scalars, scalar_names='scalars', scalar_type='int') >>> plot_surfaces(vtk_file) # doctest: +SKIP """ import numpy as np #from scipy.sparse.linalg import eigsh, lobpcg from mindboggle.guts.segment import select_largest from mindboggle.shapes.laplace_beltrami import fem_laplacian if isinstance(areas, list): areas = np.array(areas) # Check to see if there are enough points: min_points_faces = spectrum_size npoints = len(points) if npoints < min_points_faces or len(faces) < min_points_faces: raise IOError("The input size {0} ({1} faces) should be much larger " "than spectrum_size ({2})". format(npoints, len(faces), spectrum_size)) return None else: # -------------------------------------------------------------------- # Select the largest segment (connected set of indices): # -------------------------------------------------------------------- points, faces = select_largest(points, faces, exclude_labels, areas, reindex=True) # Alert if the number of indices is small: if len(points) < min_points_faces: raise IOError("The input size {0} is too small.". format(len(points))) return None elif faces: # ---------------------------------------------------------------- # Compute spectrum: # ---------------------------------------------------------------- spectrum = fem_laplacian(points, faces, spectrum_size, normalization, verbose) return spectrum else: return None
def spectrum_of_largest(points, faces, spectrum_size=10, exclude_labels=[-1], normalization=None, areas=None): """ Compute Laplace-Beltrami spectrum on largest connected segment. In case a surface patch is fragmented, we select the largest fragment, remove extraneous triangular faces, and reindex indices. Parameters ---------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh spectrum_size : integer number of eigenvalues to be computed (the length of the spectrum) exclude_labels : list of integers background values to exclude normalization : string the method used to normalize eigenvalues ('area' or None) if "area", use area of the 2D structure as in Reuter et al. 2006 areas : numpy array or list of floats (or None) surface area scalar values for all vertices Returns ------- spectrum : list first spectrum_size eigenvalues for Laplace-Beltrami spectrum Examples -------- >>> # Spectrum for left postcentral + pars triangularis pial surfaces: >>> import os >>> import numpy as np >>> from mindboggle.mio.vtks import read_scalars, read_vtk, write_vtk >>> from mindboggle.guts.mesh import remove_faces, reindex_faces_points >>> from mindboggle.shapes.laplace_beltrami import spectrum_of_largest >>> path = os.environ['MINDBOGGLE_DATA'] >>> label_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT31.manual.vtk') >>> area_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.area.vtk') >>> spectrum_size = 6 >>> exclude_labels = [-1] >>> normalization = None >>> faces, lines, indices, points, u1, labels, u2,u3 = read_vtk(label_file, >>> return_first=True, return_array=True) >>> I20 = [i for i,x in enumerate(labels) if x==20] # pars triangularis >>> I22 = [i for i,x in enumerate(labels) if x==22] # postcentral >>> I22.extend(I20) >>> faces = remove_faces(faces, I22) >>> faces, points, o1 = reindex_faces_points(faces, points) >>> areas, u1 = read_scalars(area_file, True, True) >>> # >>> spectrum_of_largest(points, faces, spectrum_size, exclude_labels, >>> normalization, areas) [6.3469513010430304e-18, 0.0005178862383467463, 0.0017434911095630772, 0.003667561767487686, 0.005429017880363784, 0.006309346984678924] >>> # View both segments: >>> from mindboggle.mio.plots import plot_surfaces >>> scalars = np.zeros(np.shape(labels)) >>> scalars[I22] = 1 >>> vtk_file = 'test_two_labels.vtk' >>> write_vtk(vtk_file, points, indices, lines, faces, >>> scalars, scalar_names='scalars', scalar_type='int') >>> plot_surfaces(vtk_file) """ from scipy.sparse.linalg import eigsh, lobpcg import numpy as np from mindboggle.guts.segment import select_largest from mindboggle.shapes.laplace_beltrami import fem_laplacian if isinstance(areas, list): areas = np.array(areas) # Check to see if there are enough points: min_points_faces = spectrum_size npoints = len(points) if npoints < min_points_faces or len(faces) < min_points_faces: print("The input size {0} ({1} faces) should be much larger " "than spectrum_size ({2})". format(npoints, len(faces), spectrum_size)) return None else: #--------------------------------------------------------------------- # Select the largest segment (connected set of indices): #--------------------------------------------------------------------- points, faces = select_largest(points, faces, exclude_labels, areas, reindex=True) # Alert if the number of indices is small: if len(points) < min_points_faces: print("The input size {0} is too small.".format(len(points))) return None elif faces: #----------------------------------------------------------------- # Compute spectrum: #----------------------------------------------------------------- spectrum = fem_laplacian(points, faces, spectrum_size, normalization) return spectrum else: return None
def spectrum_of_largest(points, faces, spectrum_size=10, exclude_labels=[-1], normalization=None, areas=None): """ Compute Laplace-Beltrami spectrum on largest connected segment. In case a surface patch is fragmented, we select the largest fragment, remove extraneous triangular faces, and reindex indices. Parameters ---------- points : list of lists of 3 floats x,y,z coordinates for each vertex of the structure faces : list of lists of 3 integers 3 indices to vertices that form a triangle on the mesh spectrum_size : integer number of eigenvalues to be computed (the length of the spectrum) exclude_labels : list of integers background values to exclude normalization : string the method used to normalize eigenvalues ('area' or None) if "area", use area of the 2D structure as in Reuter et al. 2006 areas : numpy array or list of floats (or None) surface area scalar values for all vertices Returns ------- spectrum : list first spectrum_size eigenvalues for Laplace-Beltrami spectrum Examples -------- >>> # Spectrum for left postcentral + pars triangularis pial surfaces: >>> import os >>> import numpy as np >>> from mindboggle.mio.vtks import read_scalars, read_vtk, write_vtk >>> from mindboggle.guts.mesh import remove_faces, reindex_faces_points >>> from mindboggle.shapes.laplace_beltrami import spectrum_of_largest >>> path = os.environ['MINDBOGGLE_DATA'] >>> label_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT31.manual.vtk') >>> area_file = os.path.join(path, 'arno', 'shapes', 'lh.pial.area.vtk') >>> spectrum_size = 6 >>> exclude_labels = [-1] >>> normalization = None >>> faces, lines, indices, points, u1, labels, u2,u3 = read_vtk(label_file, >>> return_first=True, return_array=True) >>> I20 = [i for i,x in enumerate(labels) if x==20] # pars triangularis >>> I22 = [i for i,x in enumerate(labels) if x==22] # postcentral >>> I22.extend(I20) >>> faces = remove_faces(faces, I22) >>> faces, points, o1 = reindex_faces_points(faces, points) >>> areas, u1 = read_scalars(area_file, True, True) >>> # >>> spectrum_of_largest(points, faces, spectrum_size, exclude_labels, >>> normalization, areas) [6.3469513010430304e-18, 0.0005178862383467463, 0.0017434911095630772, 0.003667561767487686, 0.005429017880363784, 0.006309346984678924] >>> # View both segments: >>> from mindboggle.mio.plots import plot_surfaces >>> scalars = np.zeros(np.shape(labels)) >>> scalars[I22] = 1 >>> vtk_file = 'test_two_labels.vtk' >>> write_vtk(vtk_file, points, indices, lines, faces, >>> scalars, scalar_names='scalars', scalar_type='int') >>> plot_surfaces(vtk_file) """ from scipy.sparse.linalg import eigsh, lobpcg import numpy as np from mindboggle.guts.segment import select_largest from mindboggle.shapes.laplace_beltrami import fem_laplacian if isinstance(areas, list): areas = np.array(areas) # Check to see if there are enough points: min_points_faces = spectrum_size npoints = len(points) if npoints < min_points_faces or len(faces) < min_points_faces: print("The input size {0} ({1} faces) should be much larger " "than spectrum_size ({2})".format(npoints, len(faces), spectrum_size)) return None else: #--------------------------------------------------------------------- # Select the largest segment (connected set of indices): #--------------------------------------------------------------------- points, faces = select_largest(points, faces, exclude_labels, areas, reindex=True) # Alert if the number of indices is small: if len(points) < min_points_faces: print("The input size {0} is too small.".format(len(points))) return None elif faces: #----------------------------------------------------------------- # Compute spectrum: #----------------------------------------------------------------- spectrum = fem_laplacian(points, faces, spectrum_size, normalization) return spectrum else: return None