(self, cls=np.ndarray)
| 201 | assert_array_equal(res, np.array([3, 3, 3, 3, 3, 3]).view(cls)) |
| 202 | |
| 203 | def test_axis_insertion(self, cls=np.ndarray): |
| 204 | def f1to2(x): |
| 205 | """produces an asymmetric non-square matrix from x""" |
| 206 | assert_equal(x.ndim, 1) |
| 207 | return (x[::-1] * x[1:, None]).view(cls) |
| 208 | |
| 209 | a2d = np.arange(6 * 3).reshape((6, 3)) |
| 210 | |
| 211 | # 2d insertion along first axis |
| 212 | actual = apply_along_axis(f1to2, 0, a2d) |
| 213 | expected = np.stack([ |
| 214 | f1to2(a2d[:, i]) for i in range(a2d.shape[1]) |
| 215 | ], axis=-1).view(cls) |
| 216 | assert_equal(type(actual), type(expected)) |
| 217 | assert_equal(actual, expected) |
| 218 | |
| 219 | # 2d insertion along last axis |
| 220 | actual = apply_along_axis(f1to2, 1, a2d) |
| 221 | expected = np.stack([ |
| 222 | f1to2(a2d[i, :]) for i in range(a2d.shape[0]) |
| 223 | ], axis=0).view(cls) |
| 224 | assert_equal(type(actual), type(expected)) |
| 225 | assert_equal(actual, expected) |
| 226 | |
| 227 | # 3d insertion along middle axis |
| 228 | a3d = np.arange(6 * 5 * 3).reshape((6, 5, 3)) |
| 229 | |
| 230 | actual = apply_along_axis(f1to2, 1, a3d) |
| 231 | expected = np.stack([ |
| 232 | np.stack([ |
| 233 | f1to2(a3d[i, :, j]) for i in range(a3d.shape[0]) |
| 234 | ], axis=0) |
| 235 | for j in range(a3d.shape[2]) |
| 236 | ], axis=-1).view(cls) |
| 237 | assert_equal(type(actual), type(expected)) |
| 238 | assert_equal(actual, expected) |
| 239 | |
| 240 | def test_subclass_preservation(self): |
| 241 | class MinimalSubclass(np.ndarray): |
no test coverage detected