psychopy.iohub.util.NumPyRingBuffer

class psychopy.iohub.util.NumPyRingBuffer(max_size, dtype=<type 'numpy.float32'>)[source]

Bases: object

NumPyRingBuffer is a circular buffer implemented using a one dimensional numpy array on the backend. The algorithm used to implement the ring buffer behavour does not require any array copies to occur while the ring buffer is maintained, while at the same time allowing sequential element access into the numpy array using a subset of standard slice notation.

When the circular buffer is created, a maximum size , or maximum number of elements, that the buffer can hold must be specified. When the buffer becomes full, each element added to the buffer removes the oldest element from the buffer so that max_size is never exceeded.

The class supports simple slice type access to the buffer contents with the following restrictions / considerations:

  1. Negative indexing is not supported.

Items area dded to the ring buffer using the classes append method.

The current number of elements in the buffer can be retrieved using the getLength() method of the class.

The isFull() method can be used to determine if the ring buffer has reached its maximum size, at which point each new element added will disregard the oldest element in the array.

The getElements() method is used to retrieve the actual numpy array containing the elements in the ring buffer. The element in index 0 is the oldest remaining element added to the buffer, and index n (which can be up to max_size-1) is the the most recent element added to the buffer.

Methods that can be called from a standard numpy array can also be called using the NumPyRingBuffer instance created. However Numpy module level functions will not accept a NumPyRingBuffer as a valid arguement.

To clear the ring buffer and start with no data in the buffer, without needing to create a new NumPyRingBuffer object, call the clear() method of the class.

Example:

ring_buffer=NumPyRingBuffer(10)

for i in xrange(25):
    ring_buffer.append(i)
    print '-------'
    print 'Ring Buffer Stats:'
    print '     Window size: ',len(ring_buffer)
    print '     Min Value: ',ring_buffer.min()
    print '     Max Value: ',ring_buffer.max()
    print '     Mean Value: ',ring_buffer.mean()
    print '     Standard Deviation: ',ring_buffer.std()
    print '     First 3 Elements: ',ring_buffer[:3]
    print '     Last 3 Elements: ',ring_buffer[-3:]
__init__(max_size, dtype=<type 'numpy.float32'>)[source]

Methods

__init__(max_size[, dtype])
append(element) Add element e to the end of the RingBuffer. The element must match the
clear() Clears the RingBuffer. The next time an element is added to the buffer, it will have a size of one.
getElements() Return the numpy array being used by the RingBuffer, the length of
isFull() Indicates if the RingBuffer is at it’s max_size yet.
append(element)[source]

Add element e to the end of the RingBuffer. The element must match the numpy data type specified when the NumPyRingBuffer was created. By default, the RingBuffer uses float32 values.

If the Ring Buffer is full, adding the element to the end of the array removes the currently oldest element from the start of the array.

Parameters:element (numpy.dtype) – An element to add to the RingBuffer.
Returns None:
getElements()[source]

Return the numpy array being used by the RingBuffer, the length of which will be equal to the number of elements added to the list, or the last max_size elements added to the list. Elements are in order of addition to the ring buffer.

Parameters:None
Returns numpy.array:
 The array of data elements that make up the Ring Buffer.
isFull()[source]

Indicates if the RingBuffer is at it’s max_size yet.

Parameters:None
Returns bool:True if max_size or more elements have been added to the RingBuffer; False otherwise.
clear()[source]

Clears the RingBuffer. The next time an element is added to the buffer, it will have a size of one.

Parameters:None
Returns None: