I’m relatively new to SPM and related tools, and run into some issues running CAT12 segmentation in batches.
Individual subjects through the GUI works fine, but when we submit a script to process several subjects, several issues arise:
- issue 1:
MATLAB:sizeDimensionsMustMatch
Arrays have incompatible sizes for this operation.
Which arrays is it referring to here? How can I check and correct?
- issue 2:
MATLAB:UndefinedFunction
Unrecognized function or variable “VI”.
Which variable are they referring to?
- issue 3:
Error reading header file
For some of these issues, some output is still produced, but we struggle to distinguish when a subject has run succesfully. For issue 3, the complete output is not produced.
We run a bash submission script on a slurm cluster that calls a matlab script for the CAT12 segmentation:
Bash script:
#!/bin/bash
#SBATCH --job-name=segmentations_ahead
#SBATCH --mem=4G
#SBATCH --partition=luna-cpu-short
#SBATCH --qos=anw-cpu
#SBATCH --cpus-per-task=4
#SBATCH --time=00-03:00:00
#SBATCH --nice=2000
#SBATCH --array=1-3%3
#SBATCH --output=slurm-%A.%a.out
# Load Matlab module
module load matlab/R2021b
export MATLABPATH=path/to/cat12
inputdir=path/to/input
cd ${inputdir}
ls -d sub-*/ | sed 's:/.*::' > subjects.txt
subjects=${inputdir}/subjects.txt
subj=$(sed -n "${SLURM_ARRAY_TASK_ID}p" ${subjects})
# random delay
duration=$((RANDOM % 20 + 2))
echo -e "${YELLOW}INITIALIZING...(wait a sec)${NC}"
echo
sleep ${duration}
matlab -nodisplay -batch "AHEAD_segmentation $inputdir $subj"
Matlab script:
function AHEAD_segmentation(inputdir,subj)
% Define paths
addpath('path/to/spm12');
% Get a list of all subject directories
%subjects = dir(fullfile(input_dir, 'sub-*'));
% Loop over each subject
%for i = 1:length(subjects)
% Define the subject's anat directory within ses-1
subj_dir = fullfile(inputdir, subj, 'ses-1', 'anat');
% t1_image_gz = fullfile(subj_dir, [subj '_ses-1_acq-wb_mod-t1w_orient-std_brain.nii.gz']);
t1_image = fullfile(subj_dir, [subj '_ses-1_acq-wb_mod-t1w_orient-std_brain.nii']);
% Show file path
disp(['Checking: ', t1_image]);
% Check if file exists
if exist(t1_image, 'file') ~= 2
fprintf('Warning: File %s not found!n', t1_image);
error('no T1 file'); % Skip to next subject if T1 is missing
end
% Check if the gzipped T1 image exists
if ~isfile(t1_image)
fprintf('T1 image not found for subject %sn', subj);
end
% Print status
fprintf('Processing subject %sn', subj);
% Set up CAT12 batch job for segmentation
matlabbatch{1}.spm.tools.cat.estwrite.data = {t1_image};
matlabbatch{1}.spm.tools.cat.estwrite.nproc = 1;
%matlabbatch{1}.spm.tools.cat.estwrite.opts.tpm = {fullfile(spm('path/to/spm12'), 'tpm', 'TPM.nii')}; % Path to SPM's TPM file
spm_dir = spm('dir');
% Path to TPM file
tpm_file = fullfile(spm_dir, 'tpm', 'TPM.nii');
% Check if the file exists
if ~exist(tpm_file, 'file')
error('TPM file not found: %s', tpm_file);
end
matlabbatch{1}.spm.tools.cat.estwrite.opts.tpm = {tpm_file};
% select modality for SPM
matlabbatch{1}.spm.tools.cat.estwrite.opts.modality = 'T1';
% Output options for volume-based analysis
matlabbatch{1}.spm.tools.cat.estwrite.output.surface = 1; % Disable surface-based outputs
matlabbatch{1}.spm.tools.cat.estwrite.output.GM.native = 1; % Output native gray matter volume
matlabbatch{1}.spm.tools.cat.estwrite.output.WM.native = 1; % Output native white matter volume
matlabbatch{1}.spm.tools.cat.estwrite.output.CSF.native = 1; % Output native CSF volume
matlabbatch{1}.spm.tools.cat.estwrite.output.GM.warped = 0; % Output normalized gray matter volume (in MNI space)
matlabbatch{1}.spm.tools.cat.estwrite.output.WM.warped = 0; % Output normalized white matter volume (in MNI space)
matlabbatch{1}.spm.tools.cat.estwrite.output.CSF.warped = 0; % Output normalized CSF volume (in MNI space)
matlabbatch{1}.spm.tools.cat.estwrite.output.GM.mod = 0; % Output modulated gray matter volume (useful for VBM)
matlabbatch{1}.spm.tools.cat.estwrite.output.WM.mod = 0; % Output modulated white matter volume
matlabbatch{1}.spm.tools.cat.estwrite.output.warps = [1 1]; % Save forward and inverse deformation fields
% Run the CAT12 segmentation
% spm('defaults', 'FMRI');
% spm_jobman('initcfg');
spm_jobman('run', matlabbatch);
Any thoughts on how to tackle any of these three issues (or links to similar issues/documentation/FAQ) would be greatly appreciated!
1