From 88f3fdff5889d54e1cc9742dadf80210b77331b7 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 17 Jul 2024 15:39:15 -0500 Subject: [PATCH 1/8] expose encoding parameter in read_raw_nirx --- mne/io/nirx/nirx.py | 12 ++++++++---- mne/utils/docs.py | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/mne/io/nirx/nirx.py b/mne/io/nirx/nirx.py index b5a13a478b5..9f17be5f9de 100644 --- a/mne/io/nirx/nirx.py +++ b/mne/io/nirx/nirx.py @@ -35,7 +35,7 @@ @fill_doc def read_raw_nirx( - fname, saturated="annotate", preload=False, verbose=None + fname, saturated="annotate", *, preload=False, encoding=None, verbose=None ) -> "RawNIRX": """Reader for a NIRX fNIRS recording. @@ -45,6 +45,7 @@ def read_raw_nirx( Path to the NIRX data folder or header file. %(saturated)s %(preload)s + %(encoding_nirx)s %(verbose)s Returns @@ -61,7 +62,9 @@ def read_raw_nirx( ----- %(nirx_notes)s """ - return RawNIRX(fname, saturated, preload, verbose) + return RawNIRX( + fname, saturated, preload=preload, encoding=encoding, verbose=verbose + ) def _open(fname): @@ -78,6 +81,7 @@ class RawNIRX(BaseRaw): Path to the NIRX data folder or header file. %(saturated)s %(preload)s + %(encoding_nirx)s %(verbose)s See Also @@ -90,7 +94,7 @@ class RawNIRX(BaseRaw): """ @verbose - def __init__(self, fname, saturated, preload=False, verbose=None): + def __init__(self, fname, saturated, *, preload=False, encoding=None, verbose=None): logger.info(f"Loading {fname}") _validate_type(fname, "path-like", "fname") _validate_type(saturated, str, "saturated") @@ -178,7 +182,7 @@ def __init__(self, fname, saturated, preload=False, verbose=None): # Read header file # The header file isn't compliant with the configparser. So all the # text between comments must be removed before passing to parser - with _open(files["hdr"]) as f: + with open(files["hdr"], encoding=encoding) as f: hdr_str_all = f.read() hdr_str = re.sub("#.*?#", "", hdr_str_all, flags=re.DOTALL) if is_aurora: diff --git a/mne/utils/docs.py b/mne/utils/docs.py index 0f123661758..048cc5d66b3 100644 --- a/mne/utils/docs.py +++ b/mne/utils/docs.py @@ -1269,6 +1269,11 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75): Whether to emit warnings when cropping or omitting annotations. """ +docdict["encoding_nirx"] = """ +encoding : str + Text encoding of the NIRX header file. See :std:label:`standard-encodings`. +""" + docdict["encoding_edf"] = """ encoding : str Encoding of annotations channel(s). Default is "utf8" (the only correct From fb66eadff0af3d1744b6b70d75bbd0affaedfb02 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 17 Jul 2024 15:40:30 -0500 Subject: [PATCH 2/8] support ZH locale datetime format --- mne/io/nirx/nirx.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mne/io/nirx/nirx.py b/mne/io/nirx/nirx.py index 9f17be5f9de..46bff6b7e17 100644 --- a/mne/io/nirx/nirx.py +++ b/mne/io/nirx/nirx.py @@ -246,6 +246,7 @@ def __init__(self, fname, saturated, *, preload=False, encoding=None, verbose=No '"%a %d %b %Y""%H:%M:%S.%f"', '"%a, %d %b %Y""%H:%M:%S.%f"', "%Y-%m-%d %H:%M:%S.%f", + '"%Y年%m月%d日""%H:%M:%S.%f"', ]: try: meas_date = dt.datetime.strptime(loc_datetime_str, dt_code) From 593c00560017e90c0a42e62bcd137e98f33ff669 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 17 Jul 2024 15:40:51 -0500 Subject: [PATCH 3/8] add TODO re: locale parsing --- mne/io/nirx/_localized_abbr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mne/io/nirx/_localized_abbr.py b/mne/io/nirx/_localized_abbr.py index a3fe8e0b3c9..77622c496e7 100644 --- a/mne/io/nirx/_localized_abbr.py +++ b/mne/io/nirx/_localized_abbr.py @@ -41,6 +41,7 @@ print('}\n') """ +# TODO: this should really be outsourced to a dedicated module like arrow or babel _localized_abbr = { "en_US.utf8": { "month": { From 47d19c4dcb905c0be9b3c11e5fc374c6ee187cbf Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 17 Jul 2024 15:41:02 -0500 Subject: [PATCH 4/8] random unrelated simplification --- mne/io/nirx/nirx.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mne/io/nirx/nirx.py b/mne/io/nirx/nirx.py index 46bff6b7e17..726d394fe54 100644 --- a/mne/io/nirx/nirx.py +++ b/mne/io/nirx/nirx.py @@ -106,10 +106,7 @@ def __init__(self, fname, saturated, *, preload=False, encoding=None, verbose=No fname = str(_check_fname(fname, "read", True, "fname", need_dir=True)) json_config = glob.glob(f"{fname}/*{'config.json'}") - if len(json_config): - is_aurora = True - else: - is_aurora = False + is_aurora = len(json_config) if is_aurora: # NIRSport2 devices using Aurora software From ee3d91110a53bc5824f760fa379b47add06e2ec2 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 17 Jul 2024 15:45:59 -0500 Subject: [PATCH 5/8] make default actually backward compatible --- mne/io/nirx/nirx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/io/nirx/nirx.py b/mne/io/nirx/nirx.py index 726d394fe54..95fa061ea4e 100644 --- a/mne/io/nirx/nirx.py +++ b/mne/io/nirx/nirx.py @@ -35,7 +35,7 @@ @fill_doc def read_raw_nirx( - fname, saturated="annotate", *, preload=False, encoding=None, verbose=None + fname, saturated="annotate", *, preload=False, encoding="latin-1", verbose=None ) -> "RawNIRX": """Reader for a NIRX fNIRS recording. From 344dd6d2a15cc6fa769ec74e4bed305a25240000 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 19 Jul 2024 10:46:57 -0500 Subject: [PATCH 6/8] fix docdict order --- mne/utils/docs.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mne/utils/docs.py b/mne/utils/docs.py index 048cc5d66b3..25e2c5f8756 100644 --- a/mne/utils/docs.py +++ b/mne/utils/docs.py @@ -1269,17 +1269,17 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75): Whether to emit warnings when cropping or omitting annotations. """ -docdict["encoding_nirx"] = """ -encoding : str - Text encoding of the NIRX header file. See :std:label:`standard-encodings`. -""" - docdict["encoding_edf"] = """ encoding : str Encoding of annotations channel(s). Default is "utf8" (the only correct encoding according to the EDF+ standard). """ +docdict["encoding_nirx"] = """ +encoding : str + Text encoding of the NIRX header file. See :std:label:`standard-encodings`. +""" + docdict["epochs_preload"] = """ Load all epochs from disk when creating the object or wait before accessing each epoch (more memory From d73ecf97a99a4b3f6db7ddf0e7cee675c410b7a4 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 19 Jul 2024 10:51:10 -0500 Subject: [PATCH 7/8] changelog --- doc/changes/devel/12730.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changes/devel/12730.bugfix.rst diff --git a/doc/changes/devel/12730.bugfix.rst b/doc/changes/devel/12730.bugfix.rst new file mode 100644 index 00000000000..b6180815110 --- /dev/null +++ b/doc/changes/devel/12730.bugfix.rst @@ -0,0 +1 @@ +Fix bug when reading NIRX files saved in a non-western encoding, by `Daniel McCloy`_. From 203af00860409d91057e9c14bd7f3dc59f8431ed Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 19 Jul 2024 12:38:52 -0500 Subject: [PATCH 8/8] fix crossref role --- mne/utils/docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/utils/docs.py b/mne/utils/docs.py index 25e2c5f8756..f59aaa31306 100644 --- a/mne/utils/docs.py +++ b/mne/utils/docs.py @@ -1277,7 +1277,7 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75): docdict["encoding_nirx"] = """ encoding : str - Text encoding of the NIRX header file. See :std:label:`standard-encodings`. + Text encoding of the NIRX header file. See :ref:`standard-encodings`. """ docdict["epochs_preload"] = """