-
Notifications
You must be signed in to change notification settings - Fork 0
/
ged.py
819 lines (684 loc) · 27.9 KB
/
ged.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import os
import glob
from datetime import datetime
from pathlib import Path
import copy
import mne
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from scipy import linalg
from . import utils
class ged(object):
"""Class ged for fitting models to neural time series data in mne.
Args:
model (str): GED-type: singletrial, average, regressor. Defaults to 'singletrial'.
win_s (tuple): Time window (in seconds) for S covariance matrix. e.g., [0.2, 0.8]. Defaults to None (all timepoints).
win_r (tuple): Time window (in seconds) for R covariance matrix. e.g., [-0.2, 0.0]. Defaults to None (all timepoints).
ch_names (list): Channels/sensors to use. Defaults to None (all channels).
regularize (float, optional): Regularization parameter (0: none, 1: full). Defaults to 0.0.
status (str): Model fit status. Defaults to "".
verbose (bool): Print logging messages. Defaults to False.
"""
def __init__(
self,
model="singletrial",
win_s=None,
win_r=None,
ch_names=None,
regularize=0.0,
status="",
verbose=False,
):
self.model = model
self.win_s = win_s
self.win_r = win_r
self.ch_names = ch_names
self.sfreq = None
self.regularize = regularize
self.verbose = verbose
self.status = status
self.params = {}
self.update_params("model", model)
self.update_params("regularize", regularize)
def update_params(self, key, value=None):
"""Updates dictionary in instance based on key-value pair. If key exists in dictionary and value is None, that key will be removed from the dictionary.
Args:
key (str): Dictionary key.
value (obj, optional): Value for dictionary key. Defaults to None.
"""
if value is None and key in self.params:
del self.params[key]
else:
self.params[key] = value
def get_params(self, key=None):
"""Return model parameters dictionary or value of a key.
Args:
key (str, optional): Dictionary key. If None returns, returns the entire dictionary. Defaults to None.
Returns:
dict if key is None; value of key if key is not None; NoneType if key not found
"""
if key is None:
return self.params
else:
return self.params.get(key)
def fit(
self, epochs_s, epochs_r=None, feature=None, feature_range=(1, 2),
):
"""Fit model to data with generalized eigendecomposition.
Args:
epochs_s (mne.Epochs): mne.Epochs instance
epochs_r (mne.Epochs, optional): mne.Epochs instance. Defaults to None.
feature (str, optional): Feature/column in epochs_s.metadata to use for single-trial regression GED. Defaults to None.
feature_range (tuple, optional): Rescale feature range. Defaults to (1, 2).
Raises:
TypeError: If GED results contain imaginary values.
"""
if epochs_r is None: # if no epochs_r provided, epochs_r is epochs_s
epochs_r = epochs_s.copy()
# update params
self.update_params("info", epochs_s.info)
self.update_params("regularize", self.regularize)
self.update_params("epoch_times", epochs_s.times)
self.update_params("epoch_pnts", epochs_s.times.shape[0])
self.sfreq = epochs_s.info["sfreq"]
if self.ch_names is None:
self.ch_names = epochs_s.ch_names # if None, use all channels
self.update_params("ch_names", self.ch_names)
self.update_params("nbchan", len(self.ch_names))
if self.win_s is None:
self.win_s = [epochs_s.times[0], epochs_s.times[-1]]
self.update_params("win_s", self.win_s)
# copy epochs for computing time series later on
epochs_s_original = epochs_s.copy().pick_channels(self.ch_names)
# select times and channesl to compute covariance matrix S
epochs_s = (
epochs_s.copy()
.crop(tmin=self.win_s[0], tmax=self.win_s[1])
.pick_channels(self.ch_names)
)
self.update_params("win_s_times", epochs_s.times)
self.update_params("win_s_pnts", epochs_s.times.shape[0])
if self.win_r is None:
self.win_r = [epochs_r.times[0], epochs_r.times[-1]]
self.update_params("win_r", self.win_r)
# select times and channels to compute covariance matrix R
epochs_r = (
epochs_r.copy()
.crop(tmin=self.win_r[0], tmax=self.win_r[1])
.pick_channels(self.ch_names)
)
self.update_params("win_r_times", epochs_r.times)
self.update_params("win_r_pnts", epochs_r.times.shape[0])
# compute covariance matrices
if self.model == "singletrial":
covS = utils.cov_singletrial(epochs_s)
covR = utils.cov_singletrial(epochs_r)
elif self.model == "average":
covS = utils.cov_avg(epochs_s)
covR = utils.cov_avg(epochs_r)
elif self.model == "regressor":
assert feature is not None and epochs_s.metadata is not None
self.update_params("feature", feature)
self.update_params("feature_value", epochs_s.metadata[feature])
self.update_params("feature_range", feature_range)
covS = utils.cov_singletrial_scale(epochs_s, feature, feature_range)
covR = utils.cov_singletrial(epochs_r)
# scale matrices to increase precision (otherwise too small)
mult = 1e15
if self.regularize:
print(f"Applying {self.regularize} regularization.")
covR_reg = utils.cov_regularize(covR * mult, shrink=self.regularize)
else:
covR_reg = covR * mult # no regularization (simply scale it)
# perform generalized eigen decomposition
evals, evecs = linalg.eig(covS * mult, covR_reg)
# regularize to fix imaginary values
while np.any(np.iscomplex(evals)) or np.any(np.iscomplex(evecs)):
self.regularize += 0.005 # increase by 0.5% each time
self.update_params("regularize", self.regularize)
print(
f"GED returned imaginary eigenvalues. Applying {self.regularize} regularization."
)
covR_reg = utils.cov_regularize(covR * mult, shrink=self.regularize)
evals, evecs = linalg.eig(covS * mult, covR_reg)
assert (not np.any(np.iscomplex(evals))) and (
not np.any(np.iscomplex(evecs))
), "GED returned imaginary eigenvectors after regularization."
evals = np.real(evals)
evecs = np.real(evecs)
self.covS_ = covS
self.covR_ = covR
self.covR_reg = covR_reg / mult
# sort eigenvalues and eigenvectors by descending eigenvalues
evals, evecs = utils.sort_evals_evecs(evals, evecs)
self.evals_ = evals
self.evecs_ = evecs # already normalized (each vector's norm/length is 1)
self.evalsperc_ = evals / np.sum(evals) * 100 # percent variance
self.info = epochs_s.info
# compute activation pattern (flip sign if necessary)
self.comp_pattern()
# compute component time series
self.comp_timeseries(epochs_s_original, covR.shape[0])
# compute S and R win ERP topography values
self.Swintopo_ = epochs_s.average().data.mean(axis=1)
self.Rwintopo_ = epochs_r.average().data.mean(axis=1)
# time-log when the analysis was completed
self.status = f"fitted {datetime.now()}"
def get_top(self, n):
"""Get the top n eigenvalues, eigenvectors, and activation patterns
Args:
n (int): Number of components to return.
Returns:
dict: Dictionary with evals, evecs, and patterns
"""
evals, evecs = utils.sort_evals_evecs(self.evalsperc_, self.evecs_, top=n)
pattern = self.comp_pattern_[:, :n]
return {"evals": evals, "evecs": evecs, "pattern": pattern}
def get_pattern(self, index=0):
"""Get activation pattern for a selected component.
Args:
index (int, optional): Index of component/pattern. Defaults to 0.
Returns:
numpy.array: 1D numpy.array.
Notes:
Uses get_top(self, n) method.
"""
return self.get_top(index + 1)["pattern"][:, -1].flatten()
def get_timeseries(self, index=0):
ts = self.comp_timeseries_[index, :]
times = self.get_params("epoch_times")
return {"timeseries": ts, "times": times}
def get_comp(self, index):
"""[summary]
Args:
index ([type]): [description]
Returns:
[type]: [description]
"""
pattern = self.get_pattern(index)
ts = self.get_timeseries(index)
return {
"pattern": pattern,
"timeseries": ts["timeseries"],
"times": ts["times"],
}
def flipsign_comp_pattern(self, pattern=None, evecs=None):
"""[summary]
Args:
pattern ([type], optional): [description]. Defaults to None.
evecs ([type], optional): [description]. Defaults to None.
Returns:
[type]: [description]
"""
if pattern is None:
pattern = self.comp_pattern_
if evecs is None:
evecs = self.evecs_
signflip = []
# flip sign of each component pattern
for col in range(pattern.shape[1]):
colval = pattern[:, col]
idx = np.abs(colval).argmax()
sign = np.sign(colval[idx])
signflip.append(sign)
pattern[:, col] *= sign
evecs[:, col] *= sign
return pattern, evecs, np.array(signflip)
def comp_pattern(self, flipsign=True):
"""[summary]
Args:
flipsign (bool, optional): [description]. Defaults to True.
Returns:
[type]: [description]
"""
self.comp_pattern_ = (self.evecs_.T @ self.covS_).T
if flipsign:
(
self.comp_pattern_,
self.evecs_,
self.signflip_,
) = self.flipsign_comp_pattern()
print("Computing component activation pattern using covS_")
return self.comp_pattern_
def comp_timeseries(self, data, n=10):
"""Compute averaged component time series for first n components using matrix multiplication (eigenvector @ data).
Args:
data (mne.Epochs, mne.Evoked): mne.Epochs or mne.Evoked instance.
n (int, optional): Number of components' timeseries to compute. Defaults to 10.
Raises:
TypeError: If data isn't an instance of mne.Epochs or mne.Evoked.
Returns:
dict: Dictionary with timeseries and times.
"""
if self.covS_.shape[0] < n:
n = self.covS_.shape[0]
print(f"Computing component time series for {n} components/dimensions")
# evoked
if isinstance(data, mne.Evoked):
dat = data.copy()
elif len(data) > 1:
dat = data.copy().average()
else:
raise TypeError("Only Epochs or Evoked objects are allowed")
assert self.evecs_.shape[0] == dat.data.shape[0]
self.comp_timeseries_ = self.evecs_[:, :n].T @ dat.data
return {"timeseries": self.comp_timeseries_, "times": data.times}
def plot_eigenspectrum(self, axes=None, n=20, cmap="viridis_r", **kwargs):
"""[summary]
Args:
axes ([type], optional): [description]. Defaults to None.
n (int, optional): [description]. Defaults to 20.
cmap (str, optional): [description]. Defaults to "viridis_r".
Returns:
[type]: [description]
"""
if axes is None:
fig, axes = plt.subplots()
evals = self.get_top(n).get("evals")
sns.scatterplot(
np.arange(evals.shape[0]), evals, ax=axes, palette=cmap, hue=evals, **kwargs
)
axes.set(xlabel=f"Eigen index (top {n})", ylabel="Variance explained (%)")
axes.set_xticks(np.arange(n, step=5))
axes.set_title(f"Regularize: {self.get_params('regularize')}", size=10)
axes.legend_.remove()
sns.despine(fig=plt.gcf(), ax=axes)
return axes
def plot_component(
self, n=0, flip=False, tmin=None, tmax=None, cmap="viridis", **kwargs
):
"""[summary]
Args:
n (int, optional): [description]. Defaults to 0.
flip (bool, optional): [description]. Defaults to False.
tmin ([type], optional): [description]. Defaults to None.
tmax ([type], optional): [description]. Defaults to None.
cmap (str, optional): [description]. Defaults to "viridis".
Returns:
[type]: [description]
"""
fig, axes = plt.subplots(2, 1)
dat2plot = self.get_pattern(n)
if flip:
dat2plot *= -1
# plot activation pattern
utils.topomap(dat2plot, self.get_params("info"), cmap=cmap, axes=axes[0])
utils.colorbar(dat2plot, axes[0], multiplier=1e10, cmap=cmap)
axes[0].set_title(f"Component {n}\n{self.get_params('win_s')}", size=10)
# plot component timeseries
if tmin is None or tmax is None:
timewins = []
timewins.extend(self.get_params("win_s"))
timewins.extend(self.get_params("win_r"))
tmin = np.min(timewins)
tmax = np.max(timewins)
dat2plot = self.get_comp(n)
idx2plot = np.where(
(dat2plot.get("times") >= tmin) & (dat2plot.get("times") <= tmax)
)
ts = dat2plot.get("timeseries")
if flip:
ts = dat2plot.get("timeseries") * -1
sns.lineplot(
dat2plot.get("times")[idx2plot],
ts[idx2plot],
ax=axes[1],
lw=1,
color=sns.color_palette(cmap)[0],
)
axes[1].ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
axes[1].set_xticks(np.arange(tmin, tmax + 0.01, step=0.2))
axes[1].set(xlabel="Time (s)", ylabel="Component amplitude")
plt.tight_layout(rect=[0, 0, 1, 0.95])
return fig, axes
def save_ged_model(model, path=None, filename=None):
"""Saves GED results in ununcompressed .npz format with numpy.savez.
Args:
model (eeg.ged.ged): eeg.ged.ged instance
path (str, optional): Directory to save to. Defaults to None (current directory).
filename (str, optional): Filename. Defaults to None ("_unnamed_model.npz").
"""
if filename is None:
filename = "_unnamed_model.npz"
if path is None:
path = "."
Path(path).mkdir(parents=True, exist_ok=True)
outfile = os.path.join(path, filename)
dct = {"model": model}
np.savez(outfile, **dct)
def load_ged_model(path=".", filename="_unnamed_model.npz"):
"""Read GED results saved by save_ged_model.
Args:
path (str, optional): Directory to load model from. Defaults to current directory.
filename (str, optional): Filename. Defaults to "_unnamed_model.npz".
Returns:
eeg.ged.ged instance: instance of eeg.ged.ged
"""
infile = os.path.join(path, filename)
x = np.load(infile, allow_pickle=True)
model = x["model"].item()
print(f"Loaded {infile}")
return model
def load_ged_models(path, subjects, string=""):
"""Load all ged models in .npz files in path/directory.
Args:
path (str): Location/path of all models (.npz files).
subjects (list): List of subject ids or file names.
string (str): string to match in filenames. Defaults to "".
Returns:
list: list containing all models
"""
models = []
for s in subjects:
fname = glob.glob(os.path.join(path, f"*{s}*{string}*.npz"))[0]
fname = fname[(len(path)) :] # weird.. might break...
if fname[0] == "/":
fname = fname[1:]
print(f"Loading {fname}")
m = load_ged_model(path, fname)
models.append(m)
return models
# def plot_ged_results(
# model,
# comps2plot=(0, 1, 2, 3, 4, 5),
# nrows=3,
# cmap="viridis",
# path=None,
# filename=None,
# figsize=(34, 13),
# fontsize=15,
# ):
# """[summary]
# Args:
# model ([type]): [description]
# comps2plot (tuple, optional): [description]. Defaults to (0, 1, 2, 3, 4, 5).
# nrows (int, optional): [description]. Defaults to 3.
# cmap (str, optional): Colormap. Defaults to "viridis".
# path ([type], optional): [description]. Defaults to None.
# filename ([type], optional): [description]. Defaults to None.
# figsize (tuple, optional): [description]. Defaults to (34, 13).
# fontsize (int, optional): [description]. Defaults to 15.
# Returns:
# Figure, Axes: Instance of matplotlib Figure and Axes
# """
# if isinstance(comps2plot, int) or len(comps2plot) < 3:
# raise ValueError(
# "comps2plot must be tuple with length greater than 3, e.g., (0, 1, 2)"
# )
# fig, ax = plt.subplots(nrows, len(comps2plot), figsize=figsize)
# # plt.get_current_fig_manager().window.showMaximized() # maximize figure
# # plot eigenvalue spectrum/screeplot (top left)
# cax = ax[0, 0]
# model.plot_eigenspectrum(cax, cmap=cmap + "_r", n=model.evecs_.shape[0])
# # plot component activation patterns (second row)
# ax_array = ax[1, :]
# for idx, cax in enumerate(ax_array):
# dat2plot = model.get_pattern(comps2plot[idx])
# utils.topomap(dat2plot, model.get_params("info"), cmap=cmap, axes=cax)
# utils.colorbar(dat2plot, cax, multiplier=1e10, cmap=cmap)
# cax.set_title(f"Component {comps2plot[idx]}", size=fontsize)
# if idx == 0:
# cax.set(ylabel=f'S win: {model.get_params("win_s")}')
# # plot component time series (third row)
# timewins = []
# timewins.extend(model.get_params("win_s"))
# timewins.extend(model.get_params("win_r"))
# tmin = np.min(timewins)
# tmax = np.max(timewins)
# ax_array = ax[2, :]
# for idx, cax in enumerate(ax_array):
# dat2plot = model.get_comp(comps2plot[idx])
# idx2plot = np.where(
# (dat2plot.get("times") >= tmin) & (dat2plot.get("times") <= tmax)
# )
# sns.lineplot(
# dat2plot.get("times")[idx2plot],
# dat2plot.get("timeseries")[idx2plot],
# ax=cax,
# lw=0.5,
# color=sns.color_palette(cmap)[0],
# )
# cax.ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
# cax.set_xticks(np.arange(tmin, tmax + 0.01, step=0.2))
# cax.set(xlabel="Time (s)", ylabel="Component amplitude")
# # plot S window topography (top right)
# cax = ax[0, -2]
# utils.topomap(model.Swintopo_, model.get_params("info"), cmap=cmap, axes=cax)
# cax.set_title(f"EEG S win: {model.get_params('win_s')}", size=fontsize)
# utils.colorbar(model.Swintopo_, cax, cmap=cmap)
# # plot R window topography
# cax = ax[0, -1]
# utils.topomap(model.Rwintopo_, model.get_params("info"), cmap=cmap, axes=cax)
# utils.colorbar(model.Rwintopo_, cax, cmap=cmap)
# cax.set_title(f"EEG R win: {model.get_params('win_r')}", size=fontsize)
# # delete unused axes in first row
# idx = -3 # start deleting from the -3 axes on the first row
# while ax[0, idx] is not ax[0, 0]:
# fig.delaxes(ax[0, idx])
# idx -= 1
# fig.suptitle(
# f"Subject: {model.get_params('info')['subject_info']['his_id']}",
# fontsize=fontsize,
# )
# fig.set_size_inches(figsize[0], figsize[1])
# fig.tight_layout(rect=[0, 0, 1, 0.95])
# if filename:
# if path is None:
# path = "."
# fig.savefig(os.path.join(path, filename))
# return fig, ax
def transform_pattern(model, data, win=None, singletrial=True, flipsign=False):
"""[summary]
Args:
model ([type]): [description]
data ([type]): [description]
win ([type], optional): [description]. Defaults to None.
singletrial (bool, optional): [description]. Defaults to True.
flipsign (bool, optional): [description]. Defaults to False.
Raises:
Exception: [description]
Returns:
[type]: [description]
"""
if model.evecs_.shape[0] != len(data.info["ch_names"]):
raise Exception("model and data must have same channels")
if singletrial:
assert len(data.get_data().shape) == 3
if win is None:
win = (data.times[0], data.times[-1])
data = data.copy().crop(*win)
if singletrial:
print("Single-trial covariance")
covS = utils.cov_singletrial(data)
else:
print("Average covariance")
covS = utils.cov_avg(data)
# transform pattern
out = copy.deepcopy(model)
out.covS_ = covS
out.comp_pattern()
out.update_params("covS_new_win", win)
return out
def transform_timeseries(model, data, win=None, singletrial=True, flipsign=False):
"""[summary]
Args:
model ([type]): [description]
data ([type]): [description]
win ([type], optional): [description]. Defaults to None.
singletrial (bool, optional): [description]. Defaults to True.
flipsign (bool, optional): [description]. Defaults to False.
Raises:
TypeError: [description]
Exception: [description]
Returns:
[type]: [description]
"""
assert model.evecs_.shape[0] == len(data.ch_names)
n_comps = len(data.ch_names)
result = data.copy()
if win is not None:
result.crop(tmin=win[0], tmax=win[1])
if singletrial and isinstance(result, mne.Evoked):
raise TypeError("If singletrial=True, provide epochs instead of evoked object.")
elif not singletrial and isinstance(result, mne.Evoked):
print(f"Using model method comp_timeseries...")
compts = model.comp_timeseries(result, n=n_comps)
assert result._data.shape == compts["timeseries"].shape
result._data = compts["timeseries"]
elif singletrial and not isinstance(result, mne.Evoked):
# ensure we have epochs, which are 3D (epochs_chan_time)
assert len(result._data.shape) == 3
print(f"Computing component time-series for {len(result)} epochs.")
for e in range(len(result)):
result._data[e] = model.evecs_.T @ result._data[e]
else:
raise Exception("Check parameters.")
return result
def select_comp(models, comps_idx, pattern_tmin=None):
"""Select components for different subjects. For each ged instance in models, select the corresponding component in comps_idx. For exampmle, if [ged1, ged2, ged3] and [0, 2, 1], select components 0, 2, 1 from ged1, ged2, and ged respectively.
Args:
models (list): list of ged instances
comps_idx (list): list of component indices
Returns:
: TODO
"""
if len(comps_idx) == 1:
comps_idx *= len(models)
else:
assert len(models) == len(comps_idx)
print(f"Selecting component from each subject: {comps_idx}")
check = {}
# get pattern and timeseries for each subject
evoked_ts = []
evoked_pattern = []
inf = models[0].info
info_ts = mne.create_info(ch_names=["C1"], sfreq=inf["sfreq"], ch_types=["eeg"])
info_pat = inf
for i, m in enumerate(models): # for each subject/model
temp_comp = m.get_comp(comps_idx[i])
temp_times = temp_comp["times"]
temp_ts = temp_comp["timeseries"].reshape(1, -1)
# timeseries in evoked array
e_ts = mne.EvokedArray(
temp_ts,
info=info_ts,
tmin=temp_times[0],
comment=f"Comp {comps_idx[i]}",
nave=1,
)
e_ts.info["subject_info"] = {}
e_ts.info["subject_info"]["his_id"] = m.info["subject_info"]["his_id"]
evoked_ts.append(e_ts)
# pattern in evoked array
temp_pat = temp_comp["pattern"].reshape(-1, 1)
pat_tmin = np.mean(m.get_params("win_s"))
if pattern_tmin is not None:
pat_tmin = pattern_tmin
e_pat = mne.EvokedArray(
temp_pat,
info=info_pat,
tmin=pat_tmin,
comment=f"Comp {comps_idx[i]} Swin: {m.get_params('win_s')}",
nave=1,
)
evoked_pattern.append(e_pat)
# check
check[m.info["subject_info"]["his_id"]] = comps_idx[i]
print(check)
return evoked_ts, evoked_pattern
def plot_subj_comp(path, subj, comp_idx):
"""Plots the component pattern and time series for a subject. Subject model is loaded from path. Generally used for inspecting components.
Args:
path (str): Location/path of all .npz models.
subj (str): Subject ID or filename.
comp_idx (int): Component index to plot
Returns:
figure, axes, model: matplotlib figure, list of axes, ged instance
"""
fpath = glob.glob(os.path.join(path, f"*{subj}*.npz"))
assert len(fpath) == 1
fname = fpath[0][(len(path) + 1) :]
print(f"Loading {fname}")
model = load_ged_model(path, fname)
f, a = model.plot_component(comp_idx)
txt = a[0].title.get_text()
a[0].title.set_text(f"{subj}\n" + txt)
f.tight_layout()
return f, a, model
def plot_all_comps(
model,
n_comp=None,
y_data=None,
figsize=None,
fontsize=10,
cmap="viridis",
tmin=None,
tmax=None,
):
if y_data is not None:
assert (
model.comp_timeseries_.shape == y_data.shape
), "y_data must have same dimensions as GED component time series"
model = copy.deepcopy(model)
model.comp_timeseries_ = y_data
# determine no. of components to plot
max_n_comp = model.comp_pattern_.shape[0]
if n_comp is None:
n_comp = max_n_comp
else:
n_comp = np.min([max_n_comp, n_comp])
# determine figure size and subplot
# +2 for eigenspectrum (2 subplots) & Swin Rwin (2 subplots)
n_row, n_col = utils.subplotgrid(n_comp + 2)
n_col *= 2
fig, ax = plt.subplots(n_row, n_col, figsize=figsize)
print(f"Plotting {n_comp} components. May take some time...")
ax = ax.ravel() # flatten axes
i = 0
# plot eigenspectrum
evals = model.get_top(n_comp).get("evals")
sns.scatterplot(np.arange(evals.shape[0]), evals, ax=ax[i], palette=cmap, hue=evals)
ax[i].legend_.remove()
i += 1
# plot Swin
utils.topomap(model.Swintopo_, model.get_params("info"), cmap=cmap, axes=ax[i])
utils.colorbar(model.Swintopo_, ax[i], cmap=cmap)
ax[i].set_title(f"EEG S win: {model.get_params('win_s')}", size=fontsize)
i += 1
# plot Rwin
utils.topomap(model.Rwintopo_, model.get_params("info"), cmap=cmap, axes=ax[i])
utils.colorbar(model.Rwintopo_, ax[i], cmap=cmap)
ax[i].set_title(f"EEG R win: {model.get_params('win_r')}", size=fontsize)
i += 1
# remove subplot
fig.delaxes(ax[i])
i += 1
# plot components
for c in range(n_comp):
# plot topography
dat2plot = model.get_pattern(c)
utils.topomap(dat2plot, model.get_params("info"), cmap=cmap, axes=ax[i])
utils.colorbar(dat2plot, ax[i], multiplier=1e10, cmap=cmap)
ax[i].set_title(f"Component {c}\n{model.get_params('win_s')}", size=fontsize)
i += 1
# plot time series
dat2plot = model.get_comp(c)
temp_x = dat2plot.get("times")
temp_y = dat2plot.get("timeseries")
if tmin is None:
tmin = temp_x[0]
if tmax is None:
tmax = temp_x[-1]
idx2plot = np.where((temp_x >= tmin) & (temp_x <= tmax))
ax[i].plot(temp_x[idx2plot], temp_y[idx2plot], color="black")
ax[i].axvline(color="gray")
ax[i].axhline(color="gray")
i += 1
# delete unused subplots
for sp in range(i, len(ax)):
fig.delaxes(ax[sp])
fig.suptitle(f"{model.info['subject_info']['his_id']}", fontsize=fontsize * 1.5)
print("Finished plotting.")
fig.tight_layout()
return fig, ax