First-Level Analysis Using afniproc.py
Step 1: Download data from XNAT and automatically convert to BIDS format using xnat2bids
Running xnat2bids on the command line in Oscar will download the data we need, convert it to BIDS format, and run the BIDS validator to check for any issues. We will be using the xnat-tools Oscar utility script. The Demodat2 dataset has 3 subjects with 2 sessions each. We will be exporting this entire project.
Create a configuration file
The xnat2bids configuration .toml file contains information that xnat-tools needs to download the correct data and put it where we want. Name this file x2b_demodat2_config.toml and place it wherever you'd like. Paste the following into your .toml file, and change mail-user to your email address. The script will default to placing the downloaded and BIDS-converted data in a folder called "bids-export" in your home directory; if you'd like to change this location, add a new line at the bottom with your desired path, i.e.: bids_root="/oscar/home/<yourusername>/data/<projectname>/xnat-export". Make sure to save this .toml file when you are done editing.
# Configuring arguments here will override default parameters.
[slurm-args]
mail-user = "example-user@brown.edu"
mail-type = "ALL"
output = "/oscar/home/example-user/logs/%x-%J.txt"
[xnat2bids-args]
bids_root = "/oscar/home/<example-user>/data/Demodat2"
project="BNC_DEMODAT2"
verbose=2Run the xnat2bids command
To run the xnat-tools export and BIDS conversion, ensure that you are working in a terminal in the same directory where you stored this .toml file (and if not, then give the full path to the .toml file in your command). On the command line, type:
module load anaconda3
python /oscar/data/bnc/scripts/run_xnat2bids.py --config x2b_demodat2_config.tomlEnter your XNAT username and password when prompted.
If you entered your email address, you should receive an email when each of the xnat2bids jobs begin, and another when they finish.
This will create a sourcedata folder for each subject within $bids_root/bnc/study-demodat2/xnat-export and a BIDS-compatible data directory within $bids_root/bnc/study-demodat2/bids/.
Step 2: Convert psychopy timing files to be used by AFNI
The aim of this step is to make our behavioral data BIDS compatible and facilitate future data sharing. To do this, we first need to create events.tsv files that correspond to each of our functional runs and contain information about each stimulus event of interest (onset time, condition, etc.). Then, in order to use AFNI for a regression, those tsv files must be converted into AFNI 1D files and saved in the bids derivatives directory.
Download the psychopy behavioral timing files (csv)
First, download the participant's behavioral timing files (in our case, created by PsychoPy) and manually place them in a directory named 'beh', in the sourcedata subfolder of your BIDS directory. For each individual subject and session, the full path should be: $bidsdir/sourcedata/sub-xxx/ses-xx/beh.
Repeat this step for each subject and session, placing the files in their respective beh folders. Files are zipped per subject (unzip the file to view ses-01 and ses-02 separately).
Convert the timing files using a batch script
Batch scripting improves the efficiency of data processing because it allows you to automatically launch the same job on all individual subjects/sessions, rather than running the script/command one by one. It takes advantage of the many resources available on our HPC (CPU, cores, etc) and runs the same script on all your subjects in parallel (rather than sequentially).
In this tutorial, we will include the data from both runs of each session (4 runs total per subject) in our afniproc.py command. To do so, our example script preprocess_behavior.py will 1) convert and move the psychopy csv files to BIDS-organized tsv files, and then 2) convert and move those tsv files to 1D files that will be used by AFNI in the bids derivatives folder.
Here you can download our example python processing script (preprocess_behavior.py ), the batch script that will be used to run it (run_preprocess_behavior.sh ), and the text file containing subject IDs (subjects.txt). Save these so they can be accessed on your oscar account. Open the batch script run_preprocess_behavior.sh and fill in your email and the path to your bids directory.
Run the batch script
To run the batch script, which will then launch multiple iterations of the preprocess_behavior.py script (one job/launch per subject), navigate to the directory where you saved these 3 files. On the command line, type sbatch run_preprocess_behavior.sh. Since you filled in your email, you should receive a message when each of the 3 jobs have launched.
If you are unable to run this script for any reason, you can download the events.tsv output files here, and manually place them in each respective directory, i.e.: $bidsdir/sub-xxx/ses-xx/func/ . Similarly, you can download the .1D files and manually place them in a new folder called $bidsdir/derivatives/afni/sub-xxx/stimtimes/.
Step 3: Prepare fMRI data for preprocessing by warping to standard space
It is possible to quickly warp your data into standard space within the afniproc.py script, but this does not provide the most accurate warping, since it is done via a linear transformation. To perform a nonlinear transformation into standard space, there is a separate AFNI command called sswarper2 (the successor of @SSwarper). This command takes roughly an hour and half to run on Oscar, and is used on each individual subject's T1 anatomical file. You only need to warp one of the T1s per subject- in the next step, all functional data from session 1 and 2 will be aligned to it.
Copy and paste this script into a file called SSW.sh. Change the example email to your own, and edit the path to your bids directory. Ensure that it is saved in the same location as your subjects file (subjects.txt). To launch this script on all 3 subjects sequentially, navigate to the directory on the command line and type sbatch SSW.sh.

Step 4: Use afni_proc.py to create a preprocessing stream and run the general linear model per subject
This basic example of a univariate analysis with AFNI is based on example 6b of the afni_proc.py documentation.
Run the afniproc.py batch script
This afniproc.sh script will iterate over your list of subjects and launch afniproc.py over them individually. Afniproc.py is a metascript, meaning that it will create your actual preprocessing script proc.sub-xxx for each subject. Since we included the -execute flag at the bottom, proc.sub-xxx will be automatically launched when you run afniproc.py. Looking at the proc.sub-xxx script is the best way to gain a deeper understanding of each of AFNI's processing steps.
Copy the text in the box below into a file editor on Oscar. Change your email in the beginning section, and change the value of the bidsdir variable to your own location (path should end in /bids). Save this script as a file called afniproc.sh, and then execute it on the command line with sbatch demodat2_afniproc.sh. It will launch as a batch script, similar to how we ran run_preprocess_behavior.sh. You will receive an email when the job has completed.
Now that each subject's data has been preprocessed (both sessions included), we are ready to move on to the second-level/group analysis!
Last updated
Was this helpful?