class MaxWindow(object): """Maintains a list of the 'window_size' largest samples seen. After every_nth sample, the largest is dropped. Dropping allows the sampler to eventually recover from spurious outliers. This class is meant to be derived. See MedianOfMaxWindow, etc.""" def __init__(self, window_size, every_nth): self._window = LargestDropSizedList(window_size) # reverse w/ -sample self._i = 0 self._every_nth = every_nth def __len__(self): return len(self._window) def __str__(self): l = list(self._window) l.reverse() l = [-x for x in l] return str(l) def __iter__(self): class Iterator: def __init__(self, minwindow): self._minwindow = minwindow self._i = 0 def next(self): if self._i < len(self._minwindow): val = self._minwindow[self._i] self._i += 1 return val raise StopIteration() return Iterator(self) def __eq__(self, l): if isinstance(l, MaxWindow): return l._window == self._window elif isinstance(l, list): return l == [x for x in self] else: return False def __ne__(self, l): return not l == self def __getitem__(self, i): """retrieves the ith element in the window where element at index 0 is the smallest.""" return -self._window[len(self._window) - i - 1] def update(self, sample): self._i = (self._i + 1) % self._every_nth if self._i == 0: # drop largest sample. self._window.popleft() # maintain order. last element should be smallest so insert -sample. self._window.insort(-sample)
class MaxWindow(object): """Maintains a list of the 'window_size' largest samples seen. After every_nth sample, the largest is dropped. Dropping allows the sampler to eventually recover from spurious outliers. This class is meant to be derived. See MedianOfMaxWindow, etc.""" def __init__(self, window_size, every_nth ): self._window = LargestDropSizedList( window_size ) # reverse w/ -sample self._i = 0 self._every_nth = every_nth def __len__( self ): return len(self._window) def __str__(self): l = list(self._window) l.reverse() l = [-x for x in l] return str(l) def __iter__(self): class Iterator: def __init__(self,minwindow): self._minwindow = minwindow self._i = 0 def next(self): if self._i < len(self._minwindow): val = self._minwindow[self._i] self._i += 1 return val raise StopIteration() return Iterator(self) def __eq__(self,l): if isinstance(l,MaxWindow): return l._window == self._window elif isinstance(l,list): return l == [x for x in self] else: return False def __ne__(self,l): return not l == self def __getitem__(self,i): """retrieves the ith element in the window where element at index 0 is the smallest.""" return -self._window[len(self._window)-i-1] def update(self, sample): self._i = (self._i+1)%self._every_nth if self._i == 0: # drop largest sample. self._window.popleft() # maintain order. last element should be smallest so insert -sample. self._window.insort(-sample)
class MinWindow(object): """Maintains a list of the 'window_size' smallest samples seen. After every_nth sample, the smallest is dropped. Dropping allows the sampler to eventually recover from spurious outliers. This class is meant to be derived. See MedianOfMinWindow, etc. """ def __init__(self, window_size, every_nth): self._window = LargestDropSizedList(window_size) self._i = 0 self._every_nth = every_nth def __getitem__(self, i): return self._window[i] def __len__(self): return len(self._window) def __str__(self): return str(self._window) def __iter__(self): return self._window.__iter__() def update(self, sample): self._i = (self._i + 1) % self._every_nth if self._i == 0: # drop smallest sample. self._window.popleft() self._window.insort(sample) def timeout(self): """If a timeout occurs then it is possible that our propagation estimate is too large. We therefore drop the top sample.""" if len(self._window) > 1: self._window.pop() # drop largest sample.
class MinWindow(object): """Maintains a list of the 'window_size' smallest samples seen. After every_nth sample, the smallest is dropped. Dropping allows the sampler to eventually recover from spurious outliers. This class is meant to be derived. See MedianOfMinWindow, etc. """ def __init__(self, window_size, every_nth ): self._window = LargestDropSizedList( window_size ) self._i = 0 self._every_nth = every_nth def __getitem__(self, i): return self._window[i] def __len__( self ): return len(self._window) def __str__(self): return str(self._window) def __iter__(self): return self._window.__iter__() def update(self, sample): self._i = (self._i+1)%self._every_nth if self._i == 0: # drop smallest sample. self._window.popleft() self._window.insort( sample ) def timeout(self): """If a timeout occurs then it is possible that our propagation estimate is too large. We therefore drop the top sample.""" if len(self._window) > 1: self._window.pop() # drop largest sample.