circbuff module

class opeth.circbuff.CircularBuffer(capacity, allocated, initial_shape, dtype=<class 'numpy.float64'>, append_axis=0, **kwargs)

Bases: collections.abc.Sequence

Ring buffer implementation with continuous memory storage.

Appends/expands happen along specified axis.

The data is always stored continuously in the memory in a numpy array targeted for quick “bulk” read access. As the elements get inserted at end and read/released from the beginning the actual buffer slowly reaches the end of the allocated space, when it needs to be moved to the start of the allocated space to keep it continuous. Inserts are supported at the end only, data removals (“drop data”) only at the beginning of the array.

Array in memory is not from [0..len(arr)), instead from some offset: [self._left_index .. self._right_index), where self._right_index <= self._allocated and self._right_index - self._left_index <= self._capacity.

Was tested for OE purposes only, other usage patterns may bring unexpected errors.

Parameters:
  • capacity (int) – Max num of rows/cols along append_axis to be stored in the circular buffer.
  • allocated (int) – Actual storage area for storing the continuous ring buffer. Bigger allocated storage results in less data moves but more memory consumption. Must be greater than capacity (depends on usage patterns).
  • initial_shape (list(row,col,.)) – full array size to be allocated. Size must match allocated in the append_axis direction.
  • dtype (type) – data type to be stored
  • append_axis (int) – axis in which direction data is appended to the already stored data.
Raises:

ValueError – triggered if appends are not row/columnwise (axis > 1) - others were not tested yet

append(value)

Insert an item at the end of the array.

Not an O(1) operation in case the new items would span over the end of the allocated space: in this case the array contents are moved to the start of the allocated space first.

Parameters:value (dtype as specified during instantiation) – an array of values with the expected shape (all dimensions must match initial_shape’s dimensions except the dimension of append_axis).
Raises:BufferError – if number of items in array would be over capacity limit after the append
drop(nof_elements)

Remove elements from the beginning of the array.

Parameters:nof_elements (int) – number of rows/cols/… along append_axis that should be removed from the start of the array.
Raises:BufferError – if more elements are attempted to be released than present.
dtype

Returns the data type for the array items

max()

Return the maximum value.

min()

Return the minimum value.

shape

Returns the shape of the array currently stored. In a freshly created array all dimensions except the append_axis will report the initial_shape; the append_axis will report 0 as no items (rows, cols) available yet.

The array used for storing data is 0..allocated in the append_axis direction, but the shape returns only the rows/cols currently available in that direction

size()

Return capacity of array: as set in constructor. Available data count is accessible through len(), free size is cb.size() - len(cb).