From 675cfa7d7577432134f02018bd7b35c195665733 Mon Sep 17 00:00:00 2001 From: Marin Manuel Date: Mon, 23 Mar 2026 21:57:48 +0000 Subject: [PATCH 1/4] added utf8 decoding for strings if first attemp fails --- neo/rawio/openephysbinaryrawio.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/neo/rawio/openephysbinaryrawio.py b/neo/rawio/openephysbinaryrawio.py index bd767f1e0..b79b584ca 100644 --- a/neo/rawio/openephysbinaryrawio.py +++ b/neo/rawio/openephysbinaryrawio.py @@ -405,16 +405,31 @@ def _parse_header(self): # all theses data are put in event array annotations if "text" in info: # text case - info["labels"] = info["text"].astype("U") + try: + info["labels"] = info["text"].astype("U") + except UnicodeDecodeError: + info["labels"] = np.array([e.decode('utf-8') for e in info['text']], dtype='U') elif "metadata" in info: # binary case - info["labels"] = info["channels"].astype("U") + try: + info["labels"] = info["channels"].astype("U") + except UnicodeDecodeError: + # non-ascii character, fallback on UTF-8 decoding byte by byte + info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') elif "channels" in info: # ttl case use channels - info["labels"] = info["channels"].astype("U") + try: + info["labels"] = info["channels"].astype("U") + except UnicodeDecodeError: + # non-ascii character, fallback on UTF-8 decoding byte by byte + info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') elif "states" in info: # ttl case use states - info["labels"] = info["states"].astype("U") + try: + info["labels"] = info["states"].astype("U") + except UnicodeDecodeError: + # non-ascii character, fallback on UTF-8 decoding byte by byte + info["labels"] = np.array([e.decode('utf-8') for e in info['states']], dtype='U') else: raise ValueError(f"There is no possible labels for this event!") From cb1db663c7558bf502668228751c78f1b7857eae Mon Sep 17 00:00:00 2001 From: Marin Manuel Date: Mon, 23 Mar 2026 21:57:48 +0000 Subject: [PATCH 2/4] added utf8 decoding for strings if first attemp fails --- neo/rawio/openephysbinaryrawio.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/neo/rawio/openephysbinaryrawio.py b/neo/rawio/openephysbinaryrawio.py index bd767f1e0..b79b584ca 100644 --- a/neo/rawio/openephysbinaryrawio.py +++ b/neo/rawio/openephysbinaryrawio.py @@ -405,16 +405,31 @@ def _parse_header(self): # all theses data are put in event array annotations if "text" in info: # text case - info["labels"] = info["text"].astype("U") + try: + info["labels"] = info["text"].astype("U") + except UnicodeDecodeError: + info["labels"] = np.array([e.decode('utf-8') for e in info['text']], dtype='U') elif "metadata" in info: # binary case - info["labels"] = info["channels"].astype("U") + try: + info["labels"] = info["channels"].astype("U") + except UnicodeDecodeError: + # non-ascii character, fallback on UTF-8 decoding byte by byte + info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') elif "channels" in info: # ttl case use channels - info["labels"] = info["channels"].astype("U") + try: + info["labels"] = info["channels"].astype("U") + except UnicodeDecodeError: + # non-ascii character, fallback on UTF-8 decoding byte by byte + info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') elif "states" in info: # ttl case use states - info["labels"] = info["states"].astype("U") + try: + info["labels"] = info["states"].astype("U") + except UnicodeDecodeError: + # non-ascii character, fallback on UTF-8 decoding byte by byte + info["labels"] = np.array([e.decode('utf-8') for e in info['states']], dtype='U') else: raise ValueError(f"There is no possible labels for this event!") From 3f194f8886db8d9bf144ae3d94e8069ec849b83b Mon Sep 17 00:00:00 2001 From: Marin Manuel Date: Wed, 1 Apr 2026 03:26:04 +0000 Subject: [PATCH 3/4] replaced try/except with logic test --- neo/rawio/openephysbinaryrawio.py | 37 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/neo/rawio/openephysbinaryrawio.py b/neo/rawio/openephysbinaryrawio.py index b79b584ca..05aef21c2 100644 --- a/neo/rawio/openephysbinaryrawio.py +++ b/neo/rawio/openephysbinaryrawio.py @@ -391,7 +391,7 @@ def _parse_header(self): # check that events have timestamps assert "timestamps" in info, "Event stream does not have timestamps!" # Updates for OpenEphys v0.6: - # In new vesion (>=0.6) timestamps.npy is now called sample_numbers.npy + # In new version (>=0.6) timestamps.npy is now called sample_numbers.npy # The timestamps are already in seconds, so that event times don't require scaling # see https://open-ephys.github.io/gui-docs/User-Manual/Recording-data/Binary-format.html#events if "sample_numbers" in info: @@ -399,37 +399,34 @@ def _parse_header(self): else: self._use_direct_evt_timestamps = False - # for event the neo "label" will change depending the nature + # for event the neo "label" will change depending on the nature # of event (ttl, text, binary) - # and this is transform into unicode - # all theses data are put in event array annotations + # and this is transform into Unicode + # all these data are put in event array annotations if "text" in info: # text case - try: - info["labels"] = info["text"].astype("U") - except UnicodeDecodeError: + if info["text"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['text']], dtype='U') + else: + info["labels"] = info["text"].astype("U") elif "metadata" in info: # binary case - try: - info["labels"] = info["channels"].astype("U") - except UnicodeDecodeError: - # non-ascii character, fallback on UTF-8 decoding byte by byte - info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') + if info["metadata"].dtype.kind in ["U", "S"]: + info["labels"] = np.array([e.decode('utf-8') for e in info['metadata']], dtype='U') + else: + info["labels"] = info["metadata"].astype("U") elif "channels" in info: # ttl case use channels - try: - info["labels"] = info["channels"].astype("U") - except UnicodeDecodeError: - # non-ascii character, fallback on UTF-8 decoding byte by byte + if info["channels"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') + else: + info["labels"] = info["metadata"].astype("U") elif "states" in info: # ttl case use states - try: - info["labels"] = info["states"].astype("U") - except UnicodeDecodeError: - # non-ascii character, fallback on UTF-8 decoding byte by byte + if info["states"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['states']], dtype='U') + else: + info["labels"] = info["states"].astype("U") else: raise ValueError(f"There is no possible labels for this event!") From e89119c5e1868248a19aec6a37bbbf607c6a7c0c Mon Sep 17 00:00:00 2001 From: Marin Manuel Date: Wed, 1 Apr 2026 03:26:04 +0000 Subject: [PATCH 4/4] replaced try/except with logic test --- neo/rawio/openephysbinaryrawio.py | 35 ++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/neo/rawio/openephysbinaryrawio.py b/neo/rawio/openephysbinaryrawio.py index b79b584ca..20dce89b4 100644 --- a/neo/rawio/openephysbinaryrawio.py +++ b/neo/rawio/openephysbinaryrawio.py @@ -391,7 +391,7 @@ def _parse_header(self): # check that events have timestamps assert "timestamps" in info, "Event stream does not have timestamps!" # Updates for OpenEphys v0.6: - # In new vesion (>=0.6) timestamps.npy is now called sample_numbers.npy + # In new version (>=0.6) timestamps.npy is now called sample_numbers.npy # The timestamps are already in seconds, so that event times don't require scaling # see https://open-ephys.github.io/gui-docs/User-Manual/Recording-data/Binary-format.html#events if "sample_numbers" in info: @@ -399,37 +399,34 @@ def _parse_header(self): else: self._use_direct_evt_timestamps = False - # for event the neo "label" will change depending the nature + # for event the neo "label" will change depending on the nature # of event (ttl, text, binary) - # and this is transform into unicode - # all theses data are put in event array annotations + # and this is transform into Unicode + # all these data are put in event array annotations if "text" in info: # text case - try: - info["labels"] = info["text"].astype("U") - except UnicodeDecodeError: + if info["text"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['text']], dtype='U') + else: + info["labels"] = info["text"].astype("U") elif "metadata" in info: # binary case - try: - info["labels"] = info["channels"].astype("U") - except UnicodeDecodeError: - # non-ascii character, fallback on UTF-8 decoding byte by byte + if info["metadata"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') + else: + info["labels"] = info["channels"].astype("U") elif "channels" in info: # ttl case use channels - try: - info["labels"] = info["channels"].astype("U") - except UnicodeDecodeError: - # non-ascii character, fallback on UTF-8 decoding byte by byte + if info["channels"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['channels']], dtype='U') + else: + info["labels"] = info["channels"].astype("U") elif "states" in info: # ttl case use states - try: - info["labels"] = info["states"].astype("U") - except UnicodeDecodeError: - # non-ascii character, fallback on UTF-8 decoding byte by byte + if info["states"].dtype.kind in ["U", "S"]: info["labels"] = np.array([e.decode('utf-8') for e in info['states']], dtype='U') + else: + info["labels"] = info["states"].astype("U") else: raise ValueError(f"There is no possible labels for this event!")