ftsdata.apply_blockwise
- ftsdata.apply_blockwise(func, data, axis, n_out, *, mask=None, out_dtype, bytes_work_per_pix, mem_safety=0.8, small_cube_bytes=10000000.0, ncpu=None, mem_available=None, use_dask=None)[source]
Apply
functo a cube in parallel, over blocks of pixels.The transform
axisis never sliced: every task receives the full extent ofdataalongaxisand returnsn_outsamples along that axis. The remaining (pixel) axes are split into contiguous blocks, one per thread of aconcurrent.futures.ThreadPoolExecutor, or one per dask worker ifuse_daskis True.- Parameters:
- funccallable
Function applied to each block, called as
func(block, mask_block, workers=...).blockis anumpy.ndarraywith the pixel axes restricted to the block and the transform axis complete.mask_blockis the matching slice ofmask, orNoneifmaskisNone. It must return an array with the same shape asblock, except alongaxiswhere it must haven_outsamples.- dataarray_like
Input cube. It is only read, never modified in place.
- axisint
Index of the transform axis, kept whole in every block.
- n_outint
Size of the output along
axis.- maskarray_like, optional
Boolean mask with the same shape as
data, passed tofuncblock by block. Default isNone.- out_dtypenumpy.dtype
dtype of the allocated output array.
- bytes_work_per_pixfloat
Estimated peak memory, in bytes, used by
funcper output pixel. Together with the available memory and the number of threads, it sets the size of a block.- mem_safetyfloat, optional
Fraction of the currently available memory that the output plus the working blocks may use. Default is 0.8.
- small_cube_bytesfloat, optional
If the total estimated work is below this value, or if
datahas no pixel axis,funcis called once on the whole cube withworkers=ncpuinstead of block by block. Default is 1e7.- ncpuint, optional
Number of worker threads. Default is
None, which usesftsdata._utils.cpu_count().- mem_availablefloat, optional
Available memory, in bytes. Default is
None, which usesftsdata._utils.available_memory().- use_daskbool, optional
If True, use dask to process the cube in parallel. If None (default), use dask if available. If False, use ThreadPoolExecutor.
- Returns:
- outnumpy.ndarray
Output array with the same shape as
data, except alongaxiswhere it hasn_outsamples.
- Raises:
- MemoryError
If the output alone does not fit in the available memory, or if one row of pixels does not fit in the per-thread memory budget.
Notes
When
ncpuandmem_availableare not given, they are read withftsdata._utils.cpu_count()andftsdata._utils.available_memory().In the block path,
funcis called withworkers=1because parallelism already comes from the thread pool/dask; the single-call path usesworkers=ncpu.funcmay ignore that keyword if it does not parallelise internally.