Looks like you have figured all this out. Just to add a few tricks to your quiver:
1. 3dMean. this is a great program for combining lots of data with wildcards on the command line. Each dataset is loaded one at a time, so you are less likely to have memory issues. It doesn't provide as many statistical functions as 3dTstat though.
3dMean -prefix testschaef.nii.gz -mask_union ~/afni_sy_atlas/Schaefer_17N_400.nii.gz'<1..200>' ... # more datasets on the command line
2. Globbing. AFNI commands from compiled C code can glob (expand filenames from wildcard characters) instead of the shell. This is a relatively new addition to AFNI that's described in the help for afni_open. It does the wildcard expansion in the C code and avoids line length limitations of the shell.
[
afni.nimh.nih.gov]
3dMean -DAFNI_GLOB_SELECTORS=YES -prefix testschaefmulti.nii.gz -mask_union \
'<<gm /Some/long/path/afni_sy_atlas/Schaefer_17N_?00.nii.gz'"<${rangesel}>"
That doesn't have a different range for each dataset, which is what you want, I think, but still generally useful. 3dinfo can show the history of how the command expanded with the globbing. Also see gen_group_command.py for some similar functionality.
3. Concatenated datasets on the command line. AFNI C programs can take lists of datasets as a single concated dataset as one way to "remaster" a dataset. This is particularly useful with programs that take a single dataset input that's usually across time, like 3dTstat. The shell is doing the globbing here, but you can still see the result with 3dinfo on the output.
export dsetlist=(/Users/glend/afni_sy_atlas/Schaefer_17N_?00.nii.gz)
3dTstat -nzcount -prefix test3dts.nii.gz -mrange 1 200 "$dsetlist"
All these methods combine data without first extracting the datasets to a separate file, so that can reduce disk access.
BTW, Pete's method converts to byte because 3dfractionize only takes byte as input. That's a particular way of handling affine transformations that's useful for moving ROIs, so that's not relevant to your scripting here. Byte mode also helps cut down on memory usage by half vs the 16-bit short integer. Probably not critical here either.
Edited 1 time(s). Last edit at 02/21/2023 11:55AM by Daniel Glen.