Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix bug with reading dig strings #13083

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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/devel/13083.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug with reading digitization points from digitization strings with newer MEGIN systems, by `Eric Larson`_.
3 changes: 3 additions & 0 deletions mne/_fiff/_digitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ def _read_dig_fif(fid, meas_info, *, return_ch_names=False):
if kind == FIFF.FIFF_DIG_POINT:
tag = read_tag(fid, pos)
dig.append(tag.data)
elif kind == FIFF.FIFF_DIG_STRING:
tag = read_tag(fid, pos)
dig.extend(tag.data)
elif kind == FIFF.FIFF_MNE_COORD_FRAME:
tag = read_tag(fid, pos)
coord_frame = _coord_frame_named.get(int(tag.data.item()))
Expand Down
26 changes: 4 additions & 22 deletions mne/_fiff/meas_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
write_dig,
)
from .compensator import get_current_comp
from .constants import FIFF, _ch_unit_mul_named, _coord_frame_named
from .constants import FIFF, _ch_unit_mul_named
from .ctf_comp import _read_ctf_comp, write_ctf_comp
from .open import fiff_open
from .pick import (
Expand Down Expand Up @@ -1961,7 +1961,7 @@ def _simplify_info(info, *, keep=()):


@verbose
def read_fiducials(fname, verbose=None):
def read_fiducials(fname, *, verbose=None):
"""Read fiducials from a fiff file.

Parameters
Expand All @@ -1981,26 +1981,8 @@ def read_fiducials(fname, verbose=None):
fname = _check_fname(fname=fname, overwrite="read", must_exist=True)
fid, tree, _ = fiff_open(fname)
with fid:
isotrak = dir_tree_find(tree, FIFF.FIFFB_ISOTRAK)
isotrak = isotrak[0]
pts = []
coord_frame = FIFF.FIFFV_COORD_HEAD
for k in range(isotrak["nent"]):
kind = isotrak["directory"][k].kind
pos = isotrak["directory"][k].pos
if kind == FIFF.FIFF_DIG_POINT:
tag = read_tag(fid, pos)
pts.append(DigPoint(tag.data))
elif kind == FIFF.FIFF_MNE_COORD_FRAME:
tag = read_tag(fid, pos)
coord_frame = tag.data[0]
coord_frame = _coord_frame_named.get(coord_frame, coord_frame)

# coord_frame is not stored in the tag
for pt in pts:
pt["coord_frame"] = coord_frame

return pts, coord_frame
pts = _read_dig_fif(fid, tree)
return pts, pts[0]["coord_frame"]


@verbose
Expand Down
2 changes: 1 addition & 1 deletion mne/_fiff/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _show_tree(
postpend += " ... list len=" + str(len(tag.data))
elif issparse(tag.data):
postpend += (
f" ... sparse ({tag.data.getformat()}) shape="
f" ... sparse ({tag.data.__class__.__name__}) shape="
f"{tag.data.shape}"
)
else:
Expand Down
22 changes: 15 additions & 7 deletions mne/_fiff/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,26 @@ def _read_id_struct(fid, tag, shape, rlims):
)


def _read_dig_point_struct(fid, tag, shape, rlims):
def _read_dig_point_struct(fid, tag, shape, rlims, *, string=False):
"""Read dig point struct tag."""
kind = int(np.frombuffer(fid.read(4), dtype=">i4").item())
kind = _dig_kind_named.get(kind, kind)
ident = int(np.frombuffer(fid.read(4), dtype=">i4").item())
if kind == FIFF.FIFFV_POINT_CARDINAL:
ident = _dig_cardinal_named.get(ident, ident)
return dict(
kind=kind,
ident=ident,
r=np.frombuffer(fid.read(12), dtype=">f4"),
coord_frame=FIFF.FIFFV_COORD_UNKNOWN,
)
n = 1 if not string else int(np.frombuffer(fid.read(4), dtype=">i4").item())
out = [
dict(
kind=kind,
ident=ident,
r=np.frombuffer(fid.read(12), dtype=">f4"),
coord_frame=FIFF.FIFFV_COORD_UNKNOWN,
)
for _ in range(n)
]
if not string:
out = out[0]
return out


def _read_coord_trans_struct(fid, tag, shape, rlims):
Expand Down Expand Up @@ -378,6 +385,7 @@ def _read_julian(fid, tag, shape, rlims):
FIFF.FIFFT_COMPLEX_DOUBLE: _read_complex_double,
FIFF.FIFFT_ID_STRUCT: _read_id_struct,
FIFF.FIFFT_DIG_POINT_STRUCT: _read_dig_point_struct,
FIFF.FIFFT_DIG_STRING_STRUCT: partial(_read_dig_point_struct, string=True),
FIFF.FIFFT_COORD_TRANS_STRUCT: _read_coord_trans_struct,
FIFF.FIFFT_CH_INFO_STRUCT: _read_ch_info_struct,
FIFF.FIFFT_OLD_PACK: _read_old_pack,
Expand Down
Loading