% This batch script analyses the Face Group dataset available from the SPM site:
% http://www.fil.ion.ucl.ac.uk/spm/data/#con
% as described in the manual Chapter 30.

% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging

% Guillaume Flandin
% $Id: face_rfx_spm5_batch.m 1 2014-04-02 15:36:08Z guillaume $

%% Path containing data
%--------------------------------------------------------------------------
data_path = 'C:\work\data\face_repetition_rfx';

%% Set Matlab path
%--------------------------------------------------------------------------
addpath('C:\work\spm5');

%% Initialise SPM defaults
%--------------------------------------------------------------------------
spm('Defaults','fMRI');

spm_jobman('initcfg'); % SPM8 only

%% WORKING DIRECTORY
%---------------------------------------------------------------------
clear jobs
jobs{1}.util{1}.cdir.directory = cellstr(data_path);
spm_jobman('run',jobs);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CANONICAL HRF
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = spm_select('FPList', fullfile(data_path,'cons_can'), '^con_.*\.img$') ;

clear jobs

jobs{1}.util{1}.md.basedir = cellstr(data_path);
jobs{1}.util{1}.md.name = 'canonical';

jobs{2}.stats{1}.factorial_design.dir = cellstr(fullfile(data_path,'canonical'));
jobs{2}.stats{1}.factorial_design.des.t1.scans = cellstr(f);

jobs{2}.stats{2}.fmri_est.spmmat = cellstr(fullfile(data_path,'canonical','SPM.mat'));

jobs{2}.stats{3}.con.spmmat = cellstr(fullfile(data_path,'canonical','SPM.mat'));
jobs{2}.stats{3}.con.consess{1}.fcon.name = 'Faces vs Baseline: Canonical HRF';
jobs{2}.stats{3}.con.consess{1}.fcon.convec{1} = 1;

jobs{2}.stats{4}.results.spmmat = cellstr(fullfile(data_path,'canonical','SPM.mat'));
jobs{2}.stats{4}.results.conspec(1).contrasts = Inf;
jobs{2}.stats{4}.results.conspec(1).threshdesc = 'FWE';

spm_jobman('run',jobs);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% INFORMED BASIS SET
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = spm_select('FPList', fullfile(data_path,'cons_informed'), '^con_.*\.img$') ;

clear jobs

jobs{1}.util{1}.md.basedir = cellstr(data_path);
jobs{1}.util{1}.md.name = 'informed';

jobs{2}.stats{1}.factorial_design.dir = cellstr(fullfile(data_path,'informed'));
jobs{2}.stats{1}.factorial_design.des.fd.fact.name = 'Basis';
jobs{2}.stats{1}.factorial_design.des.fd.fact.levels = 3;
jobs{2}.stats{1}.factorial_design.des.fd.fact.dept = 1;
jobs{2}.stats{1}.factorial_design.des.fd.icell(1).levels = 1;
jobs{2}.stats{1}.factorial_design.des.fd.icell(1).scans = cellstr(f(1:12,:));
jobs{2}.stats{1}.factorial_design.des.fd.icell(2).levels = 2;
jobs{2}.stats{1}.factorial_design.des.fd.icell(2).scans = cellstr(f(13:24,:));
jobs{2}.stats{1}.factorial_design.des.fd.icell(3).levels = 3;
jobs{2}.stats{1}.factorial_design.des.fd.icell(3).scans = cellstr(f(25:36,:));

jobs{2}.stats{2}.fmri_est.spmmat = cellstr(fullfile(data_path,'informed','SPM.mat'));

jobs{2}.stats{3}.con.spmmat = cellstr(fullfile(data_path,'informed','SPM.mat'));
jobs{2}.stats{3}.con.consess{1}.fcon.name = 'Faces vs Baseline: Informed';
jobs{2}.stats{3}.con.consess{1}.fcon.convec{1,1} = [1 0 0];
jobs{2}.stats{3}.con.consess{1}.fcon.convec{1,2} = [0 1 0];
jobs{2}.stats{3}.con.consess{1}.fcon.convec{1,3} = [0 0 1];
jobs{2}.stats{3}.con.consess{2}.tcon.name = 'Informed: t canonical contrast';
jobs{2}.stats{3}.con.consess{2}.tcon.convec = 1;
jobs{2}.stats{3}.con.consess{3}.fcon.name = 'Ftemp';
jobs{2}.stats{3}.con.consess{3}.fcon.convec{1,1} = [0 1 0];
jobs{2}.stats{3}.con.consess{4}.fcon.name = 'Fdisp';
jobs{2}.stats{3}.con.consess{4}.fcon.convec{1,1} = [0 0 1];

jobs{2}.stats{4}.results.spmmat = cellstr(fullfile(data_path,'informed','SPM.mat'));
jobs{2}.stats{4}.results.conspec(1).contrasts = Inf;
jobs{2}.stats{4}.results.conspec(1).threshdesc = 'FWE';

spm_jobman('run',jobs);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% FIR BASIS SET
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear jobs

jobs{1}.util{1}.md.basedir = cellstr(data_path);
jobs{1}.util{1}.md.name = 'FIR';

jobs{2}.stats{1}.factorial_design.dir = cellstr(fullfile(data_path,'FIR'));
jobs{2}.stats{1}.factorial_design.des.fd.fact.name = 'TimeBin';
jobs{2}.stats{1}.factorial_design.des.fd.fact.levels = 12;
jobs{2}.stats{1}.factorial_design.des.fd.fact.dept = 1;
for i=1:12
	jobs{2}.stats{1}.factorial_design.des.fd.icell(i).levels = i;
	f = spm_select('FPList', fullfile(data_path,'cons_fir'), sprintf('^con_fir_bin%02d.*\\.img$',i));
	jobs{2}.stats{1}.factorial_design.des.fd.icell(i).scans = cellstr(f);
end

jobs{2}.stats{2}.fmri_est.spmmat = cellstr(fullfile(data_path,'FIR','SPM.mat'));

jobs{2}.stats{3}.con.spmmat = cellstr(fullfile(data_path,'FIR','SPM.mat'));
jobs{2}.stats{3}.con.consess{1}.fcon.name = 'Faces vs Baseline: FIR';
fcont = eye(12);
for i=1:size(fcont,1)
	jobs{2}.stats{3}.con.consess{1}.fcon.convec{1,i} = fcont(i,:);
end

xBF.dt = 1;
xBF.name = 'hrf (with time and dispersion derivatives)';
xBF.length = 32;
xBF.order = 1;
xBF = spm_get_bf(xBF);
all = xBF.bf(2:2:24,:)';
can = all(1,:);
tem = all(2,:);
dis = all(3,:);
disp(corrcoef(all')');
nullcan = eye(12) - pinv(can)*can;
nullall = eye(12) - pinv(all)*all;

jobs{2}.stats{3}.con.consess{2}.tcon.name = 'Can-weighted FIR';
jobs{2}.stats{3}.con.consess{2}.tcon.convec = can;
jobs{2}.stats{3}.con.consess{3}.fcon.name = 'Null space of canonical HRF';
for i=1:size(nullcan,1)
	jobs{2}.stats{3}.con.consess{3}.fcon.convec{1,i} = nullcan(i,:);
end
jobs{2}.stats{3}.con.consess{4}.fcon.name = 'Null space of informed basis';
for i=1:size(nullall,1)
	jobs{2}.stats{3}.con.consess{4}.fcon.convec{1,i} = nullall(i,:);
end

jobs{2}.stats{4}.results.spmmat = cellstr(fullfile(data_path,'FIR','SPM.mat'));
jobs{2}.stats{4}.results.conspec(1).contrasts = Inf;
jobs{2}.stats{4}.results.conspec(1).threshdesc = 'FWE';

spm_jobman('run',jobs);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
