Skip to main content

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.

Metadata

Release files for autofmri 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autofmri 0.1.0
File Size Uploaded Provenance
autofmri-0.1.0.tar.gz 24.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for autofmri 0.1.0
File Interpreter ABI Platform
autofmri-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 51.3 kB

Release files / autofmri-0.1.0.tar.gz

Download URL autofmri-0.1.0.tar.gz
Size 24.6 kB
Tags Source
SHA-256 checksum
How to use checksums
edb7d9660f0928b4b9ec0a54ef5055f605c00a970babf44aa2f01322c464bf47
BLAKE2b-256 checksum
How to use checksums
afc7389a96008c2ebc9a9a3323279bf0854f316ad79c34c2241c72cd3ed83243
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.13

Release files / autofmri-0.1.0-py3-none-any.whl

Download URL autofmri-0.1.0-py3-none-any.whl
Size 26.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60533b9b5b052448ae98a7e6365dd825668c55a95e8cf41eae0f6c3757d8b19e
BLAKE2b-256 checksum
How to use checksums
be42c03b929bd4a5e186d11190f3663fc5e6ea01a2eadd1a036b1cc8085f2c96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.13

Security for autofmri 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page