Skip to content

Fix utf-8 character writing #277

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

Merged
merged 4 commits into from
Feb 28, 2025
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
8 changes: 4 additions & 4 deletions pyedflib/_extensions/c/edflib.c
Original file line number Diff line number Diff line change
Expand Up @@ -6585,10 +6585,10 @@ int edfwrite_annotation_utf8(int handle, long long onset, long long duration, co
break;
}

if(list_annot->annotation[i] < 32)
{
list_annot->annotation[i] = '.';
}
//if(list_annot->annotation[i] < 32)
//{
// list_annot->annotation[i] = '.';
//}
}

hdrlist[handle]->annots_in_file++;
Expand Down
37 changes: 18 additions & 19 deletions pyedflib/edfwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,14 @@ def check_signal_header_correct(channels: List[Dict[str, Union[str, float, None]
str(ch['physical_max'])[:8]))


def u(x: bytes) -> str:
return x.decode("utf_8", "strict")


def du(x: Union[str, bytes]) -> bytes:
"""encode string to unicode"""
if isinstance(x, bytes):
return x
else:
return x.encode("utf_8")


def isstr(s: Any) -> bool:
warnings.warn("Function 'isstr' is deprecated.", DeprecationWarning, stacklevel=2)
return isinstance(s, str)


def isbytestr(s: Any) -> bool:
warnings.warn("Function 'isbytestr' is deprecated.", DeprecationWarning, stacklevel=2)
return isinstance(s, bytes)


def sex2int(sex: Union[int, str, None]) -> Optional[int]:
if isinstance(sex, int) or sex is None:
return sex
Expand Down Expand Up @@ -1001,7 +988,9 @@ def writeSamples(self, data_list: Union[List[np.ndarray], np.ndarray], digital:
if success<0:
raise OSError(f'Unknown error while calling writeSamples: {success}')

def writeAnnotation(self, onset_in_seconds: Union[int, float], duration_in_seconds: Union[int, float], description: str, str_format: str = 'utf_8') -> int:
def writeAnnotation(self, onset_in_seconds: Union[int, float],
duration_in_seconds: Union[int, float],
description: str, str_format: str = 'utf_8') -> int:
"""
Writes an annotation/event to the file
"""
Expand All @@ -1013,16 +1002,26 @@ def writeAnnotation(self, onset_in_seconds: Union[int, float], duration_in_secon

if str_format == 'utf_8':
if duration_in_seconds >= 0:
return write_annotation_utf8(self.handle, np.round(onset_in_seconds*10000).astype(np.int64), np.round(duration_in_seconds*10000).astype(int), du(description))
return write_annotation_utf8(self.handle,
np.round(onset_in_seconds*10000).astype(np.int64),
np.round(duration_in_seconds*10000).astype(int),
du(description))
else:
return write_annotation_utf8(self.handle, np.round(onset_in_seconds*10000).astype(np.int64), -1, du(description))
return write_annotation_utf8(self.handle,
np.round(onset_in_seconds*10000).astype(np.int64),
-1, du(description))
else:
if duration_in_seconds >= 0:
# FIX: description must be bytes. string will fail in u function
return write_annotation_latin1(self.handle, np.round(onset_in_seconds*10000).astype(np.int64), np.round(duration_in_seconds*10000).astype(int), u(description).encode('latin1')) # type: ignore
return write_annotation_latin1(self.handle,
np.round(onset_in_seconds*10000).astype(np.int64),
np.round(duration_in_seconds*10000).astype(int), description.encode('latin1')) # type: ignore
else:
# FIX: description must be bytes. string will fail in u function
return write_annotation_latin1(self.handle, np.round(onset_in_seconds*10000).astype(np.int64), -1, u(description).encode('latin1')) # type: ignore
return write_annotation_latin1(self.handle,
np.round(onset_in_seconds*10000).astype(np.int64),
-1,
description.encode('latin1')) # type: ignore

def close(self) -> None:
"""
Expand Down
Loading
Loading