-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathROI_decoding.m
More file actions
112 lines (80 loc) · 4.07 KB
/
Copy pathROI_decoding.m
File metadata and controls
112 lines (80 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function ROI_decoding(sbj)
% classification of location across category in fMRI experiment for all ROIs
% in single subject.
% Duration: 6 seconds
% Input:
% sbj: string
tic
addpath('./HelperFunctions');
addpath('./LibsvmFunctions');
savepath = './Results/fMRI/';
if ~isdir(savepath); mkdir(savepath); end
filename = ['s' sprintf('%.2d',sbj) '_ROI'];
ROIs = {'V1' 'V2' 'V3' 'V4' 'LO' 'IPS0' 'IPS1' 'IPS2' 'SPL'};
ROI.labels = ROIs;
% define classification parameters
runs = 10; % number of fMRI runs
attentions = 2; % n attention conditions
bg = 2; % n background conditions
locations = 2; % n locations
categories = 2; % n categories
bins = 2;
result = nan(length(ROIs),attentions,bg); % pre-allocate results matrix
chance_level = 50;
%% SVM classification
% loop through ROI's
for iROI = 1:length(ROIs)
% load data. Dimensions: 10 runs x 2 attention x 2 backgrounds x 2
% locations x 2 categories x voxels
load(sprintf(['./Data/fMRI/s%.2d/s%.2d_' ROIs{iROI} '.mat'],sbj,sbj));
% randomize and average in bins of 2 to decode on 5 pseudo-runs
data = data(randperm(size(data,1)),:,:,:,:,:); % randomize runs
data = reshape(data,[bins (runs/bins) attentions bg locations categories size(data,6)]);
data = squeeze(nanmean(data,1)); % average trials in bins to get new pseudo-trials
% set the labels for SVM
labels_train = [ones(1,(size(data,1))-1) 2*ones(1,(size(data,1))-1)]; % labels for training
labels_test = [1 2]; % labels for the left out run
% preallocate
RDM = nan(size(data,1),attentions,bg,locations,locations,categories,categories);
for iRun = 1:size(data,1)
iTrainRun = find([1:size(data,1)]~=iRun); % index to runs for training (all except one)
iTestRun = iRun; % index to run for testing (the one left out)
for iAtt = 1:attentions
for iBG = 1:bg
for LocationA = 1:locations
for LocationB = 1:locations
for CatA = 1:categories
for CatB = 1:categories
data_train = [squeeze(data(iTrainRun,iAtt,iBG,LocationA,CatA,:));...
squeeze(data(iTrainRun,iAtt,iBG,LocationB,CatA,:))];
data_test = [squeeze(data(iTestRun,iAtt,iBG,LocationA,CatB,:))';...
squeeze(data(iTestRun,iAtt,iBG,LocationB,CatB,:))'];
model = libsvmtrain(labels_train',data_train,'-s 0 -t 0 -q');
[predicted_label, accuracy, decision_values] = libsvmpredict(labels_test', data_test, model);
RDM(iRun,iAtt,iBG,LocationA,LocationB,CatA,CatB) = accuracy(1); % save accuracy
end
end
end
end
end
end
end
% average across runs
% runs x att x bg x loc x loc x cat x cat
temp = squeeze(nanmean(RDM,1))-chance_level;
ROI.RDM(iROI,:,:,:,:,:,:) = temp; clear RDM % mean across runs
%--> % att x bg x loc x loc x cat x cat
% put location in the back
results = permute(temp,[5 6 1 2 3 4]); clear temp
%--> cat x cat x att x bg x loc x loc
% average across upper off-diagonal, which is location decoding
results = results(:,:,:,:,triu(ones(2,2),1)>0);
% --> cat x cat x att x bg
% put category in the back to take across cateroy
results = permute(results,[3 4 1 2]);
% --> att x bg x cat x cat
ROI.results(iROI,:,:) = squeeze(nanmean(results(:,:,eye(2,2)==0),3)); clear results
end
duration = toc;
% save
save([savepath filename '.mat'],'ROI','duration');