BrainSpace: Macroscale Gradient Mapping Toolbox

repository·master·Indexed 42 days ago

https://github.com/mica-mni/brainspace

A cross-platform toolbox for macroscale gradient mapping and analysis in neuroimaging and connectomics, supporting both Python and MATLAB. It provides tools for deriving gradients from connectivity matrices using the GradientMaps class, mapping gradients to cortical surfaces, and visualizing results with plot_hemispheres. Includes built-in datasets for conte69 surfaces, Schaefer parcellations, and Human Connectome Project connectivity matrices.

Tokens
33.3K
Snippets
119
Records
149
Agent score
52%

What's inside BrainSpace

  1. Overview of the BrainSpace MATLAB Package

    master

    The BrainSpace MATLAB toolbox is organized into five functional groups to facilitate neuroimaging analysis and visualization:

    1. Main Object: The core computational engine of the toolbox.
    2. Surface Handling: Functions for reading, writing, parcellating, and manipulating brain surfaces.
    3. Null Hypothesis Testing: Statistical tools including spin permutations, memory computation, and Moran randomization.
    4. Visualization: Tools for plotting and visualizing data.
    5. Data Loaders: Utilities to load sample datasets for tutorials and testing.

    Note: The toolbox also includes Support Functions which are internal requisites for the main features and are not intended for direct user interaction.

  2. Overview of BrainSpace

    master
    BrainSpace is a lightweight, cross-platform toolbox designed for macroscale gradient mapping and the analysis of neuroimaging and connectome-level data. It provides tools for exploratory analysis of gradient correspondence with other brain-derived features and includes utilities to generate spatial null models. The toolbox is available in both Python and MATLAB.
  3. Use Gradient Maps for manifold learning and dimensionality reduction

    master

    The brainspace.gradient module provides tools for manifold learning, dimensionality reduction, and alignment. It is organized into several functional submodules:

    • Gradients: Core gradient map implementations via brainspace.gradient.gradient.GradientMaps.
    • Embedding: Dimensionality reduction techniques including Embedding, DiffusionMaps, LaplacianEigenmaps, and PCAMaps.
    • Alignment: Methods to align different data representations, specifically using ProcrustesAlignment or the procrustes_alignment function.
    • Kernels: Affinity computation via brainspace.gradient.kernels.compute_affinity.
    • Utility functions: Mathematical helpers like dominant_set, is_symmetric, and make_symmetric.
  4. Evaluate gradient importance using eigenvalues

    master

    To determine how many gradients to keep, inspect the eigenvalues (lambdas) of the components. In GradientMaps, these are stored in the lambdas_ attribute.

    A scree plot can be used to identify a cut-off point: higher eigenvalues (or lower in Laplacian eigenmaps) indicate more important components.

    import matplotlib.pyplot as plt
    
    # Create a scree plot of the eigenvalues
    fig, ax = plt.subplots(1, figsize=(5, 4))
    ax.scatter(range(gm.lambdas_.size), gm.lambdas_)
    ax.set_xlabel('Component Nb')
    ax.set_ylabel('Eigenvalue')
    plt.show()
  5. Choosing between 'singleton' and 'pair' procedures in Moran randomization

    master

    The moran_randomization function provides two procedures for generating null models:

    1. 'singleton': This procedure matches the input data's spatial autocorrelation more closely. However, it is limited by the number of possible randomizations (maximum of $2^n$, where $n$ is the number of datapoints). In most use cases, this provides sufficient randomizations.
    2. 'pair': If the required number of perturbations (n_rep) is so high that it exceeds the maximum possible randomizations allowed by the 'singleton' method, you should use the 'pair' procedure instead.
  6. Understanding the VTK high-level interface

    master
    BrainSpace's surface functionality is built on top of the Visualization Toolkit (VTK). The package provides a high-level interface (pymod.vtkinterface) to work with VTK. While BrainSpace uses a specific wrapping scheme for VTK, understanding this interface is optional and not a requirement for general use of the package.
  7. How BrainSpace VTK wrappers automate object creation

    master
    BrainSpace uses a pattern of 'lazy creation' in its wrapper classes to simplify user workflows. When calling methods like .AddActor(), .SetMapper(), or .SetLookupTable(), the library checks if the requested object already exists. If it does not, the library automatically instantiates the appropriate wrapper class (BSRenderer, BSActor, BSPolyDataMapper, etc.) and attaches it to the parent object. This allows users to build complex rendering pipelines with minimal boilerplate.
  8. Getting started with the BrainSpace Python package

    master

    To get familiar with BrainSpace, start with the tutorials which cover the core functionalities:

    1. Creation and alignment of gradients (pymod.gradient)
    2. Creation of null models (pymod.nullmodels)
    3. Working with datasets (pymod.datasets)
    4. Plotting and visualization (pymod.plotting)

    For detailed technical specifications, refer to the API documentation.

  9. Load sample brain connectivity and parcellation data

    master

    BrainSpace provides utility functions to load common datasets for testing and tutorials:

    • load_group_fc(name, scale, group): Loads group-level functional connectivity matrices. Example: load_group_fc('schaefer', scale=400).
    • load_parcellation(name, scale, join): Loads parcellation labels. Example: load_parcellation('schaefer', scale=400, join=True).
    • load_conte69(): Loads the Conte69 hemisphere surfaces for visualization.
    from brainspace.datasets import load_group_fc, load_parcellation, load_conte69
    
    conn_matrix = load_group_fc('schaefer', scale=400)
    labeling = load_parcellation('schaefer', scale=400, join=True)
    surf_lh, surf_rh = load_conte69()
  10. Use load_marker to load metric data in MATLAB

    master

    The load_marker function is used to load specific cortical metric data for both the left and right hemispheres. It returns two matrices representing the data for each hemisphere.

    Parameters:

    • name: A string specifying the type of surface metric to load. Valid options are:
      • 'thickness': Cortical thickness.
      • 't1wt2w': T1w/T2w intensity.
      • 'curvature': Curvature.

    Returns:

    • metric_lh: Data matrix for the left hemisphere.
    • metric_rh: Data matrix for the right hemisphere.
    [metric_lh, metric_rh] = load_marker('thickness');
  11. Map and plot gradients on cortical surfaces

    master

    Gradients computed on parcellated data (e.g., 400 nodes) must be mapped back to the original surface resolution (e.g., 32492 points) before plotting. Use map_to_labels from brainspace.utils.parcellation to perform this mapping. Once mapped, use plot_hemispheres with the array_name parameter to visualize the gradient values on the surface.

    import numpy as np
    from brainspace.utils.parcellation import map_to_labels
    from brainspace.plotting import plot_hemispheres
    
    # Map the first gradient component back to the original surface resolution
    # labeling is the parcellation object used during gradient computation
    grad = map_to_labels(gm.gradients_[:, 0], labeling, mask=labeling != 0, fill=np.nan)
    
    # Plot the mapped gradient on the cortical surface
    plot_hemispheres(surf_lh, surf_rh, array_name=grad, size=(800, 200))