|
19 | 19 | from keras.src.testing.test_utils import named_product
|
20 | 20 |
|
21 | 21 |
|
| 22 | +class NumPyTestRot90(testing.TestCase): |
| 23 | + def test_basic(self): |
| 24 | + array = np.array([[1, 2], [3, 4]]) |
| 25 | + rotated = knp.rot90(array) |
| 26 | + expected = np.array([[2, 4], [1, 3]]) |
| 27 | + assert np.array_equal(rotated, expected), f"Failed basic 2D test: {rotated}" |
| 28 | + |
| 29 | + def test_multiple_k(self): |
| 30 | + array = np.array([[1, 2], [3, 4]]) |
| 31 | + |
| 32 | + # k=2 (180 degrees rotation) |
| 33 | + rotated = knp.rot90(array, k=2) |
| 34 | + expected = np.array([[4, 3], [2, 1]]) |
| 35 | + assert np.array_equal(rotated, expected), f"Failed k=2 test: {rotated}" |
| 36 | + |
| 37 | + # k=3 (270 degrees rotation) |
| 38 | + rotated = knp.rot90(array, k=3) |
| 39 | + expected = np.array([[3, 1], [4, 2]]) |
| 40 | + assert np.array_equal(rotated, expected), f"Failed k=3 test: {rotated}" |
| 41 | + |
| 42 | + # k=4 (full rotation) |
| 43 | + rotated = knp.rot90(array, k=4) |
| 44 | + expected = array |
| 45 | + assert np.array_equal(rotated, expected), f"Failed k=4 test: {rotated}" |
| 46 | + |
| 47 | + def test_axes(self): |
| 48 | + array = np.arange(8).reshape((2, 2, 2)) |
| 49 | + rotated = knp.rot90(array, k=1, axes=(1, 2)) |
| 50 | + expected = np.array([[[1, 3], [0, 2]], [[5, 7], [4, 6]]]) |
| 51 | + assert np.array_equal(rotated, expected), f"Failed custom axes test: {rotated}" |
| 52 | + |
| 53 | + def test_single_image(self): |
| 54 | + array = np.random.random((4, 4, 3)) |
| 55 | + rotated = knp.rot90(array, k=1, axes=(0, 1)) |
| 56 | + expected = np.rot90(array, k=1, axes=(0, 1)) |
| 57 | + assert np.allclose(rotated, expected), "Failed single image test" |
| 58 | + |
| 59 | + def test_batch_images(self): |
| 60 | + array = np.random.random((2, 4, 4, 3)) |
| 61 | + rotated = knp.rot90(array, k=1, axes=(1, 2)) |
| 62 | + expected = np.rot90(array, k=1, axes=(1, 2)) |
| 63 | + assert np.allclose(rotated, expected), "Failed batch images test" |
| 64 | + |
| 65 | + def test_invalid_axes(self): |
| 66 | + array = np.array([[1, 2], [3, 4]]) |
| 67 | + try: |
| 68 | + knp.rot90(array, axes=(0, 0)) |
| 69 | + except ValueError as e: |
| 70 | + assert ( |
| 71 | + "Invalid axes: (0, 0). Axes must be a tuple of two different dimensions." |
| 72 | + in str(e) |
| 73 | + ), f"Failed invalid axes test: {e}" |
| 74 | + else: |
| 75 | + raise AssertionError("Failed to raise error for invalid axes") |
| 76 | + |
| 77 | + def test_invalid_rank(self): |
| 78 | + array = np.array([1, 2, 3]) # 1D array |
| 79 | + try: |
| 80 | + knp.rot90(array) |
| 81 | + except ValueError as e: |
| 82 | + assert ( |
| 83 | + "Input array must have at least 2 dimensions." in str(e) |
| 84 | + ), f"Failed invalid rank test: {e}" |
| 85 | + else: |
| 86 | + raise AssertionError("Failed to raise error for invalid input rank") |
| 87 | + |
| 88 | + |
22 | 89 | class NumpyTwoInputOpsDynamicShapeTest(testing.TestCase):
|
23 | 90 | def test_add(self):
|
24 | 91 | x = KerasTensor((None, 3))
|
|
0 commit comments