Aluode/PerceptionLabPortable
0
1# Authors: The MNE-Python contributors.2# License: BSD-3-Clause3# Copyright the MNE-Python contributors.4 5import numpy as np6from sklearn.base import BaseEstimator7from sklearn.utils.validation import check_is_fitted8 9from ..time_frequency.tfr import _compute_tfr10from ..utils import _check_option, fill_doc11from .transformer import MNETransformerMixin12 13 14@fill_doc15class TimeFrequency(MNETransformerMixin, BaseEstimator):16 """Time frequency transformer.17 18 Time-frequency transform of times series along the last axis.19 20 Parameters21 ----------22 freqs : array-like of float, shape (n_freqs,)23 The frequencies.24 sfreq : float | int, default 1.025 Sampling frequency of the data.26 method : 'multitaper' | 'morlet', default 'morlet'27 The time-frequency method. 'morlet' convolves a Morlet wavelet.28 'multitaper' uses Morlet wavelets windowed with multiple DPSS29 multitapers.30 n_cycles : float | array of float, default 7.031 Number of cycles in the Morlet wavelet. Fixed number32 or one per frequency.33 time_bandwidth : float, default None34 If None and method=multitaper, will be set to 4.0 (3 tapers).35 Time x (Full) Bandwidth product. Only applies if36 method == 'multitaper'. The number of good tapers (low-bias) is37 chosen automatically based on this to equal floor(time_bandwidth - 1).38 use_fft : bool, default True39 Use the FFT for convolutions or not.40 decim : int | slice, default 141 To reduce memory usage, decimation factor after time-frequency42 decomposition.43 If `int`, returns tfr[..., ::decim].44 If `slice`, returns tfr[..., decim].45 46 .. note:: Decimation may create aliasing artifacts, yet decimation47 is done after the convolutions.48 49 output : str, default 'complex'50 * 'complex' : single trial complex.51 * 'power' : single trial power.52 * 'phase' : single trial phase.53 %(n_jobs)s54 The number of epochs to process at the same time. The parallelization55 is implemented across channels.56 %(verbose)s57 58 See Also59 --------60 mne.time_frequency.tfr_morlet61 mne.time_frequency.tfr_multitaper62 """63 64 def __init__(65 self,66 freqs,67 sfreq=1.0,68 method="morlet",69 n_cycles=7.0,70 time_bandwidth=None,71 use_fft=True,72 decim=1,73 output="complex",74 n_jobs=1,75 verbose=None,76 ):77 """Init TimeFrequency transformer."""78 self.freqs = freqs79 self.sfreq = sfreq80 self.method = method81 self.n_cycles = n_cycles82 self.time_bandwidth = time_bandwidth83 self.use_fft = use_fft84 self.decim = decim85 # Check that output is not an average metric (e.g. ITC)86 self.output = output87 self.n_jobs = n_jobs88 self.verbose = verbose89 90 def __sklearn_tags__(self):91 """Return sklearn tags."""92 out = super().__sklearn_tags__()93 from sklearn.utils import TransformerTags94 95 if out.transformer_tags is None:96 out.transformer_tags = TransformerTags()97 out.transformer_tags.preserves_dtype = [] # real->complex98 return out99 100 def fit_transform(self, X, y=None):101 """Time-frequency transform of times series along the last axis.102 103 Parameters104 ----------105 X : array, shape (n_samples, n_channels, n_times)106 The training data samples. The channel dimension can be zero- or107 1-dimensional.108 y : None109 For scikit-learn compatibility purposes.110 111 Returns112 -------113 Xt : array, shape (n_samples, n_channels, n_freqs, n_times)114 The time-frequency transform of the data, where n_channels can be115 zero- or 1-dimensional.116 """117 return self.fit(X, y).transform(X)118 119 def fit(self, X, y=None): # noqa: D401120 """Do nothing (for scikit-learn compatibility purposes).121 122 Parameters123 ----------124 X : array, shape (n_samples, n_channels, n_times)125 The training data.126 y : array | None127 The target values.128 129 Returns130 -------131 self : object132 Return self.133 """134 # Check non-average output135 _check_option("output", self.output, ["complex", "power", "phase"])136 self._check_data(X, y=y, fit=True)137 self.fitted_ = True138 return self139 140 def transform(self, X):141 """Time-frequency transform of times series along the last axis.142 143 Parameters144 ----------145 X : array, shape (n_samples, [n_channels, ]n_times)146 The training data samples. The channel dimension can be zero- or147 1-dimensional.148 149 Returns150 -------151 Xt : array, shape (n_samples, [n_channels, ]n_freqs, n_times)152 The time-frequency transform of the data, where n_channels can be153 zero- or 1-dimensional.154 """155 X = self._check_data(X, atleast_3d=False)156 check_is_fitted(self, "fitted_")157 # Ensure 3-dimensional X158 shape = X.shape[1:-1]159 if not shape:160 X = X[:, np.newaxis, :]161 162 # Compute time-frequency163 Xt = _compute_tfr(164 X,165 freqs=self.freqs,166 sfreq=self.sfreq,167 method=self.method,168 n_cycles=self.n_cycles,169 zero_mean=True,170 time_bandwidth=self.time_bandwidth,171 use_fft=self.use_fft,172 decim=self.decim,173 output=self.output,174 n_jobs=self.n_jobs,175 verbose=self.verbose,176 )177 178 # Back to original shape179 if not shape:180 Xt = Xt[:, 0, :]181 182 return Xt183 