-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoh_connectivity.py
1364 lines (1152 loc) · 50.9 KB
/
coh_connectivity.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
"""Classes for calculating connectivity between signals.
CLASSES
-------
ConnectivityCoherence : subclass of the abstract base class 'ProcMethod'
- Calculates the coherence (standard or imaginary) between signals.
ConectivityMultivariate : subclass of the abstract base class 'ProcMethod'
- Calculates the multivariate connectivity (multivariate interaction measure,
MIM, or maximised imaginary coherence, MIC) between signals.
"""
from typing import Union
from numpy.typing import NDArray
import numpy as np
from scipy.stats import t, sem
from pybispectra import TDE, compute_fft, set_precision
from coh_exceptions import ProcessingOrderError
from coh_connectivity_processing_methods import (
ProcBivariateConnectivity,
ProcMultivariateConnectivity,
)
from coh_handle_entries import combine_dicts
from coh_progress_bar import ProgressBar
from coh_saving import save_dict
import coh_signal
set_precision("single") # set pybispectra precision
class ConnectivityCoherence(ProcBivariateConnectivity):
"""Calculates the coherence (standard or imaginary) between signals.
PARAMETERS
----------
signal : coh_signal.Signal
- The preprocessed data to analyse.
verbose : bool; default True
- Whether or not to print information about the information processing.
METHODS
-------
process
- Performs coherence analysis.
save_object
- Saves the object as a .pkl file.
save_results
- Saves the results and additional information as a file.
results_as_dict
- Returns the results and additional information as a dictionary.
get_results
- Extracts and returns results.
"""
def __init__(self, signal: coh_signal.Signal, verbose: bool = True) -> None:
super().__init__(signal, verbose)
super()._sort_inputs()
self._accepted_con_methods = ["coh", "cohy", "imcoh"]
def process(
self,
con_methods: list[str],
power_method: str,
seeds: Union[str, list[str], dict],
targets: Union[str, list[str], dict],
fmin: Union[Union[float, tuple], None] = None,
fmax: Union[float, tuple] = np.inf,
fskip: int = 0,
faverage: bool = False,
tmin: Union[float, None] = None,
tmax: Union[float, None] = None,
mt_bandwidth: Union[float, None] = None,
mt_adaptive: bool = False,
mt_low_bias: bool = True,
cwt_freqs: Union[NDArray, None] = None,
cwt_n_cycles: Union[float, NDArray] = 7.0,
average_windows: bool = False,
average_timepoints: bool = False,
absolute_connectivity: bool = False,
block_size: int = 1000,
n_jobs: int = 1,
) -> None:
"""Applies the connectivity analysis using the
spectral_connectivity_epochs function of the mne-connectivity package.
PARAMETERS
----------
con_methods : list[str]
- The methods for calculating connectivity.
- Supported inputs are: 'coh' - standard coherence; 'cohy' -
coherency; and 'imcoh' - imaginary part of coherence.
power_method : str
- The mode for calculating connectivity using 'method'.
- Supported inputs are: 'multitaper'; 'fourier'; and 'cwt_morlet'.
seeds : str | list[str]
- The channels to use as seeds for the connectivity analysis.
Connectivity is calculated from each seed to each target.
- If a string, can either be a single channel name, or a single
channel type. In the latter case, the channel type should be
preceded by 'type_', e.g. 'type_ecog'.
- If a list of strings, each entry of the list should be a channel
name.
targets : str | list[str]
- The channels to use as targets for the connectivity analysis.
Connectivity is calculated from each seed to each target.
- If a string, can either be a single channel name, or a single
channel type. In the latter case, the channel type should be
preceded by 'type_', e.g. 'type_ecog'.
- If a list of strings, each entry of the list should be a channel
name.
fmin : float | tuple | None
- The lower frequency of interest.
- If a float, this frequency is used.
- If a tuple, multiple bands are defined, with each entry being the
lower frequency for that band. E.g. (8., 20.) would give two bands
using 8 Hz and 20 Hz, respectively, as their lower frequencies.
- If None, no lower frequency is used.
fmax : float | tuple; default infinite
- The higher frequency of interest.
- If a float, this frequency is used.
- If a tuple, multiple bands are defined, with each entry being the
higher frequency for that band. E.g. (8., 20.) would give two bands
using 8 Hz and 20 Hz, respectively, as their higher frequencies.
- If an infinite float, no higher frequency is used.
fskip : int; default 0
- Omit every 'fskip'+1th frequency bin to decimate the frequency
domain.
- If 0, no bins are skipped.
faverage : bool; default False
- Whether or not to average the connectivity values for each frequency
band.
tmin : float | None; default None
- Time to start the connectivity estimation.
- If None, the data is used from the beginning.
tmax : float | None; default None
- Time to end the connectivity estimation.
- If None, the data is used until the end.
mt_bandwidth : float | None
- The bandwidth, in Hz, of the multitaper windowing function.
- Only used if 'mode' is 'multitaper'.
mt_adaptive : bool; default False
- Whether or not to use adaptive weights to combine the tapered
spectra into power spectra.
- Only used if 'mode' is 'multitaper'.
mt_low_bias : bool: default True
- Whether or not to only use tapers with > 90% spectral concentration
within bandwidth.
- Only used if 'mode' is 'multitaper'.
cwt_freqs: array of float | None; default None
- The frequencies of interest to calculate connectivity for.
- Only used if 'mode' is 'cwt_morlet'. In this case, 'cwt_freqs'
cannot be None.
cwt_n_cycles: float | array of float; default 7.0
- The number of cycles to use when calculating connectivity.
- If an single integer or float, this number of cycles is for each
frequency.
- If an array, the entries correspond to the number of cycles to use
for each frequency being analysed.
- Only used if 'mode' is 'cwt_morlet'.
average_windows : bool; default False
- Whether or not to average connectivity results across windows.
average_timepoints : bool; default False
- Whether or not to average connectivity results across timepoints.
absolute_connectivity : bool; default False
- Whether or not to take the absolute connectivity values.
block_size : int; default 1000
- The number of connections to compute at once.
n_jobs : int; default 1
- The number of epochs to calculate connectivity for in parallel.
"""
if self._processed:
ProcessingOrderError(
"The data in this object has already been processed. "
"Initialise a new instance of the object if you want to "
"perform other analyses on the data."
)
self.con_methods = con_methods
self.power_method = power_method
self.seeds = seeds
self.targets = targets
self.fmin = fmin
self.fmax = fmax
self.fskip = fskip
self.faverage = faverage
self.tmin = tmin
self.tmax = tmax
self.mt_bandwidth = mt_bandwidth
self.mt_adaptive = mt_adaptive
self.mt_low_bias = mt_low_bias
self.cwt_freqs = cwt_freqs
self.cwt_n_cycles = cwt_n_cycles
self.average_windows = average_windows
self.average_timepoints = average_timepoints
self.absolute_connectivity = absolute_connectivity
self.block_size = block_size
self.n_jobs = n_jobs
self._sort_processing_inputs()
self._get_results()
def _sort_processing_inputs(self) -> None:
"""Checks that the processing inputs are appropriate and implements them
appropriately."""
if not set(self.con_methods).issubset(self._accepted_con_methods):
raise NotImplementedError(
f"Not all the connectivity methods {self.con_methods} are "
f"supported: {self._accepted_con_methods}."
)
self._sort_seeds_targets()
super()._sort_processing_inputs()
self._sort_used_settings()
class ConnectivityTDE(ProcBivariateConnectivity):
"""Calculate estimates of time delay between signals.
PARAMETERS
----------
signal : coh_signal.Signal
- The preprocessed data to analyse.
verbose : bool; default True
- Whether or not to print information about the information processing.
METHODS
-------
process
- Performs time delay estimation analysis.
save_object
- Saves the object as a .pkl file.
save_results
- Saves the results and additional information as a file.
results_as_dict
- Returns the results and additional information as a dictionary.
get_results
- Extracts and returns results.
"""
def __init__(self, signal: coh_signal.Signal, verbose: bool = True) -> None:
super().__init__(signal, verbose)
super()._sort_inputs()
def process(
self,
seeds: Union[str, list[str], dict],
targets: Union[str, list[str], dict],
freq_bands: dict = {"all": (0.0, np.inf)},
method: int | tuple[int] = 1,
antisym: bool | tuple[bool] = False,
window_func: str = "hamming",
conf_interval: float = 0.99,
average_windows: bool = False,
n_jobs: int = 1,
) -> None:
"""Applies the connectivity analysis using the
spectral_connectivity_epochs function of the mne-connectivity package.
PARAMETERS
----------
seeds : str | list[str]
- The channels to use as seeds for the connectivity analysis.
Connectivity is calculated from each seed to each target.
- If a string, can either be a single channel name, or a single
channel type. In the latter case, the channel type should be
preceded by 'type_', e.g. 'type_ecog'.
- If a list of strings, each entry of the list should be a channel
name.
targets : str | list[str]
- The channels to use as targets for the connectivity analysis.
Connectivity is calculated from each seed to each target.
- If a string, can either be a single channel name, or a single
channel type. In the latter case, the channel type should be
preceded by 'type_', e.g. 'type_ecog'.
- If a list of strings, each entry of the list should be a channel
name.
freq_bands : dict (default {"all": (0.0, np.inf)})
The frequency bands to use for the analysis, where the keys are the
names of the bands and the values are tuples of the lower and upper
frequencies of the bands, respectively. Takes all frequencies by
default.
method : int | tuple of int (default 1)
Method(s) to use to compute TDE, as in Nikias & Pan (1988).
antisym : bool | tuple of bool (default False)
Whether or not to antisymmetrise the bispectrum when computing TDE.
window_func : str (default "hamming")
Window function to apply when computing the FFT of the data. Can be
"hanning" or "hamming".
conf_interval : float (default 0.99)
Confidence interval to use to determine whether the time delay
estimate can be trusted, between 0 and 1. Only used if the data
consists of multiple windows.
average_windows : bool (default False)
Whether or not to average TDE results across windows.
n_jobs : int; default 1
- The number of epochs to calculate connectivity for in parallel.
"""
if self._processed:
ProcessingOrderError(
"The data in this object has already been processed. "
"Initialise a new instance of the object if you want to "
"perform other analyses on the data."
)
self.freq_bands = freq_bands
self.antisym = antisym
self.method = method
self.seeds = seeds
self.targets = targets
self.window_func = window_func
self.conf_interval = conf_interval
self.average_windows = average_windows
self.n_jobs = n_jobs
self._sort_processing_inputs()
self._get_results()
def _sort_processing_inputs(self) -> None:
"""Checks that the processing inputs are appropriate and implements
them appropriately."""
self._sort_seeds_targets()
self._sort_fbands()
super()._sort_processing_inputs()
method_name_mapping = {
val: name
for val, name in zip([1, 2, 3, 4], ["i", "ii", "iii", "iv"])
}
sym_name_mapping = {False: "standard", True: "antisym"}
self.tde_methods = []
for method in self.method:
for symmetrise in self.antisym:
self.tde_methods.append(
f"{method_name_mapping[method]}_"
f"{sym_name_mapping[symmetrise]}"
)
self._sort_used_settings()
def _sort_fbands(self) -> None:
"""Sort `fbands` input."""
if not isinstance(self.freq_bands, dict):
raise TypeError("`freq_bands` must be a dictionary.")
fmin = []
fmax = []
for fband in self.freq_bands.values():
if len(fband) != 2:
raise ValueError(
"The values of `freq_bands` must have length 2."
)
fmin.append(fband[0])
fmax.append(fband[1])
self._fmin = tuple(fmin)
self._fmax = tuple(fmax)
def _sort_used_settings(self) -> None:
"""Collects the settings that are relevant for the processing being
performed and adds only these settings to the 'processing_steps'
dictionary."""
used_settings = {
"method": self.method,
"symmetrise": self.antisym,
"window_func": self.window_func,
}
self.processing_steps["time_delay_estimation"] = used_settings
def _get_results(self) -> None:
"""Performs the time delay estimation analysis."""
if self.verbose:
self._progress_bar = ProgressBar(
n_steps=len(self.signal.data),
title="Computing time delay estimation",
)
self.results = [[] for _ in range(len(self.tde_methods))]
self.taus = [[] for _ in range(len(self.tde_methods))]
for window_idx, window_data in enumerate(self.signal.data):
if self.verbose:
print(
f"Computing time delay for window {window_idx+1} of "
f"{len(self.signal.data)}.\n"
)
data = window_data.get_data()
fft_coeffs, freqs = compute_fft(
data=data,
sampling_freq=self.signal.data[0].info["sfreq"],
n_points=2 * data.shape[2] + 1,
window=self.window_func,
n_jobs=self.n_jobs,
)
window_tde = TDE(
data=fft_coeffs,
freqs=freqs,
sampling_freq=self.signal.data[0].info["sfreq"],
verbose=self.verbose,
)
window_tde.compute(
indices=(
tuple(self.indices[0].tolist()),
tuple(self.indices[1].tolist()),
),
fmin=self._fmin,
fmax=self._fmax,
antisym=self.antisym,
method=self.method,
n_jobs=self.n_jobs,
)
window_tde_results = window_tde.results
if not isinstance(window_tde_results, tuple):
window_tde_results = (window_tde_results,)
for method_i, tde_result in enumerate(window_tde_results):
self.results[method_i].append(tde_result.get_results())
self.taus[method_i].append(tde_result.tau)
if self._progress_bar is not None:
self._progress_bar.update_progress()
if self._progress_bar is not None:
self._progress_bar.close()
for method_i in range(len(self.results)):
self.results[method_i] = np.array(self.results[method_i])
self.taus[method_i] = np.array(self.taus[method_i])
self._timepoints = tde_result.times
if len(self.signal.data) > 1:
self._confidence = []
for taus_result in self.taus:
conf_intervals = t.interval(
alpha=self.conf_interval,
df=len(self.signal.data) - 1,
loc=np.array(taus_result).mean(axis=0),
scale=sem(taus_result, axis=0),
)
self._confidence.append(
~((conf_intervals[0] <= 0) & (conf_intervals[1] >= 0))
)
self._sort_dimensions()
self._generate_extra_info()
self._processed = True
def _sort_dimensions(self) -> None:
"""Sort the dimensions of the results."""
self._results_dims = [
"windows",
"connections",
"frequencies",
"timepoints",
]
if self.average_windows:
n_windows = len(self.results[0])
for method_i in range(len(self.results)):
self.results[method_i] = np.mean(
self.results[method_i], axis=0
)[np.newaxis]
self._windows_averaged = True
if self.verbose:
print(f"Averaging the data over {n_windows} windows.\n")
def save_results(
self,
fpath: str,
ftype: Union[str, None] = None,
ask_before_overwrite: Union[bool, None] = None,
) -> None:
"""Saves the results and additional information as a file.
PARAMETERS
----------
fpath : str
- Location where the data should be saved.
ftype : str | None; default None
- The filetype of the data that will be saved, without the leading
period. E.g. for saving the file in the json format, this would be
"json", not ".json".
- The information being saved must be an appropriate type for saving
in this format.
- If None, the filetype is determined based on 'fpath', and so the
extension must be included in the path.
ask_before_overwrite : bool | None; default the object's verbosity
- If True, the user is asked to confirm whether or not to overwrite a
pre-existing file if one exists.
- If False, the user is not asked to confirm this and it is done
automatically.
- By default, this is set to None, in which case the value of the
verbosity when the Signal object was instantiated is used.
"""
if ask_before_overwrite is None:
ask_before_overwrite = self.verbose
save_dict(
to_save=self.results_as_dict(),
fpath=fpath,
ftype=ftype,
ask_before_overwrite=ask_before_overwrite,
convert_numpy_to_python=True,
verbose=self.verbose,
)
def results_as_dict(self) -> dict:
"""Returns the connectivity 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 TDE results.
RETURNS
-------
core_info : dict
- The core information about the TDE results.
"""
tau_dims = ["channels", "windows"] # tau never averaged over windows
if self._n_windows == 1 or self._windows_averaged:
window_idcs = 0
if self._windows_averaged:
tde_dims = ["channels", "timepoints"]
else:
tde_dims = ["channels", "windows", "timepoints"]
window_idcs = np.arange(self._n_windows)
windows = (window_idcs + 1).tolist()
tde_data = []
tau_data = []
confidence = []
for method_i in range(len(self.results)):
tde_data.append([])
tau_data.append([])
confidence.append([])
for con_i in range(len(self.indices[0])):
for fband_i in range(len(self.freq_bands.keys())):
tde_data[method_i].append(
self.results[method_i][
window_idcs, con_i, fband_i, :
].tolist()
)
tau_data[method_i].append(
self.taus[method_i][:, con_i, fband_i]
)
if self._n_windows > 1:
confidence[method_i].append(
self._confidence[method_i][con_i, fband_i].tolist()
)
fband_names = list(self.freq_bands.keys()) * len(self.seeds)
fband_freqs = list(self.freq_bands.values()) * len(self.seeds)
seeds = np.repeat(self.seeds, len(self.freq_bands.keys())).tolist()
targets = np.repeat(self.targets, len(self.freq_bands.keys())).tolist()
core_info = {}
for method_i, method_name in enumerate(self.tde_methods):
core_info[f"tde-{method_name}"] = tde_data[method_i]
core_info[f"tde-{method_name}_dimensions"] = tde_dims
core_info[f"tde-{method_name}_tau"] = tau_data[method_i]
core_info[f"tde-{method_name}_tau_dimensions"] = tau_dims
if self._n_windows > 1:
core_info[f"tde-{method_name}_confidence"] = confidence[
method_i
]
core_info.update(
freq_band_names=fband_names,
freq_band_bounds=fband_freqs,
seed_names=seeds,
target_names=targets,
timepoints=self._timepoints,
sampling_frequency=self.signal.data[0].info["sfreq"],
processing_steps=self.processing_steps,
subject_info=self.signal.data[0].info["subject_info"],
)
if not self._windows_averaged:
core_info.update(windows=windows)
return core_info
def _extra_info_for_results_dict(self) -> dict:
"""Get extra info and pad according to number of frequency bands."""
extra_info = super()._extra_info_for_results_dict()
n_fbands = len(self.freq_bands.keys())
for key, value in extra_info.items():
if key != "metadata":
rep_val = []
for val in value:
rep_val.extend([val for _ in range(n_fbands)])
extra_info[key] = np.array(rep_val).tolist()
return extra_info
class ConnectivityMultivariateCoh(ProcMultivariateConnectivity):
"""Calculates the maximised imaginary coherence (MIC) and multivariate
interaction measure (MIM) between signals.
PARAMETERS
----------
signal : coh_signal.Signal
- The preprocessed data to analyse.
verbose : bool; default True
- Whether or not to print information about the information processing.
METHODS
-------
process
- Performs multivariate connectivity analysis.
save_object
- Saves the object as a .pkl file.
save_results
- Saves the results and additional information as a file.
results_as_dict
- Returns the results and additional information as a dictionary.
get_results
- Extracts and returns results.
"""
def __init__(self, signal: coh_signal.Signal, verbose: bool = True) -> None:
super().__init__(signal, verbose)
super()._sort_inputs()
self.con_methods = ["mic", "mim"]
def process(
self,
power_method: str,
seeds: Union[str, list[str], dict],
targets: Union[str, list[str], dict],
fmin: Union[Union[float, tuple], None] = None,
fmax: Union[float, tuple] = np.inf,
fskip: int = 0,
faverage: bool = False,
tmin: Union[float, None] = None,
tmax: Union[float, None] = None,
mt_bandwidth: Union[float, None] = None,
mt_adaptive: bool = False,
mt_low_bias: bool = True,
cwt_freqs: Union[NDArray, None] = None,
cwt_n_cycles: Union[float, NDArray] = 7.0,
n_components: Union[list[int], str] = "rank",
average_windows: bool = False,
average_timepoints: bool = False,
block_size: int = 1000,
n_jobs: int = 1,
) -> None:
"""Applies the connectivity analysis using the
multivar_spectral_connectivity_epochs function of the mne-connectivity
package.
PARAMETERS
----------
power_method : str
- The spectral method for calculating the cross-spectral density.
- Supported inputs are: 'multitaper'; 'fourier'; and 'cwt_morlet'.
seeds : str | list[str] | dict
- The channels to use as seeds for the connectivity analysis.
Connectivity is calculated from each seed to each target.
- If a string, can either be a single channel name, or a single
channel type. In the latter case, the channel type should be
preceded by 'type_', e.g. 'type_ecog'. In this case, channels
belonging to each type with different epoch orders and rereferencing
types will be handled separately.
- If a list of strings, each entry of the list should be a channel
name.
targets : str | list[str] | dict
- The channels to use as targets for the connectivity analysis.
Connectivity is calculated from each seed to each target.
- If a string, can either be a single channel name, or a single
channel type. In the latter case, the channel type should be
preceded by 'type_', e.g. 'type_ecog'. In this case, channels
belonging to each type with different epoch orders and rereferencing
types will be handled separately.
- If a list of strings, each entry of the list should be a channel
name.
fmin : float | tuple | None
- The lower frequency of interest.
- If a float, this frequency is used.
- If a tuple, multiple bands are defined, with each entry being the
lower frequency for that band. E.g. (8., 20.) would give two bands
using 8 Hz and 20 Hz, respectively, as their lower frequencies.
- If None, no lower frequency is used.
fmax : float | tuple; default infinite
- The higher frequency of interest.
- If a float, this frequency is used.
- If a tuple, multiple bands are defined, with each entry being the
higher frequency for that band. E.g. (8., 20.) would give two bands
using 8 Hz and 20 Hz, respectively, as their higher frequencies.
- If an infinite float, no higher frequency is used.
fskip : int; default 0
- Omit every 'fskip'+1th frequency bin to decimate the frequency
domain.
- If 0, no bins are skipped.
faverage : bool; default False
- Whether or not to average the connectivity values for each frequency
band.
tmin : float | None; default None
- Time to start the connectivity estimation.
- If None, the data is used from the beginning.
tmax : float | None; default None
- Time to end the connectivity estimation.
- If None, the data is used until the end.
mt_bandwidth : float | None
- The bandwidth, in Hz, of the multitaper windowing function.
- Only used if 'mode' is 'multitaper'.
mt_adaptive : bool; default False
- Whether or not to use adaptive weights to combine the tapered
spectra into power spectra.
- Only used if 'mode' is 'multitaper'.
mt_low_bias : bool: default True
- Whether or not to only use tapers with > 90% spectral concentration
within bandwidth.
- Only used if 'mode' is 'multitaper'.
cwt_freqs: numpy array[int | float] | None
- The frequencies of interest to calculate connectivity for.
- Only used if 'mode' is 'cwt_morlet'. In this case, 'cwt_freqs'
cannot be None.
cwt_n_cycles: float | array of float; default 7.0
- The number of cycles to use when calculating connectivity.
- If an single integer or float, this number of cycles is for each
frequency.
- If an array, the entries correspond to the number of cycles to use
for each frequency being analysed.
- Only used if 'mode' is 'cwt_morlet'.
n_components ######################################################
average_windows : bool; default False
- Whether or not to average connectivity results across windows.
average_timepoints : bool; default False
- Whether or not to average connectivity results across timepoints.
block_size : int; default 1000
- The number of connections to compute at once.
n_jobs : int; default 1
- The number of epochs to calculate connectivity for in parallel.
"""
if self._processed:
ProcessingOrderError(
"The data in this object has already been processed. "
"Initialise a new instance of the object if you want to "
"perform other analyses on the data."
)
self.power_method = power_method
self.seeds = seeds
self.targets = targets
self.fmin = fmin
self.fmax = fmax
self.fskip = fskip
self.faverage = faverage
self.tmin = tmin
self.tmax = tmax
self.mt_bandwidth = mt_bandwidth
self.mt_adaptive = mt_adaptive
self.mt_low_bias = mt_low_bias
self.cwt_freqs = cwt_freqs
self.cwt_n_cycles = cwt_n_cycles
self.n_components = n_components
self.average_windows = average_windows
self.average_timepoints = average_timepoints
self.block_size = block_size
self.n_jobs = n_jobs
self._sort_processing_inputs()
self._get_results()
def _sort_processing_inputs(self) -> None:
"""Checks that the processing inputs are appropriate and implements
them appropriately."""
super()._sort_processing_inputs()
self._sort_used_settings()
def _sort_used_settings(self) -> None:
"""Collects the settings that are relevant for the processing being
performed and adds only these settings to the 'processing_steps'
dictionary."""
used_settings = {
"con_methods": self.con_methods,
"power_method": self.power_method,
"n_components": self.n_components,
"average_windows": self.average_windows,
"average_timepoints": self.average_timepoints,
"absolute_connectivity": self.absolute_connectivity,
"t_min": self.tmin,
"t_max": self.tmax,
}
if self.power_method == "multitaper":
add_settings = {
"mt_bandwidth": self.mt_bandwidth,
"mt_adaptive": self.mt_adaptive,
"mt_low_bias": self.mt_low_bias,
}
elif self.power_method == "cwt_morlet":
add_settings = {"cwt_n_cycles": self.cwt_n_cycles}
used_settings.update(add_settings)
self.processing_steps["spectral_connectivity"] = used_settings
def save_results(
self,
fpath: str,
ftype: Union[str, None] = None,
ask_before_overwrite: Union[bool, None] = None,
) -> None:
"""Saves the results and additional information as a file.
PARAMETERS
----------
fpath : str
- Location where the data should be saved.
ftype : str | None; default None
- The filetype of the data that will be saved, without the leading
period. E.g. for saving the file in the json format, this would be
"json", not ".json".
- The information being saved must be an appropriate type for saving
in this format.
- If None, the filetype is determined based on 'fpath', and so the
extension must be included in the path.
ask_before_overwrite : bool | None; default the object's verbosity
- If True, the user is asked to confirm whether or not to overwrite a
pre-existing file if one exists.
- If False, the user is not asked to confirm this and it is done
automatically.
- By default, this is set to None, in which case the value of the
verbosity when the Signal object was instantiated is used.
"""
super().save_results(
fpath=fpath, ftype=ftype, ask_before_overwrite=ask_before_overwrite
)
if ask_before_overwrite is None:
ask_before_overwrite = self.verbose
save_dict(
to_save=self.topographies_as_dict(),
fpath=f"{fpath}_topographies",
ftype=ftype,
ask_before_overwrite=ask_before_overwrite,
convert_numpy_to_python=True,
verbose=self.verbose,
)
def topographies_as_dict(self) -> dict:
""""""
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:
""""""
topos, topo_dimensions = self._rearrange_topographies()
concatenated_names = [
name
for names in [*self._seeds_list, *self._targets_list]
for name in names
]
core_info = {
"connectivity-mic_topographies": topos,
"connectivity-mic_topographies_dimensions": topo_dimensions,
"ch_names": concatenated_names,
"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())
seed_names = []
target_names = []
con_boundaries = []
increment_val = 0
for con_i in range(self._n_cons):
n_chs = len(self.indices[0][con_i]) + len(self.indices[1][con_i])
con_boundaries.append(n_chs + increment_val)
increment_val += n_chs
con_i = 0
for entry_i in range(len(concatenated_names)):
if entry_i > con_boundaries[con_i]:
con_i += 1
seed_names.append(self.seeds[con_i])
target_names.append(self.targets[con_i])
entry_i += 1
core_info.update(seed_names=seed_names, target_names=target_names)
return core_info
def _rearrange_topographies(self) -> tuple[list, list[str]]:
"""Rearrange topography results into a list containing the concatenated
topographies for the connections of each seed, and then the connections
of each target, with dimensions [channels x (windows) x freqs x
(times)].
RETURNS
-------
topgraphies : list
- The rearranged topography results.
topography_dimensions : list of str
- Names of the dimensions of the topography results.
"""
mic_idx = self.con_methods.index("mic")
topos = []
entry_i = 0
for group_i in range(2):
for con_i in range(self._n_cons):
for ch_i in range(len(self.indices[group_i][con_i])):
if self._n_windows != 0: