sigpyproc.io.fileio#

File I/O base class and readers/writers.

This module contains the FileBase class and the FileReader and FileWriter classes for reading and writing filterbank files.

Classes

FileBase

File I/O base class.

FileReader

A file reader class that can read from multiple files.

FileWriter

A file writer class that can write to a sigproc format file.

Functions

allocate_buffer

Allocate a buffer of the given size safely using the given allocator.

class sigpyproc.io.fileio.FileBase(files, mode)[source]#

Bases: object

File I/O base class.

Parameters:
fileslist[str]

list of file names to open

modestr

file opening mode

Attributes:
file_cur

str: Name of the currently open file.

Methods

close()

Close the stream.

eos()

Check if the end of the file stream has been reached.

property file_cur#

str: Name of the currently open file.

close()[source]#

Close the stream.

eos()[source]#

Check if the end of the file stream has been reached.

class sigpyproc.io.fileio.FileReader(sinfo, mode='r', nbits=8)[source]#

Bases: FileBase

A file reader class that can read from multiple files.

Files should have format similar to sigproc.

Parameters:
sinfoStreamInfo

stream information object containing header and data lengths

modestr, optional

file opening mode, by default “r”

nbitsint, optional

number of bits per sample in the files, by default 8

Attributes:
cur_data_pos_file

int: Current data position in the current file.

cur_data_pos_stream

int: Current data position in the data stream.

Methods

cread(nunits)

Read nunits data of the given number of bits from the file stream.

creadinto(read_buffer[, unpack_buffer])

Read from file stream into a buffer of pre-defined length.

seek(offset[, whence])

Change the file stream position to the given offset relative to the whence.

property cur_data_pos_file#

int: Current data position in the current file.

property cur_data_pos_stream#

int: Current data position in the data stream.

cread(nunits)[source]#

Read nunits data of the given number of bits from the file stream.

Parameters:
nunitsint

number of units to read

Returns:
ndarray

an 1-D array containing the read data

Raises:
OSError

if no file is open for reading

creadinto(read_buffer, unpack_buffer=None)[source]#

Read from file stream into a buffer of pre-defined length.

Parameters:
read_bufferBuffer

An object exposing the Python Buffer Protocol interface [PEP 3118]

unpack_bufferBuffer, optional

An object exposing the Python Buffer Protocol interface [PEP 3118], by default None

Returns:
int

Number of bytes readinto the buffer

Raises:
OSError

if no file is open for reading

Notes

It is the responsibility of the caller to handle the case than fewer bytes than requested are read into the buffer. When at the end of the file stream the number of bytes returned will be zero.

seek(offset, whence=0)[source]#

Change the file stream position to the given offset relative to the whence.

Parameters:
offsetint

number of bytes (in the data stream) to move from the reference position given by whence

whenceint

0 (SEEK_SET) 1 (SEEK_CUR), by default 0

Raises:
ValueError

if whence is not 0 or 1.

class sigpyproc.io.fileio.FileWriter(file, *, mode='w', nbits=8, rescale=False)[source]#

Bases: FileBase

A file writer class that can write to a sigproc format file.

Parameters:
filestr

file name to write to

modestr, optional

file writing mode, by default “w”

nbitsint, optional

number of bits per sample in the file, by default 8

scale_facfloat, optional

Additional scale factor to apply to data, by default 1.0

rescalebool, optional

whether to rescale the data using the nbit-dependent values, by default True

Methods

cwrite(arr)

Write an array to file.

write(bo)

Write the given bytes-like object, bo to the file stream.

Raises:
ValueError

if quantize is True and outut nbits is 32.

cwrite(arr)[source]#

Write an array to file.

Parameters:
arndarray

a 1-D numpy array containing the data

Notes

Input data will be packed with a bitsize determined by the nbits

write(bo)[source]#

Write the given bytes-like object, bo to the file stream.

Wrapper for io.RawIOBase.write.

Parameters:
bobytes

bytes-like object

sigpyproc.io.fileio.allocate_buffer(allocator, nbytes)[source]#

Allocate a buffer of the given size safely using the given allocator.

Parameters:
allocatorCallable[[int], Buffer]

A callable that takes an integer argument and returns a buffer object.

nbytesint

Number of bytes to allocate.

Returns:
Buffer

A buffer object of the given size. collections.abc.Buffer (PEP 688).

Raises:
ValueError

if nbytes is less than or equal to zero.

RuntimeError

if the buffer allocation fails.

TypeError

if the allocator does not return a buffer object.

ValueError

if the allocated buffer is not of the expected size.