An end-to-end pipeline for in-utero fetal diffusion MRI (DWI) of the baboon, written for the BaboFet dataset (weekly fetal baboon MRI, MeCA team, Institut de Neurosciences de la Timone, Marseille). From several motion-corrupted single-shell DWI stacks of one session, it produces a motion-corrected super-resolution DWI at 0.5 mm aligned with the T2-weighted super-resolution of the same session, then diffusion tensor maps and fibre orientation distributions (FODs).
- Preprocessing of each stack: denoising, Gibbs-ringing removal, susceptibility distortion correction (TOPUP with a reversed phase-encoding fieldmap, or a field estimated from the T2 when there is none), N4 bias correction, FSL eddy (eddy currents, motion, slice-to-volume).
- Brain extraction with an nnU-Net model trained on fetal baboon b=0 images.
- Super-resolution reconstruction of b=0, b=1000 and of the full DWI signal (spherical harmonics) with MIRTK/SVRTK, in the space of the T2 super-resolution.
- Diffusion modelling with MRtrix3: tensor and its metrics (FA, MD, AD, RD), constrained spherical deconvolution (CSD) FODs.
- Alignment: rigid T2 → DWI transform, and the tissue segmentation of the T2 brought into DWI space.
Method: Cazzolla M, et al. An open-source pipeline for super-resolution of fetal non-human primate diffusion MRI. ISBI 2026.
- Pipeline steps
- Inputs
- Outputs
- Requirements
- Installation
- Configuration
- Usage
- Provenance
- Known limitations
- Repository layout
00_run_pipeline.sh runs the steps below in order, for one subject and one session. Each step
is a separate script; working files go to derivatives/intermediate/svrtk/sub-X/ses-Y/.
| step | script | what it does | main tools |
|---|---|---|---|
| 1 | 01_preprocess_stacks.sh |
per stack: denoising, Gibbs removal, nnU-Net brain mask on the mean b=0, TOPUP (or a field from the T2 via FNIRT), N4 bias correction, eddy with slice-to-volume correction, eddy QC, mean b=0 and b=1000 | MRtrix, FSL, ANTs, nnU-Net |
| 1a | 01a_brain_extraction.sh |
new brain mask on the corrected b=0 of each stack; masked b=0 and b=1000 | nnU-Net, FSL |
| 2 | 02_register_stacks.sh |
picks the reference stack (structural similarity), registers every stack to it (rigid) | Python, FSL, MIRTK |
| 3a | 03a_reconstruct_b0.sh |
super-resolution of b=0 | MIRTK (container) |
| 3b | 03b_reconstruct_b1000.sh |
super-resolution of b=1000 | MIRTK (container) |
| 4 | 04_align_to_t2.sh |
registers the b=0 reconstruction to the T2 super-resolution; transforms of every stack to the T2 | FSL, MIRTK |
| 5 | 05_reconstruct_dwi.sh |
super-resolution of the full DWI signal (spherical harmonics, order 4, 0.5 mm) on the T2 grid | SVRTK (container) |
| 6 | 06_fit_tensor.sh |
rigid T2 → mean DWI registration, final 4D DWI with its b=0, tensor and metrics, response function (tournier) and CSD FODs (lmax 6) |
FSL, MRtrix |
| 7 | 07_mask_propagation.sh |
brings the tissue segmentation of the T2 into DWI space (nearest label) | c3d, ANTs |
09_tractography.sh is not called and reads an obsolete folder layout; it is kept for
reference only.
- Distortion correction, per stack. TOPUP is used when the reverse phase-encoding
fieldmap of the same run (
fmap/..._dir-PA_run-N_epi) is usable:scripts/check_fieldmap.pychecks that it lies on the same grid as the DWI (voxel axes may be stored in another order) and that its phase encoding is along the same axis with the opposite polarity, comparing the directions in anatomical space. Otherwise (no fieldmap, a fieldmap acquired with the same polarity, another grid) the field is estimated by registering the b=0 to the T2 (FLIRT then FNIRT) and passed to eddy. The log prints the check and the choice, and the provenance record keeps them per run (Runs, see Provenance). - T2-based field. FNIRT registers the b=0 to the T2 (rigidly brought onto the b=0 grid
by FLIRT), and step 1 keeps the displacement along the phase encoding. FNIRT gives it in mm,
while eddy reads
--field(Hz) timesTotalReadoutTimeas a shift in voxels. Step 1 therefore writes<run>_off_resonance_field_hz.nii.gz= displacement / (voxel size along the phase encoding ×TotalReadoutTime), with the sign of the phase-encoding polarity. - TOPUP acquisition parameters take both phase-encoding vectors in the voxel axes of the
DWI:
mrcat, which builds the b=0 pair, puts the fieldmap on those axes. - Sign convention of the phase-encoding vectors. The vectors in the acquisition files
follow the voxel axes as stored (
i-gives-1 0 0). FSL flips the first axis of images whose voxel-to-world matrix has a positive determinant, so for those images, with the phase encoding along the first axis, the sign is the opposite ofmrconvert -export_pe_eddy's. The correction does not change: TOPUP's field and the T2-based field are computed with the same vectors, so only the sign of the field maps in Hz is reversed for those stacks. - eddy acquisition parameters. With TOPUP, eddy reads TOPUP's own file
(
<run>_topup_acqp.txt, row 1 is the DWI). Without TOPUP, step 1 writes a one-row file,<run>_dwi_acqp.txt, with the DWI's phase-encoding vector andTotalReadoutTime; eddy reads the T2-based field with it.eddy_quadgets the same file as eddy. Step 1 stops when the DWI sidecar has no validPhaseEncodingDirection(i,jork, optionally with-) orTotalReadoutTime. - JSON sidecar.
mrconvert -json_importrejects arrays that are not all numbers or all strings (for exampleDeidentificationMethodCodeSequence, a list of objects, in some raw sidecars). Step 1 writes a copy of the sidecar without such entries (<run>_sidecar.jsonin the working folder) and uses it formrconvert,eddyandeddy_quad; the raw sidecar is not modified. The entries left out are logged and recorded. - Failures. Step 1 stops at the first failing command, so that a stack never goes on with
files left in the working folder by an earlier run.
eddy_quadis the exception: a failure of this QC report is logged and recorded (EddyQC: failed), and the stack goes on.
The pipeline reads a BIDS dataset (paths set in config/config.sh):
| input | path (under the BIDS root) |
|---|---|
| raw DWI stacks, single shell b=1000, phase encoding AP | sourcedata/raw/sub-X/ses-Y/dwi/sub-X_ses-Y_dir-AP_run-NN_dwi.{nii.gz,bvec,bval,json} |
| reversed phase-encoding b=0 (optional, one per run) | sourcedata/raw/sub-X/ses-Y/fmap/sub-X_ses-Y_dir-PA_run-NN_epi.{nii.gz,json} |
| T2 super-resolution (NiftyMIC), brain only | derivatives/niftymic/sub-X/ses-Y/anat/sub-X_ses-Y_rec-niftymic_desc-brain_T2w.nii.gz |
| its brain mask | derivatives/niftymic/sub-X/ses-Y/anat/sub-X_ses-Y_rec-niftymic_desc-brain_mask.nii.gz |
| tissue segmentation of the T2 | derivatives/longiseg/sub-X/ses-Y/anat/sub-X_ses-Y_desc-longiseg_dseg.nii.gz |
When the brain-only T2 is missing, step 4 builds it from
..._rec-niftymic_desc-brainbg_T2w.nii.gz and the mask, and writes it next to them.
| file | step | content |
|---|---|---|
derivatives/svrtk/sub-X/ses-Y/dwi/sub-X_ses-Y_dir-AP_run-NN_desc-brain_mask.nii.gz |
1 | nnU-Net brain mask of each raw stack |
derivatives/svrtk/sub-X/ses-Y/dwi/sub-X_ses-Y_dir-AP_rec-svrtk_SH.nii.gz |
5 | spherical-harmonic coefficients of the reconstructed signal |
derivatives/svrtk/sub-X/ses-Y/dwi/sub-X_ses-Y_rec-svrtk_dwi.{nii.gz,bvec,bval,json} |
6 | reconstructed DWI, b=0 first, 0.5 mm; the JSON sidecar gives the code version |
derivatives/svrtk/sub-X/ses-Y/dwi/sub-X_ses-Y_dir-AP_from-T2w_to-dwi_mode-image_xfm.txt |
6 | rigid T2 → DWI transform, an FSL flirt matrix (input T2, reference mean DWI) |
derivatives/svrtk/sub-X/ses-Y/dwi/sub-X_ses-Y_desc-tissue_segmentation.nii.gz |
7 | tissue segmentation of the T2 in DWI space |
derivatives/mrtrix/sub-X/ses-Y/dwi/sub-X_ses-Y_{FA,MD,AD,RD}.nii.gz |
6 | tensor metrics |
derivatives/mrtrix/sub-X/ses-Y/dwi/sub-X_ses-Y_colFA.nii.gz |
6 | principal eigenvector modulated by FA |
derivatives/mrtrix/sub-X/ses-Y/dwi/sub-X_ses-Y_tensor.nii.gz |
6 | diffusion tensor |
derivatives/{svrtk,mrtrix}/sub-X/ses-Y/sub-X_ses-Y_desc-babofetdwi_provenance.json |
all | provenance record of the run (see Provenance) |
The response function and the CSD FODs (*_response_tournier.txt, *_fod_csd.{mif,nii.gz})
are only in the working folder derivatives/intermediate/svrtk/sub-X/ses-Y/ for now
(#8).
-
Linux, bash. One session uses up to 64 threads and takes several hours; the provided Slurm script asks for 250 GB of memory.
-
Tools on the
$PATH(at INT, through the modules ofconfig/env_setup.sh). The run stops if a version differs from the one below (see Configuration):tool version INT module MRtrix3 3.0.8 mrtrix/3.0.8FSL (incl. eddy, topup, eddy_quad) 6.0.7.19 ( $FSLDIR/etc/fslversion)FSL/0.6.0.7.18ANTs 2.6.4 ANTS/0.2.6.4Singularity 3.11 tested singularityjq,git,wget,unzipany system -
conda (or mamba) to create the Python environment.
-
The pipeline must run from a git clone (the provenance record reads the commit).
git clone https://github.com/MecaLab/Babofet_DWI.git
cd Babofet_DWIThe MIRTK and SVRTK Singularity images (sif_images/) and the nnU-Net weights
(tools/nnunet/.../fold_*/checkpoint_final.pth) are not in git. Download them with:
bash scripts/download_dependencies.shThe download links currently point to a personal AMU cloud folder; moving them to a team location with checksums is #6.
All the Python code of the pipeline (nnU-Net, stack selection, padding, provenance) runs in one
conda environment, babofet_dwi, defined in environment.yml with exact versions (Python 3.12,
PyTorch 2.6.0 CPU build, nnU-Net v2 2.6.4). Create it either for yourself, by name:
conda env create -f environment.ymlor once for the whole team, in a shared folder, by path:
conda env create -f environment.yml -p /path/to/shared/envs/babofet_dwithen give that path to the pipeline with export BABOFET_DWI_ENV=/path/to/shared/envs/babofet_dwi
(in your ~/.bashrc, for example). Editing config/config.sh instead works too, but it leaves the
clone with an uncommitted change, and runs from such a clone are refused unless
BABOFET_ALLOW_DIRTY=1 (see Provenance). There is nothing to activate by
hand: 00_run_pipeline.sh activates the environment itself.
If environment.yml changes in a later version, update the environment:
conda env update -f environment.yml --pruneconfig/env_setup.sh loads the modules of the tools. The version shipped is the one of the INT
cluster:
module purge
module load all
module load ANTS/0.2.6.4
module load mrtrix/3.0.8
module load singularity
module load FSL/0.6.0.7.18Adapt it to your cluster, or delete it if the tools are already on the $PATH.
source scripts/setup_environment.sh && bash scripts/check_environment.shscripts/setup_environment.sh loads the modules, then activates the conda environment of
BABOFET_DWI_ENV and puts it first on the $PATH (the INT FSL module ships its own python3,
which must not run the pipeline's scripts). scripts/check_environment.sh checks the commands,
the tool versions, the Python packages (against environment.yml) and the downloaded files,
and prints what is missing. Every run does both before the first step.
config/config.sh, read by every step:
| variable | meaning | default |
|---|---|---|
BABOFET_BIDS_DIR |
root of the BIDS dataset | /envau/work/meca/data/BaboFet_BIDS |
RAWDATA_DIR |
raw data | ${BABOFET_BIDS_DIR}/sourcedata/raw |
DERIVATIVES_DIR |
derivatives (inputs and outputs) | ${BABOFET_BIDS_DIR}/derivatives |
OUTPUT_TMP_DIR |
working folder of each session | ${DERIVATIVES_DIR}/intermediate/svrtk |
SVR_B0_RESOLUTION, SVR_B0_ITERATIONS |
b=0 super-resolution | 0.5 mm, 6 |
SVR_B1000_RESOLUTION, SVR_B1000_ITERATIONS |
b=1000 super-resolution | 0.5 mm, 6 |
SVR_DWI_RESOLUTION, SVR_DWI_ITERATIONS, SVR_DWI_SH_ORDER, SVR_DWI_BVAL |
DWI super-resolution | 0.5 mm, 10, 4, 1000 |
mirtk_path, svrtk_path, C3D_TOOL_PATH |
containers and c3d tool | under the clone |
BABOFET_DWI_ENV |
conda environment: a name or a path | babofet_dwi |
EXPECTED_MRTRIX_VERSION, EXPECTED_FSL_VERSION, EXPECTED_ANTS_VERSION |
versions required at start | 3.0.8, 6.0.7.19, 2.6.4 |
NTHR |
threads | 64 |
EXCLUDE_RUNS |
raw runs to skip, as sub-X_ses-Y_dir-AP_run-NN |
two runs of Aziza 01 |
config/config.sh is part of the code: a change must be committed before a run (see
Provenance), and its full text is kept in the provenance record of every run.
Run every command from the root of the clone. There is no need to load modules or activate the environment beforehand.
bash 00_run_pipeline.sh sub-Aziza ses-01sbatch sbatch_run.sh sub-Aziza ses-01sbatch_run.sh submits one session (partition, memory and time are set in its header); the
logs go to logs2/<job id>.out and .err. A job array for a batch of sessions is
#11.
Comment out the steps to skip in 00_run_pipeline.sh (this makes the working tree dirty: see
BABOFET_ALLOW_DIRTY below). The steps read the files of the earlier steps from the working
folder.
# Example: skip the preprocessing
#echo "STEP 1: Preprocessing individual DWI stacks..."
#run_step 01_preprocess_stacks.sh
#echo "✅ STEP 1 complete."
#echo "-----------------------------------------"
#echo "STEP 1a: Extracting brain mask..."
#run_step 01a_brain_extraction.sh
#echo "✅ STEP 1a complete."
#echo "-----------------------------------------"
echo "STEP 2: Registering stacks to reference..."
run_step 02_register_stacks.sh
...Skipped steps are absent from the provenance record of the run; the steps of an earlier run
stay listed under PreviousRuns.
| variable | effect |
|---|---|
BABOFET_DWI_ENV=<name or path> |
use another conda environment than the one of config/config.sh |
BABOFET_ALLOW_DIRTY=1 |
run even though the clone has uncommitted changes (recorded as dirty) |
BABOFET_ALLOW_VERSION_MISMATCH=1 |
run even though a tool or Python package differs from the expected version (recorded) |
For example: BABOFET_ALLOW_DIRTY=1 bash 00_run_pipeline.sh sub-Aziza ses-01.
Every run writes one JSON record per session (scripts/provenance.py, Python standard library
only):
- the code version: commit,
git describe, branch, and whether the working tree had uncommitted changes (with the list of files and a hash of the diff); - tool versions (MRtrix, FSL, ANTs, Singularity, jq), loaded modules, conda environment, Slurm job, host;
- the name and version of every Python package of the conda environment;
- SHA-256 of the two containers (cached next to each image in
<image>.sha256); - the full text of
config/config.shandconfig/env_setup.sh; - the inputs (raw DWI and fieldmaps, T2 super-resolution and its mask, tissue segmentation) with SHA-256;
- each step with its start, end and status, and the failed step if any;
- per run (stack), under
Runs: the fieldmap check, the distortion correction used (topuport2-registration), the sidecar entries left out, and a failed eddy QC if any; - the files of the session's derivative folders, with SHA-256 for those written by this run and
NotWrittenByThisRunfor the others (leftovers of earlier runs); - a summary of earlier runs of the same session (
PreviousRuns).
Where it goes:
| file | content |
|---|---|
derivatives/intermediate/svrtk/sub-X/ses-Y/sub-X_ses-Y_provenance.json |
the record, updated during the run |
derivatives/{svrtk,mrtrix}/sub-X/ses-Y/sub-X_ses-Y_desc-babofetdwi_provenance.json |
copies, written at the end (success or failure) |
derivatives/svrtk/sub-X/ses-Y/dwi/sub-X_ses-Y_rec-svrtk_dwi.json |
BIDS sidecar: SkullStripped, code version, pointer to the record |
derivatives/{svrtk,mrtrix}/dataset_description.json |
GeneratedBy gains one entry per code version that wrote into the dataset |
The run refuses to start when the pipeline has uncommitted changes (file-mode changes and
untracked files are ignored), so that every output can be traced to a commit. To run anyway, set
BABOFET_ALLOW_DIRTY=1; the record and the version string then say dirty.
- Steps 1a, 4, 5 and 7 do not stop at the first failing command (no
set -e): a failure inside them can go unnoticed, and the step is recorded as finished. - Files of an earlier run stay in the working folder and can be mistaken for new ones by a step that does not stop on errors. Run a session again from an empty working folder.
- The T2-based distortion correction (stacks without a usable fieldmap) failed in eddy in every earlier version of this repository (eddy was given TOPUP's acquisition file, which only exists with TOPUP), and its field was too strong by the voxel size along the phase encoding (the displacement in mm was used as if in voxels). Stacks corrected this way by an earlier version must be processed again.
- The CSD FODs and response stay in the working folder: #8.
- The run exclusions (
EXCLUDE_RUNS) and the segmentation propagated in step 7 are not yet versioned per session: #9. - The working folder keeps about 13 GB per session: #10.
00_run_pipeline.sh runs all steps for one session
01_ ... 07_*.sh the steps (see Pipeline steps)
09_tractography.sh unused, kept for reference
sbatch_run.sh Slurm submission of one session
environment.yml conda environment of the Python code
config/config.sh paths, parameters, environment name, expected tool versions
config/env_setup.sh module loads (INT cluster)
scripts/ helpers: nnU-Net brain mask, fieldmap check, reference-stack
selection, padding, provenance, environment check, dependency download
tools/ c3d affine tool, identity transforms, nnU-Net model metadata
images/ figures of this README
