From 15f2ad31a0abe8dcdae94f541899005e0d0609a9 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Mon, 13 Nov 2023 10:08:13 -0600 Subject: [PATCH] WIP AverageTFR --- mne/evoked.py | 14 ++++- mne/time_frequency/spectrogram.py | 87 +++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/mne/evoked.py b/mne/evoked.py index 8b657772594..4cbc64651e9 100644 --- a/mne/evoked.py +++ b/mne/evoked.py @@ -1143,13 +1143,23 @@ def compute_tfr( Notes ----- - .. versionadded:: 1.3 + .. versionadded:: 1.6 References ---------- .. footbibliography:: """ - pass + return AverageTFR( + self, + method=method, + freqs=freqs, + picks=picks, + proj=proj, + decim=decim, + n_jobs=n_jobs, + verbose=verbose, + **method_kw, + ) @verbose def plot_psd( diff --git a/mne/time_frequency/spectrogram.py b/mne/time_frequency/spectrogram.py index d4443494cf8..ab24769c66d 100644 --- a/mne/time_frequency/spectrogram.py +++ b/mne/time_frequency/spectrogram.py @@ -1094,3 +1094,90 @@ def average(self, method="mean", *, dim="epochs", copy=False): """ # TODO probably can reuse the existing method from EpochsTFR pass + + +class AverageTFR(BaseTFR): + """Data object for spectrotemporal representations of averaged data. + + .. warning:: The preferred means of creating AverageTFR objects is via the instance + method :meth:`mne.Evoked.compute_tfr` or via + :meth:`mne.time_frequency.EpochsTFR.average`. Direct class + instantiation is not supported. + + Parameters + ---------- + inst : instance of Evoked + The data from which to compute the time-frequency representation. + + + %(method_tfr)s + %(freqs_tfr)s + %(picks_good_data_noref)s + %(proj_psd)s + %(decim_tfr)s + %(n_jobs)s + %(verbose)s + %(method_kw_tfr)s + + Attributes + ---------- + ch_names : list + The channel names. + freqs : array + Frequencies at which the amplitude, power, or fourier coefficients + have been computed. + %(info_not_none)s + method : str + The method used to compute the spectra (``'morlet'``, ``'multitaper'`` + or ``'stockwell'``). + + See Also + -------- + RawTFR + EpochsTFR + mne.Evoked.compute_tfr + mne.time_frequency.EpochsTFR.average + + References + ---------- + .. footbibliography:: + """ + + def __init__( + self, + inst, + method, + freqs, + *, + tmin=None, + tmax=None, + picks=None, + proj=False, + decim=1, + n_jobs=None, + verbose=None, + **method_kw, + ): + from ..evoked import Evoked + + # instance type check + msg = f"AverageTFR got instance type {type(inst).__name__}" + assert isinstance(inst, (Evoked, dict)), msg + + super().__init__( + inst, + method, + freqs, + tmin=tmin, + tmax=tmax, + picks=picks, + proj=proj, + decim=decim, + n_jobs=n_jobs, + verbose=verbose, + **method_kw, + ) + + def _get_instance_data(self): + # prepend a singleton "epochs" axis + return self.inst.get_data(picks=self._picks)[np.newaxis, :, self._time_mask]