Skip to content

Commit 423f8d5

Browse files
committed
add _has_source_detector_distances
1 parent f85db9d commit 423f8d5

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

mne/preprocessing/nirs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright the MNE-Python contributors.
66

77
from .nirs import (
8+
_has_source_detector_distances,
89
short_channels,
910
source_detector_distances,
1011
_check_channels_ordered,

mne/preprocessing/nirs/nirs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def short_channels(info, threshold=0.01):
6262
return source_detector_distances(info) < threshold
6363

6464

65+
def _has_source_detector_distances(info, picks=None):
66+
"""Return True if source-detector distances can be computed."""
67+
distances = source_detector_distances(info, picks=picks)
68+
return len(distances) > 0 and np.isfinite(distances).all() and np.all(distances > 0)
69+
70+
6571
def _channel_frequencies(info):
6672
"""Return the light frequency for each channel."""
6773
# Only valid for fNIRS data before conversion to haemoglobin

mne/preprocessing/nirs/tests/test_nirs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
_check_channels_ordered,
1919
_fnirs_optode_names,
2020
_fnirs_spread_bads,
21+
_has_source_detector_distances,
2122
_optode_position,
2223
_validate_nirs_info,
2324
beer_lambert_law,
@@ -538,6 +539,22 @@ def test_optode_names():
538539
assert_array_equal(det_names, [f"D{n}" for n in ["1", "11", "17"]])
539540

540541

542+
def test_has_source_detector_distances():
543+
"""Ensure source-detector distance availability is detected."""
544+
ch_names = ["S1_D1 760", "S1_D1 850", "S2_D1 760", "S2_D1 850"]
545+
info = create_info(ch_names=ch_names, ch_types=np.repeat("fnirs_od", 4), sfreq=1.0)
546+
assert not _has_source_detector_distances(info)
547+
for idx in range(2):
548+
info["chs"][idx]["loc"][3:6] = [0.0, 0.0, 0.0]
549+
info["chs"][idx]["loc"][6:9] = [0.03, 0.0, 0.0]
550+
assert _has_source_detector_distances(info, picks=[0, 1])
551+
assert not _has_source_detector_distances(info) # some chs missing
552+
for idx in range(2, 4):
553+
info["chs"][idx]["loc"][3:6] = [0.01, 0.0, 0.0]
554+
info["chs"][idx]["loc"][6:9] = [0.04, 0.0, 0.0]
555+
assert _has_source_detector_distances(info)
556+
557+
541558
@testing.requires_testing_data
542559
def test_optode_loc():
543560
"""Ensure optode location extraction is correct."""

0 commit comments

Comments
 (0)