A robust, multi-modal Bash script for converting DICOM datasets into BIDS (Brain Imaging Data Structure) format using dcm2niix.
- Multi-modal support — anat, func, dwi, fmap, perf, pet, meg, eeg, ieeg, micr, motion
- Automatic folder detection — maps common DICOM folder naming conventions to BIDS modalities via configurable pattern matching
- Session-aware — automatically detects flat vs. multi-session directory structures
- Run numbering — auto-numbers duplicate acquisitions with BIDS
run-entities - Source archival — optionally copies originals into
sourcedata/before converting - Safe deletion — deletes source DICOMs only after verifying successful NIfTI output for that specific conversion
- Dry-run mode — preview every action without modifying anything
- Parallel conversion — run N subject conversions simultaneously with
-p N - JSON enrichment — automatically injects
TaskNameandRepetitionTimeinto sidecars; warns whenIntendedForneeds to be set manually in fieldmap sidecars - Full BIDS scaffold — generates
dataset_description.json,participants.tsv,README,CHANGES, and.bidsignore - Conversion logs — dcm2niix logs preserved in
logs/for post-hoc QC
| Tool | Required | Notes |
|---|---|---|
| dcm2niix | ✅ | DICOM to NIfTI converter (install) |
| python3 | ✅ | Used for JSON sidecar enrichment |
| jq | Optional | Falls back to python3 if not installed |
| bash ≥ 4.0 | ✅ | Uses associative arrays and extended globbing |
# Ubuntu / Debian
sudo apt-get install dcm2niix python3 jq
# macOS (Homebrew)
brew install dcm2niix python3 jq
# Conda
conda install -c conda-forge dcm2niix# Clone the repository
git clone https://github.com/<your-username>/bids-convert.git
cd bids-convert
# Make the script executable
chmod +x bids_convert.sh
# Basic conversion
./bids_convert.sh -i /path/to/dicoms -o /path/to/BIDS
# Preview without making changes
./bids_convert.sh -i /path/to/dicoms -o /path/to/BIDS --dry-run --verbose./bids_convert.sh -i <source_dir> [OPTIONS]
REQUIRED:
-i, --input DIR Source data root directory
OPTIONS:
-o, --output DIR BIDS output directory [default: ./BIDS]
-c, --config FILE Folder-to-modality mapping [default: auto-detect]
-d, --delete-source Delete source files after successful conversion
-s, --sourcedata Archive originals in BIDS sourcedata/
-n, --dry-run Preview without executing
-p, --parallel N Parallel conversion jobs [default: 1]
-v, --verbose Verbose logging
-h, --help Show help
# Convert with archival + source cleanup
./bids_convert.sh -i /data/raw -o /data/BIDS -s -d
# Use a custom mapping config
./bids_convert.sh -i /data/raw -o /data/BIDS -c my_mapping.conf
# Parallel conversion (4 subjects at a time)
./bids_convert.sh -i /data/raw -o /data/BIDS -p 4The script supports two directory layouts, detected automatically:
raw/
├── SUBJECT_001/
│ ├── SAG_T1/
│ ├── BOLD_REST/
│ └── DTI_64DIR/
└── SUBJECT_002/
├── SAG_T1/
└── BOLD_REST/
raw/
├── SUBJECT_001/
│ ├── SESSION_01/
│ │ ├── SAG_T1/
│ │ └── BOLD_REST/
│ └── SESSION_02/
│ ├── SAG_T1/
│ └── BOLD_REST/
Generate a sample config, then edit it:
# Run once to generate sample_mapping.conf in the output directory
./bids_convert.sh -i /data/raw -o /data/BIDS
# Edit the generated file
vim /data/BIDS/sample_mapping.conf
# Re-run with custom mapping
./bids_convert.sh -i /data/raw -o /data/BIDS -c /data/BIDS/sample_mapping.conf# <source_folder_pattern> <bids_modality> <bids_suffix> [task_label]
SAG*T1* anat T1w
*MPRAGE* anat T1w
*DTI* dwi dwi
*REST*BOLD* func bold rest
*NBACK* func bold nback
*PHASE*DIFF* fmap phasediff
*ASL* perf asl
*PET* pet pet
*MEG* meg meg
*EEG* eeg eeg
Patterns use bash globbing and are matched case-insensitively. More specific patterns should appear before general ones — the first match wins.
The script produces a BIDS-compliant directory:
BIDS/
├── dataset_description.json
├── participants.tsv
├── participants.json
├── README
├── CHANGES
├── .bidsignore
├── logs/ ← dcm2niix conversion logs
├── sub-001/
│ ├── anat/
│ │ ├── sub-001_T1w.nii.gz
│ │ └── sub-001_T1w.json
│ ├── func/
│ │ ├── sub-001_task-rest_bold.nii.gz
│ │ └── sub-001_task-rest_bold.json
│ └── dwi/
│ ├── sub-001_dwi.nii.gz
│ ├── sub-001_dwi.json
│ ├── sub-001_dwi.bval
│ └── sub-001_dwi.bvec
After conversion, you should:
- Validate — run
bids-validatoron the output directory - Update metadata — fill in
dataset_description.jsonwith your study details - Add demographics — update
participants.tsvwith age, sex, and group info - Review task labels — rename any
task-unknownlabels to their correct task names - Set IntendedFor — add
IntendedForpaths in all fieldmap JSON sidecars (the script will warn you which files need this) - Verify JSON sidecars — confirm acquisition parameters are correct
# Install and run the BIDS validator
pip install bids-validator
bids-validator /path/to/BIDSContributions are welcome! Please see CONTRIBUTING.md for guidelines.
This project is licensed under the MIT License — see LICENSE for details.
See CHANGELOG.md for release history.