|
| 1 | +import pytest |
| 2 | +import cupy as cp |
| 3 | + |
| 4 | +from httomolibgpu.misc.sorting import argsort_with_reverse |
| 5 | + |
| 6 | + |
| 7 | +class TestArgsort1D: |
| 8 | + def test_1d_sorted(self): |
| 9 | + data = cp.array([1, 2, 3, 4, 5]) |
| 10 | + sort_idx, rev_idx = argsort_with_reverse(data) |
| 11 | + cp.testing.assert_array_equal(sort_idx, cp.array([0, 1, 2, 3, 4])) |
| 12 | + cp.testing.assert_array_equal(data[sort_idx], data) |
| 13 | + cp.testing.assert_array_equal(rev_idx[sort_idx], cp.arange(5)) |
| 14 | + |
| 15 | + def test_1d_reverse_sorted(self): |
| 16 | + data = cp.array([5, 4, 3, 2, 1]) |
| 17 | + sort_idx, rev_idx = argsort_with_reverse(data) |
| 18 | + cp.testing.assert_array_equal(sort_idx, cp.array([4, 3, 2, 1, 0])) |
| 19 | + cp.testing.assert_array_equal(data[sort_idx], cp.array([1, 2, 3, 4, 5])) |
| 20 | + cp.testing.assert_array_equal(rev_idx[sort_idx], cp.arange(5)) |
| 21 | + |
| 22 | + def test_1d_random(self): |
| 23 | + data = cp.array([10, 3, 7, 1, 9]) |
| 24 | + sort_idx, rev_idx = argsort_with_reverse(data) |
| 25 | + sorted_data = data[sort_idx] |
| 26 | + assert cp.all(sorted_data[:-1] <= sorted_data[1:]) |
| 27 | + cp.testing.assert_array_equal(rev_idx[sort_idx], cp.arange(5)) |
| 28 | + |
| 29 | + def test_1d_duplicates(self): |
| 30 | + data = cp.array([2, 1, 2, 1]) |
| 31 | + sort_idx, rev_idx = argsort_with_reverse(data) |
| 32 | + sorted_data = data[sort_idx] |
| 33 | + assert cp.all(sorted_data[:-1] <= sorted_data[1:]) |
| 34 | + cp.testing.assert_array_equal(rev_idx[sort_idx], cp.arange(4)) |
| 35 | + |
| 36 | + def test_1d_single_element(self): |
| 37 | + data = cp.array([42]) |
| 38 | + sort_idx, rev_idx = argsort_with_reverse(data) |
| 39 | + cp.testing.assert_array_equal(sort_idx, cp.array([0])) |
| 40 | + cp.testing.assert_array_equal(rev_idx[sort_idx], cp.array([0])) |
| 41 | + |
| 42 | + def test_1d_negative_axis(self): |
| 43 | + data = cp.array([3, 1, 2]) |
| 44 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=-1) |
| 45 | + cp.testing.assert_array_equal(sort_idx, cp.argsort(data)) |
| 46 | + cp.testing.assert_array_equal(rev_idx[sort_idx], cp.arange(3)) |
| 47 | + |
| 48 | + |
| 49 | +class TestArgsort2D: |
| 50 | + def test_2d_axis0_sorted(self): |
| 51 | + data = cp.array([[1, 2], [3, 4], [5, 6]]) |
| 52 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=0) |
| 53 | + sorted_data = data[sort_idx, cp.arange(data.shape[1])[None, :]] |
| 54 | + assert cp.all(sorted_data[:-1, :] <= sorted_data[1:, :]) |
| 55 | + rows, cols = data.shape |
| 56 | + col_indices = cp.arange(cols)[None, :] |
| 57 | + cp.testing.assert_array_equal( |
| 58 | + rev_idx[sort_idx, col_indices], cp.tile(cp.arange(rows)[:, None], [1, 2]) |
| 59 | + ) |
| 60 | + |
| 61 | + def test_2d_axis0_random(self): |
| 62 | + data = cp.array([[5, 1], [2, 4], [3, 6]]) |
| 63 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=0) |
| 64 | + rows, cols = data.shape |
| 65 | + col_indices = cp.arange(cols)[None, :] |
| 66 | + cp.testing.assert_array_equal( |
| 67 | + rev_idx[sort_idx, col_indices], cp.tile(cp.arange(rows)[:, None], [1, 2]) |
| 68 | + ) |
| 69 | + |
| 70 | + def test_2d_axis0_negative_axis(self): |
| 71 | + data = cp.array([[1, 2], [3, 4]]) |
| 72 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=-2) |
| 73 | + rows, cols = data.shape |
| 74 | + col_indices = cp.arange(cols)[None, :] |
| 75 | + cp.testing.assert_array_equal( |
| 76 | + rev_idx[sort_idx, col_indices], cp.tile(cp.arange(rows)[:, None], [1, 2]) |
| 77 | + ) |
| 78 | + |
| 79 | + def test_2d_axis1_sorted(self): |
| 80 | + data = cp.array([[1, 2, 3], [4, 5, 6]]) |
| 81 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=1) |
| 82 | + rows, cols = data.shape |
| 83 | + row_indices = cp.arange(rows)[:, None] |
| 84 | + cp.testing.assert_array_equal( |
| 85 | + rev_idx[row_indices, sort_idx], cp.tile(cp.arange(cols), [2, 1]) |
| 86 | + ) |
| 87 | + |
| 88 | + def test_2d_axis1_random(self): |
| 89 | + data = cp.array([[3, 1, 2], [6, 4, 5]]) |
| 90 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=1) |
| 91 | + rows, cols = data.shape |
| 92 | + row_indices = cp.arange(rows)[:, None] |
| 93 | + cp.testing.assert_array_equal( |
| 94 | + rev_idx[row_indices, sort_idx], cp.tile(cp.arange(cols), [2, 1]) |
| 95 | + ) |
| 96 | + |
| 97 | + def test_2d_axis1_negative_axis(self): |
| 98 | + data = cp.array([[1, 2], [3, 4]]) |
| 99 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=-1) |
| 100 | + rows, cols = data.shape |
| 101 | + row_indices = cp.arange(rows)[:, None] |
| 102 | + cp.testing.assert_array_equal( |
| 103 | + rev_idx[row_indices, sort_idx], cp.tile(cp.arange(cols), [2, 1]) |
| 104 | + ) |
| 105 | + |
| 106 | + def test_2d_single_row(self): |
| 107 | + data = cp.array([[3, 1, 2]]) |
| 108 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=1) |
| 109 | + rows, cols = data.shape |
| 110 | + row_indices = cp.arange(rows)[:, None] |
| 111 | + cp.testing.assert_array_equal( |
| 112 | + rev_idx[row_indices, sort_idx], cp.arange(cols)[None, :] |
| 113 | + ) |
| 114 | + |
| 115 | + def test_2d_single_col(self): |
| 116 | + data = cp.array([[3], [1], [2]]) |
| 117 | + sort_idx, rev_idx = argsort_with_reverse(data, axis=0) |
| 118 | + rows, cols = data.shape |
| 119 | + col_indices = cp.arange(cols)[None, :] |
| 120 | + cp.testing.assert_array_equal( |
| 121 | + rev_idx[sort_idx, col_indices], cp.arange(rows)[:, None] |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +class TestArgsortErrors: |
| 126 | + def test_invalid_dim_3d(self): |
| 127 | + data = cp.ones((2, 2, 2)) |
| 128 | + with pytest.raises(ValueError): |
| 129 | + argsort_with_reverse(data) |
| 130 | + |
| 131 | + def test_invalid_dim_0d(self): |
| 132 | + data = cp.array(1) |
| 133 | + with pytest.raises(ValueError): |
| 134 | + argsort_with_reverse(data) |
| 135 | + |
| 136 | + def test_invalid_axis_1d(self): |
| 137 | + data = cp.array([1, 2, 3]) |
| 138 | + with pytest.raises(ValueError): |
| 139 | + argsort_with_reverse(data, axis=1) |
| 140 | + |
| 141 | + def test_invalid_axis_2d(self): |
| 142 | + data = cp.array([[1, 2], [3, 4]]) |
| 143 | + with pytest.raises(ValueError): |
| 144 | + argsort_with_reverse(data, axis=2) |
0 commit comments