how to use if conditions to do contrast are in two different experiments

I’m totally new in afni. Here are some questions and i really hope you can help me. Thanks a lot!
In my experiments, there are three experiment designs, every design has been done for just one run by every of 48 participants. In experiment 1 and 2, their are both stimulus M, N and W with some differences between experiments. In experiment 3, there are just one stimulus A. So if I want to do the contrast ‘M1-M2’ and ‘M2-A’, what should i do? In short, the two conditions i want to do contrast with are not in one same experiment.
Looking forward to hearing back!
Thank a lot!


It is okay to give all these runs to together,
assuming the subjects did not leave the scanner. The
timing files should specify which run they apply to.
The beta weights should end up the same as if the
analysis were done one run at a time (assuming still
doing registration to the same EPI volume).

Please feel free to post an command for

  • rick

Thanks for replying, Rick!

what i have for one subject here are:
a rest file and three task files: Rest.nii, Experiment_1.nii, Experiment_2.nii, Experiment_3.nii (three experiment designs, one run each experiment)
ten stimulus timing files: W_1.txt, M_1.txt, N_1.txt, Mistake_1.txt (in experiment 1); W_2.txt, M_2.txt, N_2.txt, Mistake_2.txt (in experiment 2); A.txt, Mistake_3.txt (in experiment 3)
subjects need to press button ‘2’ with thumb of one hand when seeing W (or A), press ‘3’ with the other hand when seeing M and press nothing when seeing N. Mistakes means the wrong responses. left and right hand were counterbalanced.
if what i want to do in gltsym are M2-M1 and M1-A, how should i set the -dset and -regress_stim_times

And second, in this set of ER fMRI experiments, the ISI randomly varied just from 1-3s, with stimuli randomly designed (4 kinds of stimulus in experiment 1, 4 in experiment 2 and 2 kinds in experiment 3). which kind of basic function should i choose.

thanks a lot!
looking forward to hearing back soon!


Hi Yuan,

Consider Example 6, say. The -regress_stim_times
option is passed blk_times.*.1D, which expands to 9
files (not shown in the example). Those 9 files
correspond to the 9 -regress_stim_labels, attaching
a text label to each timing file. All the basis
functions are the same, BLOCK(30,1), so it is passed
via -regress_basis.

Since the 9 labels are associated with the stim classes,
they can be used in the -gltsym options, as with option

-gltsym ‘SYM: +eneg -fneg’ -glt_label 1 eneg_vs_fneg

You might want a similar option to compare M2-M1, say.

How long does each stimulus event last? That is what
will probably determine the basis function. Will all
stim classes use the same basis function?

  • rick

thanks for replying, rick!
sorry i have to bother you again and again and thank you very much!
my question maybe too dumb but i still wish you could read and help me, please!
i’m sorry that maybe i didn’t express myself clear enough. the answer on how to set the stumulus timing files in my experiments seems not in the experiment 6.
here’s the difference between my experiment and experiment6:
in experiment 6 there are 9 stimulus classes, which all can be found in the ‘sb23/epi_r??+orig.HEAD’, right? so all ‘sb23/stim_files/blk_times.*.1D’ and the other 8 1D files are set for the one ‘sb23/epi_r??+orig.HEAD’ file, right?
however, in my experiment, there will be 10 timulus calsses, in which first four classes will be in the 1st epi file, the second four classes in the 2nd and the left two classes will be in the 3rd epi file. i want afni the software to know i set three epi files as different experiments with different stimulus classes, not three runs.
that is, i want afni the software to understand that in the 10 1D files, i want the first four 1D files to be read only for the first epi file i set, the second four 1D files to be read only for the second epi file and the last two 1D file for the third epi file. are there any way to do that? i don’t know if i get myself clear this time.
what i’m doing now is to run three times separately:

  1. -dset the_first_experiment.nii -regress_stim_times N1.1D W1.1D M1.1D error1.1D -regress_stim_labels N1 W1 M1 error1 -regress_basis ‘TENT(0,12,10)’
  2. -dset the_second_experiment.nii -regress_stim_times N2.1D W2.1D M2.1D error2.1D -regress_stim_labels N2 W2 M2 error2 -regress_basis ‘TENT(0,12,10)’
  3. -dset the_third_experiment.nii -regress_stim_times A.1D error3.1D -regress_stim_labels A error3 -regress_basis ‘TENT(0,12,10)’
    i am doing three times like this, but i can’t set ‘-gltsym ‘SYM: M1 -M2’’ if i do so. so can i do just once, not three times, to set my 3 epi files and 10 1D files in. And how can i do ‘-gltsym ‘SYM: M1 -M2’’ after the three different

and about the second question, here’s the experiment procedures:
there were 192 trials in every experiment, every one of them was a letter (M, N or W). subjects had to respond (press left or right button or not) according to the letters they saw. a fixation (+) were between every two trials for 0.5-2s. as to the stimulus durations, even though i know longer ones are preferable, i chose very short ones like 1s, some of which are even shorter, because in the trials that needed subjects to press a button, the stimulus will be terminated and the screen will jump to the next fixation once button was pressed. doing this was because of the particularity of this experiment and similar design in previous studies.
so the stimulus duration were very short and different from each other, and so do the ISIs. so the time from the appearance of one stimulus to to another one were just 1-3s.
i want to use the same basic function in all stim classes, well, for now. i just still can’t figure out which basic function(s) to choose and is there anything wrong in my experimrnts. waiting for your advice on basic function(s), and all your other reccoments are welcomed.

  • Yuan

Hi Yuan,

Sorry for being slow.

The number of EPI datasets equals the number of runs
at the scanner. It does not need to match the number
of stimulus classes or timing files. That sb23 data
just happens to have 9 runs and 9 classes.

How is your experiment different from 3 runs? Does
the subject leave the scanner between them?

So the first 4 timing files would have all of the
event times on the first row. Then they would have
‘*’ characters for runs 2 and 3.

The second 4 files would have ‘’ in the first row,
valid times in the second, and another '
’ for the
last run.

Since your stimuli vary in duration based on the
subject pressing a button, you should probably use
duration modulation, with dmUBLOCK for the basis
function. That means for every stimulus event time,
you attach the duration of it, e.g. 53.8:0.721 would
mean a stimulus starting at 53.8 seconds from the
beginning of the run, and lasting for 0.721 seconds
(until the button press).

dear rick:

i sincerely apprecieate your answer. it’s really of great help!
i think i just finally got what you meant. so should stimulus timing files include the onset time and duration of each trial, like, if i take one file of them as an example:
12.3:0.72 13.52:0.6 15.62:0.84 …
is that right?

at the first time, i made a mistake, which was that i didn’t attach the durations, and i got an fatal error alert:
‘-stim_times 1’: basis function model ‘dmUBLOCK’ uses 1 parameters, more than the -1 found in timing file ‘stimuli/111_sub01_1.txt’[nopt=13]

so if i attach the duration to the onset-time,like changing “12.3 13.52 15.62 …” to “12.3:0.72 13.52:0.6 15.62:0.84 …”, will that error disappear?

after changing as forementioned, a new fatal error accured:
" “-stim_times 1” file “stimuli/sub01_A.txt” has 1 auxiliary values per time point [nopt=11] "
how did that happened?


Hi Yuan,

Yes, that is right.

I do not see any fatal error there, or any error at all.
The “has 1 auxiliary values per time point” is saying
just that, you have attached durations to the event
times, which is an auxiliary value per time point.

Are there message that say “ERROR” or “WARNING”?

  • rick

thanks again for your patient answering!
i totally understand what you meant. the error was not supposed to happen at all, so i don’t know what to do now. here are all the warnings that appeared:

*+ WARNING: If you are performing spatial transformations on an oblique dset, such as ./pb00.sub01.r01.tcat+orig.BRIK, or viewing/combining it with volumes of differing obliquity, you should consider running: 3dWarp -deoblique on this and other oblique datasets in the same session. See 3dWarp -help for details.

*+ WARNING: Input dataset is not 3D+time; assuming TR=1.0
set ovals = ( -set_run_lengths $tr_counts -index_to_run_tr $minindex ) -set_run_lengths 221 -index_to_run_tr 18
set minoutrun = 01
set minouttr = 18
echo min outlier: run 01, TR 18
tee out.min_outlier.txt
min outlier: run 01, TR 18

*+ WARNING: dataset is already aligned in time!
*+ WARNING: ==>> output dataset is just a copy of input dataset

*+ WARNING: Changing the space of an ORIG view dataset may cause confusion!
*+ WARNING: NIFTI copies will be interpreted as TLRC view (not TLRC space).
*+ WARNING: Consider changing the view of the dataset to TLRC view also

*+ WARNING: input ‘a’ is not used in the expression

*+ WARNING: No output dataset will be calculated

** FATAL ERROR: ‘-stim_times 1’ file ‘stimuli/sub01_A.txt’ has 1 auxiliary values per time point [nopt=11]

i really didn’t see anything too wrong. looking forward to hearing back!


It would be helpful to show the exact command(s) you
are using. But it looks like you did not specify AM1 as
the stim type. Are you using If so, then
consider -regress_stim_types to specify AM1 for that
stim class.

  • rick

after changing this time, the code could finish, thank you so much for all your help!
but it’ll be great help if you can help me understand deeper, like what is AM1 or AM2 and what is the difference between. i can just use it but i don’t know the meaning.
here’s my code and you can take a look if convenient, in case there are some more mistakes.(a loop was made to analyse all the subjects automatically, and this code is just a test on task3 EPI file):


Design=YuanRS # change all 10 to 30

	# Loop over all subjects
	for i in /home/acr/ACR_YRS/sub*; do

    	# Check if fMRI data exists for this directory
	   # if [ -e ${i}/*.nii.gz ]; then

    	    # Go to current directory
	        cd $i
	        # Get subject name
			echo "-------------------------------"
	   		echo "Processing" $Subject
			echo "-------------------------------"

	        # set data directories

    	    # run to create a single subject processing script -subj_id $Subject                                   \
    	            -script proc.$Subject -scr_overwrite                     \
    	            -blocks tshift align tlrc volreg blur mask scale regress \
    	            -copy_anat $anat_dir/subgeservice.ABI1_t1iso_fspgr.nii   \
    	            -tcat_remove_first_trs 0                                 \
    	            -dsets                                               \
			$epi_dir/subgeservice.ABI1_bold_task3.nii          \
                        -test_stim_files no                                \
    	            -align_opts_aea -giant_move                          \
                    -tlrc_base MNI152_T1_2009c+tlrc                      \
                    -tlrc_NL_warp                                        \
    	            -volreg_align_to MIN_OUTLIER                         \
    	            -volreg_align_e2a                                    \
    	            -volreg_tlrc_warp                                    \
                  -volreg_warp_dxyz 3	                                 \
    	            -blur_size 6.0                                       \
    	            -regress_stim_times                                  \
                         	$stim_dir/sub*_A1.txt                 \

-regress_stim_types AM1
-regress_basis ‘dmUBLOCK’
-regress_censor_motion 0.3
-regress_make_ideal_sum sum_ideal.1D
-regress_opts_reml -Rwherr whitened_errts.${Subject}_REML
# echo “This directory does not contain any fMRI data!”


Hi Yuan,

That command seems good to me.

To get a better idea of AM regression, see the class
handout afni07_advanced.pdf.

AM regression begins on slide 12, with duration
modulation beginning on slide 21.

  • rick