spectralbrain.statistics.analysis#
Statistical analysis toolkit for spectral morphometry.
Covers the full analytical pipeline from vertex-wise group comparison to connectome-level network analysis, including dimension-collapsing methods for converting per-vertex descriptors into per-shape global vectors.
Sections#
§1 Vertex-wise group comparison (t-test, Mann-Whitney, TFCE, FDR, permutation) §2 Effect sizes (Cohen’s d, Hedges’ g — vertex-wise maps) §3 Vertex-wise correlation with clinical scores §4 Surprise / anomaly maps (z-score against normative) §5 Classification (SVM, LogReg + CV) §6 Dimension collapsing (Fisher vectors, Bag-of-Spectral-Words, kernel mean embedding) §7 Dissimilarity measures (EMD, KL, JS divergence, energy distance) §8 RSA — Representational Similarity Analysis §9 Connectome & network analysis (modularity, participation, NBS, Mantel) §10 Asymmetry analysis (lateralisation indices) §11 Dimensionality reduction (PCA, MDS, UMAP)
Functions
|
Test whether L and R descriptors differ significantly. |
|
Bag-of-Words encoding of per-vertex descriptors. |
|
Cross-validated classification with feature importance. |
|
Vertex-wise Cohen's d effect-size map. |
|
Earth Mover's Distance (1D Wasserstein) between distributions. |
|
Energy distance between two multivariate samples. |
|
Fisher vector encoding of per-vertex descriptors. |
|
Fit a GMM codebook on pooled descriptors from a population. |
|
Vertex-wise Hedges' g (bias-corrected Cohen's d). |
|
Intra- vs inter-community connectivity ratio. |
|
Jensen-Shannon divergence (symmetric, bounded [0, ln2]). |
|
Kernel mean embedding of a descriptor distribution. |
|
KL divergence estimated via histogram binning. |
|
|
|
Mantel test — correlation between two distance matrices. |
|
Newman's modularity Q for a given community partition. |
|
Participation coefficient per node. |
|
Representational Dissimilarity Matrix. |
|
Compare two RDMs via Representational Similarity Analysis. |
|
Classical MDS embedding from a distance matrix. |
|
PCA on spectral features. |
|
UMAP embedding of spectral features. |
|
Z-score anomaly map against a normative distribution. |
|
Percentile-based anomaly map. |
|
Threshold-Free Cluster Enhancement (Smith & Nichols 2009). |
|
Correlate a per-vertex descriptor with a clinical score. |
|
Non-parametric Mann-Whitney U test at each vertex. |
|
Permutation test at each vertex with multiple-comparison control. |
|
Independent two-sample t-test at each vertex (vectorised). |
Classes
|
Output of a classification analysis. |
|
Results of a vertex-wise statistical test. |
- class spectralbrain.statistics.analysis.ClassificationResult(accuracy, accuracy_std, auc, auc_std, feature_importance, confusion_matrix, model_name)[source]#
Bases:
objectOutput of a classification analysis.
- Parameters:
- class spectralbrain.statistics.analysis.VertexWiseResult(statistic, p_values, p_corrected, correction, significant, alpha, effect_size=None)[source]#
Bases:
objectResults of a vertex-wise statistical test.
- Parameters:
- spectralbrain.statistics.analysis.asymmetry_test(left, right, *, test='wilcoxon')[source]#
Test whether L and R descriptors differ significantly.
- spectralbrain.statistics.analysis.bag_of_spectral_words(descriptor, codebook, *, soft=True, sigma=None)[source]#
Bag-of-Words encoding of per-vertex descriptors.
- Parameters:
- Returns:
ndarray, shape (K,) – Normalised histogram over codebook words.
- Return type:
- spectralbrain.statistics.analysis.classify(features, labels, *, model='svm', n_folds=5, seed=42)[source]#
Cross-validated classification with feature importance.
- Parameters:
- Returns:
ClassificationResult
- Return type:
- spectralbrain.statistics.analysis.cohens_d_map(group_a, group_b)[source]#
Vertex-wise Cohen’s d effect-size map.
- spectralbrain.statistics.analysis.emd_distance(a, b)[source]#
Earth Mover’s Distance (1D Wasserstein) between distributions.
For multi-dimensional descriptors, averages across columns.
- Parameters:
a (ndarray, shape (N,) or (N, T))
b (ndarray, shape (N,) or (N, T))
- Returns:
float
- Return type:
- spectralbrain.statistics.analysis.energy_distance(a, b)[source]#
Energy distance between two multivariate samples.
- Parameters:
a (ndarray, shape (N_a, d))
b (ndarray, shape (N_b, d))
- Returns:
float
- Return type:
- spectralbrain.statistics.analysis.fisher_vector(descriptor, gmm_means, gmm_covs, gmm_weights)[source]#
Fisher vector encoding of per-vertex descriptors.
Projects a per-vertex descriptor distribution onto the gradient of a Gaussian Mixture Model, producing a fixed-length global vector regardless of the number of vertices.
- Parameters:
descriptor (ndarray, shape (N, T)) – Per-vertex descriptor matrix.
gmm_means (ndarray, shape (K, T)) – GMM component means.
gmm_covs (ndarray, shape (K, T)) – GMM diagonal covariances.
gmm_weights (ndarray, shape (K,)) – GMM component weights (sum to 1).
- Returns:
ndarray, shape (2·K·T,) – Fisher vector (concatenation of first and second order gradient statistics).
- Return type:
References
Perronnin F, Dance C. Fisher kernels on visual vocabularies for image categorization. CVPR 2007. Sánchez J, Perronnin F, Mensink T, Verbeek J. Image classification with the Fisher vector. IJCV 105(3):222–245, 2013.
- spectralbrain.statistics.analysis.fit_gmm_codebook(all_descriptors, n_components=32, *, seed=42)[source]#
Fit a GMM codebook on pooled descriptors from a population.
- Parameters:
- Returns:
means (ndarray, shape (K, T))
covariances (ndarray, shape (K, T)) – Diagonal covariances.
weights (ndarray, shape (K,))
- Return type:
- spectralbrain.statistics.analysis.hedges_g_map(group_a, group_b)[source]#
Vertex-wise Hedges’ g (bias-corrected Cohen’s d).
- spectralbrain.statistics.analysis.intra_inter_ratio(connectome, community_labels)[source]#
Intra- vs inter-community connectivity ratio.
- spectralbrain.statistics.analysis.js_divergence(a, b, **kwargs)[source]#
Jensen-Shannon divergence (symmetric, bounded [0, ln2]).
- Parameters:
a (ndarray)
b (ndarray)
- Returns:
float
- Return type:
- spectralbrain.statistics.analysis.kernel_mean_embedding(descriptor, *, kernel='rbf', sigma=None, n_landmarks=100, seed=None)[source]#
Kernel mean embedding of a descriptor distribution.
Embeds the empirical distribution of per-vertex descriptors into an RKHS, approximated by random Fourier features (Rahimi & Recht 2007) for scalability.
- spectralbrain.statistics.analysis.kl_divergence(a, b, *, bins=50)[source]#
KL divergence estimated via histogram binning.
- spectralbrain.statistics.analysis.lateralisation_index(left, right)[source]#
Lateralisation Index: LI = (L - R) / (|L| + |R|).
- Parameters:
left (ndarray) – Matching descriptor values for L and R hemispheres.
right (ndarray) – Matching descriptor values for L and R hemispheres.
- Returns:
ndarray – LI ∈ [-1, +1]. Positive = left > right.
- Return type:
- spectralbrain.statistics.analysis.mantel_test(matrix_a, matrix_b, *, n_permutations=5000, method='spearman', seed=None)[source]#
Mantel test — correlation between two distance matrices.
Tests whether two distance matrices are correlated by comparing the observed correlation to a null distribution generated by row/column permutation.
- spectralbrain.statistics.analysis.modularity(connectome, community_labels, *, gamma=1.0)[source]#
Newman’s modularity Q for a given community partition.
Q = (1/2m) Σ_{ij} [A_{ij} - γ·k_i·k_j/(2m)] · δ(c_i, c_j)
- spectralbrain.statistics.analysis.participation_coefficient(connectome, community_labels)[source]#
Participation coefficient per node.
PC_i = 1 - Σ_k (s_{ik} / s_i)²
High PC → hub connected to multiple communities. Low PC → provincial node within one community.
- Parameters:
connectome (ndarray, shape (R, R))
community_labels (ndarray, shape (R,))
- Returns:
ndarray, shape (R,)
- Return type:
- spectralbrain.statistics.analysis.rdm(features, *, metric='correlation')[source]#
Representational Dissimilarity Matrix.
- spectralbrain.statistics.analysis.rsa_compare(rdm_a, rdm_b, *, method='spearman', permutations=0, seed=None)[source]#
Compare two RDMs via Representational Similarity Analysis.
- Parameters:
- Returns:
r (float) – Correlation between upper triangles.
p_value (float) – p-value (parametric if permutations=0, permutation otherwise).
- Return type:
- spectralbrain.statistics.analysis.spectral_mds(distance_matrix, n_components=2, *, seed=None)[source]#
Classical MDS embedding from a distance matrix.
- spectralbrain.statistics.analysis.spectral_pca(features, n_components=2)[source]#
PCA on spectral features.
- spectralbrain.statistics.analysis.spectral_umap(features, n_components=2, *, n_neighbors=15, min_dist=0.1, seed=None)[source]#
UMAP embedding of spectral features.
- spectralbrain.statistics.analysis.surprise_map(subject_descriptor, normative_mean, normative_std)[source]#
Z-score anomaly map against a normative distribution.
- spectralbrain.statistics.analysis.surprise_map_percentile(subject_descriptor, normative_distribution)[source]#
Percentile-based anomaly map.
- spectralbrain.statistics.analysis.tfce(statistic_map, adjacency, *, E=0.5, H=2.0, n_steps=100)[source]#
Threshold-Free Cluster Enhancement (Smith & Nichols 2009).
Enhances a statistical map by integrating cluster extent and height across all thresholds.
TFCE(v) = ∫₀^h(v) e(h)^E · h^H dh
- Parameters:
statistic_map (ndarray, shape (N,)) – Vertex-wise test statistic (e.g. t-values).
adjacency (sparse matrix, shape (N, N)) – Vertex adjacency (from mesh or kNN graph).
E (float) – Cluster extent exponent (default 0.5).
H (float) – Height exponent (default 2.0).
n_steps (int) – Number of threshold steps for numerical integration.
- Returns:
ndarray, shape (N,) – TFCE-enhanced statistic map.
- Return type:
References
Smith SM, Nichols TE. Threshold-free cluster enhancement. NeuroImage 44(1):83–98, 2009.
- spectralbrain.statistics.analysis.vertexwise_correlation(descriptors, scores, *, method='pearson', correction='fdr', alpha=0.05, covariates=None)[source]#
Correlate a per-vertex descriptor with a clinical score.
- Parameters:
descriptors (ndarray, shape (S, N) or (S, N, T)) – Per-subject descriptor values. If 3D, averaged over T.
scores (ndarray, shape (S,)) – Clinical score per subject.
method (str)
correction (str)
alpha (float)
covariates (ndarray, shape (S, C), optional) – If given, partial correlation controlling for covariates.
- Returns:
VertexWiseResult
- Return type:
- spectralbrain.statistics.analysis.vertexwise_mannwhitney(group_a, group_b, *, correction='fdr', alpha=0.05)[source]#
Non-parametric Mann-Whitney U test at each vertex.
- Parameters:
group_a (ndarray, shape (n, N))
group_b (ndarray, shape (n, N))
correction (as above.)
alpha (as above.)
- Returns:
VertexWiseResult
- Return type:
- spectralbrain.statistics.analysis.vertexwise_permutation(group_a, group_b, *, n_permutations=5000, stat_func='t', correction='max', seed=None, alpha=0.05)[source]#
Permutation test at each vertex with multiple-comparison control.
The label permutation builds an exact (non-parametric) null. Crucially, the per-vertex permutation p-value is not corrected for multiple comparisons on its own. This function offers proper correction:
correction="max"(default): family-wise error rate (FWER) control via the maximum-statistic null distribution (Westfall & Young; Nichols & Holmes 2002). On each permutation the maximum of|statistic|across all vertices is recorded; a vertex is significant if its observed statistic exceeds the(1-alpha)quantile of that null. This is the standard rigorous correction for vertex/voxel-wise permutation testing.correction="fdr": per-vertex permutation p-values, then Benjamini-Hochberg.correction="none": raw per-vertex permutation p-values.
- Parameters:
- Returns:
VertexWiseResult –
p_valuesare always the raw per-vertex permutation p-values;p_correctedreflects the chosen correction. For"max",p_correctedis the FWER-adjusted p-value (fraction of the max-null at or above each observed statistic).- Return type:
References
Nichols TE, Holmes AP. Nonparametric permutation tests for functional neuroimaging. Hum Brain Mapp 15(1):1–25, 2002.
- spectralbrain.statistics.analysis.vertexwise_ttest(group_a, group_b, *, correction='fdr', alpha=0.05, equal_var=False)[source]#
Independent two-sample t-test at each vertex (vectorised).
- Parameters:
group_a (ndarray, shape (n_a, N) or (n_a, N, T)) – Descriptor values for group A (subjects × vertices [× scales]). If 3D, tests are run on the mean across the last axis.
group_b (ndarray, shape (n_b, N)) – Descriptor values for group B.
correction (str) –
"fdr"— Benjamini-Hochberg;"bonferroni";"none".alpha (float)
equal_var (bool) – If
False(default), Welch’s t-test (does not assume equal variances) — the safer default for groups with unequal size or spread. IfTrue, Student’s pooled-variance t-test.
- Returns:
VertexWiseResult
- Return type: