From 06d9052542833965369cd4db0125a98ccfacccf0 Mon Sep 17 00:00:00 2001 From: Joan Massich Date: Fri, 2 Aug 2019 17:58:57 +0200 Subject: [PATCH 1/4] UPS: this is not working. --- mne/io/brainvision/tests/test_brainvision.py | 93 ++++++++++++-------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/mne/io/brainvision/tests/test_brainvision.py b/mne/io/brainvision/tests/test_brainvision.py index fef44efbae5..85322973584 100644 --- a/mne/io/brainvision/tests/test_brainvision.py +++ b/mne/io/brainvision/tests/test_brainvision.py @@ -77,50 +77,67 @@ def test_orig_units(recwarn): assert orig_units['ReRef'] == 'C' -@pytest.fixture() -def _get_date_test_data(tmpdir): +@pytest.fixture(scope='session') +def mocked_meas_date_file(tmpdir_factory): """Return vmrk text and index of New Segment line for date tests.""" - # Make a temporary copy of the test files - tmpdir = str(tmpdir) - for ff in [vhdr_path, vmrk_path, eeg_path]: - sh.copyfile(ff, op.join(tmpdir, op.basename(ff))) + 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 + + ('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'), + + ], dtype=np.dtype({ + 'names': ['content', 'meas_date', 'meas_date_repr'], + 'formats': [object, object, 'U22'] + })) + MEAS_DATE_LINE = 11 + + # 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 info and the line where the date is specified with open(vmrk_path, 'r') as fin: lines = fin.readlines() - idx = lines.index('Mk1=New Segment,,1,1,0,20131113161403794232\n') - - # Return header and marker file paths - tmp_vhdr_file = op.join(tmpdir, op.basename(vhdr_path)) - tmp_vmrk_file = op.join(tmpdir, op.basename(vmrk_path)) - - return tmp_vhdr_file, tmp_vmrk_file, lines, idx - - -@pytest.mark.parametrize('newstring, expected', [ - pytest.param('Mk1=New Segment,,1,1,0,20131113161403794232\n', '2013-11-13 16:14:03 GMT'), # noqa: E501 - pytest.param('Mk1=New Segment,,1,1,0,20070716122240937454\nMk2=New Segment,,2,1,0,20070716122240937455\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 - pytest.param('Mk1=STATUS,,1,1,0\n', 'unspecified'), - pytest.param('Mk1=New Segment,,1,1,0,\n', 'unspecified'), - pytest.param('Mk1=New Segment,,1,1,0\n', 'unspecified'), - pytest.param('Mk1=New Segment,,1,1,0,00000000000304125000', 'unspecified'), - pytest.param('Mk1=New Segment,,1,1,0,\nMk2=New Segment,,2,1,0,20070716122240937454\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 -]) -def test_dates(_get_date_test_data, newstring, expected): - """Test valid dates.""" - tmp_vhdr_file, tmp_vmrk_file, lines, idx = _get_date_test_data - - # Replace the New Segment - lines[idx] = newstring - - # Write it to tmp file - with open(tmp_vmrk_file, 'w') as fout: - fout.writelines(lines) - - # Read it back in and assert - raw = read_raw_brainvision(tmp_vhdr_file) - assert expected in repr(raw.info) + for current_test in TEST_CASES: + lines[MEAS_DATE_LINE] = current_test['content'] + with open(vmrk_fname, 'w') as fout: + fout.writelines(lines) + + yield dict(vhdr_fname=vhdr_fname, + vmrk_fname=vmrk_fname, + eeg_fname=eeg_fname, + expected_meas_date=current_test['meas_date'], + expected_meas_date_repr=current_test['meas_date_repr']) + + +def test_meas_date(mocked_meas_date_file): + """Test successful extraction of measurement date.""" + for kk, vv in mocked_meas_date_file.items(): + print(kk, vv) + # raw = read_raw_brainvision(mocked_meas_date_file['vhdr_fname']) + # assert_allclose(raw.info['meas_date'], + # mocked_meas_date_file['expected_meas_date']) + # assert mocked_meas_date_file['expected_meas_date_repr'] in repr(raw.info) + +# @pytest.mark.parametrize('newstring, expected', [ +# ('Mk1=New Segment,,1,1,0,20131113161403794232\n', '2013-11-13 16:14:03 GMT'), # noqa: E501 +# ('Mk1=New Segment,,1,1,0,20070716122240937454\nMk2=New Segment,,2,1,0,20070716122240937455\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 +# ('Mk1=STATUS,,1,1,0\n', 'unspecified'), +# ('Mk1=New Segment,,1,1,0,\n', 'unspecified'), +# ('Mk1=New Segment,,1,1,0\n', 'unspecified'), +# ('Mk1=New Segment,,1,1,0,00000000000304125000', 'unspecified'), +# ('Mk1=New Segment,,1,1,0,\nMk2=New Segment,,2,1,0,20070716122240937454\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 +# ]) def test_vhdr_codepage_ansi(): From cf69e87b36b36f2e167be397ce60ddfcb89bc010 Mon Sep 17 00:00:00 2001 From: Joan Massich Date: Fri, 2 Aug 2019 18:22:18 +0200 Subject: [PATCH 2/4] wip --- mne/io/brainvision/tests/test_brainvision.py | 36 +++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/mne/io/brainvision/tests/test_brainvision.py b/mne/io/brainvision/tests/test_brainvision.py index 85322973584..a75d0029eda 100644 --- a/mne/io/brainvision/tests/test_brainvision.py +++ b/mne/io/brainvision/tests/test_brainvision.py @@ -77,22 +77,23 @@ def test_orig_units(recwarn): assert orig_units['ReRef'] == 'C' +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 + + ('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'), + +], dtype=np.dtype({ + 'names': ['content', 'meas_date', 'meas_date_repr'], + 'formats': [object, object, 'U22'] +})) + @pytest.fixture(scope='session') def mocked_meas_date_file(tmpdir_factory): """Return vmrk text and index of New Segment line for date tests.""" - 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 - - ('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'), - - ], dtype=np.dtype({ - 'names': ['content', 'meas_date', 'meas_date_repr'], - 'formats': [object, object, 'U22'] - })) MEAS_DATE_LINE = 11 # Prepare the files @@ -119,6 +120,15 @@ def mocked_meas_date_file(tmpdir_factory): expected_meas_date=current_test['meas_date'], expected_meas_date_repr=current_test['meas_date_repr']) +@pytest.fixture(scope='session', params=[tt for tt in TEST_CASES]) +def foo(request): + param = request.param + yield param + +def test_foo(foo): + # import pdb; pdb.set_trace() + print(foo) + pass def test_meas_date(mocked_meas_date_file): """Test successful extraction of measurement date.""" From 5ab749e0b296d74eba3616eab02787e859ba30fa Mon Sep 17 00:00:00 2001 From: Joan Massich Date: Fri, 2 Aug 2019 18:43:45 +0200 Subject: [PATCH 3/4] fix --- mne/io/brainvision/tests/test_brainvision.py | 51 ++++++++++---------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/mne/io/brainvision/tests/test_brainvision.py b/mne/io/brainvision/tests/test_brainvision.py index a75d0029eda..570f5c67e1e 100644 --- a/mne/io/brainvision/tests/test_brainvision.py +++ b/mne/io/brainvision/tests/test_brainvision.py @@ -79,8 +79,8 @@ def test_orig_units(recwarn): 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 + [1384359243, 794231], # meas_date internal representation + '2013-11-13 16:14:03 GMT'), # meas_date representation ('Mk1=STATUS,,1,1,0\n', None, 'unspecified'), ('Mk1=New Segment,,1,1,0,\n', None, 'unspecified'), @@ -92,9 +92,8 @@ def test_orig_units(recwarn): })) @pytest.fixture(scope='session') -def mocked_meas_date_file(tmpdir_factory): - """Return vmrk text and index of New Segment line for date tests.""" - MEAS_DATE_LINE = 11 +def _mocked_meas_date_data(tmpdir_factory): + """Helper funct. preparing the files for mocked_meas_date_file fixture.""" # Prepare the files tmpdir = str(tmpdir_factory.mktemp('brainvision_mocked_meas_date')) @@ -109,35 +108,37 @@ def mocked_meas_date_file(tmpdir_factory): with open(vmrk_path, 'r') as fin: lines = fin.readlines() - for current_test in TEST_CASES: - lines[MEAS_DATE_LINE] = current_test['content'] - with open(vmrk_fname, 'w') as fout: - fout.writelines(lines) + return {'vhdr_fname': vhdr_fname, 'vmrk_fname': vmrk_fname, + 'eeg_fname': eeg_fname, 'lines': lines} - yield dict(vhdr_fname=vhdr_fname, - vmrk_fname=vmrk_fname, - eeg_fname=eeg_fname, - expected_meas_date=current_test['meas_date'], - expected_meas_date_repr=current_test['meas_date_repr']) @pytest.fixture(scope='session', params=[tt for tt in TEST_CASES]) -def foo(request): - param = request.param - yield param +def mocked_meas_date_file(_mocked_meas_date_data, request): + """Return vmrk text and index of New Segment line for date tests.""" + MEAS_DATE_LINE = 11 + + vhdr_fname = _mocked_meas_date_data['vhdr_fname'] + vmrk_fname = _mocked_meas_date_data['vmrk_fname'] + eeg_fname = _mocked_meas_date_data['eeg_fname'] + lines = _mocked_meas_date_data['lines'] + + lines[MEAS_DATE_LINE] = request.param['content'] + with open(vmrk_fname, 'w') as fout: + fout.writelines(lines) + + yield dict(vhdr_fname=vhdr_fname, + vmrk_fname=vmrk_fname, + eeg_fname=eeg_fname, + expected_meas_date=request.param['meas_date'], + expected_meas_date_repr=request.param['meas_date_repr']) -def test_foo(foo): - # import pdb; pdb.set_trace() - print(foo) - pass def test_meas_date(mocked_meas_date_file): """Test successful extraction of measurement date.""" - for kk, vv in mocked_meas_date_file.items(): - print(kk, vv) - # raw = read_raw_brainvision(mocked_meas_date_file['vhdr_fname']) + raw = read_raw_brainvision(mocked_meas_date_file['vhdr_fname']) # assert_allclose(raw.info['meas_date'], # mocked_meas_date_file['expected_meas_date']) - # assert mocked_meas_date_file['expected_meas_date_repr'] in repr(raw.info) + assert mocked_meas_date_file['expected_meas_date_repr'] in repr(raw.info) # @pytest.mark.parametrize('newstring, expected', [ # ('Mk1=New Segment,,1,1,0,20131113161403794232\n', '2013-11-13 16:14:03 GMT'), # noqa: E501 From 7389349a7a2b8c2bd69ab4a403dd1fbec3d356f8 Mon Sep 17 00:00:00 2001 From: Joan Massich Date: Fri, 2 Aug 2019 18:49:28 +0200 Subject: [PATCH 4/4] FIX: Allow for testing meas_date == None --- mne/io/brainvision/tests/test_brainvision.py | 25 +++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mne/io/brainvision/tests/test_brainvision.py b/mne/io/brainvision/tests/test_brainvision.py index 570f5c67e1e..5a80f8722df 100644 --- a/mne/io/brainvision/tests/test_brainvision.py +++ b/mne/io/brainvision/tests/test_brainvision.py @@ -90,6 +90,15 @@ def test_orig_units(recwarn): 'names': ['content', 'meas_date', 'meas_date_repr'], 'formats': [object, object, 'U22'] })) +# @pytest.mark.parametrize('newstring, expected', [ +# ('Mk1=New Segment,,1,1,0,20131113161403794232\n', '2013-11-13 16:14:03 GMT'), # noqa: E501 +# ('Mk1=New Segment,,1,1,0,20070716122240937454\nMk2=New Segment,,2,1,0,20070716122240937455\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 +# ('Mk1=STATUS,,1,1,0\n', 'unspecified'), +# ('Mk1=New Segment,,1,1,0,\n', 'unspecified'), +# ('Mk1=New Segment,,1,1,0\n', 'unspecified'), +# ('Mk1=New Segment,,1,1,0,00000000000304125000', 'unspecified'), +# ('Mk1=New Segment,,1,1,0,\nMk2=New Segment,,2,1,0,20070716122240937454\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 +# ]) @pytest.fixture(scope='session') def _mocked_meas_date_data(tmpdir_factory): @@ -136,19 +145,13 @@ def mocked_meas_date_file(_mocked_meas_date_data, request): def test_meas_date(mocked_meas_date_file): """Test successful extraction of measurement date.""" raw = read_raw_brainvision(mocked_meas_date_file['vhdr_fname']) - # assert_allclose(raw.info['meas_date'], - # mocked_meas_date_file['expected_meas_date']) assert mocked_meas_date_file['expected_meas_date_repr'] in repr(raw.info) + if mocked_meas_date_file['expected_meas_date'] is None: + assert raw.info['meas_date'] is None + else: + assert_allclose(raw.info['meas_date'], + mocked_meas_date_file['expected_meas_date']) -# @pytest.mark.parametrize('newstring, expected', [ -# ('Mk1=New Segment,,1,1,0,20131113161403794232\n', '2013-11-13 16:14:03 GMT'), # noqa: E501 -# ('Mk1=New Segment,,1,1,0,20070716122240937454\nMk2=New Segment,,2,1,0,20070716122240937455\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 -# ('Mk1=STATUS,,1,1,0\n', 'unspecified'), -# ('Mk1=New Segment,,1,1,0,\n', 'unspecified'), -# ('Mk1=New Segment,,1,1,0\n', 'unspecified'), -# ('Mk1=New Segment,,1,1,0,00000000000304125000', 'unspecified'), -# ('Mk1=New Segment,,1,1,0,\nMk2=New Segment,,2,1,0,20070716122240937454\n', '2007-07-16 12:22:40 GMT'), # noqa: E501 -# ]) def test_vhdr_codepage_ansi():