# Path: ephypype/interfaces/mne/power.py
# class Power(BaseInterface):
#     """Compute power spectral density on epochs or raw data.
# 
#     Inputs
#     ------
#         data_file : str
#             Filename of the data
#         fmin : float
#             Lower psd frequency
#         fmax : float
#             Higher psd frequency
#         sfreq : float
#             Sampling frequency
#         nfft : int
#             The length of FFT used
#         overlap : float
#             Number of points of overlap between segments
#         method : str
#             Power spectral density computation method. Possible values are
#             'welch', 'multitaper'
#         is_epoched : bool
#             If true input data are mne.Epochs
#         is_sensor_space : bool
#             True for PSD on sensor space, False for PSD on source
# 
#     Outputs
#     -------
#         psds_file : str
#             Name of the .npz file containing psd tensor and frequencies
#     """
# 
#     input_spec = PowerInputSpec
#     output_spec = PowerOutputSpec
# 
#     def _run_interface(self, runtime):
#         data_file = self.inputs.data_file
#         sfreq = self.inputs.sfreq
#         fmin = self.inputs.fmin
#         fmax = self.inputs.fmax
#         nfft = self.inputs.nfft
#         overlap = self.inputs.overlap
#         method = self.inputs.method
#         is_epoched = self.inputs.is_epoched
#         is_sensor_space = self.inputs.is_sensor_space
#         if is_sensor_space:
#             self.psds_file = _compute_and_save_psd(data_file, fmin, fmax,
#                                                    method, is_epoched)
#         else:
#             self.psds_file = _compute_and_save_src_psd(data_file, sfreq,
#                                                        fmin=fmin, fmax=fmax,
#                                                        n_fft=nfft,
#                                                        n_overlap=overlap,
#                                                        is_epoched=is_epoched)
#         return runtime
# 
#     def _list_outputs(self):
#         outputs = self._outputs().get()
#         outputs['psds_file'] = self.psds_file
#         return outputs
# 
# Path: ephypype/nodes/power_tools.py
# class PowerBand(BaseInterface):
#     """Compute mean power spectral density for each frequency band.
# 
#     Then, it's save in a  numpy file .npy
# 
#     Parameters
#     ----------
#     psds_file
#         type = File, exists=True, desc='psd tensor and frequencies in .npz
#                format', mandatory=True
#     freq_bands
#         type = List of Float, desc='frequency bands', mandatory=True
# 
#     Returns
#     -------
#     mean_power_band_file
#         type = File, exists=True, desc="mean psd in bands in .npy format"
#     """
# 
#     input_spec = PowerBandInputSpec
#     output_spec = PowerBandOutputSpec
# 
#     def _run_interface(self, runtime):
# 
#         psds_file = self.inputs.psds_file
#         freq_bands = self.inputs.freq_bands
# 
#         self.mean_power_band_file = _compute_mean_band_psd(psds_file,
#                                                            freq_bands)
# 
#         return runtime
# 
#     def _list_outputs(self):
# 
#         outputs = self._outputs().get()
#         outputs['mean_power_band_file'] = self.mean_power_band_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.power import Power
from ephypype.nodes.power_tools import PowerBand
"""Test power."""


matplotlib.use('Agg')  # for testing don't use X server

data_path = mne.datasets.testing.data_path()
sbj = 'sample'
raw_fname = op.join(data_path, 'MEG', sbj, 'sample_audvis_trunc_raw.fif')
stc_fname = op.join(data_path, 'MEG', sbj, 'sample_audvis_trunc-meg-lh.stc')


@pytest.mark.usefixtures("change_wd")
def test_power_welch():
    """Test computing and saving PSD."""
    fmin = 0.1
    fmax = 40


# Path: ephypype/interfaces/mne/power.py
# class Power(BaseInterface):
#     """Compute power spectral density on epochs or raw data.
# 
#     Inputs
#     ------
#         data_file : str
#             Filename of the data
#         fmin : float
#             Lower psd frequency
#         fmax : float
#             Higher psd frequency
#         sfreq : float
#             Sampling frequency
#         nfft : int
#             The length of FFT used
#         overlap : float
#             Number of points of overlap between segments
#         method : str
#             Power spectral density computation method. Possible values are
#             'welch', 'multitaper'
#         is_epoched : bool
#             If true input data are mne.Epochs
#         is_sensor_space : bool
#             True for PSD on sensor space, False for PSD on source
# 
#     Outputs
#     -------
#         psds_file : str
#             Name of the .npz file containing psd tensor and frequencies
#     """
# 
#     input_spec = PowerInputSpec
#     output_spec = PowerOutputSpec
# 
#     def _run_interface(self, runtime):
#         data_file = self.inputs.data_file
#         sfreq = self.inputs.sfreq
#         fmin = self.inputs.fmin
#         fmax = self.inputs.fmax
#         nfft = self.inputs.nfft
#         overlap = self.inputs.overlap
#         method = self.inputs.method
#         is_epoched = self.inputs.is_epoched
#         is_sensor_space = self.inputs.is_sensor_space
#         if is_sensor_space:
#             self.psds_file = _compute_and_save_psd(data_file, fmin, fmax,
#                                                    method, is_epoched)
#         else:
#             self.psds_file = _compute_and_save_src_psd(data_file, sfreq,
#                                                        fmin=fmin, fmax=fmax,
#                                                        n_fft=nfft,
#                                                        n_overlap=overlap,
#                                                        is_epoched=is_epoched)
#         return runtime
# 
#     def _list_outputs(self):
#         outputs = self._outputs().get()
#         outputs['psds_file'] = self.psds_file
#         return outputs
# 
# Path: ephypype/nodes/power_tools.py
# class PowerBand(BaseInterface):
#     """Compute mean power spectral density for each frequency band.
# 
#     Then, it's save in a  numpy file .npy
# 
#     Parameters
#     ----------
#     psds_file
#         type = File, exists=True, desc='psd tensor and frequencies in .npz
#                format', mandatory=True
#     freq_bands
#         type = List of Float, desc='frequency bands', mandatory=True
# 
#     Returns
#     -------
#     mean_power_band_file
#         type = File, exists=True, desc="mean psd in bands in .npy format"
#     """
# 
#     input_spec = PowerBandInputSpec
#     output_spec = PowerBandOutputSpec
# 
#     def _run_interface(self, runtime):
# 
#         psds_file = self.inputs.psds_file
#         freq_bands = self.inputs.freq_bands
# 
#         self.mean_power_band_file = _compute_mean_band_psd(psds_file,
#                                                            freq_bands)
# 
#         return runtime
# 
#     def _list_outputs(self):
# 
#         outputs = self._outputs().get()
#         outputs['mean_power_band_file'] = self.mean_power_band_file
# 
#         return outputs


# Path: ephypype/interfaces/mne/tests/test_power.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.power import Power
from ephypype.nodes.power_tools import PowerBand

"""Test power."""


matplotlib.use('Agg')  # for testing don't use X server

data_path = mne.datasets.testing.data_path()
sbj = 'sample'
raw_fname = op.join(data_path, 'MEG', sbj, 'sample_audvis_trunc_raw.fif')
stc_fname = op.join(data_path, 'MEG', sbj, 'sample_audvis_trunc-meg-lh.stc')


@pytest.mark.usefixtures("change_wd")
def test_power_welch():
    """Test computing and saving PSD."""
    fmin = 0.1
    fmax = 40


    power_node = pe.Node(interface=Power(), name='welch_psd')