linumpy.gpu.bias_field#

GPU-accelerated helpers for N4 bias field correction pre/post-processing.

Provides block-mean downsampling, bias field upsampling, and chunked element-wise division on GPU (CuPy + PyTorch). All functions fall back to CPU (NumPy + SciPy) when GPU_AVAILABLE is False.

Functions#

downsample_gpu(vol, shrink_factor[, use_gpu])

Block-mean spatial downsampling by an integer factor.

upsample_bias_gpu(bias_low, target_shape[, use_gpu])

Trilinear upsampling of a low-resolution bias field to target_shape.

apply_bias_field_gpu(vol, bias_field[, chunk_z, ...])

Element-wise division vol / bias_field processed in Z-chunks on GPU.

Module Contents#

linumpy.gpu.bias_field.downsample_gpu(vol, shrink_factor, use_gpu=True)[source]#

Block-mean spatial downsampling by an integer factor.

Parameters:
  • vol (np.ndarray) – Float32 input (Z, Y, X).

  • shrink_factor (int) – Isotropic downsampling factor. The output shape is ceil(s / shrink_factor) on each axis.

  • use_gpu (bool) – Use CuPy when GPU is available.

Returns:

Downsampled float32 array.

Return type:

np.ndarray

linumpy.gpu.bias_field.upsample_bias_gpu(bias_low, target_shape, use_gpu=True)[source]#

Trilinear upsampling of a low-resolution bias field to target_shape.

Parameters:
  • bias_low (np.ndarray) – Low-resolution bias field (Z’, Y’, X’), float32.

  • target_shape (tuple of int) – Desired output shape (Z, Y, X).

  • use_gpu (bool) – Use PyTorch trilinear interpolation when GPU is available.

Returns:

Upsampled float32 bias field of shape target_shape.

Return type:

np.ndarray

linumpy.gpu.bias_field.apply_bias_field_gpu(vol, bias_field, chunk_z=50, floor=1e-06, use_gpu=True)[source]#

Element-wise division vol / bias_field processed in Z-chunks on GPU.

Parameters:
  • vol (np.ndarray) – Float32 input volume (Z, Y, X).

  • bias_field (np.ndarray) – Multiplicative bias field, same shape as vol.

  • chunk_z (int) – Number of Z-planes per GPU chunk.

  • floor (float) – Minimum divisor to avoid division by zero.

  • use_gpu (bool) – Use CuPy when GPU is available.

Returns:

Corrected float32 volume, same shape as vol.

Return type:

np.ndarray