Print

Print


Hi Pamela, 

First off all, I would call up that template file (posted below) in the GUI batch editor and wherever possible, specify dependencies as inputs for each module. For example, if you want to use the output of dicom import (the image files) in the coregistration module, you simply go to the field where you would select the image files and select the output of DICOM IMPORT as your dependency. Doing this would minimize the extra Matlab coding that you have to do. 

When you save this template file as a Matlab code using batch and script (or something like it), you get 1) the jobfile containing the matlab batch structures and 2) the skeleton script which you posted below. However, you will need to write up your own Matlab code to be able to utilize the skeleton script.

% =====================================
% First, define a cell array of paths to the subject directories.

subjects =  {'/home/sub1dir',...
  '/home/sub2dir',...
 '/home/sub3dir'};

Then create a loop to specify additional paths and files for each subject which you need to enter as inputs in your skeleton script

for i=1:length(subjects)
x_dir = fullfile(subjects{i},'results'} % use fullfile to look for the directory (an example)

% for inputs requiring multiple files, you can use an incarnation of the system "find" command to return multiple paths

%In order to use the skeleton script (which calls up the jobfile) you must specify the inputs that you want to use (replacing "MATLAB CODE TO FILL INPUT" with an actual matlab code or variable). If the input calls for a text %string instead, you must specify what that string is. If the input calls for a matrix of numbers, such as the vector onsets during first level specification, then you will define the input as a matrix of numbers. Once these inputs are filled, your code can run the jobfile.

% here is where your code comes in

for crun = 1:nrun % Many of the inputs need to be entered as cell strings
    inputs{1, crun} = cellstr(x_dir variable defining path to  your directory); % Named Directory Selector: Directories - cfg_repeat
    inputs{2, crun} = cellstr(variable specifyin path to new directory); % Change Directory: Directory - cfg_files
% ....and so on
    inputs{7, crun} = cellstr(cell array of paths to your files); % Realign: Estimate & Reslice: Session - cfg_files
% ....and so on
    inputs{18, crun} = load(.mat file containing definitions for each condition according to the SPM manual);

job_id = cfg_util('initjob',jobs); 
    sts = cfg_util('filljob',job_id, inputs{:}); % if the inputs are not filled, sts will not be set to 1 and the job will not run
% I added the next 2 lines to save out this jobfile so that user can inspect the settings
job_filename = fullfile(x_dir,'1st_level_model.mat');
cfg_util('savejob',job_id,job_filename);

if sts
cfg_util('run',job_id); % run the job
end

cfg_util('deljob',job_id);

end % end of subject loop

end % end of script
% =============================================

The code I provided between the double lines will only give you an idea of what you need to get your batch working, but I hope it's a good starting point. 
Conversely, you can save a template file and manually enter the inputs for your 100 subjects. Considering the large N-size, I don't think that's what you were aiming for.

Good luck,

Vy T.U. Dinh
Research Assistant, Neurological Sciences
Rush University Medical Center
Phone: (312) 563-3853
Fax: (312) 563-4660
Email: [log in to unmask]

From: SPM (Statistical Parametric Mapping) [[log in to unmask]] on behalf of Pamela LaMontagne [[log in to unmask]]
Sent: Thursday, December 09, 2010 5:53 PM
To: [log in to unmask]
Subject: [SPM] SPM8 scripting batch

I am currently working on a project with an anticipated n=100. I am interested in setting up a script to run for subjects simply replacing the subject number in each file/directory path. My main question is what exactly do I specify in the script.m file? I have set up the batch, according to the manual, with the directory and first files left unspecified. I am unsure about what I am supposed to specify in for the Matlab_Code part. My files are nifti (nii) format and I am also unsure how to specify the 192 timepoints in the single file. I am not a fluent matlab person. Alternatively, is there an easier option to employ in the giu batch?
Thanks!!
Pamela LaMontagne


% List of open inputs
% Named Directory Selector: Directories - cfg_repeat
% Change Directory: Directory - cfg_files
% Make Directory: Parent Directory - cfg_files
% Make Directory: Parent Directory - cfg_files
% DICOM Import: DICOM files - cfg_files
% DICOM Import: Output directory - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% fMRI model specification: Multiple conditions - cfg_files
% fMRI model specification: Multiple conditions - cfg_files
nrun = X; % enter the number of runs here
jobfile = {'/data/Task/Task_SPM_Batch_
job.m'};
jobs = repmat(jobfile, 1, nrun);
inputs = cell(18, nrun);
for crun = 1:nrun
    inputs{1, crun} = MATLAB_CODE_TO_FILL_INPUT; % Named Directory Selector: Directories - cfg_repeat
    inputs{2, crun} = MATLAB_CODE_TO_FILL_INPUT; % Change Directory: Directory - cfg_files
    inputs{3, crun} = MATLAB_CODE_TO_FILL_INPUT; % Make Directory: Parent Directory - cfg_files
    inputs{4, crun} = MATLAB_CODE_TO_FILL_INPUT; % Make Directory: Parent Directory - cfg_files
    inputs{5, crun} = MATLAB_CODE_TO_FILL_INPUT; % DICOM Import: DICOM files - cfg_files
    inputs{6, crun} = MATLAB_CODE_TO_FILL_INPUT; % DICOM Import: Output directory - cfg_files
    inputs{7, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{8, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{9, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{10, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{11, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{12, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{13, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{14, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{15, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{16, crun} = MATLAB_CODE_TO_FILL_INPUT; % Realign: Estimate & Reslice: Session - cfg_files
    inputs{17, crun} = MATLAB_CODE_TO_FILL_INPUT; % fMRI model specification: Multiple conditions - cfg_files
    inputs{18, crun} = MATLAB_CODE_TO_FILL_INPUT; % fMRI model specification: Multiple conditions - cfg_files
end
spm('defaults', 'FMRI');
spm_jobman('serial', jobs, '', inputs{:});