-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlotPerceptionModels.m
More file actions
175 lines (145 loc) · 6.43 KB
/
PlotPerceptionModels.m
File metadata and controls
175 lines (145 loc) · 6.43 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
function PlotPerceptionModels(cfg)
% function PlotPerceptionModels(cfg)
%% Load data
nsubjects = length(cfg.subjects);
load(fullfile(cfg.dataDir,cfg.subjects{1},'CleanData',cfg.sensorData),'data');
nTime = length(data.time{1}); clear data
sensorData = zeros(nsubjects,270,nTime);
sourceDataL = zeros(nsubjects,76,nTime);
sourceDataR = zeros(nsubjects,76,nTime);
for sub = 1:nsubjects
fprintf('PROCESSING SUBJECT %s \n',cfg.subjects{sub})
outputDir = fullfile(cfg.outputDir,cfg.subjects{sub},'PerceptionModels');
if ~exist(outputDir,'dir'); mkdir(outputDir); end
load(fullfile(cfg.dataDir,cfg.subjects{sub},'CleanData',cfg.sensorData),'data');
cfgS = []; cfgS.channel = 'MEG'; cfgS.keeptrials = 'yes';
data = ft_timelockanalysis(cfgS,data);
sensorData(sub,:,:) = data.avg;
% source reconstruction
if ~exist(fullfile(outputDir,'dataFP_atlasRegions.mat'),'file') && cfg.sourceRec
load(fullfile(cfg.dataDir,cfg.subjects{sub},...
cfg.sourceFolder,'headmodel'),'vol')
load(fullfile(cfg.dataDir,cfg.subjects{sub},...
cfg.sourceFolder,'sourcespace'),'sourcespace')
vol = ft_convert_units(vol,'mm');
% forward solution
cfgS = [];
cfgS.grad = data.grad;
cfgS.grid.pos = sourcespace.pos;
cfgS.grid.inside = 1:size(sourcespace.pos,1);
if strcmp(cfg.subjects{sub},'S15'); cfgS.channel = {'MEG','-MLT31'};
else; cfgS.channel = {'MEG'}; end
cfgS.headmodel = vol;
cfgS.backproject = 'no';
leadfield = ft_prepare_leadfield(cfgS);
% inverse solution
cfgS = [];
cfgS.lambda = 0.01; % Regularization; 0.01 corresponds to '1%' in FieldTrip
cfgS.numPerm = 1000; % 100 is fine for testing, but you want more for the real analysis
cfgS.feedback = 100; % provide feedback (soonest) every X seconds
cfgS.allowNan = 'no'; % Use nanmean, nancov, etc.?
cfgS.lf = leadfield;
cfgS.cov.method = 'fixed';
cfgS.cov.window = 'all';
cfgS.returnSqrt = 'no';
cfgS.locationRescale = 'no';
source = sourceAnalysisOneCondition(cfgS, data);
source.tri = sourcespace.tri;
% correct for positivity and depth bias
source.avg.pow2 = (source.avg.pow - source.mPerm)./source.mPerm;
source.avg.pow2(source.avg.pow2 < 0) = 0;
source.avg.pow2 = sqrt(source.avg.pow2);
% turn into atlas regions
load(fullfile(outputDir,'dataFP_atlasRegions'),'source')
load(fullfile(cfg.dataDir,cfg.subjects{sub},cfg.sourceFolder,...
'dataFP_atlasRegions'),'labIdxL','labIdxR');
% left-sided areas
actPerAreaL = zeros(max(labIdxL),size(source.avg.pow,2));
for i = 1:max(labIdxL) % for every label, get the averaged activity. Exclude 0. This is something I gave to no label assigned, so this is not informative anyway
% get grid points with a given label
selGrids = (labIdxL==i);
selGrids = logical([selGrids; zeros(length(labIdxR), 1)]); % pad, because the first half is the left side.
actPerAreaL(i,:) = nanmean(source.avg.pow2(selGrids,:),1);
end
clear selGrids
% right-sided areas
actPerAreaR = zeros(max(labIdxL),size(source.avg.pow,2));
for i = 1:max(labIdxR) % for every label, get the averaged activity. Exclude 0. This is something I gave to no label assigned, so this is not informative anyway
% get grid points with a given label
selGrids = (labIdxR==i);
selGrids = logical([zeros(length(labIdxL), 1); selGrids]); % pad, because the first half is the left side.
actPerAreaR(i,:) = nanmean(source.avg.pow2(selGrids,:),1);
end
clear selGrids
% save
save(fullfile(outputDir,'dataFP_atlasRegions'),'source','actPerAreaL','actPerAreaR');
clear source labIdx labIdxL labIdxR nAreas idx
else
load(fullfile(outputDir,'dataFP_atlasRegions'),'actPerAreaL','actPerAreaR')
end
sourceDataL(sub,:,:) = actPerAreaL;
sourceDataR(sub,:,:) = actPerAreaR;
clear data actPerAreaL actPerAreaR
end
%% Plotting
% get some data structures and info
load(fullfile(cfg.dataDir,cfg.subjects{1},'CleanData',cfg.sensorData),'data');
cfgS = []; cfgS.channel = 'MEG'; data = ft_timelockanalysis(cfgS,data);
data.avg = squeeze(mean(sensorData,1));
load(fullfile(cfg.outputDir,cfg.subjects{1},'PerceptionModels','dataFP_atlasRegions'),'source')
load(fullfile(cfg.dataDir,cfg.subjects{1},cfg.sourceFolder,...
'dataFP_atlasRegions'),'labIdxL','labIdxR')
% for the left side
for a = 1:length(labIdxL)
if labIdxL(a) == 0 % this seems to happen in desikan. In that case, give no value to this grid point, as it has no defined label.
source.avg.pow2(a,:) = NaN;
else
source.avg.pow2(a,:) = squeeze(mean(sourceDataL(:,labIdxL(a), :),1));
end
end
% for the right side
for a = 1:length(labIdxR)
if labIdxR(a) == 0 % this seems to happen in desikan. In that case, give no value to this grid point, as it has no defined label.
source.avg.pow2(a+length(labIdxL),:) = NaN;
else
source.avg.pow2(a+length(labIdxL),:) = squeeze(mean(sourceDataR(:,labIdxR(a), :),1));
end
end
bnd.pnt = source.pos;
bnd.tri = source.tri;
% plot the things
nTime = length(cfg.timePoints);
idx = zeros(nTime,1);
for t = 1:nTime
[~,idx(t)] = min(abs(cfg.timePoints(t)-data.time)); % find closest match
end
map = makeColorMaps('redblue');
map = flipud(map);
map2 = map(256/2:end,:);
for t = 1:nTime
% plot the sensor data
figure(1)
subplot(2,ceil(nTime/2),t)
cfgP = [];
cfgP.layout = 'CTF275.lay';
cfgP.xlim = [cfg.timePoints(t) cfg.timePoints(t)];
cfgP.zlim = [-1.2e-13 1.2e-13];
cfgP.marker = 'off';
cfgP.comment = ' ';
ft_topoplotER(cfgP,data);
colormap(map); colorbar
title(string(cfg.timePoints(t)))
caxis(cfgP.zlim)
% plot the source data
figure(2)
T = source.avg.pow2(:,idx(t));
subplot(2,ceil(nTime/2),t)
ft_plot_mesh(bnd, 'vertexcolor',T,'edgecolor','none');
view([-25 -10]) % right hemisphere
camlight;
lighting gouraud;
material dull;
colormap(map2);
caxis([2 10]); colorbar
title(string(cfg.timePoints(t)))
end