Skip to content
A_SpecifyIndividualRegressorMats_stimLoadOnly.m 7.69 KiB
Newer Older
Julian Kosciessa's avatar
Julian Kosciessa committed
function A_SpecifyIndividualRegressorMats_stimLoadOnly

% Create SPM experimental condition info for each subject for later
% batching with tardis

% v3: create Dim-Att regressors, include cue onset, probe onset regressors
% use FAST modelinstead of AR(1): Corbin, N., Todd, N., Friston, K. J., & Callaghan, M. F. (2018). Accurate modeling of temporal correlations in rapidly sampled fMRI time series. Human Brain Mapping, 26(3), 839?14. http://doi.org/10.1002/hbm.24218
% - AP to change back
% v4: AP remove -1 from design timing, but add durations to cue (1 volume) and
% probe (2).

% N = 44 YA + 53 OA;
% AP remove 2131, 2237, 1215 (see oNe Note)

% JQK: set up 2 regressor matrices for F-tests: load + stimulus target feature 

IDs = {'1117';'1118';'1120';'1124';'1125';'1126';'1131';'1132';'1135';'1136';...
    '1151';'1160';'1164';'1167';'1169';'1172';'1173';'1178';'1182';'1214';...
    '1216';'1219';'1223';'1227';'1228';'1233';'1234';'1237';'1239';'1240';'1243';...
    '1245';'1247';'1250';'1252';'1257';'1261';'1265';'1266';'1268';'1270';'1276';'1281';...
    '2104';'2107';'2108';'2112';'2118';'2120';'2121';'2123';'2125';'2129';'2130';...
    '2132';'2133';'2134';'2135';'2139';'2140';'2145';'2147';'2149';'2157';...
    '2160';'2201';'2202';'2203';'2205';'2206';'2209';'2210';'2211';'2213';'2214';...
    '2215';'2216';'2217';'2219';'2222';'2224';'2226';'2227';'2236';'2238';...
    '2241';'2244';'2246';'2248';'2250';'2251';'2252';'2258';'2261'};

%pn.root = '/Volumes/LNDG/Projects/StateSwitch/dynamic/data/mri/task/analyses/G_GLM/';
pn.root = '/home/beegfs/kosciessa/StateSwitch/WIP/G_GLM/';

%Path to Base Dir of local output
pn.filePathRoot = '/Volumes/LNDG/Projects/StateSwitch/dynamic/data/mri/task/analyses/G_GLM/';
pn.batchPath = [pn.filePathRoot, 'B_data/D_batchFiles1stLevelGLM_v5/'];
mkdir(pn.batchPath);
%addpath([pn.root, 'T_tools/spm12/']); % add spm functions

addpath([pn.filePathRoot, 'T_tools/spm12/']);
spm fmri

for indID = 1:numel(IDs)
    
    disp(['Processing ', IDs{indID}]);
    
    matlabbatch = cell(1);
    
    % specify general parameters
    
    matlabbatch{1}.spm.stats.fmri_spec.dir = {[pn.root, 'B_data/F_GLM1stLevelout_v1/' IDs{indID} '/']};
    matlabbatch{1}.spm.stats.fmri_spec.timing.units = 'scans';
    matlabbatch{1}.spm.stats.fmri_spec.timing.RT = 0.645;
    matlabbatch{1}.spm.stats.fmri_spec.timing.fmri_t = 16;
    matlabbatch{1}.spm.stats.fmri_spec.timing.fmri_t0 = 8;
    matlabbatch{1}.spm.stats.fmri_spec.fact = struct('name', {}, 'levels', {});
    matlabbatch{1}.spm.stats.fmri_spec.bases.hrf.derivs = [1 0];
    matlabbatch{1}.spm.stats.fmri_spec.volt = 1;
    matlabbatch{1}.spm.stats.fmri_spec.global = 'None';
    matlabbatch{1}.spm.stats.fmri_spec.mthresh = -Inf;
    matlabbatch{1}.spm.stats.fmri_spec.mask = {''};
    matlabbatch{1}.spm.stats.fmri_spec.cvi = 'AR(1)';
    
    % subjects with different run number
    
    if strcmp(IDs{indID}, '2131') || strcmp(IDs{indID}, '2237')
        numOfRuns = 2;
    else
        numOfRuns = 4;
    end
    
    for indRun = 1:numOfRuns
        
        % get regressor data
        pn.regressors = [pn.filePathRoot, 'B_data/A_regressors/'];
        load([pn.regressors, IDs{indID}, '_Run',num2str(indRun),'_regressors.mat'])
        
        % change regressor matrix, such that column 4 represents trial-by-trial
        % indices of dimensionality
        blockOnsets=find(Regressors(:,1) == 1);
        for indBlock = 1:numel(blockOnsets)
            if indBlock == numel(blockOnsets)
                Regressors(blockOnsets(indBlock):end,4) = ...
                    Regressors(blockOnsets(indBlock),4);
            else
                Regressors(blockOnsets(indBlock):blockOnsets(indBlock+1)-1,4) = ...
                    Regressors(blockOnsets(indBlock),4);
            end
        end
        
        % For the scans, we need to specify separate 3D images via comma seperator
        % Under the hood: design-specified time points are extracted from 2D voxel*time matrix
        basefile = [pn.root, 'B_data/BOLDin/',IDs{indID},'_run-',num2str(indRun),'_feat_detrended_bandpassed_FIX_MNI3mm_std.nii'];
        
        allFiles={};
        if strcmp(IDs{indID}, '2132') && indRun == 2
            disp(['Change #volumes for run2']);
            for indVol = 1:1040
                allFiles{indVol,1} = [basefile, ',', num2str(indVol)];
            end
        else
            for indVol = 1:1054
                allFiles{indVol,1} = [basefile, ',', num2str(indVol)];
            end
        end
        
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).scans = allFiles;
        
%         % stimulus viewing condition
%         % To get relevant-feature-specific conditions, we have to create 4
%         % stim conditions: color, direction, size & luminance
%         AttributeNames = {'color'; 'direction'; 'size'; 'luminance'};
%         for indAtt = 1:4 % here: contained in target set
%             AttOnsets = find(Regressors(:,3) == 1 & Regressors(:,6+indAtt) == 1);
%             PM = Regressors(AttOnsets,4);
%             AttDur = repmat(4, numel(AttOnsets),1);
%             matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).name = AttributeNames{indAtt};
%             matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).onset = AttOnsets; 
%             matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).duration = AttDur;
%             matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).tmod = 0;
%             matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).orth = 1;
%         end
        
        %% add load regressor

        LoadNames = {'load1'; 'load2'; 'load3'; 'load4'};
        for indDim = 1:4 % here: contained in target set
            DimOnsets = find(Regressors(:,3) == 1 & Regressors(:,4) == indDim);
            DimDur = repmat(4, numel(DimOnsets),1);
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indDim).name = LoadNames{indDim};
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indDim).onset = DimOnsets; 
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indDim).duration = DimDur;
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indDim).tmod = 0;
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indDim).orth = 1;
        end
        
        %% add 24 Friston motion regressors
        
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).multi_reg={[pn.root, 'B_data/M_MotionParameters/' IDs{indID} '/' IDs{indID} '_sess-' int2str(indRun) '_motion_24dof.txt']};
        
        %% add general session information
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).multi = {''};
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).regress = struct('name', {}, 'val', {});
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).hpf = 128;
        matlabbatch{1}.spm.stats.fmri_spec.fact = struct('name', {}, 'levels', {});
        matlabbatch{1}.spm.stats.fmri_spec.bases.hrf.derivs = [1 0];
        matlabbatch{1}.spm.stats.fmri_spec.volt = 1;
        matlabbatch{1}.spm.stats.fmri_spec.global = 'None';
        matlabbatch{1}.spm.stats.fmri_spec.mthresh = -Inf;
        matlabbatch{1}.spm.stats.fmri_spec.mask = {[pn.root, 'B_data/E_standards/GM_MNI3mm_mask.nii']};
        matlabbatch{1}.spm.stats.fmri_spec.cvi = 'AR(1)';
    end % session loop (i.e. runs)
    
    %% Estimate Model
    
    matlabbatch{2}.spm.stats.fmri_est.spmmat(1) = cfg_dep('fMRI model specification: SPM.mat File', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','spmmat'));
    matlabbatch{2}.spm.stats.fmri_est.write_residuals = 0;
    matlabbatch{2}.spm.stats.fmri_est.method.Classical = 1;
    
    save([pn.batchPath, IDs{indID},'_SPM1stBatchGLM.mat'], 'matlabbatch');
    
end % subject loop

end % finish completely