forked from mne-tools/mne-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsd.py
1316 lines (1108 loc) · 45.9 KB
/
csd.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
# -*- coding: utf-8 -*-
# Authors: Marijn van Vliet <[email protected]>
# Susanna Aro <[email protected]>
# Roman Goj <[email protected]>
#
# License: BSD (3-clause)
import copy as cp
import numbers
import numpy as np
from .tfr import cwt, morlet
from ..fixes import rfftfreq
from ..io.pick import pick_channels, _picks_to_idx
from ..utils import logger, verbose, warn, copy_function_doc_to_method_doc
from ..viz.misc import plot_csd
from ..time_frequency.multitaper import (_compute_mt_params, _mt_spectra,
_csd_from_mt, _psd_from_mt_adaptive)
from ..parallel import parallel_func
from ..externals.h5io import read_hdf5, write_hdf5
def pick_channels_csd(csd, include=[], exclude=[], ordered=False, copy=True):
"""Pick channels from cross-spectral density matrix.
Parameters
----------
csd : instance of CrossSpectralDensity
The CSD object to select the channels from.
include : list of str
List of channels to include (if empty, include all available).
exclude : list of str
Channels to exclude (if empty, do not exclude any).
ordered : bool
If True (default False), ensure that the order of the channels in the
modified instance matches the order of ``include``.
.. versionadded:: 0.20.0
copy : bool
If True (the default), return a copy of the CSD matrix with the
modified channels. If False, channels are modified in-place.
.. versionadded:: 0.20.0
Returns
-------
res : instance of CrossSpectralDensity
Cross-spectral density restricted to selected channels.
"""
if copy:
csd = csd.copy()
sel = pick_channels(csd.ch_names, include=include, exclude=exclude,
ordered=ordered)
data = []
for vec in csd._data.T:
mat = _vector_to_sym_mat(vec)
mat = mat[sel, :][:, sel]
data.append(_sym_mat_to_vector(mat))
ch_names = [csd.ch_names[i] for i in sel]
csd._data = np.array(data).T
csd.ch_names = ch_names
return csd
class CrossSpectralDensity(object):
"""Cross-spectral density.
Given a list of time series, the CSD matrix denotes for each pair of time
series, the cross-spectral density. This matrix is symmetric and internally
stored as a vector.
This object can store multiple CSD matrices: one for each frequency.
Use ``.get_data(freq)`` to obtain an CSD matrix as an ndarray.
Parameters
----------
data : ndarray, shape ((n_channels**2 + n_channels) / 2, n_frequencies)
For each frequency, the cross-spectral density matrix in vector format.
ch_names : list of str
List of string names for each channel.
frequencies : float | list of float | list of list of float
Frequency or frequencies for which the CSD matrix was calculated. When
averaging across frequencies (see the :func:`CrossSpectralDensity.mean`
function), this will be a list of lists that contains for each
frequency bin, the frequencies that were averaged. Frequencies should
always be sorted.
n_fft : int
The number of FFT points or samples that have been used in the
computation of this CSD.
tmin : float | None
Start of the time window for which CSD was calculated in seconds. Can
be ``None`` (the default) to indicate no timing information is
available.
tmax : float | None
End of the time window for which CSD was calculated in seconds. Can be
``None`` (the default) to indicate no timing information is available.
projs : list of Projection | None
List of projectors to apply to timeseries data when using this CSD
object to compute a DICS beamformer. Defaults to ``None``, which means
no projectors will be applied.
See Also
--------
csd_fourier
csd_multitaper
csd_morlet
csd_array_fourier
csd_array_multitaper
csd_array_morlet
"""
def __init__(self, data, ch_names, frequencies, n_fft, tmin=None,
tmax=None, projs=None):
data = np.asarray(data)
if data.ndim == 1:
data = data[:, np.newaxis]
elif data.ndim > 2:
raise ValueError('`data` should be either a 1D or 2D array.')
self._data = data
if len(ch_names) != _n_dims_from_triu(len(data)):
raise ValueError('Number of ch_names does not match the number of '
'time series in the CSD matrix.')
self.ch_names = list(ch_names)
self.tmin = tmin
self.tmax = tmax
if isinstance(frequencies, numbers.Number):
frequencies = [frequencies]
if len(frequencies) != data.shape[1]:
raise ValueError('Number of frequencies does not match the number '
'of CSD matrices in the data array (%d != %d).' %
(len(frequencies), data.shape[1]))
self.frequencies = frequencies
self.n_fft = n_fft
self.projs = cp.deepcopy(projs)
@property
def n_channels(self):
"""Number of time series defined in this CSD object."""
return len(self.ch_names)
@property
def _is_sum(self):
"""Whether the CSD matrix represents a sum (or average) of freqs."""
# If the CSD is an average, the frequencies will be stored as a list
# of lists (or like-like objects) instead of plain numbers.
return not isinstance(self.frequencies[0], numbers.Number)
def __len__(self): # noqa: D105
"""Return number of frequencies.
Returns
-------
n_freqs : int
The number of frequencies.
"""
return len(self.frequencies)
def __repr__(self): # noqa: D105
# Make a pretty string representation of the frequencies
freq_strs = []
for f in self.frequencies:
if isinstance(f, numbers.Number):
freq_strs.append(str(f))
elif len(f) == 1:
freq_strs.append(str(f[0]))
else:
freq_strs.append('{}-{}'.format(np.min(f), np.max(f)))
freq_str = ', '.join(freq_strs) + ' Hz.'
if self.tmin is not None and self.tmax is not None:
time_str = '{} to {} s'.format(self.tmin, self.tmax)
else:
time_str = 'unknown'
return (
'<CrossSpectralDensity | '
'n_channels={}, time={}, frequencies={}>'
).format(self.n_channels, time_str, freq_str)
def sum(self, fmin=None, fmax=None):
"""Calculate the sum CSD in the given frequency range(s).
If the exact given frequencies are not available, the nearest
frequencies will be chosen.
Parameters
----------
fmin : float | list of float | None
Lower bound of the frequency range in Hertz. Defaults to the lowest
frequency available. When a list of frequencies is given, these are
used as the lower bounds (inclusive) of frequency bins and the sum
is taken for each bin.
fmax : float | list of float | None
Upper bound of the frequency range in Hertz. Defaults to the
highest frequency available. When a list of frequencies is given,
these are used as the upper bounds (inclusive) of frequency bins
and the sum is taken for each bin.
Returns
-------
csd : instance of CrossSpectralDensity
The CSD matrix, summed across the given frequency range(s).
"""
if self._is_sum:
raise RuntimeError('This CSD matrix already represents a mean or '
'sum across frequencies.')
# Deal with the various ways in which fmin and fmax can be specified
if fmin is None and fmax is None:
fmin = [self.frequencies[0]]
fmax = [self.frequencies[-1]]
else:
if isinstance(fmin, numbers.Number):
fmin = [fmin]
if isinstance(fmax, numbers.Number):
fmax = [fmax]
if fmin is None:
fmin = [self.frequencies[0]] * len(fmax)
if fmax is None:
fmax = [self.frequencies[-1]] * len(fmin)
if any(fmin_ > fmax_ for fmin_, fmax_ in zip(fmin, fmax)):
raise ValueError('Some lower bounds are higher than the '
'corresponding upper bounds.')
# Find the index of the lower bound of each frequency bin
fmin_inds = [self._get_frequency_index(f) for f in fmin]
fmax_inds = [self._get_frequency_index(f) + 1 for f in fmax]
if len(fmin_inds) != len(fmax_inds):
raise ValueError('The length of fmin does not match the '
'length of fmax.')
# Sum across each frequency bin
n_bins = len(fmin_inds)
new_data = np.zeros((self._data.shape[0], n_bins),
dtype=self._data.dtype)
new_frequencies = []
for i, (min_ind, max_ind) in enumerate(zip(fmin_inds, fmax_inds)):
new_data[:, i] = self._data[:, min_ind:max_ind].sum(axis=1)
new_frequencies.append(self.frequencies[min_ind:max_ind])
csd_out = CrossSpectralDensity(data=new_data, ch_names=self.ch_names,
tmin=self.tmin, tmax=self.tmax,
frequencies=new_frequencies,
n_fft=self.n_fft)
return csd_out
def mean(self, fmin=None, fmax=None):
"""Calculate the mean CSD in the given frequency range(s).
Parameters
----------
fmin : float | list of float | None
Lower bound of the frequency range in Hertz. Defaults to the lowest
frequency available. When a list of frequencies is given, these are
used as the lower bounds (inclusive) of frequency bins and the mean
is taken for each bin.
fmax : float | list of float | None
Upper bound of the frequency range in Hertz. Defaults to the
highest frequency available. When a list of frequencies is given,
these are used as the upper bounds (inclusive) of frequency bins
and the mean is taken for each bin.
Returns
-------
csd : instance of CrossSpectralDensity
The CSD matrix, averaged across the given frequency range(s).
"""
csd = self.sum(fmin, fmax)
for i, f in enumerate(csd.frequencies):
csd._data[:, i] /= len(f)
return csd
def _get_frequency_index(self, freq):
"""Find the index of the given frequency in ``self.frequencies``.
If the exact given frequency is not available, the nearest frequencies
will be chosen, up to a difference of 1 Hertz.
Parameters
----------
freq : float
The frequency to find the index for
Returns
-------
index : int
The index of the frequency nearest to the requested frequency.
"""
if self._is_sum:
raise ValueError('This CSD object represents a mean across '
'frequencies. Cannot select a specific '
'frequency.')
distance = np.abs(np.asarray(self.frequencies) - freq)
index = np.argmin(distance)
min_dist = distance[index]
if min_dist > 1:
raise IndexError('Frequency %f is not available.' % freq)
return index
def pick_frequency(self, freq=None, index=None):
"""Get a CrossSpectralDensity object with only the given frequency.
Parameters
----------
freq : float | None
Return the CSD matrix for a specific frequency. Only available
when no averaging across frequencies has been done.
index : int | None
Return the CSD matrix for the frequency or frequency-bin with the
given index.
Returns
-------
csd : instance of CrossSpectralDensity
A CSD object containing a single CSD matrix that corresponds to the
requested frequency or frequency-bin.
See Also
--------
get_data
"""
if freq is None and index is None:
raise ValueError('Use either the "freq" or "index" parameter to '
'select the desired frequency.')
elif freq is not None:
if index is not None:
raise ValueError('Cannot specify both a frequency and index.')
index = self._get_frequency_index(freq)
return self[index]
def get_data(self, frequency=None, index=None):
"""Get the CSD matrix for a given frequency as NumPy array.
If there is only one matrix defined in the CSD object, calling this
method without any parameters will return it. If multiple matrices are
defined, use either the ``frequency`` or ``index`` parameter to select
one.
Parameters
----------
frequency : float | None
Return the CSD matrix for a specific frequency. Only available when
no averaging across frequencies has been done.
index : int | None
Return the CSD matrix for the frequency or frequency-bin with the
given index.
Returns
-------
csd : ndarray, shape (n_channels, n_channels)
The CSD matrix corresponding to the requested frequency.
See Also
--------
pick_frequency
"""
if frequency is None and index is None:
if self._data.shape[1] > 1:
raise ValueError('Specify either the frequency or index of '
'the frequency bin for which to obtain the '
'CSD matrix.')
index = 0
elif frequency is not None:
if index is not None:
raise ValueError('Cannot specify both a frequency and index.')
index = self._get_frequency_index(frequency)
return _vector_to_sym_mat(self._data[:, index])
@copy_function_doc_to_method_doc(plot_csd)
def plot(self, info=None, mode='csd', colorbar=True, cmap='viridis',
n_cols=None, show=True):
return plot_csd(self, info=info, mode=mode, colorbar=colorbar,
cmap=cmap, n_cols=n_cols, show=show)
def __setstate__(self, state): # noqa: D105
self._data = state['data']
self.tmin = state['tmin']
self.tmax = state['tmax']
self.ch_names = state['ch_names']
self.frequencies = state['frequencies']
self.n_fft = state['n_fft']
def __getstate__(self): # noqa: D105
return dict(
data=self._data,
tmin=self.tmin,
tmax=self.tmax,
ch_names=self.ch_names,
frequencies=self.frequencies,
n_fft=self.n_fft,
)
def __getitem__(self, sel): # noqa: D105
"""Subselect frequencies.
Parameters
----------
sel : ndarray
Array of frequency indices to subselect.
Returns
-------
csd : instance of CrossSpectralDensity
A new CSD instance with the subset of frequencies.
"""
return CrossSpectralDensity(
data=self._data[:, sel], ch_names=self.ch_names, tmin=self.tmin,
tmax=self.tmax,
frequencies=np.atleast_1d(self.frequencies)[sel].tolist(),
n_fft=self.n_fft,
)
def save(self, fname):
"""Save the CSD to an HDF5 file.
Parameters
----------
fname : str
The name of the file to save the CSD to. The extension '.h5' will
be appended if the given filename doesn't have it already.
See Also
--------
read_csd : For reading CSD objects from a file.
"""
if not fname.endswith('.h5'):
fname += '.h5'
write_hdf5(fname, self.__getstate__(), overwrite=True, title='conpy')
def copy(self):
"""Return copy of the CrossSpectralDensity object.
Returns
-------
copy : instance of CrossSpectralDensity
A copy of the object.
"""
return cp.deepcopy(self)
def pick_channels(self, ch_names, ordered=False):
"""Pick channels from this cross-spectral density matrix.
Parameters
----------
ch_names : list of str
List of channels to keep. All other channels are dropped.
ordered : bool
If True (default False), ensure that the order of the channels
matches the order of ``ch_names``.
Returns
-------
csd : instance of CrossSpectralDensity.
The modified cross-spectral density object.
Notes
-----
Operates in-place.
.. versionadded:: 0.20.0
"""
return pick_channels_csd(self, include=ch_names, exclude=[],
ordered=ordered, copy=False)
def _n_dims_from_triu(n):
"""Compute matrix dims from number of elements in the upper triangle.
Parameters
----------
n : int
Number of elements in the upper triangle of the symmetric matrix.
Returns
-------
dim : int
The dimensions of the symmetric matrix.
"""
return int(np.ceil(np.sqrt(n * 2))) - 1
def _vector_to_sym_mat(vec):
"""Convert vector to a symmetric matrix.
The upper triangle of the matrix (including the diagonal) will be filled
with the values of the vector.
Parameters
----------
vec : list or 1d-array
The vector to convert to a symmetric matrix.
Returns
-------
mat : 2d-array
The symmetric matrix.
See Also
--------
_sym_mat_to_vector
"""
dim = _n_dims_from_triu(len(vec))
mat = np.zeros((dim, dim) + vec.shape[1:], dtype=vec.dtype)
# Fill the upper triangle of the matrix
mat[np.triu_indices(dim)] = vec
# Fill out the lower tri (make conjugate to ensure matrix is hermitian)
mat = mat + np.rollaxis(mat, 1).conj()
# We counted the diagonal twice
if np.issubdtype(mat.dtype, np.integer):
mat[np.diag_indices(dim)] //= 2
else:
mat[np.diag_indices(dim)] /= 2
return mat
def _sym_mat_to_vector(mat):
"""Convert a symmetric matrix to a vector.
The upper triangle of the matrix (including the diagonal) will be used
as the values of the vector.
Parameters
----------
mat : 2d-array
The symmetric matrix to convert to a vector
Returns
-------
vec : 1d-array
A vector consisting of the values of the upper triangle of the matrix.
See Also
--------
_vector_to_sym_mat
"""
return mat[np.triu_indices_from(mat)]
def read_csd(fname):
"""Read a CrossSpectralDensity object from an HDF5 file.
Parameters
----------
fname : str
The name of the file to read the CSD from. The extension '.h5' will be
appended if the given filename doesn't have it already.
Returns
-------
csd : instance of CrossSpectralDensity
The CSD that was stored in the file.
See Also
--------
CrossSpectralDensity.save : For saving CSD objects.
"""
if not fname.endswith('.h5'):
fname += '.h5'
csd_dict = read_hdf5(fname, title='conpy')
return CrossSpectralDensity(**csd_dict)
@verbose
def csd_fourier(epochs, fmin=0, fmax=np.inf, tmin=None, tmax=None, picks=None,
n_fft=None, projs=None, n_jobs=1, verbose=None):
"""Estimate cross-spectral density from an array using short-time fourier.
Parameters
----------
epochs : instance of Epochs
The epochs to compute the CSD for.
fmin : float
Minimum frequency of interest, in Hertz.
fmax : float | np.inf
Maximum frequency of interest, in Hertz.
tmin : float | None
Minimum time instant to consider, in seconds. If ``None`` start at
first sample.
tmax : float | None
Maximum time instant to consider, in seconds. If ``None`` end at last
sample.
%(picks_good_data_noref)s
n_fft : int | None
Length of the FFT. If ``None``, the exact number of samples between
``tmin`` and ``tmax`` will be used.
projs : list of Projection | None
List of projectors to store in the CSD object. Defaults to ``None``,
which means the projectors defined in the Epochs object will by copied.
%(n_jobs)s
%(verbose)s
Returns
-------
csd : instance of CrossSpectralDensity
The computed cross-spectral density.
See Also
--------
csd_array_fourier
csd_array_morlet
csd_array_multitaper
csd_morlet
csd_multitaper
"""
epochs, projs = _prepare_csd(epochs, tmin, tmax, picks, projs)
return csd_array_fourier(epochs.get_data(), sfreq=epochs.info['sfreq'],
t0=epochs.tmin, fmin=fmin, fmax=fmax, tmin=tmin,
tmax=tmax, ch_names=epochs.ch_names, n_fft=n_fft,
projs=projs, n_jobs=n_jobs, verbose=verbose)
@verbose
def csd_array_fourier(X, sfreq, t0=0, fmin=0, fmax=np.inf, tmin=None,
tmax=None, ch_names=None, n_fft=None, projs=None,
n_jobs=1, verbose=None):
"""Estimate cross-spectral density from an array using short-time fourier.
Parameters
----------
X : array-like, shape (n_epochs, n_channels, n_times)
The time series data consisting of n_epochs separate observations
of signals with n_channels time-series of length n_times.
sfreq : float
Sampling frequency of observations.
t0 : float
Time of the first sample relative to the onset of the epoch, in
seconds. Defaults to 0.
fmin : float
Minimum frequency of interest, in Hertz.
fmax : float | np.inf
Maximum frequency of interest, in Hertz.
tmin : float | None
Minimum time instant to consider, in seconds. If ``None`` start at
first sample.
tmax : float | None
Maximum time instant to consider, in seconds. If ``None`` end at last
sample.
ch_names : list of str | None
A name for each time series. If ``None`` (the default), the series will
be named 'SERIES###'.
n_fft : int | None
Length of the FFT. If ``None``, the exact number of samples between
``tmin`` and ``tmax`` will be used.
projs : list of Projection | None
List of projectors to store in the CSD object. Defaults to ``None``,
which means no projectors are stored.
%(n_jobs)s
%(verbose)s
Returns
-------
csd : instance of CrossSpectralDensity
The computed cross-spectral density.
See Also
--------
csd_array_morlet
csd_array_multitaper
csd_fourier
csd_morlet
csd_multitaper
"""
X, times, tmin, tmax, fmin, fmax = _prepare_csd_array(
X, sfreq, t0, tmin, tmax, fmin, fmax)
# Slice X to the requested time window
tstart = None if tmin is None else np.searchsorted(times, tmin - 1e-10)
tstop = None if tmax is None else np.searchsorted(times, tmax + 1e-10)
X = X[:, :, tstart:tstop]
times = times[tstart:tstop]
n_times = len(times)
n_fft = n_times if n_fft is None else n_fft
# Preparing frequencies of interest
# orig_frequencies = fftfreq(n_fft, 1. / sfreq)
orig_frequencies = rfftfreq(n_fft, 1. / sfreq)
freq_mask = (orig_frequencies > fmin) & (orig_frequencies < fmax)
frequencies = orig_frequencies[freq_mask]
if len(frequencies) == 0:
raise ValueError('No discrete fourier transform results within '
'the given frequency window. Please widen either '
'the frequency window or the time window')
# Compute the CSD
return _execute_csd_function(X, times, frequencies, _csd_fourier,
params=[sfreq, n_times, freq_mask, n_fft],
n_fft=n_fft, ch_names=ch_names, projs=projs,
n_jobs=n_jobs, verbose=verbose)
@verbose
def csd_multitaper(epochs, fmin=0, fmax=np.inf, tmin=None, tmax=None,
picks=None, n_fft=None, bandwidth=None, adaptive=False,
low_bias=True, projs=None, n_jobs=1, verbose=None):
"""Estimate cross-spectral density from epochs using Morlet wavelets.
Parameters
----------
epochs : instance of Epochs
The epochs to compute the CSD for.
fmin : float | None
Minimum frequency of interest, in Hertz.
fmax : float | np.inf
Maximum frequency of interest, in Hertz.
tmin : float
Minimum time instant to consider, in seconds. If ``None`` start at
first sample.
tmax : float | None
Maximum time instant to consider, in seconds. If ``None`` end at last
sample.
%(picks_good_data_noref)s
n_fft : int | None
Length of the FFT. If ``None``, the exact number of samples between
``tmin`` and ``tmax`` will be used.
bandwidth : float | None
The bandwidth of the multitaper windowing function in Hz.
adaptive : bool
Use adaptive weights to combine the tapered spectra into PSD.
low_bias : bool
Only use tapers with more than 90%% spectral concentration within
bandwidth.
projs : list of Projection | None
List of projectors to store in the CSD object. Defaults to ``None``,
which means the projectors defined in the Epochs object will by copied.
%(n_jobs)s
%(verbose)s
Returns
-------
csd : instance of CrossSpectralDensity
The computed cross-spectral density.
See Also
--------
csd_array_fourier
csd_array_morlet
csd_array_multitaper
csd_fourier
csd_morlet
"""
epochs, projs = _prepare_csd(epochs, tmin, tmax, picks, projs)
return csd_array_multitaper(epochs.get_data(), sfreq=epochs.info['sfreq'],
t0=epochs.tmin, fmin=fmin, fmax=fmax,
tmin=tmin, tmax=tmax, ch_names=epochs.ch_names,
n_fft=n_fft, bandwidth=bandwidth,
adaptive=adaptive, low_bias=low_bias,
projs=projs, n_jobs=n_jobs, verbose=verbose)
@verbose
def csd_array_multitaper(X, sfreq, t0=0, fmin=0, fmax=np.inf, tmin=None,
tmax=None, ch_names=None, n_fft=None, bandwidth=None,
adaptive=False, low_bias=True, projs=None, n_jobs=1,
verbose=None):
"""Estimate cross-spectral density from an array using Morlet wavelets.
Parameters
----------
X : array-like, shape (n_epochs, n_channels, n_times)
The time series data consisting of n_epochs separate observations
of signals with n_channels time-series of length n_times.
sfreq : float
Sampling frequency of observations.
t0 : float
Time of the first sample relative to the onset of the epoch, in
seconds. Defaults to 0.
fmin : float
Minimum frequency of interest, in Hertz.
fmax : float | np.inf
Maximum frequency of interest, in Hertz.
tmin : float | None
Minimum time instant to consider, in seconds. If ``None`` start at
first sample.
tmax : float | None
Maximum time instant to consider, in seconds. If ``None`` end at last
sample.
ch_names : list of str | None
A name for each time series. If ``None`` (the default), the series will
be named 'SERIES###'.
n_fft : int | None
Length of the FFT. If ``None``, the exact number of samples between
``tmin`` and ``tmax`` will be used.
bandwidth : float | None
The bandwidth of the multitaper windowing function in Hz.
adaptive : bool
Use adaptive weights to combine the tapered spectra into PSD.
low_bias : bool
Only use tapers with more than 90%% spectral concentration within
bandwidth.
projs : list of Projection | None
List of projectors to store in the CSD object. Defaults to ``None``,
which means no projectors are stored.
%(n_jobs)s
%(verbose)s
Returns
-------
csd : instance of CrossSpectralDensity
The computed cross-spectral density.
See Also
--------
csd_array_fourier
csd_array_morlet
csd_fourier
csd_morlet
csd_multitaper
"""
X, times, tmin, tmax, fmin, fmax = _prepare_csd_array(
X, sfreq, t0, tmin, tmax, fmin, fmax)
# Slice X to the requested time window
tstart = None if tmin is None else np.searchsorted(times, tmin - 1e-10)
tstop = None if tmax is None else np.searchsorted(times, tmax + 1e-10)
X = X[:, :, tstart:tstop]
times = times[tstart:tstop]
n_times = len(times)
n_fft = n_times if n_fft is None else n_fft
window_fun, eigvals, mt_adaptive = \
_compute_mt_params(n_times, sfreq, bandwidth, low_bias, adaptive)
# Preparing frequencies of interest
orig_frequencies = rfftfreq(n_fft, 1. / sfreq)
freq_mask = (orig_frequencies > fmin) & (orig_frequencies < fmax)
frequencies = orig_frequencies[freq_mask]
if len(frequencies) == 0:
raise ValueError('No discrete fourier transform results within '
'the given frequency window. Please widen either '
'the frequency window or the time window')
# Compute the CSD
return _execute_csd_function(X, times, frequencies, _csd_multitaper,
params=[sfreq, n_times, window_fun, eigvals,
freq_mask, n_fft, adaptive],
n_fft=n_fft, ch_names=ch_names, projs=projs,
n_jobs=n_jobs, verbose=verbose)
@verbose
def csd_morlet(epochs, frequencies, tmin=None, tmax=None, picks=None,
n_cycles=7, use_fft=True, decim=1, projs=None, n_jobs=1,
verbose=None):
"""Estimate cross-spectral density from epochs using Morlet wavelets.
Parameters
----------
epochs : instance of Epochs
The epochs to compute the CSD for.
frequencies : list of float
The frequencies of interest, in Hertz.
tmin : float | None
Minimum time instant to consider, in seconds. If ``None`` start at
first sample.
tmax : float | None
Maximum time instant to consider, in seconds. If ``None`` end at last
sample.
%(picks_good_data_noref)s
n_cycles : float | list of float | None
Number of cycles to use when constructing Morlet wavelets. Fixed number
or one per frequency. Defaults to 7.
use_fft : bool
Whether to use FFT-based convolution to compute the wavelet transform.
Defaults to True.
decim : int | slice
To reduce memory usage, decimation factor during time-frequency
decomposition. Defaults to 1 (no decimation).
If `int`, uses tfr[..., ::decim].
If `slice`, uses tfr[..., decim].
projs : list of Projection | None
List of projectors to store in the CSD object. Defaults to ``None``,
which means the projectors defined in the Epochs object will be copied.
%(n_jobs)s
%(verbose)s
Returns
-------
csd : instance of CrossSpectralDensity
The computed cross-spectral density.
See Also
--------
csd_array_fourier
csd_array_morlet
csd_array_multitaper
csd_fourier
csd_multitaper
"""
epochs, projs = _prepare_csd(epochs, tmin, tmax, picks, projs)
return csd_array_morlet(epochs.get_data(), sfreq=epochs.info['sfreq'],
frequencies=frequencies, t0=epochs.tmin, tmin=tmin,
tmax=tmax, ch_names=epochs.ch_names,
n_cycles=n_cycles, use_fft=use_fft, decim=decim,
projs=projs, n_jobs=n_jobs, verbose=verbose)
@verbose
def csd_array_morlet(X, sfreq, frequencies, t0=0, tmin=None, tmax=None,
ch_names=None, n_cycles=7, use_fft=True, decim=1,
projs=None, n_jobs=1, verbose=None):
"""Estimate cross-spectral density from an array using Morlet wavelets.
Parameters
----------
X : array-like, shape (n_epochs, n_channels, n_times)
The time series data consisting of n_epochs separate observations
of signals with n_channels time-series of length n_times.
sfreq : float
Sampling frequency of observations.
frequencies : list of float
The frequencies of interest, in Hertz.
t0 : float
Time of the first sample relative to the onset of the epoch, in
seconds. Defaults to 0.
tmin : float | None
Minimum time instant to consider, in seconds. If ``None`` start at
first sample.
tmax : float | None
Maximum time instant to consider, in seconds. If ``None`` end at last
sample.
ch_names : list of str | None
A name for each time series. If ``None`` (the default), the series will
be named 'SERIES###'.
n_cycles : float | list of float | None
Number of cycles to use when constructing Morlet wavelets. Fixed number
or one per frequency. Defaults to 7.
use_fft : bool
Whether to use FFT-based convolution to compute the wavelet transform.
Defaults to True.
decim : int | slice
To reduce memory usage, decimation factor during time-frequency
decomposition. Defaults to 1 (no decimation).
If `int`, uses tfr[..., ::decim].
If `slice`, uses tfr[..., decim].
projs : list of Projection | None
List of projectors to store in the CSD object. Defaults to ``None``,
which means the projectors defined in the Epochs object will be copied.
%(n_jobs)s
%(verbose)s
Returns
-------
csd : instance of CrossSpectralDensity
The computed cross-spectral density.
See Also
--------
csd_array_fourier
csd_array_multitaper
csd_fourier
csd_morlet
csd_multitaper
"""
X, times, tmin, tmax, _, _ = _prepare_csd_array(X, sfreq, t0, tmin, tmax)
n_times = len(times)
# Construct the appropriate Morlet wavelets
wavelets = morlet(sfreq, frequencies, n_cycles)
# Slice X to the requested time window + half the length of the longest
# wavelet.
wave_length = len(wavelets[np.argmin(frequencies)]) // 2
tstart = tstop = None
if tmin is not None:
tstart = np.searchsorted(times, tmin)
tstart = max(0, tstart - wave_length)
if tmax is not None:
tstop = np.searchsorted(times, tmax)
tstop = min(n_times, tstop + wave_length)
X = X[:, :, tstart:tstop]
times = times[tstart:tstop]
# After CSD computation, we slice again to the requested time window.
csd_tstart = None if tmin is None else np.searchsorted(times, tmin - 1e-10)
csd_tstop = None if tmax is None else np.searchsorted(times, tmax + 1e-10)
csd_tslice = slice(csd_tstart, csd_tstop)