-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_eeg_simulate.m
429 lines (297 loc) · 10.3 KB
/
spm_eeg_simulate.m
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
function [Dnew,meshsourceind]=spm_eeg_simulate(D,prefix,patchmni,simsignal,ormni,woi,whitenoise,SNRdB,trialind,mnimesh,dipfwhm,nAmdipmom);
%function [Dnew,meshsourceind]=spm_eeg_simulate(D,prefix,patchmni,simsignal,woi,whitenoise,SNRdB,trialind,mnimesh,dipfwhm);
%% Simulate a number of MSP patches at specified locations on existing mesh
%
% Created by: Jose David Lopez - [email protected]
% Gareth Barnes - [email protected]
% Vladimir Litvak - [email protected]
%
%% D dataset
%% prefix : prefix of new simulated dataset
%% patchmni : patch centres in mni space or patch indices
%% simsignal : Nsources x time series in nAm withinn woi
%% woi: window of interest in seconds
%% whitenoise level in rms femto Tesla or micro volts
%% SNRdB power signal to noise ratio in dBs
%% trialind: trials on which the simulated data will be added to the noise
%% mnimesh : a new mesh with vertices in mni space
%% dipfwhm - patch smoothness in mm
%% Outputs
%% Dnew- new dataset
%% meshsourceind- vertex indices of sources on the mesh
% $Id: spm_eeg_simulate.m 7118 2017-06-20 10:33:27Z guillaume $
%% LOAD IN ORGINAL DATA
useind=1; % D to use
if nargin<2,
prefix='';
end;
if nargin<3,
patchmni=[];
end;
if nargin<4,
simsignal=[];
end;
if nargin<5,
ormni=[];
end;
if nargin<6,
woi=[];
end;
if nargin<7,
whitenoise=[];
end;
if nargin<8,
SNRdB=[];
end;
if nargin<9,
trialind=[];
end;
if nargin<10,
mnimesh=[];
end;
if nargin<11
dipfwhm=[]; %% number of iterations used to smooth patch out (more iterations, larger patch)
end;
if isempty(prefix),
prefix='sim';
end;
if isempty(dipfwhm),
dipfwhm=6; %% FWHM in mm
end;
if isempty(woi),
woi=[D{useind}.time(1) D{useind}.time(end)];
end;
val=D{useind}.val;
if isempty(patchmni),
patchmni=[-45.4989 -30.6967 4.9213;...
46.7322 -31.2311 4.0085];
end;
if ~xor(isempty(whitenoise),isempty(SNRdB))
error('Must specify either white noise level or sensor level SNR');
end;
[a1 b1 c1]=fileparts(D{useind}.fname);
newfilename=[prefix b1];
%% forcing overwrite of an existing file
Dnew=D{useind}.clone([prefix b1]);
if isempty(trialind)
trialind=1:Dnew.ntrials;
end;
modstr=deblank(modality(D{1}));
disp(sprintf('Simulating data on %s channels only',modstr));
if ~isempty(mnimesh),
Dnew.inv{val}.mesh.tess_mni.vert=mnimesh.vert;
Dnew.inv{val}.mesh.tess_mni.face=mnimesh.face;
Dnew.inv{val}.forward.mesh.vert=spm_eeg_inv_transform_points(Dnew.inv{val}.datareg.fromMNI,mnimesh.vert);
Dnew.inv{val}.forward.mesh.face=mnimesh.face;
end; % if
% Two synchronous sources
if ~isempty(patchmni),
Ndips=size(patchmni,1);
else
Ndips=0;
end;
if size(simsignal,1)~=Ndips,
error('number of signals given does not match number of sources');
end;
meshsourceind=[];
disp('Using closest mesh vertices to the specified coordinates')
for d=1:Ndips,
vdist= Dnew.inv{val}.mesh.tess_mni.vert-repmat(patchmni(d,:),size(Dnew.inv{val}.mesh.tess_mni.vert,1),1);
dist=sqrt(dot(vdist',vdist'));
[mnidist(d),meshsourceind(d)] =min(dist);
end;
disp(sprintf('Furthest distance from dipole location to mesh %3.2f mm',max(mnidist)));
if max(mnidist)>0.1
warning('Supplied vertices do not sit on the mesh!');
end;
Ndip = size(simsignal,1); % Number of dipoles
sensorunits = Dnew.units; %% of sensors (T or fT)
try Dnew.inv{val}.forward.vol.unit, %% units of forward model for distance (m or mm)
switch(Dnew.inv{val}.forward.vol.unit), %% correct for non-SI lead field scaling
case 'mm'
Lscale=1000*1000;
case 'cm'
Lscale=100*100;
case 'm'
Lscale=1.0;
otherwise
error('unknown volume unit');
end;
catch
disp('No distance units found');
Lscale=1.0;
end;
Ntrials = Dnew.ntrials; % Number of trials
% define period over which dipoles are active
startf1 = woi(1); % (sec) start time
endf1 = woi(2); %% end time
f1ind = intersect(find(Dnew.time>startf1),find(Dnew.time<=endf1));
if length(f1ind)~=size(simsignal,2),
error('Signal does not fit in time window');
end;
%if isequal(modstr, 'MEG')
try
chanind = Dnew.indchantype({'MEG', 'MEGPLANAR'}, 'GOOD');
catch
chanind = Dnew.indchantype(modality(D{1}), 'GOOD');
end
labels=Dnew.chanlabels(chanind);
%chans = Dnew.indchantype(modstr, 'GOOD');
simscale=1.0;
try
%% white noise is input in fT or uV so convert it to data sensorunits
switch sensorunits{chanind(1)}
case 'T'
simscale=1e-15; %% convert from fT to T
%whitenoise=whitenoise./1e15; %% rms femto Tesla
%tmp=tmp./1e15; %% also computed in fT originally
case 'fT'
simscale=1.0; %% sensors already in fT
%whitenoise=whitenoise; %% rms Tesla
%tmp=tmp;
case 'uV'
whitenoise=whitenoise; %% micro volts
tmp=tmp;
case 'V'
whitenoise=whitenoise./1e6; %% volts
tmp=tmp./1e6;
error('not supported for EEG at the moment');
otherwise
error('unknown sensor unit')
end;
catch
disp('No sensor sensorunits found');
end;
whitenoise=whitenoise.*simscale;
if ~isempty(ormni), %%%% DIPOLE SIMULATION
disp('SIMULATING DIPOLE SOURCES');
if size(ormni)~=size(patchmni),
error('A 3D orientation must be specified for each source location');
end;
posdipmm=Dnew.inv{val}.datareg.fromMNI*[patchmni ones(size(ormni,1),1)]'; %% put into MEG space
posdipmm=posdipmm(1:3)';
%% need to make a pure rotation for orientation transform to native space
M1 = Dnew.inv{val}.datareg.fromMNI;
[U, L, V] = svd(M1(1:3, 1:3));
ordip=ormni*(U*V');
ordip=ordip./sqrt(dot(ordip,ordip)); %% make sure it is unit vector
%% NB COULD ADD A PURE DIPOLE SIMULATION IN FUTURE
sens=Dnew.inv{val}.forward.sensors;
vol=Dnew.inv{val}.forward.vol;
%% Get good channels
useind=Dnew.indchantype(Dnew.modality);
useind=setxor(Dnew.badchannels,goodchans);
tmp=zeros(length(chanind),Dnew.nsamples);
for i=1:Ndip,
gmn = ft_compute_leadfield(posdipmm(i,:)*1e-3, sens, vol, 'dipoleunit', 'nA*m','chanunit',sensorunits);
gain=gmn*ordip';
tmp(:,f1ind)=tmp(:,f1ind)+gain(usedind,:)*simsignal(i,:);
end; % for i
else %%% CURRENT DENSITY ON SURFACE SIMULATION
disp('SIMULATING CURRENT DISTRIBUTIONS ON MESH');
%% CREATE A NEW FORWARD model for e mesh
fprintf('Computing Gain Matrix: ')
spm_input('Creating gain matrix',1,'d'); % Shows gain matrix computation
[L Dnew] = spm_eeg_lgainmat(Dnew); % Gain matrix
if isfield(Dnew.inv{val}.forward,'scale'),
L=L./Dnew.inv{val}.forward.scale; %% account for rescaling of lead fields
end;
Nd = size(L,2); % number of dipoles
Nchans=size(L,1);
fprintf(' - done\n')
nativemesh=Dnew.inv{val}.forward.mesh;
Qe=[];, %% SNR may be defined by sensor level data in which case we have to get data first then go back
%[Qp,Qe,priors,priorfname] = spm_eeg_invert_EBconstruct_priors(Dnew,val,nativemesh,priors,Qe,L,'sim');
base.FWHMmm=dipfwhm;
base.nAm=nAmdipmom;
[a1,b1,c1]=fileparts(Dnew.fname);
priordir=[Dnew.path filesep 'simprior_' b1 ];
mkdir(priordir);
fprintf('Saving prior in directory %s\n',priordir);
[Qp,Qe,priorfname]=spm_eeg_invert_setuppatches(meshsourceind,nativemesh,base,priordir,Qe,L);
% Add waveform of all smoothed sources to their equivalent dipoles
% QGs add up to 0.9854
fullsignal=zeros(Ndip,Dnew.nsamples); %% simulation padded with zeros
fullsignal(1:Ndip,f1ind)=simsignal;
tmp = sparse(zeros(Nchans,Dnew.nsamples)); % simulated data
X=zeros(size(full(Qp{1}.q)));
for j=1:Ndip
Lq=L*Qp{j}.q; %% lead field * prior source distribution
X=X+full(Qp{j}.q);
for i=1:Dnew.nsamples,
tmp(:,i) = tmp(:,i) + Lq*fullsignal(j,i); %% +sqrt(Qe)*randn(size(Qe,1),1);
end;
end;
tmp=tmp.*simscale; %% scale to sensor units
end; % if ori
allchanstd=std(tmp');
meanrmssignal=mean(allchanstd);
if ~isempty(SNRdB),
whitenoise = meanrmssignal.*(10^(-SNRdB/20));
disp(sprintf('Setting white noise to give sensor level SNR of %dB',SNRdB));
end;
Qe=eye(Nchans).*(whitenoise^2); %% sensor level noise
YY=zeros(length(chanind),length(chanind));
n=0;
for i=1:Ntrials
if any(i == trialind), %% only add signal to specific trials
Dnew(chanind,:,i) = full(tmp);
else
Dnew(chanind,:,i)=zeros(size(tmp));
end;
Dnew(:,:,i)=Dnew(:,:,i)+randn(size(Dnew(:,:,i))).*whitenoise; %% add white noise in fT
y=squeeze(Dnew(chanind,:,i));
YY=YY+y*y';
n=n+size(y,2); %% number of samples
end
YY=YY./n; %% NORMALIZE HERE
F=[];
UL=L;
save(priorfname,'Qp','Qe','UL','F', spm_get_defaults('mat.format'));
figure;
ploton=1;
[LQpL,Q,sumLQpL,QE,Csensor]=spm_eeg_assemble_priors(L,Qp,{Qe},ploton);
figure;
subplot(3,1,1);
imagesc(YY);colorbar;
title('Empirical data covariance per sample: YY');
subplot(3,1,2);
imagesc(Csensor);colorbar;
title('Prior total sensor covariance');
subplot(3,1,3);
imagesc(YY-Csensor);colorbar;
title('Difference');
%% Plot and save
[dum,tmpind]=sort(allchanstd);
dnewind=chanind(tmpind);
if isempty(ormni)
hold on
mnivert=Dnew.inv{val}.mesh.tess_mni.vert;
Nj = size(mnivert,1);
M = X;
G = sqrt(sparse(1:Nj,1,M,Nj,1));
Fgraph = spm_figure('GetWin','Graphics');
j = find(M);
clf(Fgraph)
figure(Fgraph)
spm_mip(G(j),mnivert(j,:)',6);
axis image
title({sprintf('Generated source activity')});
drawnow
end;
figure
aux = tmp(tmpind(end),:);
subplot(2,1,1);
plot(Dnew.time,Dnew(dnewind(end),:,1),Dnew.time,aux,'r');
title('Measured activity over max sensor');
legend('Noisy','Noiseless');
ylabel(sensorunits{chanind(1)});
subplot(2,1,2);
aux = tmp(tmpind(floor(length(tmpind)/2)),:);
plot(Dnew.time,Dnew(dnewind(floor(length(tmpind)/2)),:,1),Dnew.time,aux,'r');
title('Measured activity over median sensor');
legend('Noisy','Noiseless');
ylabel(sensorunits{chanind(1)});
xlabel('Time in sec');
Dnew.save;
fprintf('\n Finish\n')