Skip to content

Commit

Permalink
FIX: mne.io.read_raw_fil handling of bad channels (#12597)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeoneill committed May 2, 2024
1 parent e28cba9 commit 79d54dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/12597.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where :func:`mne.io.read_raw_fil` could not assign bad channels on import, by `George O'Neill`_.
4 changes: 1 addition & 3 deletions mne/io/fil/fil.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,14 @@ def _convert_channel_info(chans):
def _compose_meas_info(meg, chans):
"""Create info structure."""
info = _empty_info(meg["SamplingFrequency"])

# Collect all the necessary data from the structures read
info["meas_id"] = get_new_file_id()
tmp = _convert_channel_info(chans)
info["chs"] = _refine_sensor_orientation(tmp)
# info['chs'] = _convert_channel_info(chans)
info["line_freq"] = meg["PowerLineFrequency"]
info._update_redundant()
info["bads"] = _read_bad_channels(chans)
info._unlocked = False
info._update_redundant()
return info


Expand Down
32 changes: 32 additions & 0 deletions mne/io/fil/tests/test_fil.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
)


def _set_bads_tsv(chanfile, badchan):
"""Update channels.tsv by setting target channel to bad."""
data = []
with open(chanfile, encoding="utf-8") as f:
for line in f:
columns = line.strip().split("\t")
data.append(columns)

with open(chanfile, "w", encoding="utf-8") as f:
for row in data:
if badchan in row:
row[-1] = "bad"
f.write("\t".join(row) + "\n")


def unpack_mat(matin):
"""Extract relevant entries from unstructred readmat."""
data = matin["data"]
Expand Down Expand Up @@ -159,3 +174,20 @@ def test_fil_no_positions(tmp_path):
chs = raw.info["chs"]
locs = array([ch["loc"][:] for ch in chs])
assert isnan(locs).all()


@testing.requires_testing_data
def test_fil_bad_channel_spec(tmp_path):
"""Test FIL reader when a bad channel is specified in channels.tsv."""
test_path = tmp_path / "FIL"
shutil.copytree(fil_path, test_path)

channame = test_path / "sub-noise_ses-001_task-noise220622_run-001_channels.tsv"
binname = test_path / "sub-noise_ses-001_task-noise220622_run-001_meg.bin"
bad_chan = "G2-OG-Y"

_set_bads_tsv(channame, bad_chan)

raw = read_raw_fil(binname)
bads = raw.info["bads"]
assert bad_chan in bads

0 comments on commit 79d54dc

Please sign in to comment.