Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Nipype-FSL fMRI Analysis

Run this notebook

First-level, second-level and third-level GLM

Author: Monika Doerig

Date: 21 Oct 2023

License:

MIT License

Note: If this notebook uses neuroimaging tools from Neurocontainers, those tools retain their original licenses. Please see Neurodesk citation guidelines for details.

Citation and Resources:

Workflows this work is based on

Original paper and code:

  • Chen Y, Hopp FR, Malik M, Wang PT, Woodman K, Youk S and Weber R (2022) Reproducing FSL’s fMRI data analysis via Nipype: Relevance, challenges, and solutions. Front. Neuroimaging 1:953215. doi: 10.3389/fnimg.2022.953215

  • Original code on OSF

Tools included in this workflow

FSL:

Nipype:

  • Esteban, O., Markiewicz, C. J., Burns, C., Goncalves, M., Jarecka, D., Ziegler, E., Berleant, S., Ellis, D. G., Pinsard, B., Madison, C., Waskom, M., Notter, M. P., Clark, D., Manhães-Savio, A., Clark, D., Jordan, K., Dayan, M., Halchenko, Y. O., Loney, F., … Ghosh, S. (2025). nipy/nipype: 1.8.6 (1.8.6). Zenodo Esteban et al. (2025)

Dataset

Flanker Dataset from OpenNeuro:

  • Kelly AMC and Uddin LQ and Biswal BB and Castellanos FX and Milham MP (2018). Flanker task (event-related). OpenNeuro Dataset ds000102

  • Kelly, A.M., Uddin, L.Q., Biswal, B.B., Castellanos, F.X., Milham, M.P. (2008). Competition between functional brain networks mediates behavioral variability. Neuroimage, 39(1):527-377-37

First-level GLM using Nipype FSL

In this notebook, we recreate the first-level GLM and the first level GLM of FSL GUI using nipype code. For each nipype node, we list the corresponding fsl command from the log file. The dataset we use is a Flanker task, which can be downloaded here.

We also borrow some help from this document.

['fsl/6.0.4']

Preparation

Import all the relevant libraries needed for the preprocessing stage.

Collecting pandas==2.3.3
  Downloading pandas-2.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.metadata (91 kB)
Collecting nibabel==5.3.3
  Downloading nibabel-5.3.3-py3-none-any.whl.metadata (9.1 kB)
Requirement already satisfied: nilearn==0.13.1 in /opt/conda/lib/python3.13/site-packages (0.13.1)
Requirement already satisfied: nipype==1.11.0 in /opt/conda/lib/python3.13/site-packages (1.11.0)
Requirement already satisfied: numpy>=1.26.0 in /opt/conda/lib/python3.13/site-packages (from pandas==2.3.3) (2.4.6)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.13/site-packages (from pandas==2.3.3) (2.9.0.post0)
Collecting pytz>=2020.1 (from pandas==2.3.3)
  Downloading pytz-2026.2-py2.py3-none-any.whl.metadata (22 kB)
Requirement already satisfied: tzdata>=2022.7 in /opt/conda/lib/python3.13/site-packages (from pandas==2.3.3) (2026.2)
Requirement already satisfied: packaging>=20 in /opt/conda/lib/python3.13/site-packages (from nibabel==5.3.3) (26.2)
Requirement already satisfied: jinja2>=3.1.2 in /opt/conda/lib/python3.13/site-packages (from nilearn==0.13.1) (3.1.6)
Requirement already satisfied: joblib>=1.2.0 in /opt/conda/lib/python3.13/site-packages (from nilearn==0.13.1) (1.5.3)
Requirement already satisfied: requests>=2.30.0 in /opt/conda/lib/python3.13/site-packages (from nilearn==0.13.1) (2.33.1)
Requirement already satisfied: scikit-learn>=1.4.0 in /opt/conda/lib/python3.13/site-packages (from nilearn==0.13.1) (1.9.0)
Requirement already satisfied: scipy>=1.9.0 in /opt/conda/lib/python3.13/site-packages (from nilearn==0.13.1) (1.17.1)
Requirement already satisfied: acres>=0.3.0 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (0.5.0)
Requirement already satisfied: click>=6.6.0 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (8.4.1)
Requirement already satisfied: etelemetry>=0.3.1 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (0.3.1)
Requirement already satisfied: filelock>=3.0.0 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (3.29.1)
Requirement already satisfied: looseversion!=1.2,>=1.0.1 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (1.3.0)
Requirement already satisfied: lxml>=4.9.1 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (6.1.1)
Requirement already satisfied: networkx>=2.5 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (3.6.1)
Requirement already satisfied: prov>=1.5.2 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (2.1.1)
Requirement already satisfied: puremagic>=1.15 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (2.2.0)
Requirement already satisfied: pydot>=1.2.3 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (4.0.1)
Requirement already satisfied: rdflib>=5.0.0 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (7.6.0)
Requirement already satisfied: simplejson>=3.8.0 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (4.1.1)
Requirement already satisfied: traits>=6.2 in /opt/conda/lib/python3.13/site-packages (from nipype==1.11.0) (7.1.0)
Requirement already satisfied: ci-info>=0.2 in /opt/conda/lib/python3.13/site-packages (from etelemetry>=0.3.1->nipype==1.11.0) (0.4.0)
Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.13/site-packages (from jinja2>=3.1.2->nilearn==0.13.1) (3.0.3)
Requirement already satisfied: pyparsing>=3.1.0 in /opt/conda/lib/python3.13/site-packages (from pydot>=1.2.3->nipype==1.11.0) (3.3.2)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas==2.3.3) (1.17.0)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.13/site-packages (from requests>=2.30.0->nilearn==0.13.1) (3.4.7)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.13/site-packages (from requests>=2.30.0->nilearn==0.13.1) (3.13)
Requirement already satisfied: urllib3<3,>=1.26 in /opt/conda/lib/python3.13/site-packages (from requests>=2.30.0->nilearn==0.13.1) (2.6.3)
Requirement already satisfied: certifi>=2023.5.7 in /opt/conda/lib/python3.13/site-packages (from requests>=2.30.0->nilearn==0.13.1) (2026.4.22)
Requirement already satisfied: narwhals>=2.0.1 in /opt/conda/lib/python3.13/site-packages (from scikit-learn>=1.4.0->nilearn==0.13.1) (2.22.0)
Requirement already satisfied: threadpoolctl>=3.5.0 in /opt/conda/lib/python3.13/site-packages (from scikit-learn>=1.4.0->nilearn==0.13.1) (3.6.0)
Downloading pandas-2.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (12.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.3/12.3 MB 26.8 MB/s  0:00:00
Downloading nibabel-5.3.3-py3-none-any.whl (3.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 53.9 MB/s  0:00:00
Downloading pytz-2026.2-py2.py3-none-any.whl (510 kB)
Installing collected packages: pytz, nibabel, pandas
  Attempting uninstall: nibabel
    Found existing installation: nibabel 5.4.2
    Uninstalling nibabel-5.4.2:
      Successfully uninstalled nibabel-5.4.2
  Attempting uninstall: pandas
    Found existing installation: pandas 3.0.3
    Uninstalling pandas-3.0.3:
      Successfully uninstalled pandas-3.0.3
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3/3 [pandas]
Successfully installed nibabel-5.3.3 pandas-2.3.3 pytz-2026.2
Patched nipype MultipleRegressDesign for numpy>=2: /opt/conda/lib/python3.13/site-packages/nipype/interfaces/fsl/model.py
NIFTI_GZ
6.0.4:ddd0a010
Set up data paths
<nilearn.plotting.displays._slicers.OrthoSlicer at 0x7a7688cd78c0>
<Figure size 730x350 with 5 Axes>
Fetching long content....
Start the workflow

The following two nodes (infosource & dg) together define all inputs required for the preprocessing workflow

['01', '02', '03', '04', '05', '06', '07', '08', '09']

Initialisation

Convert functional images to float representation. Since there can be more than one functional run we use a MapNode to convert each run.

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths ../sub-11/func/sub-11_task-flanker_run-1_bold prefiltered_func_data -odt float

Extract the middle volume of the first run as the reference

(Head movement, motion-correction)

Corresponding FSL command:

/usr/local/fsl/bin/fslroi prefiltered_func_data example_func 73 1

Preprocessing

Motion Correction

Realign the functional runs to the middle volume of each run

Corresponding FSL command:

/usr/local/fsl/bin/mcflirt -in prefiltered_func_data -out prefiltered_func_data_mcf -mats -plots -reffile example_func -rmsrel -rmsabs -spline_final
save_mats (a boolean) – Save transformation matrices. Maps to a command-line argument: -mats.
save_plots (a boolean) – Save transformation parameters. Maps to a command-line argument: -plots.
save_rms (a boolean) – Save rms displacement parameters. Maps to a command-line argument: -rmsabs -rmsrel.
Interpolation (‘spline’ or ‘nn’ or ‘sinc’) – Interpolation method for transformation. Maps to a command-line argument: -%s_final.

Plot the estimated motion parameters

Corresponding FSL command:

/usr/local/fsl/bin/fsl_tsplot -i prefiltered_func_data_mcf.par -t 'MCFLIRT estimated rotations (radians)' -u 1 --start=1 --finish=3 -a x,y,z -w 640 -h 144 -o rot.png 

/usr/local/fsl/bin/fsl_tsplot -i prefiltered_func_data_mcf.par -t 'MCFLIRT estimated translations (mm)' -u 1 --start=4 --finish=6 -a x,y,z -w 640 -h 144 -o trans.png 

/usr/local/fsl/bin/fsl_tsplot -i prefiltered_func_data_mcf_abs.rms,prefiltered_func_data_mcf_rel.rms -t 'MCFLIRT estimated mean displacement (mm)' -u 1 -w 640 -h 144 -a absolute,relative -o disp.png

Functionally Masking

Passing the reference volume to the FSL command-line tool bet to generate a binary brain mask and afterward multiplying the processed functional time series by the brain mask using the fslmaths command to produce a skull-stripped time series.

See Ciric et al.(2018)

Extract the mean volume of each functional run

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_mcf -Tmean mean_func

Strip the skull from the mean functional to generate a mask

Corresponding FSL command:

/usr/local/fsl/bin/bet2 mean_func mask -f 0.3 -n -m; /usr/local/fsl/bin/immv mask_mask mask

Mask the functional data with the extracted mask

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_mcf -mas mask prefiltered_func_data_bet

Grand Mean Scaling

Determine the 2nd and 98th percentile intensities

Corresponding FSL command:

/usr/local/fsl/bin/fslstats prefiltered_func_data_bet -p 2 -p 98
0.000000 873.492249 (these numbers are for subject-11 run-01)

More info here

Threshold the first TR of the functional data at 10% of the 98th percentile

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_bet -thr 87.3492249 -Tmin -bin mask -odt char

Determine the median value of the TRs using the mask

Corresponding FSL command:

/usr/local/fsl/bin/fslstats prefiltered_func_data_mcf -k mask -p 50
728.800232 (this number is for subject-11 run-01)

Dilate the mask

The brain mask is “dilated” slightly before being used. Because it is normally important that masking be liberal (ie that there be little risk of cutting out valid brain voxels)

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths mask -dilF mask

The output of dilatemask (i.e., /srv/scratch/yc/fsl/hw2/level1/_subject_id_26/dilatemask/mapflow/_dilatemask0/sub-26_task-flanker_run-1_bold_dtype_mcf_bet_thresh_dil.nii.gz) is equivalent to mask.nii.gz from FSL GUI.

Mask the motion corrected functional runs with the dilated mask

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_mcf -mas mask prefiltered_func_data_thresh

SUSAN Noise Reduction

Determine the mean image from each TR

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_thresh -Tmean mean_func

Merge the median values with the mean functional images into a coupled list

The output of this merge node will go into susan as usans,

Smooth each run using SUSAN with the brightness threshold set to 75% of the median value for each run and a mask constituting the mean functional

Usage: susan <input> <bt> <dt> <dim> <use_median> <n_usans> [<usan1> <bt1> [<usan2> <bt2>]] <output>
<bt> is brightness threshold and should be greater than noise level and less than contrast of edges to be preserved.
<dt> is spatial size (sigma, i.e., half-width) of smoothing, in mm.
<dim> is dimensionality (2 or 3), depending on whether smoothing is to be within-plane (2) or fully 3D (3).
<use_median> determines whether to use a local median filter in the cases where single-point noise is detected (0 or 1).
<n_usans> determines whether the smoothing area (USAN) is to be found from secondary images (0, 1 or 2).
A negative value for any brightness threshold will auto-set the threshold at 10% of the robust range

Corresponding FSL command:

/usr/local/fsl/bin/susan prefiltered_func_data_thresh 546.600174 2.12314225053 3 1 1 mean_func 546.600174 prefiltered_func_data_smooth

Note:

for <bt>, Nipype uses a different algorithm to calculate it -> float(fwhm) / np.sqrt(8 * np.log(2)). Therefore, to get 2.12314225053, fwhm should be 4.9996179300001655 instead of 5

Mask the smoothed data with the dilated mask

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_smooth -mas mask prefiltered_func_data_smooth

Scale each volume of the TR so that the median value of the TR is set to 10000

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_smooth -mul 13.7211811425 prefiltered_func_data_intnorm 
(this number is for subject-11 run-01)

Temporal Filtering

Perform temporal highpass filtering on the data

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_intnorm -Tmean tempMean
/usr/local/fsl/bin/fslmaths prefiltered_func_data_intnorm -bptf 25.0 -1 -add tempMean prefiltered_func_data_tempfilt

The output of highpass (i.e., sub-11_task-flanker_run-1_bold_dtype_mcf_mask_smooth_mask_intnorm_tempfilt.nii.gz) is equivalent to filtered_func_data.nii.gz from FSL GUI.

Generate a mean functional image from the functional run

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths prefiltered_func_data_tempfilt filtered_func_data
/usr/local/fsl/bin/fslmaths filtered_func_data -Tmean mean_func

First-Level GLM

Get events

Set-up contrasts

Corresponding fsl command:

/usr/local/fsl/bin/film_gls --in=filtered_func_data --rn=stats --pd=design.mat --thr=1000.0 --sa --ms=5 --con=design.con  

--thr: threshold
--sa: smooth_autocorr
--ms: mask_size

no need to use ContrastMgr,

In interface mode this file assumes that all the required inputs are in the same location. This has deprecated for FSL versions 5.0.7+ as the necessary corrections file is no longer generated by FILMGLS.

see this link

Registration

According to FSL UserGuide, the different sessions need to be registered to each other before any multi-session or multi-subject analyses can be carried out. Registration inside FEAT uses FLIRT and is a two-statge process:

  1. An example FMRI low resolution image is registered to an example high resolution image (normally the same subject’s T1-weighted structural). The transformation for this is saved into the FEAT directory. Then the high res image is registered to a standard image (normally a T1-weighted image in standard space, such as the MNI 152 average image).

  2. The two transformations are combined into a third, which will take the low resolution FMRI images (and the statistic images derived from the first-level analyses) straight into standard space, when applied later, during group analysis.

Step 1

Corresponding FSL command:

/usr/local/fsl/bin/flirt -in example_func -ref standard -out example_func2standard -omat example_func2standard.mat -cost corratio -dof 12 -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -interp trilinear 

/usr/local/fsl/bin/convert_xfm -inverse -omat standard2example_func.mat example_func2standard.mat
Step 2

Corresponding fsl command:

/usr/local/fsl/bin/flirt -ref reg/standard -in stats/cope1 -out /srv/scratch/yc/fsl/nipype_fsl_comp/gui/sub-01/run1.feat/frgrot_chksugax -applyxfm -init reg/example_func2standard.mat -interp trilinear -datatype float
260617-05:27:40,227 nipype.workflow INFO:
	 Generated workflow graph: /home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1/graph.png (graph2use=colored, simple_form=True).
<IPython.core.display.Image object>
<networkx.classes.digraph.DiGraph at 0x7a76bba18cd0>

The first-level GLM

Here we randomly choose the four copes from subject-09 run-1

<Figure size 660x350 with 4 Axes>
<Figure size 660x350 with 4 Axes>
<Figure size 660x350 with 4 Axes>
<Figure size 660x350 with 4 Axes>

Second level GLM using Nipype FSL

The following two nodes (infosource & dg) together define all inputs required for the preprocessing workflow

Second-level GLM

Combining results from multiple runs of one subject into one

Higher-level input files preparation

Step 1: Merge registered copes & varcopes & masks

Corresponding FSL command:

/usr/local/fsl/bin/fslmerge -t mask (masks from all 52 inputs)
/usr/local/fsl/bin/fslmerge -t cope (copes from all 52 inputs)
/usr/local/fsl/bin/fslmerge -t varcop (varcopes from all 52 inputs)
Step 2: Making mask

In FSL, there are many commands about maskunique, which is unless for the second level. We can ignore it.

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths mask -Tmin mask
Step 3: Masking copes & varcopes

Corresponding FSL command:

we have four contrasts so the following commands repeat four times

/usr/local/fsl/bin/fslmaths cope1 -mas mask cope1
/usr/local/fsl/bin/fslmaths varcope1 -mas mask varcope1

Set up second-level contrasts and fixed-effects

Nipype recommends using L2Model, which only works for the single subject. It takes the number of runs (copes at the first level) as input and does estimations for subject one by one. Instead, we use MultipleRegressDesign. As it’s name indicates, this one can deal with multiple predictors (subjects) at the same time.

<IPython.core.display.Image object>
['/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc1/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc0/cope2_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc1/cope2_flirt.nii.gz']['/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc1/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc0/cope1_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc1/cope1_flirt.nii.gz']

1818

['/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc1/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc0/cope3_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc1/cope3_flirt.nii.gz']
18
['/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/01/reg_copes/_subject_id_01/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/02/reg_copes/_subject_id_02/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/03/reg_copes/_subject_id_03/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/04/reg_copes/_subject_id_04/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/05/reg_copes/_subject_id_05/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/06/reg_copes/_subject_id_06/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/07/reg_copes/_subject_id_07/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/08/reg_copes/_subject_id_08/_warpfunc1/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc0/cope4_flirt.nii.gz', '/home/jovyan/workspace/books/examples/functional_imaging/output_level1/level1_results/09/reg_copes/_subject_id_09/_warpfunc1/cope4_flirt.nii.gz']
18
<networkx.classes.digraph.DiGraph at 0x7a76bb979ba0>

The second-level GLM

Here we choose the four copes from subject-01

Regapply

This happens before the second-level GLM, all results from the first level are needed to be registered into the standard space

<Figure size 660x350 with 4 Axes>
<Figure size 660x350 with 4 Axes>
<Figure size 660x350 with 4 Axes>
<Figure size 660x350 with 4 Axes>

Copes from the second-level GLM

Copes from subject-01:

<Figure size 730x350 with 5 Axes>
<Figure size 730x350 with 5 Axes>
<Figure size 730x350 with 5 Axes>
<Figure size 730x350 with 5 Axes>

Third-level GLM using Nipype FSL

Start the workflow

The following two nodes (infosource & dg) together define all inputs required for the preprocessing workflow

Third-level GLM

Combining results from multiple runs of one subject into one

Higher-level input files preparation

Step 1: Merge registered copes & varcopes & masks

Corresponding FSL command:

/usr/local/fsl/bin/fslmerge -t mask (masks from all 26 inputs)
/usr/local/fsl/bin/fslmerge -t cope (copes from all 26 inputs)
/usr/local/fsl/bin/fslmerge -t varcop (varcopes from all 26 inputs)
Step 2: Making mask

In FSL, there are many commands about maskunique, which is unless for the second level. We can ignore it.

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths mask -Tmin mask
Step 3: Masking copes & varcopes

Corresponding FSL command:

we have four contrasts so the following commands repeat four times

/usr/local/fsl/bin/fslmaths cope1 -mas mask cope1
/usr/local/fsl/bin/fslmaths varcope1 -mas mask varcope1

Set up third-level contrasts

Since we only have a single-group set-up, we can actually use L2Model. If we have an ANOVA-like design, using MultipleRegressDesign would be a better option.

Post-Stats

Smoothness estimation

to get dlh and volume for thresholding Corresponding FSL command:

/usr/local/fsl/bin/smoothest -d 25 -m mask -r stats/res4d > stats/smoothness
Mask zstats file

preparation for thresholding

Corresponding FSL command:

/usr/local/fsl/bin/fslmaths stats/zstat1 -mas mask thresh_zstat1
Cluster-wise thresholding

Corresponding FSL command:

/usr/local/fsl/bin/cluster -i thresh_zstat1 -t 3.1 --othresh=thresh_zstat1 -o cluster_mask_zstat1 --connectivity=26 --mm --olmax=lmax_zstat1_std.txt --scalarname=Z -p 0.05 -d 0.0595781 --volume=254734 -c stats/cope1 > cluster_zstat1_std.txt

Save the output

<IPython.core.display.Image object>
<networkx.classes.digraph.DiGraph at 0x7a76bbafc640>

Unthresholded COPEs

<Figure size 400x500 with 1 Axes>
<Figure size 400x500 with 1 Axes>
<Figure size 400x500 with 1 Axes>
<Figure size 400x500 with 1 Axes>

Thresholded (p < 0.05) zstats

<Figure size 400x600 with 2 Axes>
<Figure size 400x600 with 2 Axes>
<Figure size 400x600 with 2 Axes>
<Figure size 400x600 with 2 Axes>
Dependencies in Jupyter/Python
  • Using the package watermark to document system environment and software versions used in this notebook, alongside the Neurodesktop version extracted from the JUPYTER_IMAGE or NEURODESKTOP_VERSION environment variables.

Last updated: 2026-06-17T05:36:27.914125+00:00

Python implementation: CPython
Python version       : 3.13.13
IPython version      : 9.14.0

Compiler    : GCC 14.3.0
OS          : Linux
Release     : 6.8.0-111-generic
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

IPython   : 9.14.0
logging   : 0.5.1.2
matplotlib: 3.10.9
nibabel   : 5.3.3
nilearn   : 0.13.1
nipype    : 1.11.0
numpy     : 2.4.6

Neurodesktop version: py-stack
References
  1. Jenkinson, M., Beckmann, C. F., Behrens, T. E. J., Woolrich, M. W., & Smith, S. M. (2012). FSL. NeuroImage, 62(2), 782–790. 10.1016/j.neuroimage.2011.09.015
  2. Esteban, O., Markiewicz, C. J., Burns, C., Goncalves, M., Jarecka, D., Ziegler, E., Berleant, S., Ellis, D. G., Pinsard, B., Madison, C., Waskom, M., Notter, M. P., Clark, D., Manhães-Savio, A., Clark, D., Jordan, K., Dayan, M., Halchenko, Y. O., Loney, F., … Ghosh, S. (2025). nipy/nipype: 1.8.6. Zenodo. 10.5281/ZENODO.15054147