spectralbrain.BrainMesh#
- class spectralbrain.BrainMesh(vertices, faces, metadata=None)[source]#
Bases:
objectTriangle surface mesh for brain structures.
Implements the
GeometricObjectprotocol and provides the Laplacian construction → eigendecomposition → descriptor pipeline.- Parameters:
vertices (ndarray, shape (N, 3)) – Vertex coordinates in mm.
faces (ndarray, shape (F, 3)) – Triangle indices, 0-indexed.
metadata (dict, optional) – Provenance (subject, hemisphere, structure, …).
Examples
>>> verts, faces = sb.io.load_freesurfer_surface("lh.white") >>> mesh = BrainMesh(verts, faces, metadata={"structure": "cortex"}) >>> decomp = mesh.decompose(k=100)
Methods
__init__(vertices, faces[, metadata])Initialise from vertices and faces arrays.
Indices of boundary (non-manifold edge) vertices.
Casorati curvature — identical to curvedness.
compute_laplacian([method, ...])Construct the Laplacian and mass matrix.
Compute area-weighted per-vertex normals from face topology.
Koenderink Curvedness C ≥ 0.
decompose([k, laplacian_method, backend])Compute the spectral decomposition.
All edge lengths.
Euler characteristic χ = V − E + F.
from_volume(volume, affine, *[, raw, label, ...])Build a mesh from a (label) volume, improved by default for the LBO.
Gaussian curvature via angle defect (Descartes–Euler).
genus()Genus g from χ = 2 − 2g (closed surfaces).
geodesic_distance(source_indices, *[, ...])Geodesic distance from source vertices to all others.
True if the mesh has no boundary.
laplacian_smooth([n_iterations, step_size])Laplacian smoothing (uniform weights).
Mean curvature via the Laplacian (Hn = ΔX / 2).
Principal curvatures κ₁ ≥ κ₂ from H and K.
Mesh quality summary.
Koenderink Shape Index S ∈ [−1, +1].
Total surface area in mm².
taubin_smooth([n_iterations, lambda_, mu])Taubin smoothing (shrinkage-free).
vertex_areas([method])Per-vertex area (Voronoi or barycentric lumped).
Number of edges incident to each vertex.
Local Willmore energy density H²(v).
Total Willmore energy ∫ H² dA.
Attributes
Return the vertex coordinates array.
Return the number of triangular faces.
Return the number of vertices (alias for n_vertices).
Return the number of vertices in the mesh.
- classmethod from_volume(volume, affine, *, raw=False, label=None, metadata=None, **kwargs)[source]#
Build a mesh from a (label) volume, improved by default for the LBO.
Thin wrapper over
spectralbrain.io.meshing.volume_to_mesh(). Withraw=False(default) the volume is turned into a well-conditioned, LBO-ready surface (sub-voxel iso-surface, topology cleanup, Taubin smoothing, isotropic remeshing). Withraw=Trueit reproduces the legacy plain-marching-cubes path exactly.- Parameters:
volume (ndarray, shape (X, Y, Z)) – Intensity or integer-label volume.
affine (ndarray, shape (4, 4)) – Voxel-to-world affine.
raw (bool) – Skip the improvement pipeline (legacy behaviour) if True.
label (int or sequence of int, optional) – Restrict to these label id(s) before meshing.
metadata (dict, optional) – Provenance stored on the returned mesh.
**kwargs – Forwarded to
volume_to_mesh()(e.g.closed,level,field_mode,taubin_iterations,remesh,target_edge_mm).
- Returns:
BrainMesh
- Return type:
Examples
>>> seg, affine = sb.io.load_nifti("aseg.mgz") >>> hippo = BrainMesh.from_volume(seg, affine, label=17) # improved >>> raw = BrainMesh.from_volume(seg, affine, label=17, raw=True)
- boundary_vertices()[source]#
Indices of boundary (non-manifold edge) vertices.
- Returns:
ndarray of int – Vertex indices on the mesh boundary.
- Return type:
- casorati_curvature()[source]#
Casorati curvature — identical to curvedness.
K_C = √((κ₁² + κ₂²) / 2)
Preferred over mean/Gaussian curvature for complexity quantification (Matsuyama et al. 2023).
- Returns:
ndarray, shape (N,)
- Return type:
NDArray[floating]
- compute_laplacian(method='cotangent', *, robust_mollify_factor=1e-05)[source]#
Construct the Laplacian and mass matrix.
- Parameters:
- Returns:
L (SparseMatrix, shape (N, N)) – Stiffness (Laplacian) matrix — symmetric positive semi-definite.
M (MassMatrix, shape (N, N)) – Diagonal mass matrix.
- Return type:
- compute_normals()[source]#
Compute area-weighted per-vertex normals from face topology.
- Returns:
normals (ndarray, shape (N, 3)) – Unit normals.
- Return type:
NDArray[floating]
- curvedness()[source]#
Koenderink Curvedness C ≥ 0.
C = √((κ₁² + κ₂²) / 2)
- Returns:
ndarray, shape (N,)
- Return type:
NDArray[floating]
- decompose(k=100, *, laplacian_method='cotangent', backend=None, **eigsh_kwargs)[source]#
Compute the spectral decomposition.
- Parameters:
k (int) – Number of eigenpairs to compute.
laplacian_method (str) – Passed to
compute_laplacian().backend (Backend, optional) – Compute backend. Defaults to
NumpyBackend.**eigsh_kwargs – Extra arguments to
backend.eigsh().
- Returns:
SpectralDecomposition
- Return type:
- gaussian_curvature()[source]#
Gaussian curvature via angle defect (Descartes–Euler).
K(v) = (2π − Σ θ_j) / A(v)
where θ_j are the face angles at vertex v and A(v) is the Voronoi area.
- Returns:
ndarray, shape (N,) – Gaussian curvature in 1/mm².
- Return type:
NDArray[floating]
- geodesic_distance(source_indices, *, method='heat', t_factor=1.0)[source]#
Geodesic distance from source vertices to all others.
- Parameters:
- Returns:
distances (ndarray, shape (N,)) – Geodesic distance from the closest source vertex.
- Return type:
NDArray[floating]
- mean_curvature()[source]#
Mean curvature via the Laplacian (Hn = ΔX / 2).
Requires the cotangent Laplacian and mass matrix.
- Returns:
ndarray, shape (N,) – Mean curvature (signed) in 1/mm.
- Return type:
NDArray[floating]
- principal_curvatures()[source]#
Principal curvatures κ₁ ≥ κ₂ from H and K.
κ₁ = H + √(H² − K), κ₂ = H − √(H² − K)
- shape_index()[source]#
Koenderink Shape Index S ∈ [−1, +1].
S = (2/π) · arctan((κ₂ + κ₁) / (κ₂ − κ₁))
- Returns:
ndarray, shape (N,)
- Return type:
NDArray[floating]
- taubin_smooth(n_iterations=10, lambda_=0.5, mu=-0.53)[source]#
Taubin smoothing (shrinkage-free).
Alternates positive and negative smoothing steps to avoid the volume shrinkage of pure Laplacian smoothing.
- vertex_valence()[source]#
Number of edges incident to each vertex.
- Returns:
ndarray, shape (N,), int
- Return type: