spectralbrain.backends.cpu#
CPU compute backend — NumPy/SciPy, Bayesian samplers, and parallelisation.
This module provides:
NumpyBackend — the default compute engine using SciPy sparse eigensolvers (ARPACK), sparse matrix operations, and NumPy array algebra. Every other backend mirrors this interface.
PyMCSampler / NutpieSampler — Bayesian MCMC backends for the
statistics/bayesian.pymodule.Joblib utilities — composable parallelisation helpers with Rich progress integration.
RAM management — memory monitoring, garbage collection, and estimation helpers for multi-subject pipelines.
All optional dependencies (PyMC, nutpie, joblib) are lazy-imported. Only NumPy and SciPy are hard requirements.
Functions
|
Iterate over an array in memory-safe batches. |
|
Estimate memory for an array in gigabytes. |
|
Force Python garbage collection and return bytes freed. |
|
Factory for CPU Bayesian samplers. |
|
Context manager that checks RAM before and after a block. |
|
Apply func to batches of an array in parallel. |
|
Apply func to each item in parallel with optional progress. |
Return current system RAM usage. |
|
|
Downcast an array to save memory. |
Classes
|
Snapshot of system RAM usage. |
CPU compute backend using NumPy + SciPy. |
|
|
Bayesian sampler using nutpie (Rust-based NUTS). |
|
Bayesian sampler using PyMC's native NUTS implementation. |
|
Configuration for Bayesian MCMC samplers. |
- class spectralbrain.backends.cpu.MemoryInfo(total_gb, available_gb, used_gb, percent_used)[source]#
Bases:
objectSnapshot of system RAM usage.
- class spectralbrain.backends.cpu.NumpyBackend[source]#
Bases:
objectCPU compute backend using NumPy + SciPy.
Provides the canonical interface that
CupyBackendandJaxBackendmirror. Allcore/andspectral/modules call backend methods rather than importing NumPy or SciPy directly, enabling transparent GPU acceleration.Examples
>>> from spectralbrain.backends.cpu import NumpyBackend >>> be = NumpyBackend() >>> evals, evecs = be.eigsh(L, M, k=100) >>> hks = be.exp(-evals[None, :] * t[:, None]) # broadcasting
- static eigsh(L, M=None, k=100, *, sigma=-0.01, which='LM', tol=0.0, maxiter=None)[source]#
Solve the generalised sparse eigenproblem L v = λ M v.
Uses SciPy’s ARPACK wrapper in shift-invert mode (default σ = −0.01) which is optimal for computing the smallest eigenvalues of the Laplacian.
- Parameters:
L (sparse matrix, shape (N, N)) – Stiffness (Laplacian) matrix — symmetric positive semi-definite.
M (sparse matrix, shape (N, N), optional) – Mass matrix. If
None, the standard eigenproblem L v = λ v is solved.k (int) – Number of eigenpairs to compute.
sigma (float) – Shift for shift-invert mode. A small negative value avoids the singularity at λ = 0.
which (str) – Which eigenvalues to target (
"LM"= largest magnitude of the shifted operator, yielding the smallest λ).tol (float) – Convergence tolerance (0 = machine precision).
maxiter (int, optional) – Maximum ARPACK iterations.
- Returns:
eigenvalues (ndarray, shape (k,)) – Sorted ascending, float64.
eigenvectors (ndarray, shape (N, k)) – Corresponding eigenvectors, M-orthonormal.
- Raises:
scipy.sparse.linalg.ArpackNoConvergence – If ARPACK fails to converge within maxiter iterations.
- Return type:
tuple[ndarray[tuple[Any, …], dtype[floating]], ndarray[tuple[Any, …], dtype[floating]]]
- static eye(n, dtype=<class 'numpy.float64'>)[source]#
Create an identity matrix (mirrors numpy.eye).
- static ones(shape, dtype=<class 'numpy.float64'>)[source]#
Create a ones-filled array (mirrors numpy.ones).
- static sparse_matrix(data, row, col, shape, *, format='csc')[source]#
Build a sparse matrix from COO triplets.
- class spectralbrain.backends.cpu.NutpieSampler(config=None)[source]#
Bases:
objectBayesian sampler using nutpie (Rust-based NUTS).
nutpie is a high-performance drop-in replacement for PyMC’s default sampler. It compiles the PyMC model to Rust and runs NUTS 2–10× faster on CPU.
- Parameters:
config (SamplerConfig, optional) – Sampling configuration.
Examples
>>> sampler = NutpieSampler(SamplerConfig(draws=2000)) >>> trace = sampler.sample(model)
- class spectralbrain.backends.cpu.PyMCSampler(config=None)[source]#
Bases:
objectBayesian sampler using PyMC’s native NUTS implementation.
This is the default CPU sampler. It wraps
pymc.sample()with SpectralBrain-compatible configuration and logging.- Parameters:
config (SamplerConfig, optional) – Sampling configuration.
Examples
>>> sampler = PyMCSampler(SamplerConfig(draws=1000, chains=2)) >>> with pm.Model() as model: ... mu = pm.Normal("mu", 0, 1) ... obs = pm.Normal("obs", mu, 1, observed=data) >>> trace = sampler.sample(model)
- class spectralbrain.backends.cpu.SamplerConfig(draws=2000, tune=1000, chains=4, cores=4, target_accept=0.95, random_seed=42)[source]#
Bases:
objectConfiguration for Bayesian MCMC samplers.
- Parameters:
draws (int) – Number of posterior draws per chain.
tune (int) – Number of tuning (burn-in) samples.
chains (int) – Number of independent chains.
cores (int) – CPU cores for parallel chains.
target_accept (float) – Target acceptance probability for NUTS.
random_seed (int or None) – RNG seed for reproducibility.
- spectralbrain.backends.cpu.batch_iterator(data, batch_size=1000, *, axis=0)[source]#
Iterate over an array in memory-safe batches.
Unlike
parallel_batch(), this is a sequential generator suitable for GPU-offloading loops where only one batch should be in memory at a time.- Parameters:
- Yields:
ndarray – A view (not copy) of the batch.
- Return type:
Examples
>>> for batch in batch_iterator(big_array, batch_size=500): ... result = expensive_compute(batch) ... accumulate(result)
- spectralbrain.backends.cpu.estimate_array_memory(shape, dtype=<class 'numpy.float64'>)[source]#
Estimate memory for an array in gigabytes.
- Parameters:
- Returns:
float – Estimated size in GB.
- Return type:
Examples
>>> estimate_array_memory((160_000, 300), np.float64) 0.358 # ~358 MB for cortical eigenvectors
- spectralbrain.backends.cpu.gc_collect(generations=2)[source]#
Force Python garbage collection and return bytes freed.
- Parameters:
generations (int) – GC generations to collect (0, 1, or 2).
- Returns:
int – Number of unreachable objects collected.
- Return type:
Examples
>>> del large_array >>> freed = gc_collect() >>> logger.info("GC freed %d objects", freed)
- spectralbrain.backends.cpu.get_bayesian_sampler(backend='nuts', config=None)[source]#
Factory for CPU Bayesian samplers.
- Parameters:
backend (
"nuts"or"nutpie") – Which sampler to use.config (SamplerConfig, optional) – Sampling parameters.
- Returns:
PyMCSampler or NutpieSampler
- Return type:
- spectralbrain.backends.cpu.memory_guard(min_available_gb=2.0, error_on_low=False)[source]#
Context manager that checks RAM before and after a block.
- Parameters:
- Return type:
Generator[None, None, None]
Examples
>>> with memory_guard(min_available_gb=4.0): ... big_result = compute_all_subjects()
- spectralbrain.backends.cpu.parallel_batch(func, data, *, batch_size=1000, n_jobs=-1, axis=0, progress=True, description='Batch processing')[source]#
Apply func to batches of an array in parallel.
Splits data along axis into chunks of batch_size, applies func to each chunk in parallel, and concatenates the results. Useful for operations that are O(N²) per vertex but can be batched (e.g. geodesic distance computation).
- Parameters:
func (callable) – Function that accepts an ndarray batch and returns an ndarray.
data (ndarray) – Full array to process.
batch_size (int) – Number of rows per batch.
n_jobs (int) – Parallel workers.
axis (int) – Axis along which to split.
progress (bool) – Show progress bar.
description (str) – Progress label.
- Returns:
ndarray – Concatenated results.
- Return type:
- spectralbrain.backends.cpu.parallel_map(func, items, *, n_jobs=-1, backend='loky', progress=True, description='Processing', **func_kwargs)[source]#
Apply func to each item in parallel with optional progress.
A thin wrapper around
joblib.Parallelthat integrates with SpectralBrain’s Rich progress bars.- Parameters:
func (callable) – Function to apply. Must accept each item as its first positional argument.
items (sequence) – Items to process.
n_jobs (int) – Number of parallel workers (
-1= all cores).backend (str) – Joblib backend (
"loky","threading","multiprocessing").progress (bool) – Show a Rich progress bar.
description (str) – Progress bar label.
**func_kwargs – Extra keyword arguments passed to func.
- Returns:
list – Results in the same order as items.
- Return type:
list[R]
Examples
>>> def compute(subj_id, k=100): ... mesh = load(subj_id) ... return decompose(mesh, k=k) >>> results = parallel_map(compute, subject_ids, n_jobs=8, k=50)
- spectralbrain.backends.cpu.ram_status()[source]#
Return current system RAM usage.
Uses
/proc/meminfoon Linux andpsutilas fallback.- Returns:
MemoryInfo
- Return type:
- spectralbrain.backends.cpu.shrink_array(arr, target_dtype=None)[source]#
Downcast an array to save memory.
If target_dtype is
None, applies safe downcasting rules: float64 → float32, int64 → int32 (if values fit).- Parameters:
arr (ndarray)
target_dtype (dtype, optional)
- Returns:
ndarray – A (possibly) smaller copy.
- Return type: