|
| 1 | +# Authors: Robert Luke <[email protected]> |
| 2 | + |
| 3 | +# Alexandre Gramfort <[email protected]> |
| 4 | +# |
| 5 | +# License: BSD (3-clause) |
| 6 | + |
| 7 | +import os.path as op |
| 8 | + |
| 9 | +import pytest |
| 10 | +import numpy as np |
| 11 | +from numpy.testing import assert_allclose, assert_array_less |
| 12 | + |
| 13 | +from mne.datasets.testing import data_path |
| 14 | +from mne.io import read_raw_nirx |
| 15 | +from mne.preprocessing.nirs import optical_density, scalp_coupling_index |
| 16 | +from mne.datasets import testing |
| 17 | + |
| 18 | +fname_nirx_15_0 = op.join(data_path(download=False), |
| 19 | + 'NIRx', 'nirx_15_0_recording') |
| 20 | +fname_nirx_15_2 = op.join(data_path(download=False), |
| 21 | + 'NIRx', 'nirx_15_2_recording') |
| 22 | +fname_nirx_15_2_short = op.join(data_path(download=False), |
| 23 | + 'NIRx', 'nirx_15_2_recording_w_short') |
| 24 | + |
| 25 | + |
| 26 | +@testing.requires_testing_data |
| 27 | +@pytest.mark.parametrize('fname', ([fname_nirx_15_2_short, fname_nirx_15_2, |
| 28 | + fname_nirx_15_0])) |
| 29 | +@pytest.mark.parametrize('fmt', ('nirx', 'fif')) |
| 30 | +def test_scalp_coupling_index(fname, fmt, tmpdir): |
| 31 | + """Test converting NIRX files.""" |
| 32 | + assert fmt in ('nirx', 'fif') |
| 33 | + raw = read_raw_nirx(fname) |
| 34 | + raw = optical_density(raw) |
| 35 | + sci = scalp_coupling_index(raw) |
| 36 | + |
| 37 | + # All values should be between -1 and +1 |
| 38 | + assert_array_less(sci, 1.0) |
| 39 | + assert_array_less(sci * -1.0, 1.0) |
| 40 | + |
| 41 | + # Fill in some data with known correlation values |
| 42 | + new_data = np.random.rand(raw._data[0].shape[0]) |
| 43 | + # Set first two channels to perfect correlation |
| 44 | + raw._data[0] = new_data |
| 45 | + raw._data[1] = new_data |
| 46 | + # Set next two channels to perfect correlation |
| 47 | + raw._data[2] = new_data |
| 48 | + raw._data[3] = new_data * 0.3 # check scale invariance |
| 49 | + # Set next two channels to anti correlation |
| 50 | + raw._data[4] = new_data |
| 51 | + raw._data[5] = new_data * -1.0 |
| 52 | + # Set next two channels to be uncorrelated |
| 53 | + # TODO: this might be a bad idea as sometimes random noise might correlate |
| 54 | + raw._data[6] = new_data |
| 55 | + raw._data[7] = np.random.rand(raw._data[0].shape[0]) |
| 56 | + # Check values |
| 57 | + sci = scalp_coupling_index(raw) |
| 58 | + assert_allclose(sci[0:6], [1, 1, 1, 1, -1, -1], atol=0.01) |
| 59 | + assert np.abs(sci[6]) < 0.5 |
| 60 | + assert np.abs(sci[7]) < 0.5 |
0 commit comments