1

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]
emuecke
  • 11
  • 2

1 Answers1

0

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 for a python implementation of a circular buffer (source and example)

Community
  • 1
  • 1
Emilien
  • 2,385
  • 16
  • 24
  • Okay, and lets say if I got a shape like (128,2,2) for a video of 128 frames and 2 tracked points times x and y. How can I index my calculated values correctly? For Example i got something like result = [[236, 218],[267, 220]] as a result for one frame. I can't just index "self.data[0] = result", because then it will just fill up the array but overwrite my data structure, which ends with IndexErrors. – emuecke Sep 05 '14 at 12:03
  • You should be able to do self.data[0] = result, unless your index calculation is wrong which I suspect (you add an integer and a numpy array), thus maybe it's simpler to use collections.deque – Emilien Sep 05 '14 at 12:18