コード例 #1
0
    def test_frame_iter_array_d(self) -> None:
        arrays = []
        for _ in range(8):
            arrays.append(list(range(8)))
        f1 = Frame.from_items(zip(range(8), arrays))

        # when called with a pool, values are gien the func as a single argument, which for an element iteration is a tuple of coord, value
        func = lambda arg: arg[0][1]
        # iter columns
        post = f1.iter_element_items().apply_pool(func,
                                                  max_workers=4,
                                                  use_threads=True)

        self.assertEqual(
            post.to_pairs(0),
            ((0, ((0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0),
                  (7, 0))), (1, ((0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
                                 (5, 1), (6, 1), (7, 1))),
             (2, ((0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2),
                  (7, 2))), (3, ((0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
                                 (5, 3), (6, 3), (7, 3))),
             (4, ((0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4),
                  (7, 4))), (5, ((0, 5), (1, 5), (2, 5), (3, 5), (4, 5),
                                 (5, 5), (6, 5), (7, 5))),
             (6, ((0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6),
                  (7, 6))), (7, ((0, 7), (1, 7), (2, 7), (3, 7), (4, 7),
                                 (5, 7), (6, 7), (7, 7)))))
コード例 #2
0
    def test_frame_iter_array_c(self) -> None:
        arrays = []
        for _ in range(8):
            arrays.append(list(range(8)))
        f1 = Frame.from_items(zip(range(8), arrays))

        func = {x: chr(x + 65) for x in range(8)}
        # iter columns
        post = f1.iter_element().apply_pool(func,
                                            max_workers=4,
                                            use_threads=True)

        self.assertEqual(post.to_pairs(0),
                         ((0, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (1, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (2, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (3, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (4, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (5, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (6, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'))),
                          (7, ((0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'),
                               (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H')))))
コード例 #3
0
    def test_matmul_f(self) -> None:
        # lhs: array 1D, rhs: array 2D, Frame

        f1 = Frame.from_items((('a', (1, 2, 3)), ('b', (3, 4, 5))),
                              index=('x', 'y', 'z'))

        self.assertEqual(matmul([3, 4, 5], f1.values).tolist(), [26, 50])

        self.assertEqual(
            matmul([3, 4, 5], f1).to_pairs(), (('a', 26), ('b', 50)))
コード例 #4
0
    def test_matmul_d(self) -> None:
        # lhs: series, rhs: frame

        f1 = Frame.from_items((('a', (1, 2, 3)), ('b', (3, 4, 5))),
                              index=('x', 'y', 'z'))

        s1 = Series((3, 4, 2), index=('x', 'y', 'z'))

        self.assertEqual(matmul(s1, f1).to_pairs(), (('a', 17), ('b', 35)))

        # produces a Series indexed 0, 1
        self.assertEqual(matmul(s1, f1.values).to_pairs(), ((0, 17), (1, 35)))
コード例 #5
0
    def test_matmul_c(self) -> None:
        # lhs: frame, rhs: Series, 1D array

        f1 = Frame.from_items((('a', (1, 2, 3)), ('b', (3, 4, 5))),
                              index=('x', 'y', 'z'))
        s1 = Series((10, 11), index=('a', 'b'))

        self.assertEqual(
            matmul(f1, s1).to_pairs(), (('x', 43), ('y', 64), ('z', 85)))

        self.assertEqual(
            matmul(f1, s1.values).to_pairs(),
            (('x', 43), ('y', 64), ('z', 85)))
コード例 #6
0
    def test_matmul_b(self) -> None:
        # lhs: frame, rhs: array

        f1 = Frame.from_items((('a', (1, 2, 3)), ('b', (3, 4, 5))),
                              index=('x', 'y', 'z'))

        # get an auto incremented integer columns
        self.assertEqual(
            matmul(f1,
                   np.arange(10).reshape(2, 5)).to_pairs(0),
            ((0, (('x', 15), ('y', 20), ('z', 25))),
             (1, (('x', 19), ('y', 26), ('z', 33))), (2, (('x', 23), ('y', 32),
                                                          ('z', 41))),
             (3, (('x', 27), ('y', 38), ('z', 49))), (4, (('x', 31), ('y', 44),
                                                          ('z', 57)))))
コード例 #7
0
    def test_frame_iter_array_b(self) -> None:

        arrays = list(np.random.rand(1000) for _ in range(100))
        f1 = Frame.from_items(
                zip(range(100), arrays)
                )

        # iter columns
        post = f1.iter_array(axis=0).apply_pool(np.sum, max_workers=4, use_threads=True)
        self.assertEqual(post.shape, (100,))
        self.assertAlmostEqual(f1.sum().sum(), post.sum())

        post = f1.iter_array(axis=0).apply_pool(np.sum, max_workers=4, use_threads=False)
        self.assertEqual(post.shape, (100,))
        self.assertAlmostEqual(f1.sum().sum(), post.sum())
コード例 #8
0
    def test_matmul_c(self) -> None:
        # lhs: frame, rhs: Series, 1D array

        f1 = Frame.from_items((('a', (1, 2, 3)), ('b', (3, 4, 5))),
                              index=('x', 'y', 'z'))
        s1 = Series((10, 11), index=('a', 'b'))

        self.assertEqual(
            matmul(f1, s1).to_pairs(), (('x', 43), ('y', 64), ('z', 85)))

        self.assertEqual(
            matmul(f1, s1.values).to_pairs(),
            (('x', 43), ('y', 64), ('z', 85)))

        with self.assertRaises(RuntimeError):
            matmul(f1, np.arange(20).reshape(5, 4))
コード例 #9
0
    def test_matmul_a(self) -> None:
        # lhs: frame, rhs: array

        f1 = Frame.from_items((('a', (1, 2, 3)), ('b', (3, 4, 5))),
                              index=('x', 'y', 'z'))

        self.assertEqual(
            matmul(f1, [4, 3]).to_pairs(), (('x', 13), ('y', 20), ('z', 27)))

        self.assertEqual(
            matmul(f1, np.array([4, 3])).to_pairs(),
            (('x', 13), ('y', 20), ('z', 27)))

        self.assertEqual(
            matmul(f1, [3, 4]).to_pairs(), (('x', 15), ('y', 22), ('z', 29)))

        self.assertEqual(
            matmul(f1, np.array([3, 4])).to_pairs(),
            (('x', 15), ('y', 22), ('z', 29)))