I think the attached script will be useful. it takes an estimated 1st level SPm.mat file (defined by pointing to the directory in which it was calculated) and a contrast matrix (one contrast per line, must be the full matrix though, meaning as many colums as there are beta_00**.nii files) and generates con and spmT files.
Easy to run in a simple loop passing many directors. 1000 contrasts shouldn't take more than a couple hours on a decent machine, and requirte no input from you.
Good luck,
Colin Hawco, PhD
Neuranalysis Consulting
Neuroimaging analysis and consultation
www.neuranalysis.com
[log in to unmask]
-----Original Message-----
From: SPM (Statistical Parametric Mapping) [mailto:[log in to unmask]] On Behalf Of Selim Onat
Sent: February-22-17 3:18 PM
To: [log in to unmask]
Subject: [SPM] beta -> tmap on command line
hi,
I would like to ask for programmers advice on transforming beta images to tmaps in an efficient way with spm 12 (or similar).
I am facing the problem of transforming >1000 beta images to tmaps, "matlabbatch + spm_jobman" is helpful as always, but very very VERY slow. Updating the SPM.mat file becomes tedious. I estimate that I could gain 3 factors of magnitude in efficiency by doing this on the command line.
Best,
s.
function analyze_spm_contrasts( directory, contrast_matrix, names)
% function analyze_spm_contrasts( directory, contrast_matrix, [names])
% Colin Hawco, revised March 2012
% calculate the t-maps and contrast beta files for an individual (one
% person's data)
% note that all old contrasts are deleted, and replaced by the contrasts
% contained within this file.
%
% Inputs:
% directory: The directory where the SPM.mat file for this participant is
% found
%
% contrast_matrix: the complete contrast matrix. Note that zeros will not
% be added to pad out the contrast matrix (which SPM's contrast managaer
% will do for you). Thus, it is important to add zeros for any regressors
% (such as motion) and a zero for the mean of each run at the end of the
% cotnrast.
%
% names: an optional variable, indicating the names of the individual
% contrast. If given, it should have one value per contrast. Two types of
% input are possible: a text file with one contrast name per line, or an
% arrary of cells (one name per cell).
%
%
if ~isempty(names)
if isstr(names)
name = textread(name,'%s','delimiter','\n')
for idx = 1:size(contasts, 1)
contrasts.names{idx} = in(idx);
end
end
if iscell(names)
for idx = 1:size(contrast_matrix, 1)
contrasts.names{1,idx} = names{idx};
end
end % if iscell
else %names not provided
for idx = 1:size(contrast_matrix, 1)
contrasts.names{1,idx} = ['c' num2str(idx)];
end
end
for idx = 1:size(contrast_matrix, 1)
tc{idx} = contrast_matrix(idx,:);
contrasts.types{idx} = 'T';
end
cd(directory)
load SPM
% any existing contrasts are removed, to make sure we know what is what
% after the program is finished.
% this also removes an empty contrast field SPM writes into the SPM.mat
% file, which gave problems in the for loop below.
SPM = rmfield(SPM, 'xCon') %#ok<NOPRT>
for c=1:length(tc)
c
SPM.xCon(c)= spm_FcUtil('Set',contrasts.names{c},contrasts.types{c},'c',tc{c}',SPM.xX.xKXs);
end
spm_contrasts(SPM, 1:length(tc));
clear SPM
end
|