def test_zip(self): out = Stream([4, 1, 2, 3]).zip(range(9), range(2, 9)).collect(ToList()) self.assertListEqual(out, [(4, 0, 2), (1, 1, 3), (2, 2, 4), (3, 3, 5)]) out = Stream([4, 1, 2, 3]).zip(range(9), range(2, 9), after=False).collect(ToList()) self.assertListEqual(out, [(0, 2, 4), (1, 3, 1), (2, 4, 2), (3, 5, 3)])
def test_cycle(self): out = Stream([4, 1, 2, 3, 9, 0, 5]).cycle(range(3)).collect(ToList()) self.assertListEqual(out, [(4, 0), (1, 1), (2, 2), (3, 0), (9, 1), (0, 2), (5, 0)]) out = Stream([4, 1, 2, 3, 9, 0, 5]).cycle(range(3), after=False).collect(ToList()) self.assertListEqual(out, [(0, 4), (1, 1), (2, 2), (0, 3), (1, 9), (2, 0), (0, 5)])
def test_1(self): rnd = random() data = rnd.int_range(1, 100, size=1000) def mean(es): return sum(es) / len(es) window_size = 4 out = Stream(data).window_function(mean, window_size).collect(ToList()) out_target = [] chunk = data[:window_size] out_target.append(mean(chunk)) for e in data[window_size:]: chunk = chunk[1:] + [e] out_target.append(mean(chunk)) for o, t in zip(out, out_target): with self.subTest(): self.assertAlmostEqual(o, t, delta=1e-8)
def test_map2(self): rnd = self.rnd a, b, size = 1, 100, 1000 add_5 = lambda x: x + 5 pow_3 = lambda x: x**3 mod_2_not_zero = lambda x: x % 2 != 0 mod_3_not_2 = lambda x: x % 3 != 2 mod_5_not_4 = lambda x: x % 5 != 4 out = (Stream(rnd.int_range_supplier( a, b)).limit(size).filter(mod_3_not_2).map(add_5).filter( mod_2_not_zero).map(pow_3).filter(mod_5_not_4).collect( ToList())) rnd.reset() out_target = [] for e in rnd.int_range(a, b, size=size): if mod_3_not_2(e): e = add_5(e) if mod_2_not_zero(e): e = pow_3(e) if mod_5_not_4(e): out_target.append(e) self.assertListEqual(out, out_target)
def test_4(self): size = 10 eps = 1e-9 class Data: def __init__(self, e): self._e = e def e(self): return self._e def __eq__(self, other) -> bool: return abs(self._e - other._e) <= eps rnd = random() out = (Stream.from_supplier(rnd.random).limit(size).map(Data).sort( key=Data.e, reverse=True).collect(ToList())) rnd.reset() out_target = sorted((Data(rnd.random()) for _ in range(size)), key=Data.e, reverse=True) for o, t in zip(out, out_target): with self.subTest(o=o): self.assertAlmostEqual(o, t)
def test_5(self): ctx = Context(prec=40) def dot_product(v1, v2): l1, l2 = len(v1), len(v2) assert l1 == l2, "dimension mismatch; v1's dim: {} and v2's dim: {}".format( l1, l2) assert v1 and v2, 'each vector must have at least one element.' out = 0 for x, y in zip(v1, v2): out += x * y return out class WeightedAverage: def __init__(self, weight: tuple): assert sum(weight) == 1, 'weight array is not normalised' self._weight = weight def __call__(self, es: tuple): return dot_product(self._weight, es) # ------------------------------------------------------------------- data = tuple( D(e, ctx) for e in random().float_range(1, 100, size=1000)) # ------------------------------------------------------------------- alpha = D(0.1, ctx) window_size = 10 _sum = alpha * (1 - alpha**window_size) / (1 - alpha) weight = tuple(alpha**n / _sum for n in range(1, window_size + 1)) # making sum(weight) == 1 wa = WeightedAverage(weight) # ------------------------------------------------------------------- out = Stream(data).window_function(wa, window_size).collect(ToList()) # ------------------------------------------------------------------- chunk = data[:window_size] self.assertEqual(out[0], dot_product(chunk, weight)) for n, e in enumerate(data[window_size:], start=1): chunk = chunk[1:] + (e, ) self.assertEqual(out[n], dot_product(chunk, weight))
def test_1(self): def is_even(x): return x % 2 == 0 data = range(10) odd_numbers = Stream(data).exclude(is_even).collect(ToList()) def is_odd(x): return x % 2 == 1 out_target = [e for e in data if is_odd(e)] self.assertListEqual(odd_numbers, out_target)
def test_zip_longest(self): out = Stream([4, 1, 2, 3]).zip_longest(range(9), range(2, 9), fillvalue=-1).collect(ToList()) self.assertListEqual(out, [(4, 0, 2), (1, 1, 3), (2, 2, 4), (3, 3, 5), (-1, 4, 6), (-1, 5, 7), (-1, 6, 8), (-1, 7, -1), (-1, 8, -1)]) out = Stream([4, 1, 2, 3]).zip_longest(range(9), range(2, 9), fillvalue=-1).collect(ToList()) self.assertListEqual(out, [(4, 0, 2), (1, 1, 3), (2, 2, 4), (3, 3, 5), (-1, 4, 6), (-1, 5, 7), (-1, 6, 8), (-1, 7, -1), (-1, 8, -1)]) out = Stream([4, 1, 2, 3]).zip_longest(range(9), range(2, 9), after=False, fillvalue=None).collect(ToList()) self.assertListEqual(out, [(0, 2, 4), (1, 3, 1), (2, 4, 2), (3, 5, 3), (4, 6, None), (5, 7, None), (6, 8, None), (7, None, None), (8, None, None)])
def test_map1(self): rnd = self.rnd a, b, size = 1, 100, 1000 add_5 = lambda x: x + 5 pow_3 = lambda x: x**3 out = (Stream(rnd.int_range_supplier( a, b)).limit(size).map(add_5).map(pow_3).collect(ToList())) rnd.reset() out_target = [pow_3(add_5(e)) for e in rnd.int_range(a, b, size=size)] self.assertListEqual(out, out_target)
def test_1(self): rnd = random() start, end = 1, 100 size = 1000 data = rnd.int_range(start, end, size=size) # finding frequency of integer numbers generated by random source "rnd". # To get this, grouping by elements on their value then collecting them in a list # and then finding number of elements in list. # For better method see "test_1a" element_frequency = Stream(data).collect( GroupingBy(identity, CollectAndThen(ToList(), len))) out_target = Counter(data) self.assertDictEqual(element_frequency, out_target)
def test_2(self): size = 1000 rnd = random() square = lambda x: x**2 out = (Stream.from_supplier(rnd.random).limit(size).map(square).sort( reverse=True).collect(ToList())) rnd.reset() out_target = sorted((square(rnd.random()) for _ in range(size)), reverse=True) for o, t in zip(out, out_target): with self.subTest(o=o): self.assertAlmostEqual(o, t, delta=1e-9)
def test_4(self): data = random().int_range(1, 100, size=200) class Statistic: def __init__(self): self._mean = 0 self._mean_sqr = 0 # mean of squares self._n = 0 @property def mean(self): return self._mean @property def std(self): return (self._mean_sqr - self.mean**2)**0.5 def __call__(self, es: list): n = self._n e = es[-1] self._mean = (self._mean * n + e) / (n + 1) self._mean_sqr = (self._mean_sqr * n + e**2) / (n + 1) self._n = n + 1 return dict(mean=self.mean, std=self.std) stat = Statistic() out: List[dict] = Stream(data).window_function(stat, None).collect(ToList()) eps = 1e-9 data_holder = [] for o, d in zip(out, data): data_holder.append(d) with self.subTest(): mu = mean(data_holder) self.assertAlmostEqual(o['mean'], mu, delta=eps) self.assertAlmostEqual(o['std'], pstdev(data_holder, mu=mu), delta=eps)
def test_3(self): data = range(100) def mean(l): return sum(l) / len(l) out = Stream(data).window_function(mean, None).collect(ToList()) val = data[0] eps = 1e-8 self.assertAlmostEqual(out[0], val, delta=eps) for n, e in enumerate(data[1:], start=1): val = (n * val + e) / (n + 1) with self.subTest(): self.assertAlmostEqual(out[n], val, delta=eps)
def test_3(self): rnd = random() start, end = 1, 100 size = 1000 def mod_10(x): return x % 10 data = rnd.int_range(start, end, size=size) # Grouping elements on the basis of their remainder when dividing them by 10 and then # finding elements in each "bucket" out = Stream(data).collect(GroupingBy(mod_10, ToList())) out_target = defaultdict(list) for e in data: out_target[mod_10(e)].append(e) self.assertDictEqual(out, out_target)
def test_accumulate1(self): data = range(10) out = Stream(data).accumulate(op.add).collect(ToList()) out_target = list(accumulate(data, op.add)) self.assertListEqual(out, out_target)
def test_enumerate(self): out = Stream(range(4, 10)).enumerate().collect(ToList()) self.assertListEqual(out, [(0, 4), (1, 5), (2, 6), (3, 7), (4, 8), (5, 9)])