
# Author: Jaakko Leppakangas <jaeilepp@student.jyu.fi>
#         Joan Massich <mailsik@gmail.com>
#
# License: BSD-3-Clause

import os.path as op

import numpy as np
from numpy.testing import assert_array_equal
import pytest

from mne import pick_types
from mne.datasets import testing
from mne.io.tests.test_raw import _test_raw_reader
from mne.io.cnt import read_raw_cnt
from mne.annotations import read_annotations

data_path = testing.data_path(download=False)
fname = op.join(data_path, 'CNT', 'scan41_short.cnt')


@testing.requires_testing_data
def test_data():
    """Test reading raw cnt files."""
    with pytest.warns(RuntimeWarning, match='number of bytes'):
        raw = _test_raw_reader(read_raw_cnt, input_fname=fname,
                               eog='auto', misc=['NA1', 'LEFT_EAR'])

    # make sure we use annotations event if we synthesized stim
    assert len(raw.annotations) == 6

    eog_chs = pick_types(raw.info, eog=True, exclude=[])
    assert len(eog_chs) == 2  # test eog='auto'
    assert raw.info['bads'] == ['LEFT_EAR', 'VEOGR']  # test bads

    # the data has "05/10/200 17:35:31" so it is set to None
    assert raw.info['meas_date'] is None


@testing.requires_testing_data
def test_compare_events_and_annotations():
    """Test comparing annotations and events."""
    with pytest.warns(RuntimeWarning, match='Could not parse meas date'):
        raw = read_raw_cnt(fname)
    events = np.array([[333, 0, 7],
                       [1010, 0, 7],
                       [1664, 0, 109],
                       [2324, 0, 7],
                       [2984, 0, 109]])

    annot = read_annotations(fname)
    assert len(annot) == 6
    assert_array_equal(annot.onset[:-1], events[:, 0] / raw.info['sfreq'])
    assert 'STI 014' not in raw.info['ch_names']

[((432, 465), 'mne.datasets.testing.data_path', 'testing.data_path', ([], {'download': '(False)'}), '(download=False)\n', (449, 465), False, 'from mne.datasets import testing\n'), ((474, 519), 'os.path.join', 'op.join', (['data_path', '"""CNT"""', '"""scan41_short.cnt"""'], {}), "(data_path, 'CNT', 'scan41_short.cnt')\n", (481, 519), True, 'import os.path as op\n'), ((922, 964), 'mne.pick_types', 'pick_types', (['raw.info'], {'eog': '(True)', 'exclude': '[]'}), '(raw.info, eog=True, exclude=[])\n', (932, 964), False, 'from mne import pick_types\n'), ((1428, 1515), 'numpy.array', 'np.array', (['[[333, 0, 7], [1010, 0, 7], [1664, 0, 109], [2324, 0, 7], [2984, 0, 109]]'], {}), '([[333, 0, 7], [1010, 0, 7], [1664, 0, 109], [2324, 0, 7], [2984, 0,\n    109]])\n', (1436, 1515), True, 'import numpy as np\n'), ((1617, 1640), 'mne.annotations.read_annotations', 'read_annotations', (['fname'], {}), '(fname)\n', (1633, 1640), False, 'from mne.annotations import read_annotations\n'), ((1672, 1744), 'numpy.testing.assert_array_equal', 'assert_array_equal', (['annot.onset[:-1]', "(events[:, (0)] / raw.info['sfreq'])"], {}), "(annot.onset[:-1], events[:, (0)] / raw.info['sfreq'])\n", (1690, 1744), False, 'from numpy.testing import assert_array_equal\n'), ((617, 670), 'pytest.warns', 'pytest.warns', (['RuntimeWarning'], {'match': '"""number of bytes"""'}), "(RuntimeWarning, match='number of bytes')\n", (629, 670), False, 'import pytest\n'), ((686, 777), 'mne.io.tests.test_raw._test_raw_reader', '_test_raw_reader', (['read_raw_cnt'], {'input_fname': 'fname', 'eog': '"""auto"""', 'misc': "['NA1', 'LEFT_EAR']"}), "(read_raw_cnt, input_fname=fname, eog='auto', misc=['NA1',\n    'LEFT_EAR'])\n", (702, 777), False, 'from mne.io.tests.test_raw import _test_raw_reader\n'), ((1316, 1379), 'pytest.warns', 'pytest.warns', (['RuntimeWarning'], {'match': '"""Could not parse meas date"""'}), "(RuntimeWarning, match='Could not parse meas date')\n", (1328, 1379), False, 'import pytest\n'), ((1395, 1414), 'mne.io.cnt.read_raw_cnt', 'read_raw_cnt', (['fname'], {}), '(fname)\n', (1407, 1414), False, 'from mne.io.cnt import read_raw_cnt\n')]