Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import numpy as np
00032 import copy
00033
00034
00035 class CircularBuffer():
00036
00037 def __init__(self, max_size, element_shape):
00038 shp = [max_size] + list(element_shape)
00039 self.buf = np.zeros(shp)
00040 self.size = max_size
00041 self.n_vals = 0
00042 self.end_idx = -1
00043 self.start_idx = 0
00044
00045 def append(self, val):
00046 self.end_idx = (self.end_idx + 1) % self.size
00047 if self.n_vals != 0 and self.end_idx == self.start_idx:
00048 self.start_idx = (self.start_idx + 1) % self.size
00049
00050 self.n_vals = min(self.n_vals + 1, self.size)
00051 self.buf[self.end_idx] = val
00052
00053
00054 def pop(self):
00055 if self.n_vals == 0:
00056 raise IndexError('pop from empty buffer')
00057 v = self.buf[self.end_idx]
00058 self.end_idx = (self.end_idx - 1) % self.size
00059 self.n_vals = self.n_vals - 1
00060 return v
00061
00062
00063 def to_list(self):
00064 return self.get_array().tolist()
00065
00066
00067
00068 def get_array(self):
00069 start_idx = self.start_idx
00070 end_idx = self.end_idx
00071 if self.n_vals == 0:
00072 return np.array([])
00073 if end_idx >= start_idx:
00074 arr = copy.copy(self.buf[start_idx:end_idx+1])
00075 else:
00076 arr1 = self.buf[start_idx:]
00077 arr2 = self.buf[:end_idx+1]
00078 arr = np.concatenate((arr1, arr2))
00079 return arr
00080
00081
00082 def get_last(self, n):
00083 if n > self.n_vals:
00084 raise IndexError('asking for too many elements')
00085 end_idx = self.end_idx
00086 start_idx = end_idx - (n-1)
00087 if start_idx < 0:
00088 a1 = self.buf[start_idx:]
00089 a2 = self.buf[:end_idx+1]
00090 a = np.concatenate((a1,a2))
00091 else:
00092 a = self.buf[start_idx:end_idx+1]
00093 return a
00094
00095 def clear(self):
00096 self.n_vals = 0
00097 self.end_idx = -1
00098 self.start_idx = 0
00099
00100 def convert_index(self, i):
00101 return (self.start_idx + i) % self.n_vals
00102
00103 def check_index(self, i):
00104 if i >= self.n_vals or -i > self.n_vals:
00105 raise IndexError('index (i = %d) out of bounds, since n_vals = %d' % (i, self.n_vals))
00106
00107 def __setitem__(self, i, val):
00108 self.check_index(i)
00109 i = self.convert_index(i)
00110 self.buf[i] = val
00111
00112 def __getitem__(self, i):
00113
00114 if type(i) is type(slice(1)):
00115 start = i.start
00116 stop = i.stop
00117 step = i.step
00118
00119
00120 if start is None:
00121 start = 0
00122 if stop is None:
00123 stop = self.n_vals
00124 if step is None:
00125 step = 1
00126 if start < 0:
00127 start = 0
00128 if stop > self.n_vals:
00129 stop = self.n_vals
00130 if start > stop:
00131 start = stop
00132
00133
00134 equal_indices = start==stop
00135 start = self.convert_index(start)
00136 stop = self.convert_index(stop - 1) + 1
00137
00138
00139 if equal_indices or (step >= self.n_vals):
00140
00141 return self.buf[start:start]
00142
00143 if start < stop:
00144
00145 return self.buf[start:stop:step]
00146 else:
00147 wrap_start = ((self.n_vals - start) + step) % step
00148
00149
00150 return np.concatenate([self.buf[start::step], self.buf[wrap_start:stop:step]])
00151 else:
00152 self.check_index(i)
00153 i = self.convert_index(i)
00154 return self.buf[i]
00155
00156 def __repr__(self):
00157 return str(self.to_list())
00158
00159 def __len__(self):
00160 return self.n_vals
00161
00162
00163
00164 if __name__ == '__main__':
00165 cb1 = CircularBuffer(5, ())
00166 cb1.append(1)
00167 cb1.append(2)
00168 cb1.append(3)
00169 cb1.append(4)
00170 cb1.append(5)
00171 cb1.append(6)
00172 cb1.append(7)
00173 cb1.append(8)
00174
00175 print 'cb1:', cb1
00176 print 'cb1.buf:', cb1.buf
00177 print 'cb1[0::3]', cb1[0::3]
00178 print 'cb1.get_array()[0::3]', cb1.get_array()[0::3]
00179
00180
00181
hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06