-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoh_ssd.py
1240 lines (1077 loc) · 44.2 KB
/
coh_ssd.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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Class for performing SSD."""
from copy import deepcopy
from typing import Any
from matplotlib import pyplot as plt
from mne import time_frequency
from mne.decoding import SSD
import numpy as np
from coh_exceptions import ProcessingOrderError
from coh_handle_entries import (
combine_dicts,
combine_vals_list,
get_eligible_idcs_lists,
get_group_names_idcs,
ordered_list_from_dict,
rearrange_axes,
unique,
)
from coh_processing_methods import ProcMethod
from coh_saving import save_dict
from coh_signal import Signal
class PowerSSD(ProcMethod):
"""Perform SSD analysis"""
channels = None
transformed_ch_names = None
_original_ch_names = None
_channels_str = None
_n_groups = None
_group_ch_types = None
_group_ch_names = None
filt_params_signal = None
_filt_params_signal = None
filt_params_noise = None
_filt_params_noise = None
_filt_params_names = None
_n_bands = None
group_ranks = None
regularisation = None
covariance_params = None
_plot = None
_ssd_results = None
_windows_averaged = False
_power_computed = False
def __init__(self, signal: Signal, verbose: bool = True) -> None:
super().__init__(signal=signal, verbose=verbose)
self._sort_inputs()
def _sort_inputs(self) -> None:
super()._sort_inputs()
if not self.signal._epoched:
raise ProcessingOrderError(
"SSD can only be computed on epoched data."
)
def fit_transform(
self,
channels: list[list[str]] | dict,
filt_params_signal: dict[dict],
filt_params_noise: dict[dict],
regularisation: str | float | None = None,
covariance_params: dict | None = None,
group_ranks: list[int] | None = None,
transformed_ch_names: list[str] | None = None,
plot: bool = False,
power_kwargs: dict = None,
) -> None:
"""Fits SSD models to the data and transforms it.
PARAMETERS
----------
channels : list of list of str | dict
- The channels to perform SSD on together (Note: MNE's SSD
implementation only supports computing SSD on a single type of
channels at a time). If a list of list of str, the str should be
channel names grouped into lists of channels that will be processed
together. If a dict, ...
filt_params_signal : dict of dict
- Dictionary where the keys are the names of the signal bands, and the
values as in `mne.decoding.ssd`. The names of the bands must match
those in `filt_params_noise`.
filt_params_noise : dict of dict
- Dictionary where the keys are the names of the noise bands, and the
values as in `mne.decoding.ssd`. The names of the bands must match
those in `filt_params_signal`.
regularisation : str | float | None; default None
- As in `mne.decoding.SSD`.
covariance_params : dict | None; default None
- As in `mne.decoding.SSD`.
group_ranks : list of int | None; default None
- Rank subspace that the data of each group should be projected to
before performing SSD.
transformed_ch_names : list of str | None; default None
- Names to store the data for the transformed channels under. One name
should be given for each group in `channels`. If None and `channels`
is a dict, information from the channel grouping will be used to
generate channel names, otherwise a generic "group-X" name will be
used.
plot : bool; default False
- Whether or not to plot information about the SSD.
power_kwargs : dict
- Keyword arguments for the multitaper computation of MNE's
psd_array_multitaper function.
RAISES
------
ProcessingOrderError
- Raised if the data in the object has already been processed.
"""
if self._processed:
raise ProcessingOrderError(
"The data in the object has already been processed."
)
self.channels = deepcopy(channels)
self.filt_params_signal = deepcopy(filt_params_signal)
self.filt_params_noise = deepcopy(filt_params_noise)
self.regularisation = deepcopy(regularisation)
self.covariance_params = deepcopy(covariance_params)
self.group_ranks = deepcopy(group_ranks)
self.transformed_ch_names = deepcopy(transformed_ch_names)
self._plot = deepcopy(plot)
self._sort_processing_inputs()
self._compute_ssd()
if self._plot:
self._plot_ssd_info()
self._compute_power(**power_kwargs)
self._sort_dimensions()
def _sort_processing_inputs(self) -> None:
"""Sorts processing inputs for performing SSD."""
self._sort_filt_params()
self._sort_channels()
self._sort_channel_names()
self._sort_ranks()
def _sort_channels(self) -> None:
"""Find the channels to perform SSD on together (if channels is a dict),
and assigns these channel groups."""
if isinstance(self.channels, dict):
features = self._features_to_df()
eligible_idcs = get_eligible_idcs_lists(
features, self.channels["eligible_entries"]
)
group_idcs = get_group_names_idcs(
features,
self.channels["grouping"],
eligible_idcs=eligible_idcs,
replacement_idcs=eligible_idcs,
keys_in_names=False,
)
self._group_ch_names = [
key.replace(" & ", "_") for key in group_idcs.keys()
]
channels_list = []
channels_str = []
for idcs in group_idcs.values():
channels_list.append(
[self.signal.data[0].ch_names[idx] for idx in idcs]
)
channels_str.append(
combine_vals_list(
[self.signal.data[0].ch_names[idx] for idx in idcs]
)
)
self.channels = channels_list
self._channels_str = channels_str
else:
if not isinstance(self.channels, list) or not isinstance(
self.channels[0], list
):
raise TypeError(
"'channels' should be a list of list of str, or a dict."
)
self._channels_str = [
combine_vals_list(names) for names in self.channels
]
self._n_groups = len(self.channels)
group_ch_types = []
for group in self.channels:
ch_types = self.signal.data[0].get_channel_types(picks=group)
if len(np.unique(ch_types)) != 1:
raise ValueError(
"Channels being processed together with SSD must be of the "
"same type."
)
group_ch_types.append(ch_types[0])
self._group_ch_types = group_ch_types
def _sort_channel_names(self) -> None:
"""Sorts the names for the transformed channels."""
if self.transformed_ch_names is None:
if self._group_ch_names is not None:
self.transformed_ch_names = deepcopy(self._group_ch_names)
else:
self.transformed_ch_names = [
f"ch_group-{group_i}"
for group_i in range(len(self.channels))
]
else:
if len(self.transformed_ch_names) != len(self.channels):
raise ValueError(
"`transformed_ch_names` must equal the number of channel "
"groups."
)
def _sort_filt_params(self) -> None:
"""Ensures filter parameters are in an appropriate format."""
if not isinstance(self.filt_params_signal, dict) or not isinstance(
self.filt_params_noise, dict
):
raise TypeError(
"Filter parameters must be given as lists of dicts."
)
if self.filt_params_signal.keys() != self.filt_params_noise.keys():
raise ValueError(
"Filter parameters for signal and noise must contain "
"information for the same bands."
)
self._filt_params_signal = list(self.filt_params_signal.values())
self._filt_params_noise = list(self.filt_params_noise.values())
self._filt_params_names = list(self.filt_params_signal.keys())
self._n_bands = len(self._filt_params_signal)
def _sort_ranks(self) -> None:
"""Check that the correct number of rank groups is given."""
if self.group_ranks is None:
self.group_ranks = [None for _ in range(len(self.channels))]
if not isinstance(self.group_ranks, list):
raise TypeError("`group_ranks` must be a list.")
if len(self.group_ranks) != len(self.channels):
raise ValueError(
"`group_ranks` must be given for each channel group."
)
def _compute_ssd(self) -> None:
"""Fits data to SSD for each band and stores information about the
transformed data, the spatial filters/patterns, and spectral ratios."""
ssd_info = []
for group_i in range(self._n_groups):
ssd_info.append([])
group_data = [
deepcopy(data).pick(self.channels[group_i])
for data in self.signal.data
]
if self.group_ranks[group_i] is not None:
rank = {
self._group_ch_types[group_i]: self.group_ranks[group_i]
}
else:
rank = None
for band_i in range(self._n_bands):
ssd_info[group_i].append({})
group_ssd = [
SSD(
info=group_data[0].info,
filt_params_signal=self._filt_params_signal[band_i],
filt_params_noise=self._filt_params_noise[band_i],
reg=self.regularisation,
n_components=None,
picks=group_data[0].ch_names,
sort_by_spectral_ratio=True,
return_filtered=True,
n_fft=group_data[0].get_data().shape[-1],
cov_method_params=self.covariance_params,
rank=rank,
).fit(data.get_data())
for data in group_data
]
if (
len(unique([ssd.patterns_.shape[0] for ssd in group_ssd]))
!= 1
):
raise ValueError(
"The rank of the data is not identical across windows, "
"meaning there is a different number of SSD components "
"across windows, but this is not supported."
)
ssd_info[group_i][band_i]["data"] = np.array(
[
ssd.transform(data.get_data())
for ssd, data in zip(group_ssd, group_data)
]
)
ssd_info[group_i][band_i]["filters"] = np.array(
[ssd.filters_ for ssd in group_ssd]
)
ssd_info[group_i][band_i]["patterns"] = np.array(
[ssd.patterns_.T for ssd in group_ssd]
)
ssd_info[group_i][band_i]["eigenvalues"] = np.array(
[ssd.eigvals_ for ssd in group_ssd]
)
ssd_info[group_i][band_i]["spectral_ratios"] = np.array(
[
ssd.get_spectral_ratio(data)[0]
for ssd, data in zip(
group_ssd, ssd_info[group_i][band_i]["data"]
)
]
)
ssd_info[group_i][band_i]["n_comps"] = group_ssd[
0
].patterns_.shape[0]
self._results = ssd_info
self._sort_transformed_ch_names()
self._generate_extra_info()
def _sort_transformed_ch_names(self) -> None:
"""Gets the names of the transformed channels for each band and
component."""
for group_i, group_name in enumerate(self.transformed_ch_names):
for band_i, band_name in enumerate(self._filt_params_names):
n_comps = self._results[group_i][band_i]["n_comps"]
self._results[group_i][band_i]["transformed_ch_names"] = []
for comp_i in range(n_comps):
self._results[group_i][band_i][
"transformed_ch_names"
].append(f"{group_name}_{band_name}_comp-{comp_i+1}")
self._transformed_ch_names = self._get_band_feature_info(
copy_from="transformed_ch_names",
repeat_for_channels=False,
copy_mode="repeat",
)
def _generate_extra_info(self) -> None:
"""Generates additional information related to the SSD analysis."""
self._generate_transformed_ch_types()
self._generate_transformed_ch_coords()
attributes = [
"ch_reref_types",
"ch_regions",
"ch_subregions",
"ch_hemispheres",
"ch_epoch_orders",
]
for attr in attributes:
if (
attr in self.signal.extra_info.keys()
and self.signal.extra_info[attr] is not None
):
self._generate_transformed_attribute(attr)
def _generate_transformed_ch_types(self) -> None:
"""Gets the types of channels in the results.
If the types of each channel in a group are identical, this type is
given as a string, otherwise the unique types are taken and joined into
a single string by the " & " characters.
"""
ch_types = {}
for group_i, transformed_name in enumerate(self.transformed_ch_names):
ch_types[transformed_name] = combine_vals_list(
unique(
self.signal.data[0].get_channel_types(
picks=self.channels[group_i]
)
)
)
self.extra_info["transformed_ch_types"] = {}
for name in self._transformed_ch_names:
for group_name in self.transformed_ch_names:
if name.startswith(group_name):
self.extra_info["transformed_ch_types"][name] = ch_types[
group_name
]
break
def _generate_transformed_ch_coords(self) -> None:
"""Gets the coordinates of channels in the results for each channel."""
ch_coords = {}
for group_i, transformed_name in enumerate(self.transformed_ch_names):
ch_coords[transformed_name] = self.signal.get_coordinates(
picks=self.channels[group_i]
)
self.extra_info["transformed_ch_coords"] = {}
for name in self._transformed_ch_names:
for group_name in self.transformed_ch_names:
if name.startswith(group_name):
self.extra_info["transformed_ch_coords"][name] = ch_coords[
group_name
]
break
def _generate_transformed_attribute(self, attribute: str) -> None:
"""Gets the information of transformed channels in the results.
If the information of an attribute for each channel in a group are
identical, this information is given as a string, otherwise the unique
information is taken and joined into a single string by the " & "
characters.
PARAMETERS
----------
attribute : str
- The name of the attribute in `extra_info` to transform.
"""
info = {}
for group_i, transformed_name in enumerate(self.transformed_ch_names):
info[transformed_name] = combine_vals_list(
unique(
ordered_list_from_dict(
list_order=self.channels[group_i],
dict_to_order=self.extra_info[attribute],
)
)
)
transformed_info = {}
for name in self._transformed_ch_names:
for group_name in self.transformed_ch_names:
if name.startswith(group_name):
transformed_info[name] = info[group_name]
break
self.extra_info[f"transformed_{attribute}"] = transformed_info
def _plot_ssd_info(self):
"""Plots the PSD of the SSD-filtered data and the spectral ratios of
each SSD component for each channel group, frequency band, and
window."""
channel_names = deepcopy(self._channels_str)
for group_i in range(self._n_groups):
if len(channel_names[group_i]) > 100:
channel_names[group_i] = f"{channel_names[group_i][:100]}..."
for band_i in range(self._n_bands):
ssd_info = self._results[group_i][band_i]
signal_freqs = [
self._filt_params_signal[band_i]["l_freq"],
self._filt_params_signal[band_i]["h_freq"],
]
noise_freqs = [
self._filt_params_noise[band_i]["l_freq"],
self._filt_params_noise[band_i]["h_freq"],
]
for window_i in range(self._n_windows):
psd, freqs = time_frequency.psd_array_welch(
ssd_info["data"][window_i],
sfreq=self.signal.data[0].info["sfreq"],
n_fft=int(self.signal.data[0].info["sfreq"]),
)
plot_idcs = self._get_freq_idcs_for_plot(freqs)
fig, axis = plt.subplots(1, 1)
self._plot_ssd_psd(
axis, psd, freqs, plot_idcs, signal_freqs, noise_freqs
)
self._plot_ssd_inset(axis, ssd_info, window_i)
title = (
f"Channels: {channel_names[group_i]}\n\nSignal "
f"frequencies: {signal_freqs[0]} - {signal_freqs[1]} "
f"Hz Noise frequencies: {noise_freqs[0]} - "
f"{noise_freqs[1]} Hz"
)
if self.signal._windowed:
title += f" Window {window_i} of {self._n_windows}"
fig.suptitle(title)
fig.legend(loc="upper left")
plt.show()
def _get_freq_idcs_for_plot(self, freqs: np.ndarray) -> list[int]:
"""Gets the indices of the frequencies to plot for visualising the SSD
models based on the bandpass filtering of the data.
PARAMETERS
----------
freqs : nunmpy ndarray
- The available frequencies to plot.
RETURNS
-------
plot_freq_idcs : list[int]
- Indices of the lower and upper frequencies, respectively, to plot.
"""
plot_freq_range = [freqs[0], freqs[-1]]
if self.signal.data[0].info["highpass"]:
plot_freq_range[0] = self.signal.data[0].info["highpass"]
if self.signal.data[0].info["lowpass"]:
plot_freq_range[-1] = self.signal.data[0].info["lowpass"]
return [
np.where(freqs == plot_freq_range[0])[0][0],
np.where(freqs == plot_freq_range[1])[0][0],
]
def _plot_ssd_psd(
self,
axis: plt.Axes,
psd: np.ndarray,
freqs: np.ndarray,
plot_idcs: list[int],
signal_freqs: list[float],
noise_freqs: list[float],
) -> None:
"""Plot the PSD of the SSD-transformed data and highlight the signal
and noise segments.
PARAMETERS
----------
axis : matplotlib pyplot Axes
- The subplot axis on which to plot the PSD.
psd : numpy ndarray
- The PSD of the SSD-transformed data.
freqs : numpy ndarray
- The frequencies corresponding to `psd`.
signal_freqs: list[int]
- The lower and upper bound, respectively, of the signal frequencies.
noise_freqs: list[int]
- The lower and upper bound, respectively, of the noise frequencies.
"""
for comp_i in range(psd.shape[1]):
axis.plot(
freqs[plot_idcs[0] : plot_idcs[1]],
psd[:, comp_i, plot_idcs[0] : plot_idcs[1]].mean(axis=0),
label=f"SSD component {comp_i + 1}",
)
axis.axvspan(signal_freqs[0], signal_freqs[1], alpha=0.2, color="grey")
axis.axvspan(
noise_freqs[0],
signal_freqs[0],
fill=False,
alpha=0.3,
hatch="///",
color="grey",
)
axis.axvspan(
signal_freqs[1],
noise_freqs[1],
fill=False,
alpha=0.3,
hatch="///",
color="grey",
)
axis.set_xlabel("Frequency (Hz)")
axis.set_ylabel("Power (dB)")
axis.spines["top"].set_visible(False)
axis.spines["right"].set_visible(False)
def _plot_ssd_inset(
self, axis: plt.Axes, ssd_info: dict, window_i: int
) -> None:
"""Plot the eigenvalues and spectral ratios of the SSD components as an
inset alongside the PSD.
PARAMETERS
----------
axis : matplotlib pyplot Axes
- The subplot axis on which to plot the PSD.
ssd_info : dict
- Dictionary containing the eigenvalues and spectral ratios.
window_i : int
- The window of the data being plotted.
"""
n_comps = len(ssd_info["eigenvalues"][window_i])
components = np.arange(n_comps, dtype=int) + 1
inset = axis.inset_axes([0.7, 0.6, 0.3, 0.4])
inset.plot(
components,
ssd_info["eigenvalues"][window_i],
label="eigenvalues",
color="blue",
)
inset.set_xlim(inset.get_xlim())
inset.set_ylabel("Eigenvalues (A.U.)")
inset.grid(True, axis="x")
inset_y2 = inset.twinx()
inset_y2.plot(
components, ssd_info["spectral_ratios"][window_i], color="orange"
)
inset.plot(
-1,
np.mean(inset.get_ylim()),
color="orange",
label="spectral ratios",
)
inset_y2.plot(components, [1] * n_comps, color="k", linestyle="--")
inset_y2.set_ylabel("Spectral ratio (dB)")
inset.set_xlabel("Components")
inset.set_xticks(components)
inset.set_xticklabels(components)
inset.legend()
def _compute_power(
self,
fmin: int | float = 0,
fmax: float = np.inf,
bandwidth: int | float | None = None,
adaptive: bool = False,
low_bias: bool = True,
normalization: str = "length",
n_jobs: int = 1,
) -> None:
"""Compute PSD for the transformed data using multitapers.
PARAMETERS
----------
fmin : int | float; default 0
- The minimum frequency of interest.
fmax : int | float; default infinite
- The maximum frequency of interest.
bandwidth : float | None; default None
- The bandwidth of the multitaper windowing function, in Hz. If
'None', this is set to a window half-bandwidth of 4.
adaptive : bool; default False
- Whether or not to use adaptive weights to combine the tapered
spectra into the power spectral density.
low_bias : bool; default True.
- Whether or not to use only tapers with more than 90% spectral
concentration within bandwidth.
normalization : str; default "length"
- The normalisation strategy to use. If "length", the power spectra is
normalised by the length of the signal. If "full", the power spectra
is normalised by the sampling rate and the signal length.
n_jobs : int; default 1
- The number of jobs to run in parallel. If '-1', this is set to the
number of CPU cores. Requires the 'joblib' package.
"""
for group_i, group_results in enumerate(self._results):
print(
f"\n---=== Computing power for group {group_i+1} of "
f"{len(self._results)} ===---\n"
)
for band_i, band_results in enumerate(group_results):
psds = []
for window_data in band_results["data"]:
psd, freqs = time_frequency.psd_array_multitaper(
x=window_data,
sfreq=self.signal.data[0].info["sfreq"],
fmin=fmin,
fmax=fmax,
bandwidth=bandwidth,
adaptive=adaptive,
low_bias=low_bias,
normalization=normalization,
n_jobs=n_jobs,
verbose=self.verbose,
)
psds.append(psd)
self._results[group_i][band_i]["power"] = np.mean(psds, axis=1)
self._freqs = freqs
self._results_dims = ["windows", "channels", "frequencies"]
self._power_computed = True
def _sort_dimensions(self) -> None:
"""Removes the window dimension of the results if the data has not been
windowed."""
results_keys = [
"data",
"filters",
"patterns",
"eigenvalues",
"spectral_ratios",
"power",
]
if "windows" not in self.results_dims:
for group_i in range(self._n_groups):
for band_i in range(self._n_bands):
for key in results_keys:
self._results[group_i][band_i][key] = self._results[
group_i
][band_i][key][0]
def power_as_array(self, dimensions: list[str] | None = None) -> np.ndarray:
"""Extracts and returns power of transformed data as an array.
PARAMETERS
----------
dimensions : list[str] | None; default None
- The dimensions of the power results that will be returned. If
'None', the current dimensions are used.
RETURNS
-------
power : numpy ndarray
- The power of the transformed data.
"""
power = []
if dimensions is None:
dimensions = self.results_dims
for group_i in range(self._n_groups):
for band_i in range(self._n_bands):
power.extend(self._results[group_i][band_i]["power"])
power = np.array(power)
return rearrange_axes(
obj=power, old_order=self.results_dims, new_order=dimensions
)
def topographies_as_array(self) -> np.ndarray:
"""Extracts the topographies as an array.
RETURNS
-------
topographies : numpy ndarray
- The topographies for each group, band, channel, and component as a
vector.
"""
return self._rearrange_topographies()[0]
def results_as_dict(self) -> dict:
"""Returns the SSD results and additional information as a dictionary.
RETURNS
-------
results_dict : dict
- The results and additional information stored as a dictionary.
"""
core_info = self._core_info_for_results_dict()
extra_info = self._extra_info_for_results_dict()
return combine_dicts([core_info, extra_info])
def _core_info_for_results_dict(self) -> dict:
"""Returns core information about the connectivity results which is
always present.
RETURNS
-------
core_info : dict
- The core information about the connectivity results.
"""
dimensions = self._get_optimal_dims()
if "windows" in dimensions:
side_result_dims = ["windows", "channels"]
else:
side_result_dims = ["channels"]
power = self.power_as_array(dimensions=dimensions)
core_info = {
"power-ssd": power.tolist(),
"frequencies": self._freqs.tolist(),
"power-ssd_dimensions": dimensions,
"component_eigenvalues": self._get_group_feature_info(
copy_from="eigenvalues",
repeat_for_channels=False,
copy_mode="repeat",
),
"component_eigenvalues_dimensions": side_result_dims,
"component_spectral_ratios": self._get_group_feature_info(
copy_from="spectral_ratios",
repeat_for_channels=False,
copy_mode="repeat",
),
"component_spectral_ratios_dimensions": side_result_dims,
"transformed_ch_names": self._transformed_ch_names,
"band_names": self._get_band_feature_info(
copy_from=self._filt_params_names,
repeat_for_channels=False,
copy_mode="repeat",
),
"original_ch_names": self._get_group_feature_info(
copy_from=self._channels_str,
repeat_for_channels=False,
copy_mode="repeat",
),
"component_numbers": (
np.array(
self._get_band_feature_info(
copy_from=None,
repeat_for_channels=False,
copy_mode="repeat",
)
)
+ 1
).tolist(),
"sampling_frequency": self.signal.data[0].info["sfreq"],
"processing_steps": self.processing_steps,
"subject_info": self.signal.data[0].info["subject_info"],
}
core_info.update(self._dimensions_info_for_results_dict())
return core_info
def _dimensions_info_for_results_dict(self) -> dict:
"""Returns information about the dimensions of the SSD results.
RETURNS
-------
dimensions_info : dict
- Information about the dimensions of the results
"""
dimensions_info = {}
if "windows" in self.results_dims:
dimensions_info["windows"] = (
np.arange(self._n_windows) + 1
).tolist()
return dimensions_info
def _extra_info_for_results_dict(self) -> dict:
"""Returns extra information about the results which is optionally
present.
RETURNS
-------
extra_info : dict
- Additional information about the results.
"""
extra_info = {}
extra_info_keys = [
"transformed_ch_types",
"transformed_ch_coords",
"transformed_ch_regions",
"transformed_ch_subregions",
"transformed_ch_hemispheres",
"transformed_ch_reref_types",
"transformed_ch_epoch_orders",
"metadata",
]
for key in extra_info_keys:
if (
key in self.extra_info.keys()
and self.extra_info[key] is not None
):
if key == "metadata":
extra_info["metadata"] = self.signal.extra_info["metadata"]
else:
extra_info[key] = [
self.extra_info[key][ch_name]
for ch_name in self._transformed_ch_names
]
return extra_info
def topographies_as_dict(self) -> dict:
"""Returns the SSD topographies and additional information as a
dictionary.
RETURNS
-------
topographies_dict : dict
- The topographies and additional information stored as a dictionary.
"""
core_info = self._core_info_for_topographies_dict()
extra_info = self._extra_info_for_topographies_dict()
return combine_dicts([core_info, extra_info])
def _core_info_for_topographies_dict(self) -> dict:
"""Returns core information about the SSD topographies which is always
present.
RETURNS
-------
core_info : dict
- The core information about the topographies.
"""
topos, topo_dimensions = self._rearrange_topographies()
self._transformed_ch_names_topos = self._get_band_feature_info(
copy_from="transformed_ch_names",
repeat_for_channels=True,
copy_mode="tile",
)
self._original_ch_names_topos = self._get_group_feature_info(
copy_from=self.channels,
repeat_for_channels=False,
copy_mode="repeat",
)
core_info = {
"ssd_topographies": topos,
"ssd_topographies_dimensions": topo_dimensions,
"ch_names": self._original_ch_names_topos,
"transformed_ch_names": self._transformed_ch_names_topos,
"component_numbers": (
np.array(
self._get_band_feature_info(
copy_from=None,
repeat_for_channels=True,
copy_mode="tile",
)
)
+ 1
).tolist(),
"component_eigenvalues": self._get_group_feature_info(
copy_from="eigenvalues",
repeat_for_channels=True,
copy_mode="tile",
),
"component_spectral_ratios": self._get_group_feature_info(
copy_from="spectral_ratios",
repeat_for_channels=True,
copy_mode="tile",
),
"band_names": self._get_band_feature_info(
self._filt_params_names,
repeat_for_channels=True,
copy_mode="repeat",
),
"sampling_frequency": self.signal.data[0].info["sfreq"],
"processing_steps": self.processing_steps,
"subject_info": self.signal.data[0].info["subject_info"],
}
dimensions_info = self._dimensions_info_for_results_dict()
if "windows" in dimensions_info.keys():
core_info.update(windows=dimensions_info["windows"])
return core_info
def _rearrange_topographies(self) -> tuple[np.ndarray, list[str]]:
"""Rearrange topography results into an array containing the
concatenated topographies for each channel and each component within
each group.
RETURNS
-------
topgraphies : numpy ndarray
- The rearranged topography results.
topography_dimensions : list of str
- Names of the dimensions of the topography results.
"""
topos = []
for group_i in range(self._n_groups):
for band_i in range(self._n_bands):
results = self._results[group_i][band_i]
for ch_i in range(len(self.channels[group_i])):
for comp_i in range(results["n_comps"]):
if not self.signal._windowed:
topos.append(results["patterns"][ch_i, comp_i])
else:
topos.extend(
[
results["patterns"][window_i][ch_i, comp_i]
for window_i in range(self._n_windows)
]
)
topos = np.array(topos)
topo_dimensions = ["channels", "windows"]
if "windows" not in self.results_dims:
topo_dimensions = [
dim for dim in topo_dimensions if dim != "windows"
]
return topos, topo_dimensions
def _extra_info_for_topographies_dict(self) -> dict:
"""Returns extra information about the topographies which is optionally