Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/dev/14191.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix reading CTF datasets whose ``.hc`` file spells the head coils ``Nasion``/``LPA``/``RPA``, by `Seyed Yahya Shirazi`_.
17 changes: 13 additions & 4 deletions mne/io/ctf/hc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
from .constants import CTF
from .res4 import _make_ctf_name

# Keys are matched against the lower-cased descriptor line, so entries must be
# lower case. "lpa"/"rpa" are an alternate spelling seen in the wild alongside
# a capitalized "Nasion"; CTF's own reader (readCTFds.m) takes the coil name
# positionally and does not check it against a vocabulary at all.
_kind_dict = {
"nasion": CTF.CTFV_COIL_NAS,
"left ear": CTF.CTFV_COIL_LPA,
"right ear": CTF.CTFV_COIL_RPA,
"lpa": CTF.CTFV_COIL_LPA,
"rpa": CTF.CTFV_COIL_RPA,
"spare": CTF.CTFV_COIL_SPARE,
}

Expand All @@ -33,21 +39,24 @@ def _read_one_coil_point(fid):
if len(one) == 0:
return None
one = one.strip().decode("utf-8")
if "Unable" in one:
# The descriptor is free text written by the acquisition software, and its
# capitalization varies between systems, so match it case-insensitively.
one_lower = one.lower()
if "unable" in one_lower:
raise RuntimeError("HPI information not available")

# Hopefully this is an unambiguous interpretation
p = dict()
p["valid"] = "measured" in one
p["valid"] = "measured" in one_lower
for key, val in _coord_dict.items():
if key in one:
if key in one_lower:
p["coord_frame"] = val
break
else:
p["coord_frame"] = -1

for key, val in _kind_dict.items():
if key in one:
if key in one_lower:
p["kind"] = val
break
else:
Expand Down
30 changes: 30 additions & 0 deletions mne/io/ctf/tests/test_ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,36 @@ def test_missing_res4(tmp_path):
read_raw_ctf(use_ds)


@testing.requires_testing_data
@pytest.mark.parametrize(
"names",
[
pytest.param(("Nasion", "LPA", "RPA"), id="Nasion-LPA-RPA"),
pytest.param(("NASION", "Left Ear", "RIGHT EAR"), id="mixed-case"),
],
)
def test_hc_coil_name_variants(tmp_path, names):
"""Test that alternate spellings of the .hc coil descriptors are read."""
raw_orig = read_raw_ctf(ctf_dir / ctf_fname_continuous)
use_ds = tmp_path / ctf_fname_continuous
copytree_rw(ctf_dir / ctf_fname_continuous, use_ds)
hc_fname = use_ds / (ctf_fname_continuous[:-2] + "hc")
hc = hc_fname.read_text()
for old, new in zip(("nasion", "left ear", "right ear"), names):
assert old in hc
hc = hc.replace(old, new)
hc_fname.write_text(hc)
raw = read_raw_ctf(use_ds)
assert_allclose(
raw.info["dev_head_t"]["trans"], raw_orig.info["dev_head_t"]["trans"]
)
assert len(raw.info["dig"]) == len(raw_orig.info["dig"])
for dig, dig_orig in zip(raw.info["dig"], raw_orig.info["dig"]):
assert dig["kind"] == dig_orig["kind"]
assert dig["ident"] == dig_orig["ident"]
assert_allclose(dig["r"], dig_orig["r"])


@testing.requires_testing_data
def test_read_ctf_mag_bad_comp(tmp_path, monkeypatch):
"""Test CTF reader with mag comps and bad comps."""
Expand Down
Loading