Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some tests #1

Merged
merged 2 commits into from
Jun 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 65 additions & 63 deletions mne/io/curry/tests/test_curry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: UTF-8 -*-
# -*- coding: utf-8 -*-
#
# Authors: Dirk Gütlin <dirk.guetlin@stud.sbg.ac.at>
#
#
# License: BSD (3-clause)

import os
import os.path as op
import numpy as np

import pytest

from numpy.testing import assert_allclose, assert_array_equal
from mne.annotations import events_from_annotations
from mne.datasets import testing
Expand All @@ -23,9 +25,9 @@
data_dir = testing.data_path(download=False)
curry_dir = op.join(data_dir, "curry")

bdf_file = op.join(data_dir, 'BDF/test_bdf_stim_channel.bdf')
bdf_file = op.join(data_dir, 'BDF', 'test_bdf_stim_channel.bdf')

bti_rfDC_file = op.join(data_dir, 'BTi/erm_HFH/c,rfDC')
bti_rfDC_file = op.join(data_dir, 'BTi', 'erm_HFH', 'c,rfDC')

curry7_rfDC_file = op.join(curry_dir, "c,rfDC Curry 7.dat")
curry8_rfDC_file = op.join(curry_dir, "c,rfDC Curry 8.cdt")
Expand All @@ -46,83 +48,83 @@
do_warn = ''


@pytest.fixture(scope='session')
def bdf_curry_ref():
"""Return a view of the reference bdf used to create the curry files."""
raw = read_raw_bdf(bdf_file, preload=True).drop_channels(['Status'])
return raw


@pytest.mark.filterwarnings(do_warn)
@testing.requires_testing_data
def test_read_raw_curry():
@pytest.mark.parametrize('fname', [
pytest.param(curry7_bdf_file, id='curry 7'),
pytest.param(curry8_bdf_file, id='curry 8'),
])
@pytest.mark.parametrize('preload', [True, False])
def test_read_raw_curry(fname, preload, bdf_curry_ref):
"""Test reading CURRY files."""
# make sure all types of files can be passed to read_raw_curry
for filename in os.listdir(curry_dir):
if "test_bdf_stim_channel Curry" in filename:
read_raw_curry(op.join(curry_dir, filename))
raw = read_raw_curry(fname, preload=preload)

if preload:
isinstance(getattr(raw, '_data'), np.ndarray)
else:
with pytest.raises(AttributeError, match="no attribute '_data'"):
getattr(raw, '_data')

assert raw.n_times == bdf_curry_ref.n_times
assert raw.info['sfreq'] == bdf_curry_ref.info['sfreq']

for field in ['kind', 'ch_name']:
assert_array_equal([ch[field] for ch in raw.info['chs']],
[ch[field] for ch in bdf_curry_ref.info['chs']])

# check no preloading for preload=False
with pytest.raises(AttributeError, match="no attribute '_data'"):
read_raw_curry(curry7_bdf_file, preload=False)._data
read_raw_curry(curry7_bdf_ascii_file, preload=False)._data
assert_allclose(raw.get_data(), bdf_curry_ref.get_data())

picks, start, stop = ["C3", "C4"], 200, 800
assert_allclose(
raw.get_data(picks=picks, start=start, stop=stop),
bdf_curry_ref.get_data(picks=picks, start=start, stop=stop),
)


@pytest.mark.filterwarnings(do_warn)
@testing.requires_testing_data
@pytest.mark.parametrize('fname,tol', [
pytest.param(curry7_rfDC_file, 1e-6, id='curry 7'),
pytest.param(curry8_rfDC_file, 1e-3, id='curry 8'), # XXX: why 1e-3?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some values that turned out slightly different, maybe due to some floating point + rounding stuff? The data from these two files is read exactly the same, so i'd say this is the best we'd get of it...

])
def test_read_raw_curry_rfDC(fname, tol):
"""Test reading CURRY files."""
# check data
bti_rfDC = read_raw_bti(pdf_fname=bti_rfDC_file, head_shape_fname=None)
curry7_rfDC = read_raw_curry(curry7_rfDC_file)
curry8_rfDC = read_raw_curry(curry8_rfDC_file)
raw = read_raw_curry(fname)

# test on the eeg chans, since these were not renamed by curry
eeg_names = [ch["ch_name"] for ch in curry7_rfDC.info["chs"]
eeg_names = [ch["ch_name"] for ch in raw.info["chs"]
if ch["kind"] == FIFF.FIFFV_EEG_CH]

assert_allclose(curry7_rfDC.get_data(eeg_names),
bti_rfDC.get_data(eeg_names), rtol=1e-6)
assert_allclose(curry8_rfDC.get_data(eeg_names),
bti_rfDC.get_data(eeg_names), rtol=1e-3)

bdf = read_raw_bdf(bdf_file)
curry7_bdf = read_raw_curry(curry7_bdf_file, preload=True)
curry8_bdf = read_raw_curry(curry8_bdf_file, preload=False)
curry7_bdf_ascii = read_raw_curry(curry7_bdf_ascii_file,
preload=False)
curry8_bdf_ascii = read_raw_curry(curry7_bdf_ascii_file,
preload=True)

assert_allclose([curry7_bdf.n_times, curry7_bdf_ascii.n_times,
curry8_bdf.n_times, curry8_bdf_ascii.n_times],
bdf.n_times)
assert_allclose([curry7_bdf.info["sfreq"], curry7_bdf_ascii.info["sfreq"],
curry8_bdf.info["sfreq"], curry8_bdf_ascii.info["sfreq"]],
bdf.info["sfreq"])

# test all types of data
picks = ["C3", "C4"]
start, stop = 200, 800
for curry_file in [curry7_bdf, curry7_bdf_ascii,
curry8_bdf, curry8_bdf_ascii]:
# use chans [:3] here since curry stim chan was already extracted
assert_allclose([ch["kind"] for ch in curry_file.info["chs"]],
[ch["kind"] for ch in bdf.info["chs"][:3]])
assert_array_equal([ch["ch_name"] for ch in curry_file.info["chs"]],
[ch["ch_name"] for ch in bdf.info["chs"][:3]])
assert_allclose(curry_file.get_data(), bdf.get_data()[:3], atol=1e-6)
# can't use bdf.get_data(picks) here, since it seems bugged
assert_allclose(curry_file.get_data(picks, start, stop),
bdf.get_data(start=start, stop=stop)[:2], atol=1e-6)
assert_allclose(raw.get_data(eeg_names),
bti_rfDC.get_data(eeg_names), rtol=tol)


@testing.requires_testing_data
def test_read_events_curry():
"""Test reading curry event files."""
events = _read_events_curry(event_file, event_ids=[1, 2, 4])
bdf = read_raw_bdf(bdf_file, preload=True)
ref_events = find_events(bdf, stim_channel="Status")

assert_allclose(events, ref_events)
@pytest.mark.parametrize('fname', [
pytest.param(curry7_bdf_file, id='curry 7'),
pytest.param(curry8_bdf_file, id='curry 8'),
])
def test_read_events_curry_are_same_as_bdf(fname):
"""Test events from curry annotations recovers the right events."""
EVENT_ID = {str(ii): ii for ii in range(5)}
REF_EVENTS = find_events(read_raw_bdf(bdf_file, preload=True))

raw = read_raw_curry(curry7_bdf_file)
annot_events, _ = events_from_annotations(raw, event_id={"1": 1,
"2": 2,
"4": 4})
assert_allclose(annot_events, ref_events)
raw = read_raw_curry(fname)
events, _ = events_from_annotations(raw, event_id=EVENT_ID)
assert_allclose(events, REF_EVENTS)


def test_check_missing_files():
"""Test checking for missing curry files."""
"""Test checking for missing curry files (smoke test)."""
invalid_fname = "/invalid/path/name"

with pytest.raises(IOError, match="file type .*? must end with"):
Expand Down