-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP: Add scalp coupling index for NIRS data #7215
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2acd037
Add scalp coupling index for NIRS data
rob-luke e806d50
Improve tests for scalp coupling index and referencing
rob-luke 90b149d
Add scalp coupling index to nirs tutorial
rob-luke 3334d86
Ensure SCI is run on optical density data
rob-luke fdc9659
Merge remote-tracking branch 'upstream/master' into fnirs_sci
rob-luke 11f5477
Fix flake errors in scalp coupling index code
rob-luke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Authors: Robert Luke <[email protected]> | ||
| # Eric Larson <[email protected]> | ||
| # Alexandre Gramfort <[email protected]> | ||
| # | ||
| # License: BSD (3-clause) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from ...io import BaseRaw | ||
| from ...utils import _validate_type, verbose | ||
| from ..nirs import _channel_frequencies, _check_channels_ordered | ||
| from ...filter import filter_data | ||
|
|
||
|
|
||
| @verbose | ||
| def scalp_coupling_index(raw, l_freq=0.7, h_freq=1.5, | ||
| l_trans_bandwidth=0.3, h_trans_bandwidth=0.3, | ||
| verbose=False): | ||
| r"""Calculate scalp coupling index. | ||
|
|
||
| This function calculates the scalp coupling index | ||
| :footcite:`pollonini2014auditory`. This is a measure of the quality of the | ||
| connection between the optode and the scalp. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raw : instance of Raw | ||
| The raw data. | ||
| %(l_freq)s | ||
| %(h_freq)s | ||
| %(l_trans_bandwidth)s | ||
| %(h_trans_bandwidth)s | ||
| %(verbose)s | ||
|
|
||
| Returns | ||
| ------- | ||
| sci : array of float | ||
| Array containing scalp coupling index for each channel. | ||
|
|
||
| References | ||
| ---------- | ||
| .. footbibliography:: | ||
| """ | ||
| raw = raw.copy().load_data() | ||
| _validate_type(raw, BaseRaw, 'fnirs_od') | ||
|
|
||
| freqs = np.unique(_channel_frequencies(raw)) | ||
| picks = _check_channels_ordered(raw, freqs) | ||
|
|
||
| filtered_data = filter_data(raw._data, raw.info['sfreq'], l_freq, h_freq, | ||
| picks=picks, verbose=verbose, | ||
| l_trans_bandwidth=h_trans_bandwidth, | ||
| h_trans_bandwidth=l_trans_bandwidth) | ||
|
|
||
| sci = np.zeros(picks.shape) | ||
| for ii in picks[::2]: | ||
| c = np.corrcoef(filtered_data[ii], filtered_data[ii + 1])[0][1] | ||
| sci[ii] = c | ||
| sci[ii + 1] = c | ||
|
|
||
| return sci | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Authors: Robert Luke <[email protected]> | ||
| # Eric Larson <[email protected]> | ||
| # Alexandre Gramfort <[email protected]> | ||
| # | ||
| # License: BSD (3-clause) | ||
|
|
||
| import os.path as op | ||
|
|
||
| import pytest | ||
| import numpy as np | ||
| from numpy.testing import assert_allclose, assert_array_less | ||
|
|
||
| from mne.datasets.testing import data_path | ||
| from mne.io import read_raw_nirx | ||
| from mne.preprocessing.nirs import optical_density, scalp_coupling_index | ||
| from mne.datasets import testing | ||
|
|
||
| fname_nirx_15_0 = op.join(data_path(download=False), | ||
| 'NIRx', 'nirx_15_0_recording') | ||
| fname_nirx_15_2 = op.join(data_path(download=False), | ||
| 'NIRx', 'nirx_15_2_recording') | ||
| fname_nirx_15_2_short = op.join(data_path(download=False), | ||
| 'NIRx', 'nirx_15_2_recording_w_short') | ||
|
|
||
|
|
||
| @testing.requires_testing_data | ||
| @pytest.mark.parametrize('fname', ([fname_nirx_15_2_short, fname_nirx_15_2, | ||
| fname_nirx_15_0])) | ||
| @pytest.mark.parametrize('fmt', ('nirx', 'fif')) | ||
| def test_scalp_coupling_index(fname, fmt, tmpdir): | ||
| """Test converting NIRX files.""" | ||
| assert fmt in ('nirx', 'fif') | ||
| raw = read_raw_nirx(fname) | ||
| raw = optical_density(raw) | ||
| sci = scalp_coupling_index(raw) | ||
|
|
||
| # All values should be between -1 and +1 | ||
| assert_array_less(sci, 1.0) | ||
| assert_array_less(sci * -1.0, 1.0) | ||
|
|
||
| # Fill in some data with known correlation values | ||
| new_data = np.random.rand(raw._data[0].shape[0]) | ||
| # Set first two channels to perfect correlation | ||
| raw._data[0] = new_data | ||
| raw._data[1] = new_data | ||
| # Set next two channels to perfect correlation | ||
| raw._data[2] = new_data | ||
| raw._data[3] = new_data * 0.3 # check scale invariance | ||
| # Set next two channels to anti correlation | ||
| raw._data[4] = new_data | ||
| raw._data[5] = new_data * -1.0 | ||
| # Set next two channels to be uncorrelated | ||
| # TODO: this might be a bad idea as sometimes random noise might correlate | ||
| raw._data[6] = new_data | ||
| raw._data[7] = np.random.rand(raw._data[0].shape[0]) | ||
| # Check values | ||
| sci = scalp_coupling_index(raw) | ||
| assert_allclose(sci[0:6], [1, 1, 1, 1, -1, -1], atol=0.01) | ||
| assert np.abs(sci[6]) < 0.5 | ||
| assert np.abs(sci[7]) < 0.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.