# Path: ephypype/interfaces/mne/spectral.py
# class TFRmorlet(BaseInterface):
#     """Compute Time-Frequency Representation (TFR) using Morlet wavelets on
#     epoched data.
# 
#     Inputs
#     ------
#     epo_file : str
#         Path of .fif file with epoched data
# 
#     freqs : list
#         frequencies in Hz
# 
#     n_cycles : int
#         the number of cycles globally or for each frequency
# 
#     Outputs
#     -------
#     power_file : str
#         Name of .npy file with average power
#     """
# 
#     input_spec = TFRmorletInputSpec
#     output_spec = TFRmorletOutputSpec
# 
#     def __init__(self):
#         BaseInterface.__init__(self)
#         self.power_file = []
# 
#     def _run_interface(self, runtime):
#         """Run interface."""
#         print('in PlotSpectralConn')
# 
#         # reading matrix and base filename from conmat_file
#         _, fname, _ = split_f(self.inputs.epo_file)
#         print(fname)
# 
#         if len(self.inputs.n_cycles) == 0:
#             n_cycles = self.inputs.freqs / 2.
#         else:
#             n_cycles = self.inputs.n_cycles
#         self.power_file = \
#             _compute_tfr_morlet(
#                     self.inputs.epo_file, self.inputs.freqs, n_cycles)  # noqa
# 
#         return runtime
# 
#     def _list_outputs(self):
#         outputs = self._outputs().get()
#         outputs["power_file"] = self.power_file
#         return outputs


import mne
import pytest
import os.path as op
import numpy as np
import nipype.pipeline.engine as pe
import matplotlib
from ephypype.interfaces.mne.spectral import TFRmorlet

data_path = mne.datasets.testing.data_path()
raw_fname = op.join(data_path, 'MEG', 'sample',
                    'sample_audvis_trunc_raw.fif')
events_file = op.join(data_path, 'MEG', 'sample',
                      'sample_audvis_trunc_raw-eve.fif')


@pytest.mark.usefixtures("change_wd")
def test_tfr():
    """Test time-frequency transform (PSD and ITC)."""

    # create epoched data
    raw = mne.io.read_raw_fif(raw_fname)
    picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False,
                           stim=False, exclude='bads')
    # Define and read epochs:
    events = mne.find_events(raw, stim_channel='STI 014')
    # Define epochs parameters:
    event_id = dict(aud_l=1, aud_r=2)  # event trigger and conditions
    tmin = -0.2  # start of each epoch (200ms before the trigger)
    tmax = 0.5  # end of each epoch (500ms after the trigger)
    epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
                        picks=picks, baseline=(None, 0), preload=False)
    # Save all epochs in a fif file:
    epo_fname = raw_fname.replace('.fif', '-epo.fif')
    epochs.save(epo_fname, overwrite=True)

    # Test
    freqs = np.arange(6, 20, 5)

# Path: ephypype/interfaces/mne/spectral.py
# class TFRmorlet(BaseInterface):
#     """Compute Time-Frequency Representation (TFR) using Morlet wavelets on
#     epoched data.
# 
#     Inputs
#     ------
#     epo_file : str
#         Path of .fif file with epoched data
# 
#     freqs : list
#         frequencies in Hz
# 
#     n_cycles : int
#         the number of cycles globally or for each frequency
# 
#     Outputs
#     -------
#     power_file : str
#         Name of .npy file with average power
#     """
# 
#     input_spec = TFRmorletInputSpec
#     output_spec = TFRmorletOutputSpec
# 
#     def __init__(self):
#         BaseInterface.__init__(self)
#         self.power_file = []
# 
#     def _run_interface(self, runtime):
#         """Run interface."""
#         print('in PlotSpectralConn')
# 
#         # reading matrix and base filename from conmat_file
#         _, fname, _ = split_f(self.inputs.epo_file)
#         print(fname)
# 
#         if len(self.inputs.n_cycles) == 0:
#             n_cycles = self.inputs.freqs / 2.
#         else:
#             n_cycles = self.inputs.n_cycles
#         self.power_file = \
#             _compute_tfr_morlet(
#                     self.inputs.epo_file, self.inputs.freqs, n_cycles)  # noqa
# 
#         return runtime
# 
#     def _list_outputs(self):
#         outputs = self._outputs().get()
#         outputs["power_file"] = self.power_file
#         return outputs


# Path: ephypype/interfaces/mne/tests/test_spectral_IF.py
import mne
import pytest
import os.path as op
import numpy as np
import nipype.pipeline.engine as pe
import matplotlib
from ephypype.interfaces.mne.spectral import TFRmorlet


data_path = mne.datasets.testing.data_path()
raw_fname = op.join(data_path, 'MEG', 'sample',
                    'sample_audvis_trunc_raw.fif')
events_file = op.join(data_path, 'MEG', 'sample',
                      'sample_audvis_trunc_raw-eve.fif')


@pytest.mark.usefixtures("change_wd")
def test_tfr():
    """Test time-frequency transform (PSD and ITC)."""

    # create epoched data
    raw = mne.io.read_raw_fif(raw_fname)
    picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False,
                           stim=False, exclude='bads')
    # Define and read epochs:
    events = mne.find_events(raw, stim_channel='STI 014')
    # Define epochs parameters:
    event_id = dict(aud_l=1, aud_r=2)  # event trigger and conditions
    tmin = -0.2  # start of each epoch (200ms before the trigger)
    tmax = 0.5  # end of each epoch (500ms after the trigger)
    epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
                        picks=picks, baseline=(None, 0), preload=False)
    # Save all epochs in a fif file:
    epo_fname = raw_fname.replace('.fif', '-epo.fif')
    epochs.save(epo_fname, overwrite=True)

    # Test
    freqs = np.arange(6, 20, 5)

    compute_tfr_morlet = pe.Node(interface=TFRmorlet(), name='tfr_morlet')