예제 #1
0
    def test_product(self):
        res = seq([]).product()
        assert res == 1

        res = seq.range(1, 5).product()
        assert res == 24

        res = seq.range(1, 5).product(projection=lambda x: 2 * x)
        assert res == 2 ** 4 * 24
예제 #2
0
    def test_cartesian(self):
        result = seq.range(3).cartesian(range(3)).list()
        self.assertListEqual(result, list(product(range(3), range(3))))

        result = seq.range(3).cartesian(range(3), range(2)).list()
        self.assertListEqual(result, list(product(range(3), range(3), range(2))))

        result = seq.range(3).cartesian(range(3), range(2), repeat=2).list()
        self.assertListEqual(result, list(product(range(3), range(3), range(2), repeat=2)))
예제 #3
0
    def test_sum(self):
        res = seq([]).sum()
        assert res == 0

        res = seq.range(1, 5).sum()
        assert res == 10

        res = seq.range(1, 5).sum(projection=lambda x: 2 * x)
        assert res == 2 * 10
예제 #4
0
    def test_aggregate(self):
        # func
        res = seq.range(5).aggregate(lambda curr, next: curr + next)
        assert res == 10

        # seed + func
        res = seq.range(5).aggregate(100, lambda curr, next: curr + next)
        assert res == 110

        # seed + func + map
        res = seq.range(5).aggregate(100, lambda curr, next: curr + next, lambda res: 'res={}'.format(res))
        assert res == 'res=110'
예제 #5
0
    def select_parents_roulette(self, pop):
        """
        pop - словарь "имя - OptiPerson"
        returns [(OptiPerson1,OptiPerson2), ...]
        """
        slst = seq(pop.values()).map(lambda op: op.get_best()).sorted(
            lambda wr_chr: wr_chr[OptiPerson.fit_key]).to_list()
        fitnss = seq(slst).map(lambda c: c[OptiPerson.fit_key]).to_list()
        delt_fit = fitnss[-1] - fitnss[0]
        p = seq(fitnss).map(lambda f: (f - fitnss[0] + 0.1 * delt_fit) / 1.1 /
                            delt_fit).to_list()
        summ = seq(p).sum()
        p = seq(p).map(lambda f: f / summ).to_list()
        n_pairs = np.random.binomial(len(slst), self.prob_cross)

        def get_by_inds(nar):
            name1 = slst[nar[0]]['name']
            name2 = slst[nar[1]]['name']
            if name1 in pop.keys() and name2 in pop.keys():
                return pop[name1], pop[name2]
            return None

        res = seq.range(n_pairs) \
            .map(lambda i: np.random.choice(len(slst), 2, replace=False)) \
            .map(get_by_inds) \
            .filter(lambda par: par) \
            .to_list()
        return res
예제 #6
0
 def transform(self, pictures: np.ndarray):
     n_blocks = seq.range(self.n_levels).map(lambda l: 4 ** l).sum()
     visual_words = np.empty((len(pictures), n_blocks * self.n_clusters), dtype=np.float32)
     for i, picture in enumerate(pictures):
         words = self._codebook.predict(picture[1])
         j = 0
         for l in range(self.n_levels):
             word_sets = self._descriptor_sets(l, picture)
             w = 1 / 2 ** (self.n_levels - l)  # descriptors at finer resolutions are weighted more
             for inds in word_sets:
                 histogram = np.bincount(words[inds], minlength=self.n_clusters)
                 histogram = self._normalize(histogram) * w
                 visual_words[i, j:j + self.n_clusters] = histogram
                 j += self.n_clusters
     return visual_words
예제 #7
0
 def test_filter_not(self):
     res = seq.range(10).filter_not(lambda x: x % 2 == 0)
     assert res == [1, 3, 5, 7, 9]
예제 #8
0
    def test_custom_functions(self):
        @extend(aslist=True)
        def my_zip(it):
            return zip(it, it)

        result = seq.range(3).my_zip().list()
        expected = list(zip(range(3), range(3)))
        self.assertEqual(result, expected)

        result = seq.range(3).my_zip().my_zip().list()
        expected = list(zip(expected, expected))
        self.assertEqual(result, expected)

        @extend
        def square(it):
            return [i**2 for i in it]

        result = seq.range(100).square().list()
        expected = [i**2 for i in range(100)]
        self.assertEqual(result, expected)

        name = "PARALLEL_SQUARE"

        @extend(parallel=True, name=name)
        def square_parallel(it):
            return [i**2 for i in it]

        result = seq.range(100).square_parallel()
        self.assertEqual(result.sum(), sum(expected))
        self.assertEqual(repr(result._lineage),
                         "Lineage: sequence -> extended[%s]" % name)

        @extend
        def my_filter(it, n=10):
            return (i for i in it if i > n)

        # test keyword args
        result = seq.range(20).my_filter(n=10).list()
        expected = list(filter(lambda x: x > 10, range(20)))
        self.assertEqual(result, expected)

        # test args
        result = seq.range(20).my_filter(10).list()
        self.assertEqual(result, expected)

        # test final
        @extend(final=True)
        def toarray(it):
            return array.array("f", it)

        result = seq.range(10).toarray()
        expected = array.array("f", range(10))
        self.assertEqual(result, expected)

        result = seq.range(10).map(lambda x: x**2).toarray()
        expected = array.array("f", [i**2 for i in range(10)])
        self.assertEqual(result, expected)

        # a more complex example combining all above
        @extend()
        def sum_pair(it):
            return (i[0] + i[1] for i in it)

        result = (seq.range(100).my_filter(
            85).my_zip().sum_pair().square_parallel().toarray())

        expected = array.array(
            "f",
            list(
                map(
                    lambda x: (x[0] + x[1])**2,
                    map(lambda x: (x, x), filter(lambda x: x > 85,
                                                 range(100))),
                )),
        )
        self.assertEqual(result, expected)
예제 #9
0
 def test_seq_range(self):
     assert seq.range(1, 8, 2) == [1, 3, 5, 7]
예제 #10
0
    def test_to_list(self):
        res = seq.range(5).to_list()
        assert res == [0, 1, 2, 3, 4]

        res = seq.range(5).to_list(n=3)
        assert res == [0, 1, 2]
예제 #11
0
 def test_drop_right(self):
     res = seq.range(10).drop_right(5)
     assert res == [0, 1, 2, 3, 4]
예제 #12
0
 def test_count(self):
     res = seq.range(10).count(lambda x: x % 2 == 1)
     assert res == 5
예제 #13
0
    def test_last_option(self):
        res = seq.range(10).last_option()
        assert res == 9

        res = seq([]).last_option()
        assert res is None
예제 #14
0
    def test_sliding(self):
        res = seq.range(5).sliding(size=3)
        assert res == [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

        res = seq.range(10).sliding(size=5, step=3)
        assert res == [[0, 1, 2, 3, 4], [3, 4, 5, 6, 7], [6, 7, 8, 9], [9]]
예제 #15
0
 def test_slice(self):
     res = seq.range(10).slice(3, 7)
     assert res == [3, 4, 5, 6]
예제 #16
0
 def test_reverse(self):
     res = seq.range(5).reverse()
     print(res)
     assert res == [4, 3, 2, 1, 0]
예제 #17
0
    def test_map_or_select(self):
        res = seq.range(5).map(lambda x: x ** 2)
        assert res == [0, 1, 4, 9, 16]

        res = seq.range(5).select(lambda x: x ** 2)
        assert res == [0, 1, 4, 9, 16]
예제 #18
0
 def test_inits(self):
     res = seq.range(5).inits()
     assert res == [[0, 1, 2, 3, 4], [0, 1, 2, 3], [0, 1, 2], [0, 1], [0], []]
예제 #19
0
 def test_init(self):
     res = seq.range(5).init()
     assert res == [0, 1, 2, 3]
예제 #20
0
    def test_head_option(self):
        res = seq.range(10).head_option()
        assert res == 0

        res = seq([]).head_option()
        assert res is None
예제 #21
0
 def test_last(self):
     res = seq.range(10).last()
     assert res == 9
예제 #22
0
 def test_tail(self):
     res = seq.range(5).tail()
     assert res == [1, 2, 3, 4]
예제 #23
0
 def test_exists(self):
     assert not seq.range(1, 10).exists(lambda x: x % 2 == 0 and x % 5 == 0)
     assert seq.range(1, 11).exists(lambda x: x % 2 == 0 and x % 5 == 0)
예제 #24
0
 def test_tails(self):
     res = seq.range(5).tails()
     assert res == [[0, 1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4], [3, 4], [4], []]
예제 #25
0
    def test_len_or_size(self):
        res = seq.range(10).len()
        assert res == 10

        res = seq.range(10).size()
        assert res == 10
예제 #26
0
 def test_take(self):
     res = seq.range(5).take(3)
     assert res == [0, 1, 2]
예제 #27
0
 def test_drop(self):
     res = seq.range(10).drop(5)
     assert res == [5, 6, 7, 8, 9]
예제 #28
0
 def test_take_while(self):
     res = seq.range(5).take_while(lambda x: x <= 3)
     assert res == [0, 1, 2, 3]
예제 #29
0
 def test_make_string(self):
     res = seq.range(5).make_string('/')
     assert res == '0/1/2/3/4'
예제 #30
0
 def test_drop_while(self):
     res = seq.range(10).drop_while(lambda x: x <= 6)
     assert res == [7, 8, 9]
예제 #31
0
 def test_repr_max_lines(self):
     sequence = seq.range(200)
     self.assertEqual(len(repr(sequence)), 395)
     sequence._max_repr_items = None
     self.assertEqual(len(repr(sequence)), 890)
예제 #32
0
    def test_find(self):
        res = seq.range(10).find(lambda x: x > 0 and x % 2 == 0 and x % 3 == 0)
        assert res == 6

        res = seq.range(10).find(lambda x: x > 0 and x % 2 == 0 and x % 7 == 0)
        assert res is None
예제 #33
0
    def test_head_or_first(self):
        res = seq.range(10).head()
        assert res == 0

        res = seq.range(10).first()
        assert res == 0
예제 #34
0
    def test_filter_or_where(self):
        res = seq.range(10).filter(lambda x: x % 2 == 0)
        assert res == [0, 2, 4, 6, 8]

        res = seq.range(10).where(lambda x: x % 2 == 0)
        assert res == [0, 2, 4, 6, 8]