forked from hharveygit/SPES_Visual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckSummative.m
More file actions
executable file
·337 lines (303 loc) · 13.7 KB
/
checkSummative.m
File metadata and controls
executable file
·337 lines (303 loc) · 13.7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
%% For each stim/recording pair, check if EPs/BBs are summative or multiplicative
%
% If this code is used in a publication, please cite the manuscript:
% "H Huang, KN Kay, NM Gregg, G Ojeda Valencia, M In, C Kapeller, Y Shu, GA Worrell, KJ Miller, and D Hermes.
% Single pulse electrical stimulation in white matter modulates iEEG visual responses in human early visual cortex. (Under Review)"
%
% A preprint is available currently at doi: https://doi.org/10.1101/2025.05.05.652264.
%
% The dataset corresponding to this code and manuscript is in BIDS format (version 1.10.0) on OpenNeuro (ds006485),
% and it will be made publicly available upon manuscript acceptance.
%
% Copyright (C) 2025 Harvey Huang
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% Configure paths
load('broc10.mat');
imgCondColors = [0, 0, 0; broc10([3, 4, 5, 8, 7, 6], :)]; % for the 7 img conds
defaultCols = [0 0.4470 0.7410; 0.8500 0.3250 0.0980; 0.9290 0.6940 0.1250]; % first 3 cols of matlab cm (matching ISIs)
saveOutputs = true;
outdir = fullfile('output', 'checkSummative');
mkdir(outdir);
loaddir = fullfile('output', 'mforge');
% configure percentage threshold and total duration to analyze signal on
durEP = 0.5; % 0.5s matches the COD calculations for EP
durBB = 0.5; % 0.5s for broadband too. if responses occur they should be predominantly in the first 500ms
minFrac = 0.2; % minimum fraction of time points required. 0.2 means 100 ms out of first 500 ms
%% 1) Check EVOKED POTENTIAL summation for ch-site pairs
%sub = '1';
%ch = 'LOC2';
%ch = 'LY11';
%ch = 'LV15';
%site = 'LOC4-LOC5';
%site = 'LG6-LG7';
sub = '2';
%ch = 'ROC1';
ch = 'RY12';
%site = 'ROC6-ROC7';
site = 'RMO3-RMO4';
% for this pair, get if the best fit was simple vs image model. Note we can assume fitISI=0 because these are for non-interactive conditions
fitCohEP = getFitCohEP(ch, site);
fprintf('Loading EP fitCoh=%s bootstraps for %s, %s, %s\n', string(fitCohEP), sub, ch, site);
% load the bootstrapped EP fits
nBoots = 1000;
temp = load(fullfile(loaddir, sprintf('firBootstrap_%s_%s_%s_fitISI-0_fitCoh-%d', sub, ch, site, double(fitCohEP)), ...
sprintf('firBootstrap_%s_%s_%s_fitISI-0_fitCoh-%d_seed-1.mat', sub, ch, site, double(fitCohEP))));
initSz = size(temp.xMat);
xMatBoot = nan(initSz(1), initSz(2), nBoots);
for ii = 1:nBoots
mdl = load(fullfile(loaddir, sprintf('firBootstrap_%s_%s_%s_fitISI-0_fitCoh-%d', sub, ch, site, double(fitCohEP)), ...
sprintf('firBootstrap_%s_%s_%s_fitISI-0_fitCoh-%d_seed-%d.mat', sub, ch, site, double(fitCohEP), ii)));
xMatBoot(:, :, ii) = mdl.xMat;
end
ttseg = mdl.ttseg;
% plot the bootstrap fits to make sure it matches our figures
xlims = [-0.2, 1];
ylims = [-400, 400];
figure('Position', [200, 200, 500, 300]);
subplot(1, 2, 1); hold on
yline(0);
plotCurvConfSample(ttseg, squeeze(xMatBoot(:, 1, :))', 'k', 0.3, 95); % CCEP
plot(ttseg, mean(xMatBoot(:, 1, :), 3), 'k-', 'LineWidth', 0.75);
hold off
set(gca, 'xtick', 0:0.2:1); xlim(xlims); ylim(ylims);
if ~fitCohEP % simple model
subplot(1, 2, 2); hold on
yline(0);
plotCurvConfSample(ttseg, squeeze(xMatBoot(:, 2, :))', defaultCols(1, :), 0.3, 95); % CCEP
plot(ttseg, mean(xMatBoot(:, 2, :), 3), 'Color', defaultCols(1, :), 'LineWidth', 0.75);
hold off
set(gca, 'xtick', 0:0.2:1); xlim(xlims); ylim(ylims);
else % img model
subplot(1, 2, 2); hold on
hs = zeros(1, 7);
yline(0);
for ii = 1:7
hs(ii) = plot(ttseg, mean(xMatBoot(:, ii+1, :), 3), 'Color', imgCondColors(ii, :), 'LineWidth', 0.75);
end
hold off
set(gca, 'xtick', 0:0.2:1); xlim(xlims); ylim(ylims);
end
% Quantify CCEP percent of tp significantly different from 0
conf95Ccep = prctile(squeeze(xMatBoot(:, 1, :)), [2.5, 97.5], 2);
conf95CcepWin = conf95Ccep(ttseg >= 0 & ttseg < durEP, :); % on window of interest for broadband
fracSigCcep = sum(conf95CcepWin(:, 1) > 0 | conf95CcepWin(:, 2) < 0) / size(conf95CcepWin, 1);
% Quantify Visual percent of tp significantly different from 0
if fitCohEP % if image model, we assume there was a visual response and just set fraction to 1 (all tps) by default
fracSigVis = 1;
else
conf95Vis = prctile(squeeze(xMatBoot(:, 2, :)), [2.5, 97.5], 2);
conf95VisWin = conf95Vis(ttseg >= 0 & ttseg < durEP, :); % on window of interest for broadband
fracSigVis = sum(conf95VisWin(:, 1) > 0 | conf95VisWin(:, 2) < 0) / size(conf95CcepWin, 1);
end
% BOTH frac sig CCEP and Visual need to be >= threshold for this to be summative
isSummative = fracSigCcep >= minFrac && fracSigVis >= minFrac;
fprintf('EP to %0.2f: Frac sig CCEP = %0.2f, Frac sig Visual = %0.2f, summative = %s\n', durEP, fracSigCcep, fracSigVis, upper(string(isSummative)));
% save a text output
if saveOutputs
f = fopen(fullfile(outdir, sprintf('checkSummativeEP_%s_%s_%s.txt', sub, ch, site)), 'w');
fprintf(f, 'durEP = %0.2f, minFrac = %0.2f\n', durEP, minFrac);
fprintf(f, 'Frac sig CCEP = %0.2f\n', fracSigCcep);
fprintf(f, 'Frac sig Visual = %0.2f\n', fracSigVis);
fprintf(f, 'Summative = %s\n', string(isSummative));
fclose(f);
end
%% 2) Check BROADBAND summation for ch-site pairs
%sub = '1';
%ch = 'LOC2';
%ch = 'LY11';
%ch = 'LV15';
%site = 'LOC4-LOC5';
%site = 'LG6-LG7';
sub = '2';
%ch = 'ROC2';
ch = 'RY12';
%site = 'ROC6-ROC7';
site = 'RMO3-RMO4';
% for this pair, get if the best fit was simple vs image model. Note we can assume fitISI=0 because these are for non-interactive conditions
fitCohBB = getFitCohBB(ch, site);
fprintf('Loading BB fitCoh=%s bootstraps for %s, %s, %s\n', string(fitCohBB), sub, ch, site);
% load the bootstrapped fits
nBoots = 200;
temp = load(fullfile(loaddir, sprintf('firBBBootstrap_%s_%s_%s_bbType-power_fitISI-0_fitCoh-%d_errType-abs', sub, ch, site, double(fitCohBB)), ...
sprintf('firBBBootstrap_%s_%s_%s_bbType-power_fitISI-0_fitCoh-%d_errType-abs_seed-1.mat', sub, ch, site, double(fitCohBB))));
initSz = size(temp.xMat);
xMatBoot = nan(initSz(1), initSz(2), nBoots);
for ii = 1:nBoots
mdl = load(fullfile(loaddir, sprintf('firBBBootstrap_%s_%s_%s_bbType-power_fitISI-0_fitCoh-%d_errType-abs', sub, ch, site, double(fitCohBB)), ...
sprintf('firBBBootstrap_%s_%s_%s_bbType-power_fitISI-0_fitCoh-%d_errType-abs_seed-%d.mat', sub, ch, site, double(fitCohBB), ii)));
xMatBoot(:, :, ii) = mdl.xMat;
end
ttseg = mdl.ttseg;
% plot the bootstrap fits to make sure it matches our figures
xlims = [-0.2, 1];
ylims = [-1, 10]; % for power
figure('Position', [200, 200, 500, 300]);
subplot(1, 2, 1); hold on
yline(0);
plotCurvConfSample(ttseg, squeeze(xMatBoot(:, 1, :))', 'k', 0.3, 95); % CCEP
plot(ttseg, mean(xMatBoot(:, 1, :), 3), 'k-', 'LineWidth', 0.75);
hold off
set(gca, 'xtick', 0:0.2:1); xlim(xlims); ylim(ylims);
if ~fitCohBB % simple model
subplot(1, 2, 2); hold on
yline(0);
plotCurvConfSample(ttseg, squeeze(xMatBoot(:, 2, :))', defaultCols(1, :), 0.3, 95); % CCEP
plot(ttseg, mean(xMatBoot(:, 2, :), 3), 'Color', defaultCols(1, :), 'LineWidth', 0.75);
hold off
set(gca, 'xtick', 0:0.2:1); xlim(xlims); ylim(ylims);
else % img model
subplot(1, 2, 2); hold on
hs = zeros(1, 7);
yline(0);
for ii = 1:7
hs(ii) = plot(ttseg, mean(xMatBoot(:, ii+1, :), 3), 'Color', imgCondColors(ii, :), 'LineWidth', 0.75);
end
hold off
set(gca, 'xtick', 0:0.2:1); xlim(xlims); ylim(ylims);
end
% Quantify CCEP percent of tp significantly different from 0
conf95Ccep = prctile(squeeze(xMatBoot(:, 1, :)), [2.5, 97.5], 2);
conf95CcepWin = conf95Ccep(ttseg >= 0 & ttseg < durBB, :); % on window of interest for broadband
fracSigCcep = sum(conf95CcepWin(:, 1) > 0 | conf95CcepWin(:, 2) < 0) / size(conf95CcepWin, 1);
% Quantify Visual percent of tp significantly different from 0
if fitCohBB % if image model, we assume there was a visual response and just set fraction to 1 (all tps) by default
fracSigVis = 1;
else
conf95Vis = prctile(squeeze(xMatBoot(:, 2, :)), [2.5, 97.5], 2);
conf95VisWin = conf95Vis(ttseg >= 0 & ttseg < durBB, :); % on window of interest for broadband
fracSigVis = sum(conf95VisWin(:, 1) > 0 | conf95VisWin(:, 2) < 0) / size(conf95CcepWin, 1);
end
% BOTH frac sig CCEP and Visual need to be >= threshold for this to be summative
isSummative = fracSigCcep >= minFrac && fracSigVis >= minFrac;
fprintf('BB to %0.2f: Frac sig CCEP = %0.2f, Frac sig Visual = %0.2f, summative = %s\n', durBB, fracSigCcep, fracSigVis, upper(string(isSummative)));
% save a text output
if saveOutputs
f = fopen(fullfile(outdir, sprintf('checkSummativeBroadband_%s_%s_%s.txt', sub, ch, site)), 'w');
fprintf(f, 'durBB = %0.2f, minFrac = %0.2f\n', durBB, minFrac);
fprintf(f, 'Frac sig CCEP = %0.2f\n', fracSigCcep);
fprintf(f, 'Frac sig Visual = %0.2f\n', fracSigVis);
fprintf(f, 'Summative = %s\n', string(isSummative));
fclose(f);
end
%% Functions
% evoked potentials
function fitCoh = getFitCohEP(ch, site)
if strcmpi(ch, 'LOC1') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LOC2') && strcmpi(site, 'LG6-LG7')
fitCoh = true;
elseif strcmpi(ch, 'ROC1') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
elseif strcmpi(ch, 'ROC2') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
% global electrodes
elseif strcmpi(ch, 'LOC10') && strcmpi(site, 'LG6-LG7') % re5
fitCoh = true; % need to save, but visually obvious there is a response in both CCEP and visual
elseif strcmpi(ch, 'LOC11') && strcmpi(site, 'LOC4-LOC5') % re6
fitCoh = false;
elseif strcmpi(ch, 'LY11') && strcmpi(site, 'LOC4-LOC5') % re 7
fitCoh = false;
elseif strcmpi(ch, 'LY11') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LV13') && strcmpi(site, 'LOC4-LOC5') % re 8
fitCoh = false;
elseif strcmpi(ch, 'LV13') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LV14') && strcmpi(site, 'LOC4-LOC5') % re 9
fitCoh = false;
elseif strcmpi(ch, 'LV14') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LV15') && strcmpi(site, 'LOC4-LOC5') % re 10
fitCoh = false;
elseif strcmpi(ch, 'LV15') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'RY10') && strcmpi(site, 'ROC6-ROC7') % re11
fitCoh = false;
elseif strcmpi(ch, 'RY10') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
elseif strcmpi(ch, 'RY11') && strcmpi(site, 'ROC6-ROC7') % re12
fitCoh = false;
elseif strcmpi(ch, 'RY11') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
elseif strcmpi(ch, 'RY12') && strcmpi(site, 'ROC6-ROC7') % re13
fitCoh = false;
elseif strcmpi(ch, 'RY12') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
else
error('This is not a non-interactive ch/site pair');
end
end
% broadband
function fitCoh = getFitCohBB(ch, site)
if strcmpi(ch, 'LOC1') && strcmpi(site, 'LOC4-LOC5')
fitCoh = false;
elseif strcmpi(ch, 'LOC2') && strcmpi(site, 'LOC4-LOC5')
fitCoh = false;
elseif strcmpi(ch, 'LOC1') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LOC2') && strcmpi(site, 'LG6-LG7')
fitCoh = true;
elseif strcmpi(ch, 'ROC1') && strcmpi(site, 'ROC6-ROC7')
fitCoh = true;
elseif strcmpi(ch, 'ROC2') && strcmpi(site, 'ROC6-ROC7')
fitCoh = false;
elseif strcmpi(ch, 'ROC1') && strcmpi(site, 'RMO3-RMO4')
fitCoh = true;
elseif strcmpi(ch, 'ROC2') && strcmpi(site, 'RMO3-RMO4')
fitCoh = true;
% global channels
elseif strcmpi(ch, 'LOC10') && strcmpi(site, 'LOC4-LOC5') % re5
fitCoh = false;
elseif strcmpi(ch, 'LOC10') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LOC11') && strcmpi(site, 'LOC4-LOC5') % re6
fitCoh = false;
elseif strcmpi(ch, 'LOC11') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LY11') && strcmpi(site, 'LOC4-LOC5') % re7
fitCoh = false;
elseif strcmpi(ch, 'LY11') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LV13') && strcmpi(site, 'LOC4-LOC5') % re8
fitCoh = false;
elseif strcmpi(ch, 'LV13') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LV14') && strcmpi(site, 'LOC4-LOC5') % re9
fitCoh = false;
elseif strcmpi(ch, 'LV14') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'LV15') && strcmpi(site, 'LOC4-LOC5') % re10
fitCoh = false;
elseif strcmpi(ch, 'LV15') && strcmpi(site, 'LG6-LG7')
fitCoh = false;
elseif strcmpi(ch, 'RY10') && strcmpi(site, 'ROC6-ROC7') % re11
fitCoh = false;
elseif strcmpi(ch, 'RY10') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
elseif strcmpi(ch, 'RY11') && strcmpi(site, 'ROC6-ROC7') % re12
fitCoh = false;
elseif strcmpi(ch, 'RY11') && strcmpi(site, 'RMO3-RMO4')
fitCoh = true;
elseif strcmpi(ch, 'RY12') && strcmpi(site, 'ROC6-ROC7') % re13
fitCoh = false;
elseif strcmpi(ch, 'RY12') && strcmpi(site, 'RMO3-RMO4')
fitCoh = false;
else
error('This is not a non-interactive ch/site pair');
end
end