Skip to main content

pyAFQ — Automated Fiber Quantification

Overview​

pyAFQ is a Python package for automated identification and quantification of major white matter tracts. It takes BIDS-formatted diffusion data and produces tract profiles — measures of diffusion properties (FA, MD, RD, AD) sampled at regular intervals along the length of each white matter bundle.

After preprocessing, pyAFQ identifies tracts like the corticospinal tract, arcuate fasciculus, and corpus callosum, then profiles how diffusion metrics vary along each tract. These profiles are the primary output used in statistical analyses.

GitHub: https://github.com/yeatmanlab/pyAFQ Documentation: https://yeatmanlab.github.io/pyAFQ/

Installation​

# Create a dedicated virtual environment (recommended)
python3 -m venv ~/envs/pyafq
source ~/envs/pyafq/bin/activate

pip install pyAFQ

Option 2: conda + pip​

conda create -n pyafq python=3.10
conda activate pyafq
pip install pyAFQ

Option 3: Development Version​

git clone https://github.com/yeatmanlab/pyAFQ.git
cd pyAFQ
pip install -e ".[dev]"

pyAFQ works best with Python 3.9–3.11. Check compatibility with your Python version on the pyAFQ releases page.

Verify Installation​

# Check that pyAFQ is importable
python -c "import AFQ; print(AFQ.__version__)"

# Check the CLI tool
pyAFQ --help

BIDS Input Requirements​

pyAFQ requires data organized in BIDS format. Specifically, it expects preprocessed diffusion data in a BIDS derivatives directory:

my_study/
derivatives/
dmriprep/ # or whatever you name your derivatives folder
dataset_description.json # Required by BIDS
sub-001/
ses-01/ # sessions are optional
dwi/
sub-001_ses-01_dwi.nii.gz
sub-001_ses-01_dwi.bval
sub-001_ses-01_dwi.bvec
anat/
sub-001_ses-01_T1w.nii.gz
sub-002/
...

dataset_description.json​

Every BIDS derivatives directory needs this file. A minimal version:

{
"Name": "My DTI Preprocessing",
"BIDSVersion": "1.6.0",
"PipelineDescription": {
"Name": "custom-dti-pipeline"
}
}

See BIDS & pyAFQ for a complete guide to organizing your preprocessed data into BIDS format.

Running pyAFQ​

Python API​

import AFQ.api.group as afq

myafq = afq.GroupAFQ(
bids_path="/path/to/my_study",
preproc_pipeline="dmriprep",
)

# Run the full pipeline
myafq.export_all()

Command Line​

# Generate a default configuration file
pyAFQ config --output afq_config.toml

# Edit afq_config.toml to match your study

# Run
pyAFQ run afq_config.toml

Configuration File (TOML)​

The configuration file controls all aspects of pyAFQ processing. Key settings:

[BIDS]
bids_path = "/path/to/my_study"
dmriprep = "dmriprep"

[TRACTOGRAPHY]
# "ift" = iterative fiber tracking (default)
# "pft" = particle filter tracking (better anatomical priors)
tractography_type = "ift"
n_seeds = 2 # seeds per voxel

[SEGMENTATION]
# Which bundles to identify
bundle_info = "default" # uses standard 24-bundle atlas

[CLEANING]
# Remove outlier streamlines
clean_rounds = 5
distance_threshold = 5

pyAFQ Outputs​

Tract Profiles​

The primary output — a CSV/JSON table with diffusion metrics (FA, MD, AD, RD) sampled at 100 points along each identified tract, for each subject:

subjecttractIDnodeIDFAMDRDAD
sub-001CST_L00.450.00080.00050.0014
sub-001CST_L10.470.00070.00050.0013
.....................

Bundle Segmentations​

3D NIfTI images showing which voxels belong to each identified tract. These can be visualized in FSLeyes or mrview.

Visualizations​

pyAFQ generates interactive HTML visualizations of the identified tracts using FURY or Plotly. You can also use AFQ-Browser for interactive web-based exploration of results.

Common Bundles Identified​

pyAFQ identifies 24 major white matter bundles by default, including:

BundleAbbreviationFunction
Corticospinal TractCST (L/R)Motor control
Arcuate FasciculusARC (L/R)Language processing
Superior Longitudinal FasciculusSLF (L/R)Attention, spatial processing
Inferior Longitudinal FasciculusILF (L/R)Visual processing
Uncinate FasciculusUNC (L/R)Memory, emotion
Corpus Callosum (forceps)FA, FPInterhemispheric communication
CingulumCIN (L/R)Limbic function

Common Issues​

IssueCauseSolution
"No diffusion data found"BIDS directory structure incorrectCheck that file naming follows BIDS conventions exactly; run the BIDS Validator
Missing dataset_description.jsonRequired BIDS file not presentCreate it in your derivatives directory (see example above)
Poor tract segmentationLow-quality preprocessing or few gradient directionsCheck FA maps; ensure eddy correction worked properly; more directions = better tract identification
Out of memoryTractography with too many seedsReduce n_seeds in configuration
Slow processingNormal for full tractographyExpect 30–60 min per subject; use n_seeds=1 for testing

References​

  • Yeatman JD, Dougherty RF, Myall NJ, Wandell BA, Feldman HM (2012). Tract profiles of white matter properties: automating fiber-tract quantification. PLoS One, 7(11), e49790.
  • Kruper J, et al. (2021). Evaluating the reliability of human brain white matter tractometry. Aperture Neuro, 1(6).
  • pyAFQ Documentation — Full API reference and tutorials
  • pyAFQ Examples — Jupyter notebook examples