MCPcopy Create free account
hub / github.com/numpy/numpy / test_axis_insertion

Method test_axis_insertion

numpy/lib/tests/test_shape_base.py:174–209  ·  view source on GitHub ↗
(self, cls=np.ndarray)

Source from the content-addressed store, hash-verified

172 assert_array_equal(res, np.array([3, 3, 3, 3, 3, 3]).view(cls))
173
174 def test_axis_insertion(self, cls=np.ndarray):
175 def f1to2(x):
176 """produces an asymmetric non-square matrix from x"""
177 assert_equal(x.ndim, 1)
178 return (x[::-1] * x[1:,None]).view(cls)
179
180 a2d = np.arange(6*3).reshape((6, 3))
181
182 # 2d insertion along first axis
183 actual = apply_along_axis(f1to2, 0, a2d)
184 expected = np.stack([
185 f1to2(a2d[:,i]) for i in range(a2d.shape[1])
186 ], axis=-1).view(cls)
187 assert_equal(type(actual), type(expected))
188 assert_equal(actual, expected)
189
190 # 2d insertion along last axis
191 actual = apply_along_axis(f1to2, 1, a2d)
192 expected = np.stack([
193 f1to2(a2d[i,:]) for i in range(a2d.shape[0])
194 ], axis=0).view(cls)
195 assert_equal(type(actual), type(expected))
196 assert_equal(actual, expected)
197
198 # 3d insertion along middle axis
199 a3d = np.arange(6*5*3).reshape((6, 5, 3))
200
201 actual = apply_along_axis(f1to2, 1, a3d)
202 expected = np.stack([
203 np.stack([
204 f1to2(a3d[i,:,j]) for i in range(a3d.shape[0])
205 ], axis=0)
206 for j in range(a3d.shape[2])
207 ], axis=-1).view(cls)
208 assert_equal(type(actual), type(expected))
209 assert_equal(actual, expected)
210
211 def test_subclass_preservation(self):
212 class MinimalSubclass(np.ndarray):

Callers 1

Calls 4

apply_along_axisFunction · 0.90
assert_equalFunction · 0.90
reshapeMethod · 0.80
viewMethod · 0.45

Tested by

no test coverage detected