This works by automatically chunking the reduced axes to a single chunk if necessary and then calling ``numpy.median`` function across the remaining dimensions
(a, axis=None, keepdims=False, out=None)
| 1482 | |
| 1483 | @derived_from(np) |
| 1484 | def median(a, axis=None, keepdims=False, out=None): |
| 1485 | """ |
| 1486 | This works by automatically chunking the reduced axes to a single chunk if necessary |
| 1487 | and then calling ``numpy.median`` function across the remaining dimensions |
| 1488 | """ |
| 1489 | if axis is None: |
| 1490 | raise NotImplementedError( |
| 1491 | "The da.median function only works along an axis. " |
| 1492 | "The full algorithm is difficult to do in parallel" |
| 1493 | ) |
| 1494 | |
| 1495 | if not isinstance(axis, Iterable): |
| 1496 | axis = (axis,) |
| 1497 | |
| 1498 | axis = [ax + a.ndim if ax < 0 else ax for ax in axis] |
| 1499 | |
| 1500 | # rechunk if reduced axes are not contained in a single chunk |
| 1501 | if builtins.any(a.numblocks[ax] > 1 for ax in axis): |
| 1502 | a = a.rechunk({ax: -1 if ax in axis else "auto" for ax in range(a.ndim)}) |
| 1503 | |
| 1504 | result = a.map_blocks( |
| 1505 | np.median, |
| 1506 | axis=axis, |
| 1507 | keepdims=keepdims, |
| 1508 | drop_axis=axis if not keepdims else None, |
| 1509 | chunks=( |
| 1510 | [1 if ax in axis else c for ax, c in enumerate(a.chunks)] |
| 1511 | if keepdims |
| 1512 | else None |
| 1513 | ), |
| 1514 | ) |
| 1515 | |
| 1516 | result = handle_out(out, result) |
| 1517 | return result |
| 1518 | |
| 1519 | |
| 1520 | @derived_from(np) |
nothing calls this directly
no test coverage detected
searching dependent graphs…