Ring Buffer for n-dimensional Vectors
Question: I am working on an real-time application. For this I need to store around 20
arrays per second. Each arrays consists of n Points with their respective x
and y coordinate (z may follow as well in the future).

What I did come up with is some kind of a Ring Buffer, which takes the length
of the total arrays (it's frames of a video btw.) and the number of the points
with their coordinate (this doesn't change within one execution, but is
variable for executions following).

My Buffer inits with an numpy array filled with zeros:
`np.zeros((lengthOfSlices,numberOfTrackedPoints))`

However this seems to be problematic, because I write the whole Points for a
Slice into the array at once, not after another. That means I can't broadcast
the array as the shape is not correct.

Is there a numPythonic way to initialize the array with zeros and store
vectorwise afterwards?

Below you can find what I have now:

    
    
    class Buffer():
      def __init__(self, lengthOfSlices, numberOfTrackedPoints):
        self.data = np.zeros((lengthOfSlices,numberOfTrackedPoints))
        self.index = 0
    
      def extend(self, x):
        'adds array x to ring buffer'
        x_index = (self.index + np.arange(x.size)) % self.data.size
        self.data[x_index] = x
        self.index = x_index[-1] + 1
    
      def get(self):
        'returns the first-in-first-out data in the ring buffer'
        idx = (self.index + np.arange(self.data.size)) % self.data.size
        return self.data[idx]
    


Answer: You need to reshape the array based on the lenght of the frame.

Simple example:

    
    
    >>> import numpy as np
    >>> A = np.zeros(100)
    >>> B = np.reshape(A, (10,10))
    >>> B[0]
    array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
    

So that's probably something like `self.data = np.reshape(self.data,
(lengthOfAFrame, 20))`

EDIT: Apparently reshaping is not your (only?) problem, you might check
[collections.deque](http://docs.python.org/library/collections.html#collections.deque)
for a python implementation of a circular buffer ([source and
example](http://stackoverflow.com/questions/4151320/efficient-circular-
buffer))

