(This docstring should be overwritten)
(func1d, axis, arr, *args, **kwargs)
| 368 | |
| 369 | |
| 370 | def apply_along_axis(func1d, axis, arr, *args, **kwargs): |
| 371 | """ |
| 372 | (This docstring should be overwritten) |
| 373 | """ |
| 374 | arr = array(arr, copy=False, subok=True) |
| 375 | nd = arr.ndim |
| 376 | axis = normalize_axis_index(axis, nd) |
| 377 | ind = [0] * (nd - 1) |
| 378 | i = np.zeros(nd, 'O') |
| 379 | indlist = list(range(nd)) |
| 380 | indlist.remove(axis) |
| 381 | i[axis] = slice(None, None) |
| 382 | outshape = np.asarray(arr.shape).take(indlist) |
| 383 | i.put(indlist, ind) |
| 384 | res = func1d(arr[tuple(i.tolist())], *args, **kwargs) |
| 385 | # if res is a number, then we have a smaller output array |
| 386 | asscalar = np.isscalar(res) |
| 387 | if not asscalar: |
| 388 | try: |
| 389 | len(res) |
| 390 | except TypeError: |
| 391 | asscalar = True |
| 392 | # Note: we shouldn't set the dtype of the output from the first result |
| 393 | # so we force the type to object, and build a list of dtypes. We'll |
| 394 | # just take the largest, to avoid some downcasting |
| 395 | dtypes = [] |
| 396 | if asscalar: |
| 397 | dtypes.append(np.asarray(res).dtype) |
| 398 | outarr = zeros(outshape, object) |
| 399 | outarr[tuple(ind)] = res |
| 400 | Ntot = np.prod(outshape) |
| 401 | k = 1 |
| 402 | while k < Ntot: |
| 403 | # increment the index |
| 404 | ind[-1] += 1 |
| 405 | n = -1 |
| 406 | while (ind[n] >= outshape[n]) and (n > (1 - nd)): |
| 407 | ind[n - 1] += 1 |
| 408 | ind[n] = 0 |
| 409 | n -= 1 |
| 410 | i.put(indlist, ind) |
| 411 | res = func1d(arr[tuple(i.tolist())], *args, **kwargs) |
| 412 | outarr[tuple(ind)] = res |
| 413 | dtypes.append(asarray(res).dtype) |
| 414 | k += 1 |
| 415 | else: |
| 416 | res = array(res, copy=False, subok=True) |
| 417 | j = i.copy() |
| 418 | j[axis] = ([slice(None, None)] * res.ndim) |
| 419 | j.put(indlist, ind) |
| 420 | Ntot = np.prod(outshape) |
| 421 | holdshape = outshape |
| 422 | outshape = list(arr.shape) |
| 423 | outshape[axis] = res.shape |
| 424 | dtypes.append(asarray(res).dtype) |
| 425 | outshape = flatten_inplace(outshape) |
| 426 | outarr = zeros(outshape, object) |
| 427 | outarr[tuple(flatten_inplace(j.tolist()))] = res |