Skip to main content

SPI connectivity-analysis pipeline for fMRI group comparisons

Project description

brain-spi

Mean Kendall-tau connectivity (left) and its significant-difference AND mask (right)

Comparison of pairwise statistics for fMRI connectivity estimation, and a pipeline for finding group differences across them.

brain_spi wraps the SPI connectivity-analysis workflow into a small, importable library. It lets you run the full pipeline in one call and then inspect either the aggregated cross-SPI result or any individual SPI's intermediate artifacts.

Try it now in Colab (no install, nothing to clone): Open In Colab

What it does

  • Connectivity — derive functional network connectivity (FNC) matrices from multivariate fMRI time series using many pairwise statistics (SPIs), via pyspi plus a small custom set.
  • Group differences — per SPI, find edges that differ between two groups using a Welch t-test (Bonferroni-corrected) and a Random-Forest importance mask, then intersect them.
  • Aggregate — average the per-SPI masks into a consensus matrix: the fraction of SPIs that flag each edge.
  • Robustness — subject-resampling (bootstrap) and label-shuffle permutation nulls.
  • Plots & caching — heatmaps with network guides, per-SPI triptychs, the aggregate panel; on-disk caching of the (slow) pyspi computations.

Install

pip install brain-spi

I recommend a separate Python environment: brain-spi relies on pyspi, which requires numpy < 2 and can conflict with other packages. For development, clone the repo and pip install -e . instead.

Quickstart

import numpy as np
from brain_spi import BrainSPI

# data: (B subjects, T timepoints, C channels/ROIs);  labels: (B,) with 2 unique values
pipe   = BrainSPI(group_names=('HC', 'patient'))   # sensible defaults (9 curated SPIs)
result = pipe.fit(data, labels)

# headline result — cross-SPI aggregate (computed lazily, cached on first access)
result.aggregate.mean_and        # (C, C) float: fraction of SPIs flagging each edge
result.aggregate.plot()          # one-shot figure

# inspect a single SPI
result['kendalltau'].mean_matrix()       # (C, C) mean connectivity across subjects
result['kendalltau'].and_mask            # significant-p AND RF-important edges
result['kendalltau'].plot_triptych()     # 3-panel: sig-p / RF / AND

# not sure what's available? every result object has a repr + .help()
result.help()

Choosing SPIs

BrainSPI()                                   # default 9-SPI curated set
BrainSPI(spis='spis_all')                    # the broad pyspi set
BrainSPI(spis='/path/to/my_spis.yaml')       # a config file
BrainSPI(spis=['kendalltau', 'spearmanr'])   # an explicit list

SPIs are computed one at a time across all subjects, with a progress bar and a per-SPI wall-time log (enable with logging.basicConfig(level=logging.INFO)), so it's easy to spot and drop slow ones.

Robustness

boot = result.bootstrap(n=20, frac=0.66)     # 20 subject-resampled cross-SPI AND maps
boot.mean                                     # average of the 20 (resampling-smoothed aggregate)
boot.survival_rate()                          # how often each edge is flagged (reproducibility)

null = result.label_shuffle(n=100)            # permutation null
null.p_value(result.aggregate.mean_and)       # per-edge permutation p-values (low = significant)

Saving results

The first fit is slow (pyspi dominates); subsequent fits on the same data are near-instant thanks to the on-disk cache (~/.cache/brain_spi/, override with BrainSPI(cache_dir=...)). To save a finished result:

result.to_npz('result.npz')              # portable — open with plain numpy.load, no package needed
result.to_pickle('result.pkl')           # exact object

import brain_spi
result = brain_spi.load_npz('result.npz')   # or load_pickle(...)

The .npz is a flat collection of arrays with a self-describing README key inside.

Example notebook

examples/colab_quickstart.ipynb Open In Colab runs end-to-end on public data — ABIDE (controls vs. autism) by default, or COBRE (schizophrenia) via a one-line switch. Part 1 computes SPIs and inspects their mean connectivity; Part 2 runs the significant-differences pipeline. Datasets download automatically via examples/datasets.py.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brain_spi-0.1.1.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brain_spi-0.1.1-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file brain_spi-0.1.1.tar.gz.

File metadata

  • Download URL: brain_spi-0.1.1.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for brain_spi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a3120d92bc8bc1f5affbf745323792a65443febfdd4a17ed2c609518c1fc1c58
MD5 da31707bbf69d99195b548a391fc5080
BLAKE2b-256 e49bf5600fdd10d40f4d52604ff8b0ee6e43696858490805e42c90675adb4370

See more details on using hashes here.

File details

Details for the file brain_spi-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: brain_spi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for brain_spi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cc4d8e4db27e55bfb611b9565dd9005bb27d9a2e79482edde2cce903e9bbc42b
MD5 40e75816ea474aa88c9abbab15a751bb
BLAKE2b-256 b16aca5ec96c70c460224f353b57ceaf4db951549d4c0ebf380a4d953d248a14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page