# Path: ephypype/compute_inv_problem.py
# def compute_noise_cov(fname_template, raw_filename):
#     """
#     Compute noise covariance data from a continuous segment of raw data.
#     Employ empty room data (collected without the subject) to calculate
#     the full noise covariance matrix.
#     This is recommended for analyzing ongoing spontaneous activity.
# 
#     Inputs
#         cov_fname : str
#             noise covariance file name template
#         raw_filename : str
#             raw filename
# 
#     Output
#         cov_fname : str
#             noise covariance file name in which is saved the noise covariance
#             matrix
#     """
#     # Check if cov matrix exists
#     cov_fname = _get_cov_fname(fname_template)
# 
#     if not op.isfile(cov_fname):
#         er_raw, cov_fname = _get_er_data(fname_template)
# 
#         if not op.isfile(cov_fname) and er_raw:
#             reject = _create_reject_dict(er_raw.info)
#             picks = pick_types(er_raw.info, meg=True,
#                                ref_meg=False, exclude='bads')
# 
#             noise_cov = compute_raw_covariance(er_raw, picks=picks,
#                                                reject=reject)
# 
#             write_cov(cov_fname, noise_cov)
#         elif op.isfile(cov_fname):
#             print(('*** NOISE cov file {} exists!!!'.format(cov_fname)))
#         elif not er_raw:
#             cov_fname = compute_cov_identity(raw_filename)
# 
#     else:
#         print(('*** NOISE cov file {} exists!!!'.format(cov_fname)))
# 
#     return cov_fname
# 
# Path: ephypype/compute_inv_problem.py
# def compute_cov_identity(raw_filename):
#     "Compute Identity Noise Covariance matrix."
#     raw = read_raw_fif(raw_filename)
# 
#     data_path, basename, ext = split_f(raw_filename)
#     cov_fname = op.join(data_path, 'identity_noise-cov.fif')
# 
#     if not op.isfile(cov_fname):
#         picks = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads')
# 
#         ch_names = [raw.info['ch_names'][k] for k in picks]
#         bads = [b for b in raw.info['bads'] if b in ch_names]
#         noise_cov = mne.Covariance(np.identity(len(picks)), ch_names, bads,
#                                    raw.info['projs'], nfree=0)
# 
#         write_cov(cov_fname, noise_cov)
# 
#     return cov_fname


import mne
import numpy as np
import os.path as op
import matplotlib
from ephypype.compute_inv_problem import compute_noise_cov
from ephypype.compute_inv_problem import compute_cov_identity
"""Test compute_inv_problem."""


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

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


def test_compute_noise_cov():
    """Test compute noise covariance data from a continuous segment
    of raw data."""

    # read raw data
    raw = mne.io.read_raw_fif(raw_fname)

    # save short segment of raw data
    segment_raw_fname = raw_fname.replace('raw.fif', '0_10s_raw.fif')
    raw.save(segment_raw_fname, tmin=0, tmax=10, overwrite=True)


# Path: ephypype/compute_inv_problem.py
# def compute_noise_cov(fname_template, raw_filename):
#     """
#     Compute noise covariance data from a continuous segment of raw data.
#     Employ empty room data (collected without the subject) to calculate
#     the full noise covariance matrix.
#     This is recommended for analyzing ongoing spontaneous activity.
# 
#     Inputs
#         cov_fname : str
#             noise covariance file name template
#         raw_filename : str
#             raw filename
# 
#     Output
#         cov_fname : str
#             noise covariance file name in which is saved the noise covariance
#             matrix
#     """
#     # Check if cov matrix exists
#     cov_fname = _get_cov_fname(fname_template)
# 
#     if not op.isfile(cov_fname):
#         er_raw, cov_fname = _get_er_data(fname_template)
# 
#         if not op.isfile(cov_fname) and er_raw:
#             reject = _create_reject_dict(er_raw.info)
#             picks = pick_types(er_raw.info, meg=True,
#                                ref_meg=False, exclude='bads')
# 
#             noise_cov = compute_raw_covariance(er_raw, picks=picks,
#                                                reject=reject)
# 
#             write_cov(cov_fname, noise_cov)
#         elif op.isfile(cov_fname):
#             print(('*** NOISE cov file {} exists!!!'.format(cov_fname)))
#         elif not er_raw:
#             cov_fname = compute_cov_identity(raw_filename)
# 
#     else:
#         print(('*** NOISE cov file {} exists!!!'.format(cov_fname)))
# 
#     return cov_fname
# 
# Path: ephypype/compute_inv_problem.py
# def compute_cov_identity(raw_filename):
#     "Compute Identity Noise Covariance matrix."
#     raw = read_raw_fif(raw_filename)
# 
#     data_path, basename, ext = split_f(raw_filename)
#     cov_fname = op.join(data_path, 'identity_noise-cov.fif')
# 
#     if not op.isfile(cov_fname):
#         picks = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads')
# 
#         ch_names = [raw.info['ch_names'][k] for k in picks]
#         bads = [b for b in raw.info['bads'] if b in ch_names]
#         noise_cov = mne.Covariance(np.identity(len(picks)), ch_names, bads,
#                                    raw.info['projs'], nfree=0)
# 
#         write_cov(cov_fname, noise_cov)
# 
#     return cov_fname


# Path: ephypype/tests/test_compute_inv_problem.py
import mne
import numpy as np
import os.path as op
import matplotlib
from ephypype.compute_inv_problem import compute_noise_cov
from ephypype.compute_inv_problem import compute_cov_identity

"""Test compute_inv_problem."""


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

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


def test_compute_noise_cov():
    """Test compute noise covariance data from a continuous segment
    of raw data."""

    # read raw data
    raw = mne.io.read_raw_fif(raw_fname)

    # save short segment of raw data
    segment_raw_fname = raw_fname.replace('raw.fif', '0_10s_raw.fif')
    raw.save(segment_raw_fname, tmin=0, tmax=10, overwrite=True)


    noise_cov_fpath = compute_noise_cov('', segment_raw_fname)