MCPcopy
hub / github.com/mne-tools/mne-python / read_from_file_or_buffer

Function read_from_file_or_buffer

mne/fixes.py:902–925  ·  view source on GitHub ↗

numpy.fromfile() wrapper, handling io.BytesIO file-like streams. Numpy requires open files to be actual files on disk, i.e., must support file.fileno(), so it fails with file-like streams such as io.BytesIO(). If numpy.fromfile() fails due to no file.fileno() support, this wrapper

(
    file: str | bytes | os.PathLike | io.IOBase,
    dtype: numpy.typing.DTypeLike = float,
    count: int = -1,
)

Source from the content-addressed store, hash-verified

900# we try to use numpy.fromfile, if a blob is used we use numpy.frombuffer to read
901# from the file-like object.
902def read_from_file_or_buffer(
903 file: str | bytes | os.PathLike | io.IOBase,
904 dtype: numpy.typing.DTypeLike = float,
905 count: int = -1,
906):
907 """numpy.fromfile() wrapper, handling io.BytesIO file-like streams.
908
909 Numpy requires open files to be actual files on disk, i.e., must support
910 file.fileno(), so it fails with file-like streams such as io.BytesIO().
911
912 If numpy.fromfile() fails due to no file.fileno() support, this wrapper
913 reads the required bytes from file and redirects the call to
914 numpy.frombuffer().
915
916 See https://github.com/numpy/numpy/issues/2230#issuecomment-949795210
917 """
918 try:
919 return np.fromfile(file, dtype=dtype, count=count)
920 except io.UnsupportedOperation as e:
921 if not (e.args and e.args[0] == "fileno" and isinstance(file, io.IOBase)):
922 raise # Nothing I can do about it
923 dtype = np.dtype(dtype)
924 buffer = file.read(dtype.itemsize * count)
925 return np.frombuffer(buffer, dtype=dtype, count=count)

Callers 2

_read_chFunction · 0.85
_read_gdf_headerFunction · 0.85

Calls 1

readMethod · 0.80

Tested by

no test coverage detected