-
Notifications
You must be signed in to change notification settings - Fork 2
/
WH_Resolution_Functions_bioRxiv0.py
executable file
·609 lines (428 loc) · 16.5 KB
/
WH_Resolution_Functions_bioRxiv0.py
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
"""
=========================================================
Functions for EEG/MEG Resolution
=========================================================
"""
# OH July 2018
import glob
from copy import deepcopy
import numpy as np
from scipy.linalg import svd as sci_svd
import mne
import mne.minimum_norm.WH_psf_ctf as psf_ctf
# TO DO: Still need to find good example for source distribution from which to compute null space.
def leadfield_nullspace(fwd, noise_cov, regpar=0.):
""" Compute null space distributions for leadfield.
Parameters
----------
fwd: forward solution
Used to get leadfield matrix.
noise_cov: noise covariance matrix
Used to (diagonally) whiten leadfield.
regpar: float
Regularisation parameter (Tiknonov) for pseudoinverse.
Default: 0.
Returns
-------
nullspace: 2D numpy array (n_ch distributions).
Distributions in leadfield nullspace.
Null projector will be applied to point sources.
"""
# get leadfield matrix from forward solution
leadfield = psf_ctf._pick_leadfield(fwd['sol']['data'], fwd, fwd['info']['ch_names'])
# number of channels and sources
n_c, n_s = leadfield.shape
# covariance matrix will be modified
cov = deepcopy(noise_cov)
# use diagonal noise covariance matrix
cov = cov.as_diag()
# compute whitener from noise covariance matrix
info = fwd['info']
info['projs'] = noise_cov['projs']
info['comps'] = ''
whitener = mne.cov.compute_whitener(cov, info)[0]
# whiten leadfield
leadfield = whitener.dot(leadfield)
# scale leadfield, maybe better for stability
leadfield = leadfield / leadfield.max()
# compute pseudoinverse the "matrix way", for intuitive regularisation
# Gram matrix
gram = leadfield.dot(leadfield.T)
trace_gram = np.trace(gram)
eye_gram = np.eye(n_c)
# invert Gram matrix using Tikhonov regularisation
# (don't use np.linalg.inv here)
gram_inv = np.linalg.pinv(gram + regpar*(trace_gram/n_c)*eye_gram)
# full pseudoinverse
pseudoinv = leadfield.T.dot(gram_inv)
# null space projector
null_proj = np.eye(n_s) - pseudoinv.dot(leadfield)
sources = np.empty([n_s,5])
# point sources, every tenth
sources = np.eye(n_s)[:,::10]
# null-space projection applied to sources
nullspace = null_proj.dot(sources)
return nullspace, leadfield, pseudoinv, null_proj
def make_resolution_matrix(fwd, invop, method, lambda2):
""" Compute resolution matrix for linear inverse operator.
Parameters
----------
fwd: forward solution
Used to get leadfield matrix.
invop: inverse operator
Inverse operator to get inverse matrix.
pick_ori='normal' will be selected.
method: string
Inverse method to use (MNE, dSPM, sLORETA).
lambda2: float
The regularisation parameter.
Returns
-------
resmat: 2D numpy array.
Resolution matrix (inverse matrix times leadfield).
"""
info = fwd['info']
# get leadfield matrix from forward solution
leadfield = psf_ctf._pick_leadfield(fwd['sol']['data'], fwd, fwd['info']['ch_names'])
invmat, _ = psf_ctf._get_matrix_from_inverse_operator(invop, fwd, method=method, lambda2=lambda2,
pick_ori='normal')
resmat = invmat.dot(leadfield)
return resmat
def make_resolution_matrix_lcmv(fwd, info, noise_cov, data_cov):
""" Compute resolution matrix for linear inverse operator.
Parameters
----------
forward : dict
The forward operator.
info: instance of Info
Should contain measurement information, e.g. sfreq, projs.
noise_cov: noise covariance matrix
Used to compute whitener. Should be regularised.
data_cov: data covariance matrix
Used to compute LCMV beamformer. Should be regularised.
Returns
-------
resmat: 2D numpy array.
Resolution matrix (inverse matrix times leadfield).
"""
# get leadfield matrix from forward solution
leadfield = psf_ctf._pick_leadfield(fwd['sol']['data'], fwd, info['ch_names'])
invmat = _get_matrix_from_LCMV_beamformer(fwd, info, noise_cov, noise_cov)
resmat = invmat.dot(leadfield)
return resmat
def mat_to_stc(mat, src):
""" Turn columns of matrix to STCs.
Parameters
----------
mat: 2D numpy array
For example resolution matrix (or its transpose).
src: SourceSpace
Source space with vertex numbers, e.g. forward['src'].
Returns
-------
stc: Instance of SourceEstimate
PSFs or CTFs as source estimate object.
"""
vertno = [src[0]['vertno'], src[1]['vertno']]
# convert norms to source estimate
stc = mne.SourceEstimate(mat, vertno, tmin=0., tstep=1.)
return stc
def relative_amplitude(resmat, locations, axis, metric='peak'):
""" Compute relative amplitude metrics for resolution matrix.
Parameters
----------
resmat: 2D numpy array
The resolution matrix (nloc-by-nloc).
locations: 2D (nloc-by-3) numpy array
Locations (in m) to be used for resolution metrics (distances etc.).
axis: integer (0 or 1)
Whether to compute metrics for columns (=0, PSFs) or rows (=1, CTFs).
metric: string ('peak')
Which amplitudes to use.
'peak': Ratio between absolute maximum amplitudes of peaks per location
and maximum peak across locations.
'sum': Ratio between sums of absolute amplitudes.
Returns
-------
relamp: 1D numpy array.
Relative amplitude metric per location.
"""
# NOTE: locations needed?
# only use absolute values
resmat = np.absolute(resmat)
# Ratio between amplitude at peak and global peak maximum
if metric.lower() == 'peak':
# maximum amplitudes along specified axis
maxamps = resmat.max(axis=axis)
# global absolute maximum
maxmaxamps = maxamps.max()
relamp = maxamps / maxmaxamps
# ratio between sums of absolute amplitudes
elif metric.lower() == 'sum':
# sum of amplitudes per location
sumamps = np.sum(resmat, axis=axis)
# maximum of summed amplitudes
sumampsmax = sumamps.max()
relamp = sumamps / sumampsmax
return relamp
def spatial_width(resmat, locations, axis, metric='sd'):
""" Compute spatial width metrics for resolution matrix.
Parameters
----------
resmat: 2D numpy array
The resolution matrix (nloc-by-nloc).
locations: 2D (nloc-by-3) numpy array
Locations (in m) to be used for resolution metrics (distances etc.).
axis: integer (0 or 1)
Whether to compute metrics for columns (=0, PSFs) or rows (=1, CTFs).
metric: string ('sd' | 'rad')
What type of width metric to compute.
'sd': spatial deviation (e.g. Molins et al.).
'maxrad': maximum radius to 50% of max amplitude.
Returns
-------
width: 1D numpy array.
Spatial width metric per location.
"""
# only use absolute values
resmat = np.absolute(resmat)
# The below will operate on columns
if axis == 1:
resmat = resmat.T
# find indices of maxima along rows
resmax = resmat.argmax(axis=0)
# initialise output array
width = np.empty(len(resmax))
# spatial deviation as in Molins et al.
if metric.lower() == 'sd':
for ii in range(0, locations.shape[0]):
# locations relative to true source
diffloc = locations - locations[ii,:]
# squared Euclidean distances to true source
locerr = np.sum(diffloc**2,1)
# pick current row
resvec = resmat[:,ii]**2
# spatial deviation (Molins et al, NI 2008, eq. 12)
width[ii] = np.sqrt(np.sum(np.multiply(locerr, resvec))/np.sum(resvec))
# maximum radius to 50% of max amplitude
elif metric.lower() == 'maxrad':
# peak amplitudes per location across columns
maxamp = resmat.max(axis=0)
for (ii,aa) in enumerate(maxamp): # for all locations
# pick current column
resvec = resmat[:,ii]
# indices of elements where values are larger than 50% of peak amplitude
amps50idx = np.where(resvec > 0.5*aa)[0]
# get distances for those indices from true source position
locs50 = locations[amps50idx,:] - locations[ii,:]
# get maximum distance
width[ii] = np.sqrt(np.sum(locs50**2, 1).max())
return width
def localisation_error(resmat, locations, axis, metric='peak'):
""" Compute localisation error metrics for resolution matrix.
Parameters
----------
resmat: 2D numpy array
The resolution matrix (nloc-by-nloc).
locations: 2D (nloc-by-3) numpy array
Locations (in m) to be used for resolution metrics (distances etc.).
axis: integer (0 or 1)
Whether to compute metrics for columns (=0, PSFs) or rows (=1, CTFs).
metric: string ('peak')
What type of localisation error to compute.
'peak': peak localisation error, Euclidean distance.
Returns
-------
locerr: 1D numpy array.
Localisation error per location (m).
"""
# only use absolute values
resmat = np.absolute(resmat)
# The below will operate on columns
if axis == 1:
resmat = resmat.T
# Euclidean distance between true location and maximum
if metric.lower() == 'peak':
# find indices of maxima along columns
resmax = resmat.argmax(axis=0)
# locations of maxima
maxloc = locations[resmax,:]
# difference between locations of maxima and true locations
diffloc = locations - maxloc
# Euclidean distance
locerr = np.sqrt(np.sum(diffloc**2,1))
# centre of gravity
elif metric.lower() == 'cog':
# initialise result array
locerr = np.empty(locations.shape[0])
for (ii, rr) in enumerate(locations): # for every vertex
# differences to true location
difflocs = locations - rr
# corresponding column of resmat
resvec = resmat[:,ii].T
# centre of gravity
cog = resvec.dot(locations) / np.sum(resvec)
# centre of gravity
locerr[ii] = np.sqrt( np.sum( (rr - cog)**2 ) )
return locerr
def sensitivity_map(fwd, noise_cov, diag=True, metric='norm', maxnorm=False):
""" Compute sensitivity maps for EEG/MEG (norms of leadfield columns).
Parameters
----------
fwd: forward solution
Used to get leadfield matrix.
noise_cov: noise covariance matrix
Used to whiten leadfield. Should already be regularised.
Diagonal will be used.
diag: Boolean
Whether to use only the diagonal (True) or whole
matrix for whitening. Default: True (diagonal).
metric: string
Whether 'SNR' (Goldenholz) or 'RMS' of columns to be computed.
Default 'SNR'.
maxnorm: Boolean
Whether to normalise sensitivity map to maximum or not.
Returns
-------
stc_metric: Source estimate.
Distribution of leadfield column metric.
"""
info = fwd['info']
# covariance matrix will be modified
cov = deepcopy(noise_cov)
# use diagonal noise covariance matrix
cov = cov.as_diag()
whitener = mne.cov.compute_whitener(cov, info)[0]
print('###\nGetting leadfield, computing column norms.\n###')
vertno = [fwd['src'][0]['vertno'], fwd['src'][1]['vertno']]
leadfield = psf_ctf._pick_leadfield(fwd['sol']['data'], fwd, fwd['info']['ch_names'])
print('Leadfield has dimensions (%d, %d).' % leadfield.shape)
print('Whitening leadfield.')
lfd_white = whitener.dot(leadfield)
if metric.lower() == 'snr':
# SNR in decibel units, as in Goldenholz et al., HBM 2009 (eq. 1)
ldf_metric = 10.*np.log10(np.average(lfd_white**2, axis=0))
elif metric.lower() == 'rms':
# compute norm per column
ldf_metric = np.sqrt(np.average(lfd_white**2, axis=0))
# if specified, normalise metrics to absolute maximum across vertices
if maxnorm:
ldf_metric = ldf_metric / np.abs(ldf_metric).max()
# fake multiple time steps
ldf_metric_rep = np.repeat(ldf_metric[:,np.newaxis],5, axis=1)
# convert norms to source estimate
stc_metric = mne.SourceEstimate(ldf_metric_rep, vertno, tmin=0., tstep=1.)
return stc_metric
def leadfield_svd(fwd, noise_cov, diag=True, norm=True):
""" Compute singular values of whitened leadfield matrix.
Parameters
----------
fwd: forward solution
Used to get leadfield matrix.
noise_cov: noise covariance matrix
Used to whiten leadfield. Should already be regularised.
Diagonal will be used.
diag: Boolean
Whether to use only the diagonal (True) or whole
matrix for whitening. Default: True (diagonal).
norm: Boolean
Whether to normalise to maximum singular value or not.
Default: True.
Returns
-------
sing_vals: numpy array.
Singular values of leadfield.
"""
info = fwd['info']
# covariance matrix will be modified
cov = deepcopy(noise_cov)
# use diagonal noise covariance matrix
cov = cov.as_diag()
whitener = mne.cov.compute_whitener(cov, info)[0]
print('###\nGetting and whitening leadfield.\n###')
vertno = [fwd['src'][0]['vertno'], fwd['src'][1]['vertno']]
leadfield = psf_ctf._pick_leadfield(fwd['sol']['data'], fwd, fwd['info']['ch_names'])
print('Leadfield has dimensions (%d, %d).' % leadfield.shape)
print('Whitening leadfield.')
lfd_white = whitener.dot(leadfield)
print('Compute SVD of whitened leadfield.')
sing_vals = sci_svd(lfd_white, compute_uv=False)
if norm: # normalise to maximum
sing_vals = sing_vals / sing_vals[0]
return sing_vals
def normalise_stc(stc):
""" Normalise data in STC object to absolute maximum.
Parameters
----------
stc: SourceEstimate
The data to normalise.
Returns
-------
stc_norm: Source estimate.
STC with data normalised to absolute maximum.
"""
data = stc.data
data = data / np.absolute(data).max()
# convert normalised data to source estimate
stc_norm = mne.SourceEstimate(data, stc.vertices, tmin=stc.tmin, tstep=stc.tstep)
return stc_norm
def find_nearest_vertex(src, vertex, hemi=0):
""" Find nearest used vertex (e.g. fwd, invop) from Freesurfer surface.
Parameters
----------
src: Source space
The source space with vertex information.
vertex: int
The vertex to look for (e.g. from mne_analyze).
hemi: 0 | 1
Hemisphere from which vertex to choose.
0: LH, 1: RH.
Returns
-------
nearest_vert: int.
Nearest used vertex.
"""
# coordinate of target vertex
loc_vertex = src[hemi]['rr'][vertex]
# vertices used in source estimate
verts_used = src[hemi]['vertno']
# coordinates of used vertices
locs_used = src[hemi]['rr'][verts_used]
# difference to target vertex
locs_diff = locs_used - loc_vertex
# squared distance
locs_dist = np.sum(locs_diff**2, axis=1)
min_idx = locs_dist.argmin()
nearest_vert = verts_used[min_idx]
return nearest_vert
def _get_matrix_from_LCMV_beamformer(forward, info, noise_cov, data_cov):
"""Get inverse matrix for LCMV beamformer.
Parameters
----------
forward : dict
The forward operator.
info: instance of Info
Should contain measurement information, e.g. sfreq.
noise_cov: noise covariance matrix
Used to compute whitener. Should be regularised.
data_cov: data covariance matrix
Used to compute LCMV beamformer. Should be regularised.
Returns
-------
invmat : ndarray
Inverse matrix associated with LCMV beamformer.
"""
# based on _get_matrix_from_inverse_operator() from psf_ctf module.
# number of channels for identity matrix
n_chs = len(info['ch_names'])
# create identity matrix as input for inverse operator
# set elements to zero for non-selected channels
id_mat = np.eye(n_chs)
# convert identity matrix to evoked data type (pretending it's an epoch)
evo_ident = mne.EvokedArray(id_mat, info=info, tmin=0.)
### apply beamformer to identity matrix
stc_lcmv = mne.beamformer.lcmv(evo_ident, forward, noise_cov, data_cov,
max_ori_out='signed')
# turn source estimate into numpsy array
invmat = stc_lcmv.data
return invmat