Skip to content

Commit 147ff59

Browse files
myd7349pre-commit-ci[bot]larsonerdrammock
authored
ENH: Add encoding parameter to Nihon Kohden reader (#13458)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Eric Larson <[email protected]> Co-authored-by: Daniel McCloy <[email protected]>
1 parent 54644a5 commit 147ff59

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``encoding`` parameter to :func:`mne.io.read_raw_nihon` for better handling of annotation decoding, by `Tom Ma`_.

mne/io/nihon/nihon.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def _ensure_path(fname):
2323

2424

2525
@fill_doc
26-
def read_raw_nihon(fname, preload=False, verbose=None) -> "RawNihon":
26+
def read_raw_nihon(
27+
fname, preload=False, *, encoding="utf-8", verbose=None
28+
) -> "RawNihon":
2729
"""Reader for an Nihon Kohden EEG file.
2830
2931
Parameters
@@ -32,6 +34,9 @@ def read_raw_nihon(fname, preload=False, verbose=None) -> "RawNihon":
3234
Path to the Nihon Kohden data file (``.EEG``).
3335
preload : bool
3436
If True, all data are loaded at initialization.
37+
%(encoding_nihon)s
38+
39+
.. versionadded:: 1.11
3540
%(verbose)s
3641
3742
Returns
@@ -44,7 +49,7 @@ def read_raw_nihon(fname, preload=False, verbose=None) -> "RawNihon":
4449
--------
4550
mne.io.Raw : Documentation of attributes and methods of RawNihon.
4651
"""
47-
return RawNihon(fname, preload, verbose)
52+
return RawNihon(fname, preload, encoding=encoding, verbose=verbose)
4853

4954

5055
_valid_headers = [
@@ -315,7 +320,7 @@ def _parse_sub_event_log(sub_event_log):
315320
return t_sub_desc, t_sub_onset
316321

317322

318-
def _read_nihon_annotations(fname):
323+
def _read_nihon_annotations(fname, encoding="utf-8"):
319324
fname = _ensure_path(fname)
320325
log_fname = fname.with_suffix(".LOG")
321326
if not log_fname.exists():
@@ -346,15 +351,10 @@ def _read_nihon_annotations(fname):
346351
t_onset += t_sub_onset
347352

348353
t_desc = t_desc.rstrip(b"\x00")
349-
for enc in _encodings:
350-
try:
351-
t_desc = t_desc.decode(enc)
352-
except UnicodeDecodeError:
353-
pass
354-
else:
355-
break
356-
else:
357-
warn(f"Could not decode log as one of {_encodings}")
354+
try:
355+
t_desc = t_desc.decode(encoding)
356+
except UnicodeDecodeError:
357+
warn(f"Could not decode log as {encoding}")
358358
continue
359359

360360
all_onsets.append(t_onset)
@@ -414,6 +414,9 @@ class RawNihon(BaseRaw):
414414
Path to the Nihon Kohden data ``.eeg`` file.
415415
preload : bool
416416
If True, all data are loaded at initialization.
417+
%(encoding_nihon)s
418+
419+
.. versionadded:: 1.11
417420
%(verbose)s
418421
419422
See Also
@@ -422,7 +425,7 @@ class RawNihon(BaseRaw):
422425
"""
423426

424427
@verbose
425-
def __init__(self, fname, preload=False, verbose=None):
428+
def __init__(self, fname, preload=False, *, encoding="utf-8", verbose=None):
426429
fname = _check_fname(fname, "read", True, "fname")
427430
data_name = fname.name
428431
logger.info(f"Loading {data_name}")
@@ -468,7 +471,7 @@ def __init__(self, fname, preload=False, verbose=None):
468471
)
469472

470473
# Get annotations from LOG file
471-
annots = _read_nihon_annotations(fname)
474+
annots = _read_nihon_annotations(fname, encoding)
472475

473476
# Annotate acquisition skips
474477
controlblock = header["controlblocks"][0]

mne/utils/docs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,11 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
12851285
encoding according to the EDF+ standard).
12861286
"""
12871287

1288+
docdict["encoding_nihon"] = """
1289+
encoding : str
1290+
Text encoding of Nihon Kohden annotations. See :ref:`standard-encodings`.
1291+
"""
1292+
12881293
docdict["encoding_nirx"] = """
12891294
encoding : str
12901295
Text encoding of the NIRX header file. See :ref:`standard-encodings`.

0 commit comments

Comments
 (0)