Skip to content

Commit

Permalink
Fix reading bad cnt chans
Browse files Browse the repository at this point in the history
  • Loading branch information
withmywoessner committed Nov 9, 2023
1 parent 70a915b commit 4989271
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 19 additions & 3 deletions mne/io/cnt/cnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def read_raw_cnt(
emg=(),
data_format="auto",
date_format="mm/dd/yy",
*,
header="old",
preload=False,
verbose=None,
):
Expand Down Expand Up @@ -219,6 +221,9 @@ def read_raw_cnt(
Defaults to ``'auto'``.
date_format : ``'mm/dd/yy'`` | ``'dd/mm/yy'``
Format of date in the header. Defaults to ``'mm/dd/yy'``.
header : ``'new'`` | ``'old'``
Defines the header format. Used to describe how bad channels
are formatted.
%(preload)s
%(verbose)s
Expand All @@ -244,12 +249,13 @@ def read_raw_cnt(
emg=emg,
data_format=data_format,
date_format=date_format,
header=header,
preload=preload,
verbose=verbose,
)


def _get_cnt_info(input_fname, eog, ecg, emg, misc, data_format, date_format):
def _get_cnt_info(input_fname, eog, ecg, emg, misc, data_format, date_format, header):
"""Read the cnt header."""
data_offset = 900 # Size of the 'SETUP' header.
cnt_info = dict()
Expand Down Expand Up @@ -344,7 +350,12 @@ def _get_cnt_info(input_fname, eog, ecg, emg, misc, data_format, date_format):
fid.seek(data_offset + 75 * ch_idx)
ch_name = read_str(fid, 10)
ch_names.append(ch_name)
fid.seek(data_offset + 75 * ch_idx + 4)

# Some files have bad channels marked differently in the header.
if header == "new":
fid.seek(data_offset + 75 * ch_idx + 14)
if header == "old":
fid.seek(data_offset + 75 * ch_idx + 4)
if np.fromfile(fid, dtype="u1", count=1).item():
bads.append(ch_name)
fid.seek(data_offset + 75 * ch_idx + 19)
Expand Down Expand Up @@ -451,6 +462,9 @@ class RawCNT(BaseRaw):
Defaults to ``'auto'``.
date_format : ``'mm/dd/yy'`` | ``'dd/mm/yy'``
Format of date in the header. Defaults to ``'mm/dd/yy'``.
header : ``'new'`` | ``'old'``
Defines the header format. Used to describe how bad channels
are formatted.
%(preload)s
stim_channel : bool | None
Add a stim channel from the events. Defaults to None to trigger a
Expand Down Expand Up @@ -478,6 +492,8 @@ def __init__(
emg=(),
data_format="auto",
date_format="mm/dd/yy",
*,
header="old",
preload=False,
verbose=None,
): # noqa: D102
Expand All @@ -489,7 +505,7 @@ def __init__(

input_fname = path.abspath(input_fname)
info, cnt_info = _get_cnt_info(
input_fname, eog, ecg, emg, misc, data_format, _date_format
input_fname, eog, ecg, emg, misc, data_format, _date_format, header
)
last_samps = [cnt_info["n_samples"] - 1]
super(RawCNT, self).__init__(
Expand Down
11 changes: 10 additions & 1 deletion mne/io/cnt/tests/test_cnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@testing.requires_testing_data
def test_data():
def test_old_data():
"""Test reading raw cnt files."""
with pytest.warns(RuntimeWarning, match="number of bytes"):
raw = _test_raw_reader(
Expand All @@ -37,6 +37,15 @@ def test_data():
assert raw.info["meas_date"] is None


@testing.requires_testing_data
def test_new_data():
"""Test reading raw cnt files with different header."""
with pytest.warns(RuntimeWarning):
raw = read_raw_cnt(input_fname=fname_bad_spans, header="new")

assert raw.info["bads"] == ["F8"] # test bads


@testing.requires_testing_data
def test_compare_events_and_annotations():
"""Test comparing annotations and events."""
Expand Down

0 comments on commit 4989271

Please sign in to comment.