spectralbrain.core.base#
Core geometric objects and shared processing functions.
This module defines:
SpectralDecomposition— the central object of the library, holding eigenvalues/eigenvectors and providing access to all spectral descriptors.GeometricObject— abstract protocol that bothBrainMeshandBrainPointCloudimplement.Shared geometric functions that operate on raw
(N, 3)coordinate arrays regardless of mesh or point-cloud origin.
Design principle#
SpectralBrain’s data flow is:
io.loaders → core.meshes / core.pointclouds → core.base.SpectralDecomposition → spectral.*
The SpectralDecomposition is what core/ produces and spectral/
consumes. It is the single handoff point between geometry and
analysis.
Functions
|
Align points so principal axes coincide with coordinate axes. |
|
Translate points so the centroid is at the origin. |
|
L² Chamfer distance between two point sets. |
|
Build a kNN adjacency matrix (binary or weighted). |
|
Axis-aligned bounding box. |
|
Compute the centroid (center of mass) of a point set. |
|
PCA of a point set — principal axes and explained variance. |
|
Convex hull surface area. |
|
Convex hull volume of a point set. |
|
Flag points with anomalously low or high local density. |
|
Estimate local point density via k-NN distance. |
|
Farthest-point sampling (FPS) for uniform subsampling. |
|
Hausdorff distance between two point sets. |
|
k-nearest-neighbour search using a KD-tree. |
|
Extract a mesh from a volumetric label/mask via marching cubes. |
|
Total surface area of a triangle mesh. |
|
Scale points to unit bounding-box diagonal, unit RMS, or unit area. |
|
Rigid + uniform scale alignment (Procrustes). |
|
Fixed-radius neighbour search. |
|
Compute per-triangle areas. |
Classes
|
Protocol that |
|
Eigenvalues and eigenvectors of a Laplace–Beltrami operator. |
- class spectralbrain.core.base.GeometricObject(*args, **kwargs)[source]#
Bases:
ProtocolProtocol that
BrainMeshandBrainPointCloudboth implement.Any function that accepts a
GeometricObjectcan work with either representation.
- class spectralbrain.core.base.SpectralDecomposition(eigenvalues, eigenvectors, *, stiffness=None, mass=None, surface_area=None, metadata=None)[source]#
Bases:
objectEigenvalues and eigenvectors of a Laplace–Beltrami operator.
This is the central object of SpectralBrain. It is produced by
core.meshes.BrainMesh.decompose()orcore.pointclouds.BrainPointCloud.decompose()and consumed by every function inspectral/.- Parameters:
eigenvalues (ndarray, shape (k,)) – Non-negative LBO eigenvalues, sorted ascending.
eigenvectors (ndarray, shape (N, k)) – Corresponding eigenvectors, M-orthonormal.
stiffness (SparseMatrix, optional) – The Laplacian matrix L.
mass (MassMatrix, optional) – The mass matrix M.
surface_area (float, optional) – Total surface area (for eigenvalue normalisation).
metadata (dict, optional) – Provenance info (subject, hemisphere, structure, backend, …).
- eigenvalues#
- Type:
ndarray
- eigenvectors#
- Type:
ndarray
- stiffness#
- Type:
SparseMatrix or None
- mass#
- Type:
MassMatrix or None
Examples
>>> mesh = sb.core.BrainMesh(vertices, faces) >>> decomp = mesh.decompose(k=100) >>> decomp.n_eigenvalues 100 >>> decomp.fiedler_value 0.0023 >>> decomp.truncate(50).n_eigenvalues 50
>>> # Pass to spectral descriptors: >>> hks = sb.spectral.compute_hks(decomp, t_values)
- classmethod load(path)[source]#
Load from an HDF5 cache file.
- Parameters:
path (PathLike)
- Returns:
SpectralDecomposition
- Return type:
- normalize_eigenvalues(method='area', area=None, volume=None)[source]#
Return a copy with normalised eigenvalues.
- Parameters:
- Returns:
SpectralDecomposition
- Return type:
- save(path, **kwargs)[source]#
Save to HDF5 via
spectralbrain.io.export.save_hdf5().
- truncate(k)[source]#
Return a copy with only the first k eigenpairs.
- Parameters:
k (int) – Number of eigenpairs to keep (must be ≤ current k).
- Returns:
SpectralDecomposition
- Return type:
- property fiedler_value: float#
First non-trivial eigenvalue λ₁ (the Fiedler value).
Encodes global connectivity of the shape. Larger values indicate tighter geometry.
- property k: int#
Alias for
n_eigenvalues.
- property shape_dna: ndarray[tuple[Any, ...], dtype[floating]]#
ShapeDNA — the raw eigenvalue sequence (excluding λ₀ ≈ 0).
- Returns:
ndarray, shape (k-1,)
- spectralbrain.core.base.align_to_pca(points)[source]#
Align points so principal axes coincide with coordinate axes.
- spectralbrain.core.base.center_points(points)[source]#
Translate points so the centroid is at the origin.
- spectralbrain.core.base.chamfer_distance(A, B)[source]#
L² Chamfer distance between two point sets.
- Parameters:
A (ndarray, shape (N, 3) and (M, 3))
B (ndarray, shape (N, 3) and (M, 3))
- Returns:
float
- Return type:
- spectralbrain.core.base.compute_adjacency_from_knn(points, k=20, *, symmetric=True)[source]#
Build a kNN adjacency matrix (binary or weighted).
- spectralbrain.core.base.compute_centroid(points)[source]#
Compute the centroid (center of mass) of a point set.
- Parameters:
points (ndarray, shape (N, 3))
- Returns:
ndarray, shape (3,)
- Return type:
- spectralbrain.core.base.compute_pca_axes(points)[source]#
PCA of a point set — principal axes and explained variance.
- Parameters:
points (ndarray, shape (N, 3))
- Returns:
components (ndarray, shape (3, 3)) – Rows are the principal directions, sorted by decreasing variance.
explained_variance (ndarray, shape (3,)) – Eigenvalues of the covariance matrix (variance along each axis).
centroid (ndarray, shape (3,))
- Return type:
- spectralbrain.core.base.convex_hull_area(points)[source]#
Convex hull surface area.
- Parameters:
points (ndarray, shape (N, 3))
- Returns:
float – Area in mm².
- Return type:
- spectralbrain.core.base.convex_hull_volume(points)[source]#
Convex hull volume of a point set.
- Parameters:
points (ndarray, shape (N, 3))
- Returns:
float – Volume in cubic mm.
- Return type:
- spectralbrain.core.base.detect_density_outliers(points, k=6, threshold_sigma=3.0)[source]#
Flag points with anomalously low or high local density.
- spectralbrain.core.base.estimate_point_density(points, k=6)[source]#
Estimate local point density via k-NN distance.
Density at each point is approximated as
k / V_kwhereV_k = (4/3)π r_k³andr_kis the distance to the k-th nearest neighbour.
- spectralbrain.core.base.farthest_point_sampling(points, n_samples, *, seed=None)[source]#
Farthest-point sampling (FPS) for uniform subsampling.
Iteratively selects the point farthest from the current set, producing an approximately uniform subset. Essential for standardising point-cloud density from volumetric segmentations.
- Parameters:
- Returns:
sampled (ndarray, shape (n_samples, 3)) – Selected points.
indices (ndarray, shape (n_samples,)) – Indices into points.
- Return type:
Notes
Complexity: O(N · n_samples). For N > 100 k, consider the approximate FPS in Open3D or PyTorch3D.
- spectralbrain.core.base.hausdorff_distance(A, B, *, symmetric=True)[source]#
Hausdorff distance between two point sets.
- spectralbrain.core.base.knn_search(points, k=20, *, query_points=None)[source]#
k-nearest-neighbour search using a KD-tree.
- Parameters:
points (ndarray, shape (N, 3)) – Reference point set.
k (int) – Number of neighbours.
query_points (ndarray, shape (M, 3), optional) – Query points. Defaults to points (self-query).
- Returns:
distances (ndarray, shape (M, k))
indices (ndarray, shape (M, k))
- Return type:
- spectralbrain.core.base.marching_cubes(volume, affine, *, level=None, step_size=1)[source]#
Extract a mesh from a volumetric label/mask via marching cubes.
- spectralbrain.core.base.mesh_surface_area(vertices, faces)[source]#
Total surface area of a triangle mesh.
- Parameters:
vertices (ndarray, shape (N, 3))
faces (ndarray, shape (F, 3))
- Returns:
float – Total area in mm².
- Return type:
- spectralbrain.core.base.normalize_scale(points, method='bbox', *, area=None)[source]#
Scale points to unit bounding-box diagonal, unit RMS, or unit area.
- Parameters:
- Returns:
scaled (ndarray, shape (N, 3))
scale_factor (float) – The divisor applied.
- Return type:
- spectralbrain.core.base.procrustes_align(source, target)[source]#
Rigid + uniform scale alignment (Procrustes).
Finds the rotation R, translation t, and scale s that minimise ||s·R·source + t − target||².
- Parameters:
source (ndarray, shape (N, 3)) – Points to align.
target (ndarray, shape (N, 3)) – Reference points (must have the same N).
- Returns:
aligned (ndarray, shape (N, 3)) – Transformed source.
rotation (ndarray, shape (3, 3)) – Rotation matrix.
scale (float) – Uniform scale factor.
- Raises:
ValueError – If source and target have different shapes.
- Return type:
tuple[ndarray[tuple[Any, …], dtype[floating]], ndarray, float]