Skip to content

Commit

Permalink
TEST: use fixture and parametrize in BrainVision date tests (#6623)
Browse files Browse the repository at this point in the history
  • Loading branch information
sappelhoff authored and massich committed Aug 5, 2019
1 parent 9351930 commit 07e605f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 235 deletions.
142 changes: 0 additions & 142 deletions mne/io/brainvision/tests/data/test_bad_date.vhdr

This file was deleted.

25 changes: 0 additions & 25 deletions mne/io/brainvision/tests/data/test_bad_date.vmrk

This file was deleted.

127 changes: 59 additions & 68 deletions mne/io/brainvision/tests/test_brainvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Stefan Appelhoff <stefan.appelhoff@mailbox.org>
#
# License: BSD (3-clause)

import os.path as op
from os import unlink
import shutil as sh
Expand Down Expand Up @@ -77,82 +76,74 @@ def test_orig_units(recwarn):
assert orig_units['ReRef'] == 'C'


def test_vmrk_meas_date():
"""Test successful extraction of measurement date."""
# Test file that does have a specific date
raw = read_raw_brainvision(vhdr_path)
assert_allclose(raw.info['meas_date'], [1384359243, 794231])
assert '2013-11-13 16:14:03 GMT' in repr(raw.info)
DATE_TEST_CASES = np.array([
('Mk1=New Segment,,1,1,0,20131113161403794232\n', # content
[1384359243, 794231], # meas_date internal representation
'2013-11-13 16:14:03 GMT'), # meas_date representation

# Test file with multiple dates ... we should only take the first
with pytest.warns(RuntimeWarning, match='software filter'):
raw = read_raw_brainvision(vhdr_old_path)
assert_allclose(raw.info['meas_date'], [1184588560, 937453])
assert '2007-07-16 12:22:40 GMT' in repr(raw.info)
(('Mk1=New Segment,,1,1,0,20070716122240937454\n'
'Mk2=New Segment,,2,1,0,20070716122240937455\n'),
[1184588560, 937453],
'2007-07-16 12:22:40 GMT'),

# Test files with no date, we should get DATE_NONE from mne.io.write
with pytest.warns(RuntimeWarning, match='coordinate information'):
raw = read_raw_brainvision(vhdr_v2_path)
assert raw.info['meas_date'] is None
assert 'unspecified' in repr(raw.info)
('Mk1=New Segment,,1,1,0,\nMk2=New Segment,,2,1,0,20070716122240937454\n',
[1184588560, 937453],
'2007-07-16 12:22:40 GMT'),

# Test files with faulty dates introduced by segmenting a file without
# date information. Should not raise a strptime ValueError
raw = read_raw_brainvision(vhdr_bad_date)
assert raw.info['meas_date'] is None
assert 'unspecified' in repr(raw.info)

# Reuse our vmrk to test several other cases with dates
tmpdir = _TempDir()
tmp_vhdr_file = op.join(tmpdir, op.basename(vhdr_bad_date))
tmp_vmrk_file = tmp_vhdr_file.replace('.vhdr', '.vmrk')
tmp_eeg_file = op.join(tmpdir, op.basename(eeg_path))
sh.copyfile(vhdr_bad_date, tmp_vhdr_file)
sh.copyfile(vhdr_bad_date.replace('.vhdr', '.vmrk'), tmp_vmrk_file)
sh.copyfile(eeg_path, tmp_eeg_file)

# We'll exclusively manipulate the "New Segment" line in vmrk
with open(tmp_vmrk_file, 'r') as fin:
('Mk1=STATUS,,1,1,0\n', None, 'unspecified'),
('Mk1=New Segment,,1,1,0,\n', None, 'unspecified'),
('Mk1=New Segment,,1,1,0\n', None, 'unspecified'),
('Mk1=New Segment,,1,1,0,00000000000304125000', None, 'unspecified'),

], dtype=np.dtype({
'names': ['content', 'meas_date', 'meas_date_repr'],
'formats': [object, object, 'U22']
}))


@pytest.fixture(scope='session')
def _mocked_meas_date_data(tmpdir_factory):
"""Prepare files for mocked_meas_date_file fixture."""
# Prepare the files
tmpdir = str(tmpdir_factory.mktemp('brainvision_mocked_meas_date'))
vhdr_fname, vmrk_fname, eeg_fname = [
op.join(tmpdir, op.basename(ff))
for ff in [vhdr_path, vmrk_path, eeg_path]
]
for orig, dest in zip([vhdr_path, eeg_path], [vhdr_fname, eeg_fname]):
sh.copyfile(orig, dest)

# Get the marker information
with open(vmrk_path, 'r') as fin:
lines = fin.readlines()
idx = lines.index('Mk1=New Segment,,1,1,0,00000000000304125000\n')

# Now perform some tests
# Test that we get no error for trying to extract meas_date if there is
# no marker of type "New Segment" in the data
lines[idx] = 'Mk1=STATUS,,1,1,0\n'
with open(tmp_vmrk_file, 'w') as fout:
fout.writelines(lines)
raw = read_raw_brainvision(tmp_vhdr_file)
assert raw.info['meas_date'] is None
assert 'unspecified' in repr(raw.info)
return vhdr_fname, vmrk_fname, lines

# Test that we extract no date, if "New Segment", but no date specified
# Note the trailing comma but missing data following that comma
lines[idx] = 'Mk1=New Segment,,1,1,0,\n'
with open(tmp_vmrk_file, 'w') as fout:
fout.writelines(lines)
raw = read_raw_brainvision(tmp_vhdr_file)
assert raw.info['meas_date'] is None
assert 'unspecified' in repr(raw.info)

# Test that no error if "New Segment", but no date specified and no
# trailing comma
lines[idx] = 'Mk1=New Segment,,1,1,0\n'
with open(tmp_vmrk_file, 'w') as fout:
fout.writelines(lines)
raw = read_raw_brainvision(tmp_vhdr_file)
assert raw.info['meas_date'] is None
assert 'unspecified' in repr(raw.info)
@pytest.fixture(scope='session', params=[tt for tt in DATE_TEST_CASES])
def mocked_meas_date_file(_mocked_meas_date_data, request):
"""Prepare a generator for use in test_meas_date."""
MEAS_DATE_LINE = 11 # see test.vmrk file
vhdr_fname, vmrk_fname, lines = _mocked_meas_date_data

# Test that a fine date gets recognized, use 2000-01-01:12:00:00, assuming
# UTC timezone, which would be in unix time: 946728000
lines[idx] = 'Mk1=New Segment,,1,1,0,20000101120000000000\n'
with open(tmp_vmrk_file, 'w') as fout:
lines[MEAS_DATE_LINE] = request.param['content']
with open(vmrk_fname, 'w') as fout:
fout.writelines(lines)
raw = read_raw_brainvision(tmp_vhdr_file)
assert raw.info['meas_date'] is not None
assert raw.info['meas_date'][1] == 0 # no microseconds
assert raw.info['meas_date'][0] == 946728000

yield (
vhdr_fname, request.param['meas_date'], request.param['meas_date_repr']
)


def test_meas_date(mocked_meas_date_file):
"""Test successful extraction of measurement date."""
vhdr_f, expected_meas, expected_meas_repr = mocked_meas_date_file
raw = read_raw_brainvision(vhdr_f)
assert expected_meas_repr in repr(raw.info)
if expected_meas is None:
assert raw.info['meas_date'] is None
else:
assert_allclose(raw.info['meas_date'], expected_meas)


def test_vhdr_codepage_ansi():
Expand Down

0 comments on commit 07e605f

Please sign in to comment.