|
28 | 28 | unique_int_1d, |
29 | 29 | find_fragments, |
30 | 30 | _in2d, |
| 31 | + inverse_int_index, |
31 | 32 | ) |
32 | 33 |
|
33 | 34 |
|
@@ -103,3 +104,52 @@ def test_in2d_VE(arr1, arr2): |
103 | 104 | ValueError, match=r"Both arrays must be \(n, 2\) arrays" |
104 | 105 | ): |
105 | 106 | _in2d(arr1, arr2) |
| 107 | + |
| 108 | + |
| 109 | +def _python_reference_mask(ix, indices): |
| 110 | + mask = np.zeros_like(ix) |
| 111 | + for i, x in enumerate(indices): |
| 112 | + values = np.where(ix == x)[0] |
| 113 | + mask[values] = i |
| 114 | + return mask |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.parametrize( |
| 118 | + "ix,indices", |
| 119 | + [ |
| 120 | + # unsorted and not unique |
| 121 | + ( |
| 122 | + np.array([1, 5, 3, 3, 6], dtype=np.intp), |
| 123 | + np.array([1, 5, 3, 6], dtype=np.intp), |
| 124 | + ), |
| 125 | + # sorted and not unique |
| 126 | + ( |
| 127 | + np.array([1, 3, 3, 5, 6], dtype=np.intp), |
| 128 | + np.array([1, 3, 5, 6], dtype=np.intp), |
| 129 | + ), |
| 130 | + # unsorted and unique |
| 131 | + ( |
| 132 | + np.array([1, 5, 3, 6], dtype=np.intp), |
| 133 | + np.array([1, 5, 3, 6], dtype=np.intp), |
| 134 | + ), |
| 135 | + # sorted and unique |
| 136 | + ( |
| 137 | + np.array([1, 3, 5, 6], dtype=np.intp), |
| 138 | + np.array([1, 3, 5, 6], dtype=np.intp), |
| 139 | + ), |
| 140 | + # all elements identical |
| 141 | + ( |
| 142 | + np.array([5, 5, 5], dtype=np.intp), |
| 143 | + np.array([5], dtype=np.intp), |
| 144 | + ), |
| 145 | + # single element |
| 146 | + ( |
| 147 | + np.array([7], dtype=np.intp), |
| 148 | + np.array([7], dtype=np.intp), |
| 149 | + ), |
| 150 | + ], |
| 151 | +) |
| 152 | +def test_inverse_int_index(ix, indices): |
| 153 | + pyref = _python_reference_mask(ix, indices) |
| 154 | + cy = inverse_int_index(ix, indices) |
| 155 | + assert_equal(pyref, cy) |
0 commit comments