Skip to content

Commit

Permalink
Fix cnt response (mne-tools#13007)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric Larson <[email protected]>
  • Loading branch information
3 people authored and scottrbrtsn committed Dec 6, 2024
1 parent 202292a commit e809d71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/13007.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct :func:`mne.io.read_raw_cnt` to read responses and fix exceptions by `Jacob Woessner`_.
22 changes: 19 additions & 3 deletions mne/io/cnt/cnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..._fiff.utils import _create_chs, _find_channels, _mult_cal_one, read_str
from ...annotations import Annotations
from ...channels.layout import _topo_to_sphere
from ...utils import _check_option, _validate_type, fill_doc, warn
from ...utils import _check_option, _explain_exception, _validate_type, fill_doc, warn
from ..base import BaseRaw
from ._utils import (
CNTEventType3,
Expand Down Expand Up @@ -150,7 +150,22 @@ def _update_bad_span_onset(accept_reject, onset, duration, description):
np.array([e.KeyPad_Accept for e in my_events])
)

description = np.array([str(e.StimType) for e in my_events])
# Check to see if there are any button presses
description = []
for event in my_events:
# Extract the 4-bit fields
# Upper nibble (4 bits) currently not used
# accept = (event.KeyPad_Accept[0] & 0xF0) >> 4
# Lower nibble (4 bits) keypad button press
keypad = event.KeyPad_Accept[0] & 0x0F
if str(keypad) != "0":
description.append(f"KeyPad Response {keypad}")
elif event.KeyBoard != 0:
description.append(f"Keyboard Response {event.KeyBoard}")
else:
description.append(str(event.StimType))

description = np.array(description)

onset, duration, description = _update_bad_span_onset(
accept_reject, onset / sfreq, duration, description
Expand Down Expand Up @@ -532,7 +547,8 @@ def __init__(
)
except Exception:
raise RuntimeError(
"Could not read header from *.cnt file. mne.io.read_raw_cnt "
f"{_explain_exception()}\n"
"WARNING: mne.io.read_raw_cnt "
"supports Neuroscan CNT files only. If this file is an ANT Neuro CNT, "
"please use mne.io.read_raw_ant instead."
)
Expand Down
3 changes: 2 additions & 1 deletion mne/io/cnt/tests/test_cnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def test_auto_data():
third = pytest.warns(RuntimeWarning, match="Omitted 6 annot")
with first, second, third:
raw = read_raw_cnt(input_fname=fname_bad_spans)

# Test that responses are read properly
assert "KeyPad Response 1" in raw.annotations.description
assert raw.info["bads"] == ["F8"]

with _no_parse, pytest.warns(RuntimeWarning, match="number of bytes"):
Expand Down

0 comments on commit e809d71

Please sign in to comment.