Skip to main content

Automatic two-stage thresholding for fMRI decoding

Project description

AutoFMRI

Automatic Two-Stage Thresholding for fMRI Decoding

PyPI version Python 3.9+ License: MIT

AutoFMRI addresses the dimensionality challenge in fMRI decoding — where voxel counts vastly exceed trial counts — by introducing an automatic two-stage voxel selection framework:

  1. Stage 1 (Atlas-based Patch Scoring + SHAP): Partition the brain into atlas-defined regions, score each region via cross-validated classification, then compute SHAP values to identify the most informative voxels within each top-performing region.
  2. Stage 2 (Refined Classification): Merge the top-k SHAP-selected voxels across the best patches and train a final classifier. Hyperparameters are automatically tuned via RandomizedSearchCV.

Installation

pip install autofmri

Or install from source:

git clone https://github.com/y10ab1/AutoFMRI.git
cd AutoFMRI
pip install -e .

Optional dependencies

# CNN support (PyTorch + skorch)
pip install autofmri[cnn]

# XGBoost support
pip install autofmri[xgboost]

# Visualization (matplotlib + seaborn)
pip install autofmri[viz]

# Everything
pip install autofmri[all]

Quick Start

from autofmri import AutoEstimator, set_seed
from autofmri.data import HaxbyDataLoader
from autofmri.models import get_model
from sklearn.model_selection import StratifiedKFold

set_seed(42)

# Load data
loader = HaxbyDataLoader(data_dir="data/haxby2001/subj4")
X, y = loader.load_volume_data()

# Train with two-stage AutoFMRI
clf = AutoEstimator(
    stage1_model=get_model("rf"),
    stage2_model=get_model("rf"),
    stage1_model_type="rf",
    stage2_model_type="rf",
    atlas_name="yeo",
    ref_img=loader.reference_img,
)

skf = StratifiedKFold(n_splits=4, shuffle=True, random_state=42)
train_idx, test_idx = next(iter(skf.split(X, y)))

clf.fit(X[train_idx], y[train_idx])
accuracy = clf.score(X[test_idx], y[test_idx])
print(f"Accuracy: {accuracy:.4f}")

CLI Usage

AutoFMRI provides a unified command-line interface:

# Two-stage AutoFMRI method
autofmri train --dataset haxby --data-dir ./data/haxby2001/subj4 \
    --method autofmri --stage1-model rf --stage2-model rf --atlas yeo

# Baseline: ROI + ANOVA feature selection
autofmri train --dataset hcp_emotion \
    --data-dir ./data/emotion --method baseline_anova --subjects 1 2 3

# Baseline: ROI + RFE
autofmri train --dataset hcp_gambling \
    --data-dir ./data/gambling --method baseline_rfe

Available options

Flag Description Default
--dataset haxby, hcp_emotion, hcp_gambling, hcp_language, vkmc, gtzan (required)
--data-dir Path to dataset root (required)
--method autofmri, baseline, baseline_anova, baseline_rfe autofmri
--stage1-model Model for stage 1: rf, svm, xgb, cnn rf
--stage2-model Model for stage 2 rf
--atlas Atlas for patchification: yeo, yeo400, harvard_oxford, hcp_mmp_sym yeo
--kfold Number of CV folds 4
--n-jobs Parallel jobs -1
--result-dir Output directory results/<timestamp>

Supported Datasets

Dataset Loader Task
Haxby 2001 HaxbyDataLoader Visual object recognition
HCP Emotion EmotionDataLoader Emotion recognition
HCP Gambling GamblingDataLoader Gambling task
HCP Language LanguageDataLoader Language processing
VKMC VKMCDataLoader Musical pitch decoding
GTZAN-fMRI GTZANDataLoader Music genre classification

Supported Models

Name Type Dependency
rf Random Forest Classifier scikit-learn
rfr Random Forest Regressor scikit-learn
svm Support Vector Classifier scikit-learn
svr Support Vector Regressor scikit-learn
xgb XGBoost Classifier pip install autofmri[xgboost]
xgbr XGBoost Regressor pip install autofmri[xgboost]
cnn 1D CNN (via skorch) pip install autofmri[cnn]

Project Structure

autofmri/
├── __init__.py          # Public API
├── estimator.py         # AutoEstimator (core algorithm)
├── patchify.py          # Atlas loading & brain patchification
├── cli.py               # Unified CLI
├── models/
│   ├── factory.py       # get_model() factory
│   └── cnn.py           # 1D CNN architecture
├── data/
│   ├── base.py          # BaseFMRIDataLoader (ABC)
│   ├── haxby.py         # Haxby 2001
│   ├── hcp.py           # HCP tasks (Emotion/Gambling/Language)
│   ├── vkmc.py          # VKMC musical pitch
│   └── gtzan.py         # GTZAN music genre
└── utils/
    ├── shap.py          # SHAP value computation
    ├── image.py         # NIfTI image utilities
    └── seed.py          # Reproducibility & tSNR

Previous Repository

This project was originally developed as fMRI-feature-decoder. That repository now redirects here.

Citation

If you use AutoFMRI in your research, please cite:

@INPROCEEDINGS{10095192,
  author={Cheung, Vincent K.M. and Peng, Yueh-Po and Lin, Jing-Hua and Su, Li},
  booktitle={ICASSP 2023 - 2023 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},
  title={Decoding Musical Pitch from Human Brain Activity with Automatic Voxel-Wise Whole-Brain FMRI Feature Selection},
  year={2023},
  pages={1-5},
  doi={10.1109/ICASSP49357.2023.10095192}
}

License

This project is licensed under the MIT License. See LICENSE for details.

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

autofmri-0.1.0.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

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

autofmri-0.1.0-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file autofmri-0.1.0.tar.gz.

File metadata

  • Download URL: autofmri-0.1.0.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for autofmri-0.1.0.tar.gz
Algorithm Hash digest
SHA256 edb7d9660f0928b4b9ec0a54ef5055f605c00a970babf44aa2f01322c464bf47
MD5 5f76feb0edbc535f885ad9125f66eda9
BLAKE2b-256 afc7389a96008c2ebc9a9a3323279bf0854f316ad79c34c2241c72cd3ed83243

See more details on using hashes here.

File details

Details for the file autofmri-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: autofmri-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for autofmri-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60533b9b5b052448ae98a7e6365dd825668c55a95e8cf41eae0f6c3757d8b19e
MD5 c32534c22e87de37927e8c2a1f25425b
BLAKE2b-256 be42c03b929bd4a5e186d11190f3663fc5e6ea01a2eadd1a036b1cc8085f2c96

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