Esempio n. 1
0
 def setUp(self):
     count_trials = 1000
     keys = np.cumsum(np.random.random_integers(1, 10000, count_trials))
     vals = range(count_trials)
     self.indata = zip(keys, vals)
     self.keyfunc = lambda x: x[0]
     self.valfunc = lambda x: x[1]
     self.heap = SlidingMaxHeap(value=self.valfunc)
	def setUp(self):
		count_trials = 1000
		keys = np.cumsum(np.random.random_integers(1, 10000, count_trials))
		vals = range(count_trials)
		self.indata = zip(keys, vals)
		self.keyfunc = lambda x: x[0]
		self.valfunc = lambda x: x[1]
		self.heap = SlidingMaxHeap(value = self.valfunc)
Esempio n. 3
0
 def sliding_max(iterable, window_len, valfunc):
     heap = SlidingMaxHeap(value=valfunc)
     for i, item in enumerate(iterable):
         heap.append(item)
         heap.drop_while(lambda x: len(heap._heap) > window_len)
         yield heap.root
Esempio n. 4
0
class TestHeapMonotonic(unittest.TestCase):
    def setUp(self):
        count_trials = 1000
        keys = np.cumsum(np.random.random_integers(1, 10000, count_trials))
        vals = range(count_trials)
        self.indata = zip(keys, vals)
        self.keyfunc = lambda x: x[0]
        self.valfunc = lambda x: x[1]
        self.heap = SlidingMaxHeap(value=self.valfunc)

    def test1(self):
        """Insert monotonic time series, and then delete all elements."""
        for item in self.indata:
            self.heap.append(item)
            self.assertEqual(item, self.heap.root)
            self.assertTrue(self.heap.is_heap())
            self.assertTrue(all_distinct(self.heap.history))
        for item in self.indata:
            self.assertTrue(self.heap.is_heap())
            self.assertTrue(all_distinct(self.heap.history))
            self.heap._drop_oldest()

    def test2(self):
        """Maintain fixed window history of a monotonic time series."""
        for i, item in enumerate(self.indata):
            self.heap.append(item)
            if i > 100:
                self.heap._drop_oldest()
            self.assertEqual(item, self.heap.root)
            self.assertTrue(self.heap.is_heap())
	def sliding_max(iterable, window_len, valfunc):
		heap = SlidingMaxHeap(value = valfunc)
		for i, item in enumerate(iterable):
			heap.append(item)
			heap.drop_while(lambda x: len(heap._heap) > window_len)
			yield heap.root
class TestHeapMonotonic(unittest.TestCase):

	def setUp(self):
		count_trials = 1000
		keys = np.cumsum(np.random.random_integers(1, 10000, count_trials))
		vals = range(count_trials)
		self.indata = zip(keys, vals)
		self.keyfunc = lambda x: x[0]
		self.valfunc = lambda x: x[1]
		self.heap = SlidingMaxHeap(value = self.valfunc)

	def test1(self):
		"""Insert monotonic time series, and then delete all elements."""
		for item in self.indata:
			self.heap.append(item)
			self.assertEqual(item, self.heap.root)
			self.assertTrue(self.heap.is_heap())
			self.assertTrue(all_distinct(self.heap.history))
		for item in self.indata:
			self.assertTrue(self.heap.is_heap())
			self.assertTrue(all_distinct(self.heap.history))
			self.heap._drop_oldest()

	def test2(self):
		"""Maintain fixed window history of a monotonic time series."""
		for i, item in enumerate(self.indata):
			self.heap.append(item)
			if i > 100:
				self.heap._drop_oldest()
			self.assertEqual(item, self.heap.root)
			self.assertTrue(self.heap.is_heap())