forked from hharveygit/SPES_Visual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateModelFigs.m
More file actions
executable file
·349 lines (292 loc) · 10.6 KB
/
createModelFigs.m
File metadata and controls
executable file
·349 lines (292 loc) · 10.6 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
338
339
340
341
342
343
344
345
346
347
348
%% Create sample of how curves would look for each type of model, figure 4A
%
% 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/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
set(0, 'DefaultFigureRenderer', 'painters');
% Choose a set of fitted data to load so we can use its shapes as a starting point
sub = '1';
chName = 'LOC1';
site = 'LOC4-LOC5';
fitISI = true;
fitCoh = false;
saveOutputs = true;
outdir = fullfile('output', 'simulated');
loaddir = fullfile('output', 'mforge'); % to load results from mforge
% Configurations constant for all the plots
lw = 1; % line width of plots
ylims = [-250, 250];
%% Load data and pull out xMat
mdl = load(fullfile(loaddir, sprintf('firFullErp_%s_%s_%s_fitISI-%d_fitCoh-%d.mat', sub, chName, site, double(fitISI), double(fitCoh))));
ttseg = mdl.ttseg;
xMat = mdl.xMat;
figure; plot(ttseg, xMat);
% basic CCEP shape
SCcep0 = xMat(:, 1);
%% 1) Simple model
% basic visual response -- we'll use the 200 ms ISI prediction for this
SVisual0 = xMat(:, 4);
% process SCcep0 and use only part of it
SCcepTemp = processSig(ttseg, SCcep0);
SCcep1 = SCcep0;
SCcep1(ttseg > 0.1) = SCcepTemp(ttseg > 0.1);
SCcep1 = movmean(SCcep1, 20); % smooth out the kink
SCcep1 = [zeros(5, 1); SCcep1(1:end-5)]; % shift CCEP slightly to account for the anticausal rise caused by smoothing
% process visual
SVisual1 = processSig(ttseg, SVisual0);
% Plot components
figure('Position', [200, 200, 250, 300]);
% Plot CCEP 200 ms before visual
subplot(3, 2, 1); hold on
shiftSamps = sum(ttseg < (ttseg(1) + 0.2));
SCcep1Shift200 = circshift(SCcep1, -shiftSamps);
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SCcep1Shift200, 'k-', 'LineWidth', lw);
plot([0.1, 0.3], [ylims(2)-10, ylims(2)-10], 'k-', 'LineWidth', 3); % marker for length (200 ms)
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% Plot visual only
subplot(3, 2, 3); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% combined, with CCEP shifted 200 ms
subplot(3, 2, 5); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1 + SCcep1Shift200, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% Now plot them at 0 ms
subplot(3, 2, 2); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SCcep1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% visual only, same as 323
subplot(3, 2, 4); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% combined, at 0 ms ISI
subplot(3, 2, 6); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1 + SCcep1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
if saveOutputs
saveas(gcf, fullfile(outdir, 'erp_simulation_simple'), 'png');
saveas(gcf, fullfile(outdir, 'erp_simulation_simple'), 'svg');
end
%% 2) Coherence model. For this we'll show the CCEP and the visual response, at a few different amplitudes corresponding to different images
% Plot components
figure('Position', [200, 200, 250, 300]);
SVisualLow = SVisual1*0.5;
SVisualHigh = SVisual1*1.5;
% Plot the CCEP again, now with 100 ms shifted so it doesn't convey a sense of ISI
subplot(3, 2, 3); hold on
shiftSamps = sum(ttseg < (ttseg(1) + 0.1));
SCcep1Shift100 = circshift(SCcep1, -shiftSamps);
yline(0, 'Color', [0.5, 0.5, 0.5]);
plot(ttseg, SCcep1Shift100, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.5]); ylim(ylims);
axis off
hold off
% Low amplitude response
subplot(3, 2, 2); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
plot(ttseg, SVisualLow, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% Normal amplitude response
subplot(3, 2, 4); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
plot(ttseg, SVisual1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% High amplitude response
subplot(3, 2, 6); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
plot(ttseg, SVisualHigh, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
if saveOutputs
saveas(gcf, fullfile(outdir, 'erp_simulation_image'), 'png');
saveas(gcf, fullfile(outdir, 'erp_simulation_image'), 'svg');
end
%% 3) ISI model. We'll make similar plots as for the simple model, but using a different response for 0 ms vs 200 ms
SVisualISI0 = xMat(:, 2); % we'll use the 0ms ISI visual response for this
SVisualISI1 = processSig(ttseg, SVisualISI0);
% Plot components
figure('Position', [200, 200, 250, 300]);
% Plot CCEP 200 ms before visual
subplot(3, 2, 1); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SCcep1Shift200, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% Plot visual only
subplot(3, 2, 3); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% combined, with CCEP shifted 200 ms
subplot(3, 2, 5); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1 + SCcep1Shift200, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% Now plot them at 0 ms
subplot(3, 2, 2); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SCcep1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% visual only. Plot both, with the previous one in gray (we'll make dashed in illustrator).
% Now the idea will be we point an arrow from gray to black
subplot(3, 2, 4); hold on
yline(0, 'Color', [0.3, 0.3, 0.3]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisual1, '-', 'Color', [0.5, 0.5, 0.5], 'LineWidth', lw);
plot(ttseg, SVisualISI1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% combined, at 0 ms ISI
subplot(3, 2, 6); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisualISI1 + SCcep1, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
if saveOutputs
saveas(gcf, fullfile(outdir, 'erp_simulation_ISI'), 'png');
saveas(gcf, fullfile(outdir, 'erp_simulation_ISI'), 'svg');
end
%% 4) Full model. For this we'll show a 2x2 set of visual responses with ellipses in the middle
% Create a low and high response version of the 0ms ISI-modulated response
SVisualISILow = 0.5*SVisualISI1;
SVisualISIHigh = SVisualISI1;
% Plot components
figure('Position', [200, 200, 375, 200]); % make this 2x3 so the (1,1) plot is the CCEP
% The ccep, same as in the coherence model plot
subplot(2, 3, 1); hold on
shiftSamps = sum(ttseg < (ttseg(1) + 0.1));
SCcep1Shift100 = circshift(SCcep1, -shiftSamps);
yline(0, 'Color', [0.5, 0.5, 0.5]);
plot(ttseg, SCcep1Shift100, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.5]); ylim(ylims);
axis off
hold off
% 200 ms, low coherence visual response
subplot(2, 3, 2); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisualLow, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% 0 ms ISI, modulated low coherence visual response
subplot(2, 3, 3); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisualISILow, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% 200 ms, high coherence visual response
subplot(2, 3, 5); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(-0.2, 'Color', [1, 0.6, 0]); xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisualHigh, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
% 0 ms ISI, modulated high coherence visual response
subplot(2, 3, 6); hold on
yline(0, 'Color', [0.5, 0.5, 0.5]);
xline(0, 'Color', [0, 0, 1]);
plot(ttseg, SVisualISIHigh, 'k-', 'LineWidth', lw);
xlim([-0.2, 0.6]); ylim(ylims);
axis off
hold off
if saveOutputs
saveas(gcf, fullfile(outdir, 'erp_simulation_full'), 'png');
saveas(gcf, fullfile(outdir, 'erp_simulation_full'), 'svg');
end
%% 5) Stich a few responses together for the "b" matrix in FIR model part
% 3 different signal types concatenated
b = [SCcep1 + SVisual1; SVisualHigh; SCcep1Shift200 + SVisualISI1];
ttStitch = [ttseg, ttseg, ttseg]'; % stitched time points
figure('Position', [200, 200, 600, 100]);
plot(b, 'k-'); xlim([-inf, inf]);
xline([find(ttStitch == 0)], 'Color', 'r');
axis off
if saveOutputs
saveas(gcf, fullfile(outdir, 'erp_simulation_3stitched'), 'png');
saveas(gcf, fullfile(outdir, 'erp_simulation_3stitched'), 'svg');
end
%% Functions
function S = processSig(tt, S0)
% lowpass
d = designfilt('lowpassiir', ... % Response type
'PassbandFrequency',20, ... % Frequency constraints
'StopbandFrequency',40, ...
'PassbandRipple',3, ... % Magnitude constraints
'StopbandAttenuation',30, ...
'DesignMethod','butter', ... % Design method
'MatchExactly','passband', ... % Design method options
'SampleRate',600); % Sample rate
S = filtfilt(d, S0);
S = movmean(S, 50);
S = S .* exp(-2*tt(:));
end