forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_slice.py
More file actions
83 lines (68 loc) · 2.37 KB
/
Copy pathtest_slice.py
File metadata and controls
83 lines (68 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pytest
import iarray as ia
import numpy as np
from math import isclose
# Slice
slice_data = [
([30, 100], [20, 20], [10, 13], True, None),
([30, 130], [50, 50], [20, 25], False, None),
pytest.param([10, 78, 55, 21], [3, 30, 30, 21], [3, 12, 6, 21], True, "test_slice_acontiguous.iarr", marks=pytest.mark.heavy),
([30, 100], [30, 44], [30, 2], False, "test_slice_asparse.iarr"),
]
@pytest.mark.parametrize(
"slices",
[
(0, 1), # test with scalars
slice(10, 20),
9,
(slice(5, 30), slice(10, 40)),
(slice(5, 5), slice(5, 23)),
(slice(5, 5), slice(3, 12)),
(slice(5, 30), 47, ...),
(..., slice(5, 6)),
],
)
@pytest.mark.parametrize("dtype", [np.float32, np.float64, np.int64, np.int32, np.uint64, np.uint32])
@pytest.mark.parametrize(
"shape, chunks, blocks, acontiguous, aurlpath",
slice_data,
)
def test_slice(slices, shape, chunks, blocks, dtype, acontiguous, aurlpath):
ia.remove_urlpath(aurlpath)
cfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=acontiguous, urlpath=aurlpath)
max = 1
if dtype not in [np.float64, np.float32]:
for i in range(len(shape)):
max *= shape[i]
a = ia.arange(shape, 0, max, cfg=cfg, mode="w", dtype=dtype)
an = ia.iarray2numpy(a)
a[slices] = 0
an[slices] = 0
np.testing.assert_almost_equal(a.data, an)
data = ia.arange(shape, dtype=dtype)[slices]
a[slices] = data
an[slices] = data.data if isinstance(data, ia.IArray) else data
np.testing.assert_almost_equal(a.data, an)
b = a[slices]
an2 = an[slices]
if b.ndim == 0:
isclose(an2, b)
else:
bn = ia.iarray2numpy(b)
assert an2.shape == bn.shape
assert an2.ndim == bn.ndim
np.testing.assert_almost_equal(an[slices], bn)
ia.remove_urlpath(aurlpath)
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize(
"shape, chunks, blocks, acontiguous, aurlpath",
slice_data,
)
def test_double_slice(shape, chunks, blocks, dtype, acontiguous, aurlpath):
ia.remove_urlpath(aurlpath)
cfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=acontiguous, urlpath=aurlpath)
a = ia.linspace(shape, -10, 10, cfg=cfg, mode="a", dtype=dtype)
b1 = a[4]
b2 = a[4]
np.testing.assert_almost_equal(b1.data, b2.data)
ia.remove_urlpath(aurlpath)