Пример #1
0
    def test_padding_vec3_4(self):
        dtype = np.dtype((int, (3,)))
        a = ChunkGrid(2, dtype, np.zeros(3))
        expected = np.zeros((6, 6, 6, 3), dtype=int)

        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            value = np.array([1, 2, 3]) + (n + 1) * 3
            a.set_value(pos, value)
            s = np.sum(pos < 0) + np.sum(2 <= pos)
            if s >= 2:
                continue
            expected[i] = value

        data = a.padding_at((0, 0, 0), 2, corners=False, edges=False)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1, corners=False, edges=False)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))

        expected3_tmp = np.zeros((6, 6, 6, 3), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            value = np.array([1, 2, 3]) + (n + 1) * 3
            expected3_tmp[i] = value
        expected3 = np.zeros((8, 8, 8, 3), dtype=int)
        expected3[1:-1, 1:-1, 1:-1] = expected3_tmp
        data3 = a.padding_at((0, 0, 0), 3, corners=False, edges=False)
        self.assertEqual(expected3.shape, data3.shape)
        self.assertEqual(str(expected3), str(data3))
Пример #2
0
    def test_padding_1(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            expected[i] = n + 1

        data = a.padding_at((0, 0, 0), 2)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))

        expected3_tmp = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            expected3_tmp[i] = n + 1
        expected3 = np.zeros((8, 8, 8), dtype=int)
        expected3[1:-1, 1:-1, 1:-1] = expected3_tmp
        data3 = a.padding_at((0, 0, 0), 3)
        self.assertEqual(expected3.shape, data3.shape)
        self.assertEqual(str(expected3), str(data3))
Пример #3
0
    def test_mask_2(self):
        a = ChunkGrid(2, bool, False)
        a[0, 0, 0] = True
        a.ensure_chunk_at_index((1, 1, 1)).set_fill(True)
        a.set_value((2, 1, 1), True)

        b = a.astype(np.int8)

        adense = a.to_dense()
        bdense = b.to_dense()
        self.assertEqual(adense.shape, bdense.shape)
        self.assertEqual(str(adense.astype(np.int8)), str(bdense))

        mask = ChunkGrid(2, bool, False)
        mask.ensure_chunk_at_index((1, 0, 0)).set_fill(True)
        mask.ensure_chunk_at_index((0, 1, 0)).set_fill(True)
        mask.ensure_chunk_at_index((0, 0, 1))[0, 0, 0] = True

        b[mask] = 2
        bdense = b.to_dense()

        expected = np.zeros((4, 4, 4), dtype=np.int8)
        expected[0, 0, 0] = 1
        expected[2:4, 2:4, 2:4] = 1
        expected[2, 1, 1] = 1
        expected[2:4, 0:2, 0:2] = 2
        expected[0:2, 2:4, 0:2] = 2
        expected[0, 0, 2] = 2

        self.assertEqual(expected.shape, bdense.shape)
        self.assertEqual(str(expected), str(bdense))
Пример #4
0
    def test_padding_2(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            s = np.sum(pos < 0) + np.sum(2 <= pos)
            if s >= 2:
                continue
            expected[i] = n + 1

        data = a.padding_at((0, 0, 0), 2, corners=False, edges=False)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1, corners=False, edges=False)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))

        expected3_tmp = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            expected3_tmp[i] = n + 1
        expected3 = np.zeros((8, 8, 8), dtype=int)
        expected3[1:-1, 1:-1, 1:-1] = expected3_tmp
        data3 = a.padding_at((0, 0, 0), 3, corners=False, edges=False)
        self.assertEqual(expected3.shape, data3.shape)
        self.assertEqual(str(expected3), str(data3))
Пример #5
0
    def test_get_block_1(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            expected[i] = n + 1

        block = a.get_block_at((0, 0, 0), (3, 3, 3), corners=True, edges=True)
        self.assertEqual(str(expected), str(a.block_to_array(block)))
Пример #6
0
    def test_reflected(self):
        grid = ChunkGrid(2, float, -1.0)
        grid.set_value((0, 0, 0), 0.5)
        grid.set_value((0, 0, 1), 1.0)

        result = (0 < grid) & (grid < 1.0)
        actual = result.to_dense()

        expected = np.zeros((2, 2, 2), dtype=bool)
        expected[0, 0, 0] = True

        self.assertEqual(str(expected), str(actual))
Пример #7
0
    def test_eq_1(self):
        a = ChunkGrid(2, bool, False)
        b = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)
        result = (a == b).to_dense()
        self.assertIsInstance((a == b), ChunkGrid)

        expected = np.ones((2, 2, 2), dtype=bool)
        expected[0, 0, 0] = False

        assert result.shape == expected.shape
        self.assertEqual(str(expected), str(result))
Пример #8
0
    def _test_operator1_int(self, op):
        a = ChunkGrid(2, int, -1)

        a.set_value((0, 0, 0), 0)
        a.set_value((0, 0, 1), 2)
        a.set_value((0, 1, 0), -2)

        expected = op(a.to_dense())
        res = op(a)
        self.assertIsInstance(res, ChunkGrid)
        result = res.to_dense()
        assert result.shape == expected.shape
        self.assertTrue(np.all(result == expected), f"Failure {op}! \n{result}\n-------\n{expected}")
Пример #9
0
    def test_get_block_2(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            s = np.sum(pos < 0) + np.sum(2 <= pos)
            if s >= 2:
                continue
            expected[i] = n + 1

        block = a.get_block_at((0, 0, 0), (3, 3, 3), corners=False, edges=False)
        self.assertEqual(str(expected), str(a.block_to_array(block)))
Пример #10
0
    def test_special_dtype(self):

        t = np.dtype((np.int, (3,)))
        a = ChunkGrid(2, dtype=t, fill_value=np.zeros(3))

        a.set_value((0, 0, 0), np.ones(3))
        a.set_value((2, 2, 2), np.ones(3))
        dense = a.to_dense()

        expected = np.zeros((4, 4, 4), dtype=t)
        expected[0, 0, 0] = 1
        expected[2, 2, 2] = 1

        self.assertEqual(expected.shape, dense.shape)
        self.assertEqual(str(expected), str(dense))
Пример #11
0
    def test_eq_int(self):
        a = ChunkGrid(2, int, 0)
        a.set_value((1, 1, 1), 1)
        a.ensure_chunk_at_index((1, 1, 1))

        result = a == 0
        dense = result.to_dense()

        expected = np.ones((4, 4, 4), dtype=bool)
        expected[1, 1, 1] = False

        self.assertEqual(expected.shape, dense.shape)
        self.assertEqual(str(expected), str(dense))
        self.assertTrue(result.any())
        self.assertFalse(result.all())
Пример #12
0
    def test_set_pos(self):
        a = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)
        a.set_value((2, 2, 2), True)

        self.assertFalse(a.chunks[0, 0, 0].is_filled())

        result = a.to_dense()

        expected = np.zeros((4, 4, 4), dtype=bool)
        expected[0, 0, 0] = True
        expected[2, 2, 2] = True

        assert result.shape == expected.shape
        self.assertEqual(str(expected), str(result))
Пример #13
0
    def test_single_negative(self):
        a = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)

        res = dilate(a, steps=1)
        self.assertEqual(4, len(res.chunks))
        c0 = res.chunks.get((0, 0, 0))
        self.assertIsNotNone(c0)
        self.assertFalse(c0.is_filled())

        expected = np.zeros((4, 4, 4), dtype=bool)
        expected[2, 2, 1:4] = True
        expected[2, 1:4, 2] = True
        expected[1:4, 2, 2] = True

        self.assertEqual(str(expected), str(res.to_dense()))
Пример #14
0
    def test_eq_2(self):
        a = ChunkGrid(2, bool, False)
        b = ChunkGrid(2, bool, False)

        a.set_value((0, 0, 0), True)
        a.set_value((2, 2, 2), True)

        comp = a == b
        self.assertIsInstance(comp, ChunkGrid)
        self.assertIs(comp.dtype.type, np.bool8)

        expected = np.ones((4, 4, 4), dtype=bool)
        expected[0, 0, 0] = False
        expected[2, 2, 2] = False

        result = comp.to_dense()
        assert result.shape == expected.shape
        self.assertEqual(str(expected), str(result))
Пример #15
0
    def test_padding_vec3_2(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)

        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            value = (n + 1)
            a.set_value(pos, value)
            expected[i] = value

        data = a.padding_at((0, 0, 0), 2)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))
Пример #16
0
    def test_padding_no_corners_edges(self):
        a = ChunkGrid(2, int, -1)
        expected = np.zeros((6, 6, 6), int)

        for n, i in enumerate(np.ndindex(6, 6, 6)):
            a.set_value(i, n)
            expected[i] = n

        self.assertEqual(expected.shape, tuple(a.size()))
        self.assertEqual(str(expected), str(a.to_dense()))

        padded1 = a.padding_at((1, 1, 1), 1, corners=True, edges=True)
        expected1 = expected[1:-1, 1:-1, 1:-1]
        self.assertEqual(str(expected1), str(padded1))

        padded2 = a.padding_at((1, 1, 1), 1, corners=False, edges=False)
        expected2 = np.full((4, 4, 4), -1, int)
        expected2[1:-1, 1:-1, :] = expected[2:-2, 2:-2, 1:-1]
        expected2[1:-1, :, 1:-1] = expected[2:-2, 1:-1, 2:-2]
        expected2[:, 1:-1, 1:-1] = expected[1:-1, 2:-2, 2:-2]
        self.assertEqual(str(expected2), str(padded2))
Пример #17
0
    def _test_operator2_bool(self, op, inplace=False):
        a = ChunkGrid(2, bool, False)
        b = ChunkGrid(2, bool, True)

        a.set_value((0, 0, 0), True)
        a.set_value((0, 0, 1), True)

        b.set_value((0, 0, 0), True)
        b.set_value((0, 0, 1), False)
        b.set_value((0, 1, 0), False)
        b.set_value((0, 1, 1), False)

        a0 = a.copy()
        b0 = b.copy()
        expected = op(a.to_dense(), b.to_dense())
        res = op(a, b)
        self.assertIsInstance(res, ChunkGrid)
        result = res.to_dense()
        assert result.shape == expected.shape
        self.assertTrue(np.all(result == expected), f"Failure {op}! \n{result}\n-------\n{expected}")
        self._test_inplace_modified(op, a, a0, b, b0, inplace)
Пример #18
0
    def test_eq_and_combined(self):
        a = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)
        a.set_value((0, 0, 1), True)
        a.set_value((0, 1, 1), True)

        b = ChunkGrid(2, int, 0)
        b.set_value((0, 0, 1), 1)
        b.set_value((0, 1, 1), 2)
        b.set_value((1, 1, 1), 1)

        tmp = (b == 1)
        self.assertEqual(1, len(tmp.chunks))
        self.assertFalse(next(iter(tmp.chunks)).is_filled())

        result = tmp & a
        self.assertEqual(1, len(result.chunks))
        self.assertFalse(next(iter(result.chunks)).is_filled())

        expected_tmp = np.zeros((2, 2, 2), dtype=bool)
        expected_tmp[0, 0, 1] = True
        expected_tmp[1, 1, 1] = True

        self.assertEqual(str(expected_tmp), str(tmp.to_dense()))

        expected = np.zeros((2, 2, 2), dtype=bool)
        expected[0, 0, 1] = True
        self.assertEqual(str(expected), str(result.to_dense()))

        values = list(tmp.items(mask=a))
        self.assertEqual(3, len(values))
        self.assertEqual(str([
            (np.array([0, 0, 0]), False),
            (np.array([0, 0, 1]), True),
            (np.array([0, 1, 1]), False)
        ]), str(values))
Пример #19
0
    def test_padding_vec3_1(self):
        dtype = np.dtype((int, (3,)))
        a = ChunkGrid(2, dtype, np.zeros(3))
        a.set_value((0, 0, 2), np.ones(3) * 1)
        a.set_value((0, 2, 0), np.ones(3) * 2)
        a.set_value((2, 0, 0), np.ones(3) * 3)
        a.set_value((1, 1, 1), np.ones(3) * 4)

        a.set_value((2, 2, 2), np.ones(3) * -1)
        a.set_value((-1, -1, -1), np.ones(3) * -2)

        a.set_value((2, -1, -1), np.ones(3) * -3)
        a.set_value((-1, 2, -1), np.ones(3) * -4)
        a.set_value((-1, -1, 2), np.ones(3) * -5)

        a.set_value((2, 2, -1), np.ones(3) * -6)
        a.set_value((2, -1, 2), np.ones(3) * -7)
        a.set_value((-1, 2, 2), np.ones(3) * -8)

        pad = a.padding_at((0, 0, 0), 1, corners=False)

        expected = np.zeros((4, 4, 4, 3), dtype=int)
        expected[1, 1, 3] = 1
        expected[1, 3, 1] = 2
        expected[3, 1, 1] = 3
        expected[2, 2, 2] = 4

        self.assertEqual(expected.shape, pad.shape)
        self.assertEqual(str(expected), str(pad))