|
| 1 | +import numpy as np |
| 2 | +from numpy.testing import assert_array_equal |
| 3 | + |
| 4 | +import cubed |
| 5 | +import cubed.array_api as xp |
| 6 | + |
| 7 | + |
| 8 | +def test_map_overlap_1d(): |
| 9 | + x = np.arange(6) |
| 10 | + a = xp.asarray(x, chunks=(3,)) |
| 11 | + |
| 12 | + b = cubed.map_overlap( |
| 13 | + lambda x: x, |
| 14 | + a, |
| 15 | + dtype=a.dtype, |
| 16 | + chunks=((5, 5),), |
| 17 | + depth=1, |
| 18 | + boundary=0, |
| 19 | + trim=False, |
| 20 | + ) |
| 21 | + |
| 22 | + assert_array_equal(b.compute(), np.array([0, 0, 1, 2, 3, 2, 3, 4, 5, 0])) |
| 23 | + |
| 24 | + |
| 25 | +def test_map_overlap_2d(): |
| 26 | + x = np.arange(36).reshape((6, 6)) |
| 27 | + a = xp.asarray(x, chunks=(3, 3)) |
| 28 | + |
| 29 | + b = cubed.map_overlap( |
| 30 | + lambda x: x, |
| 31 | + a, |
| 32 | + dtype=a.dtype, |
| 33 | + chunks=((7, 7), (5, 5)), |
| 34 | + depth={0: 2, 1: 1}, |
| 35 | + boundary={0: 100, 1: 200}, |
| 36 | + trim=False, |
| 37 | + ) |
| 38 | + |
| 39 | + expected = np.array( |
| 40 | + [ |
| 41 | + [200, 100, 100, 100, 100, 100, 100, 100, 100, 200], |
| 42 | + [200, 100, 100, 100, 100, 100, 100, 100, 100, 200], |
| 43 | + [200, 0, 1, 2, 3, 2, 3, 4, 5, 200], |
| 44 | + [200, 6, 7, 8, 9, 8, 9, 10, 11, 200], |
| 45 | + [200, 12, 13, 14, 15, 14, 15, 16, 17, 200], |
| 46 | + [200, 18, 19, 20, 21, 20, 21, 22, 23, 200], |
| 47 | + [200, 24, 25, 26, 27, 26, 27, 28, 29, 200], |
| 48 | + [200, 6, 7, 8, 9, 8, 9, 10, 11, 200], |
| 49 | + [200, 12, 13, 14, 15, 14, 15, 16, 17, 200], |
| 50 | + [200, 18, 19, 20, 21, 20, 21, 22, 23, 200], |
| 51 | + [200, 24, 25, 26, 27, 26, 27, 28, 29, 200], |
| 52 | + [200, 30, 31, 32, 33, 32, 33, 34, 35, 200], |
| 53 | + [200, 100, 100, 100, 100, 100, 100, 100, 100, 200], |
| 54 | + [200, 100, 100, 100, 100, 100, 100, 100, 100, 200], |
| 55 | + ] |
| 56 | + ) |
| 57 | + |
| 58 | + assert_array_equal(b.compute(), expected) |
| 59 | + |
| 60 | + |
| 61 | +def test_map_overlap_trim(): |
| 62 | + x = np.array([1, 1, 2, 3, 5, 8, 13, 21]) |
| 63 | + a = xp.asarray(x, chunks=5) |
| 64 | + |
| 65 | + def derivative(x): |
| 66 | + out = x - np.roll(x, 1) |
| 67 | + return out[1:-1] # manual trim |
| 68 | + |
| 69 | + b = cubed.map_overlap( |
| 70 | + derivative, |
| 71 | + a, |
| 72 | + dtype=a.dtype, |
| 73 | + chunks=a.chunks, |
| 74 | + depth=1, |
| 75 | + boundary=0, |
| 76 | + trim=False, |
| 77 | + ) |
| 78 | + |
| 79 | + assert_array_equal(b.compute(), np.array([1, 0, 1, 1, 2, 3, 5, 8])) |
0 commit comments