NeuroCAPs: Neuroimaging Co-Activation Patterns#

Latest Version Python Versions DOI GitHub Repository Test Status codecov License Platform Support Docker

NeuroCAPs (Neuroimaging Co-Activation Patterns) is a Python package for performing Co-Activation Patterns (CAPs) analyses on resting-state or task-based fMRI data. CAPs identifies recurring brain states by applying k-means clustering on BOLD timeseries data [1].

Note: NeuroCAPs is most optimized for fMRI data preprocessed with fMRIPrep and assumes the data is BIDs compliant. Refer to NeuroCAPs’ BIDS Structure and Entities Documentation for additional information.

Citing#

Smith, D. (2024). NeuroCAPs. Zenodo. https://doi.org/10.5281/zenodo.15054503

Usage#

This package contains two main classes: TimeseriesExtractor for extracting the timeseries, and CAP for performing the CAPs analysis.

Main features for TimeseriesExtractor includes:#

  • Timeseries Extraction: Extract timeseries for resting-state or task data using Schaefer, AAL, or a lateralized Custom parcellation for spatial dimensionality reduction (TimeseriesExtractor.get_bold()).

  • Parallel Processing: Use parallel processing to speed up timeseries extraction.

  • Saving Timeseries: Save the nested dictionary containing timeseries as a pickle file (TimeseriesExtractor.timeseries_to_pickle()).

  • Visualization: Visualize the timeseries at the region or node level of the parcellation (TimeseriesExtractor.visualize_bold()).

Main features for CAP includes:#

  • Grouping: Perform CAPs analysis for entire sample or groups of subject IDs

  • Optimal Cluster Size Identification: Perform the Davies Bouldin, Silhouette, Elbow, or Variance Ratio criterions to identify the optimal cluster size and automatically save the optimal model as an attribute (CAP.get_caps()).

  • Parallel Processing: Use parallel processing to speed up optimal cluster size identification.

  • CAPs Visualization: Visualize the CAPs as outer products or heatmaps at either the region or node level of the parcellation (CAP.caps2plot()).

  • Save CAPs as NifTIs: Convert the atlas used for parcellation to a statistical NifTI image (CAP.caps2niftis()).

  • Surface Plot Visualization: Project CAPs onto a surface plot (CAP.caps2surf()).

  • Correlation Matrix Creation: Create a correlation matrix from CAPs (CAP.caps2corr()).

  • CAPs Metrics Calculation: Calculate several CAPs metrics as described in Liu et al., 2018 [1] and Yang et al., 2021 [2] (CAP.calculate_metrics()):
    • Temporal Fraction: The proportion of total volumes spent in a single CAP over all volumes in a run.

    • Persistence: The average time spent in a single CAP before transitioning to another CAP (average consecutive/uninterrupted time).

    • Counts: The total number of initiations of a specific CAP across an entire run. An initiation is defined as the first occurrence of a CAP.

    • Transition Frequency: The number of transitions between different CAPs across the entire run.

    • Transition Probability : The probability of transitioning from one CAP to another CAP (or the same CAP). This is calculated as (Number of transitions from A to B) / (Total transitions from A).

  • Cosine Similarity Radar Plots: Create radar plots showing the cosine similarity between positive and negative activations of each CAP and each a-priori regions in a parcellation [3] [4] (CAP.caps2radar()).

Additionally, the neurocaps.analysis submodule contains additional functions:

  • merge_dicts(): Merge the subject_timeseries dictionaries for overlapping subjects across tasks to identify similar CAPs across different tasks [5]. The merged dictionary can be saved as a pickle file.

  • standardize(): Standardizes each run independently for all subjects in the subject timeseries.

  • change_dtype(): Changes the dtype of all subjects in the subject timeseries to help with memory usage.

  • transition_matrix(): Uses the “transition_probability” output from CAP.calculate_metrics() to generate and visualize the averaged transition probability matrix for all groups from the analysis.

Please refer to the demos or tutorials for a more extensive demonstration of the features included in this package.

Dependencies#

NeuroCAPs relies on several packages:

dependencies = [
   "numpy>=1.22.0",
   "pandas>=2.0.0",
   "joblib>=1.3.0",
   "matplotlib>=3.6.0",
   "seaborn>=0.11.0",
   "kneed>=0.8.0",
   "nibabel>=3.2.0",
   "nilearn>=0.10.1, !=0.10.3",
   "scikit-learn>=1.4.0",
   "scipy>=1.6.0",
   "brainspace>=0.1.16",
   "surfplot>=0.2.0",
   "neuromaps>=0.0.5",
   "pybids>=0.16.2; platform_system != 'Windows'",
   "plotly>=4.9",
   "nbformat>=4.2.0",
   "kaleido==0.1.0.post1; platform_system == 'Windows'",
   "kaleido; platform_system != 'Windows'",
   "setuptools; python_version>='3.12'",
   "typing_extensions; python_version<'3.11'",
   "vtk<9.4.0",
   "tqdm>=4.65.0"
   ]

Acknowledgements#

Some foundational concepts in NeuroCAPs take inspiration from features or design patterns implemented in other neuroimaging Python packages, specifically:

  • mtorabi59’s pydfc, a toolbox that allows comparisons among several popular dynamic functionality methods.

  • 62442katieb’s IDConn, a pipeline for assessing individual differences in resting-state or task-based functional connectivity.

References#