swectral.pixel_apply#

swectral.pixel_apply(image_path, spectral_function, function_type, output_path=None, dtype='float32', tile_size=-1, *, progress=True, return_output_path=True, override=True, _space_wait_timeout=6, _reserve_free_pct=5.0, _preprocess_status=None)[source]#

Apply a function to process the 1D spectra of every pixel of a raster image.

Parameters:
image_pathstr

Input raster image path in string.

spectral_functioncallable(), optional

Function to apply to 1D spectra of every pixel. The type of the function is specified to parameter function_type

function_typecallable(), optional

Specifies the type of spectral processing function to apply. Must be one of:

  • "spec"

    Processes individual spectra.

    The function must:

    Accept a 1D array-like as its only required input. Return 1D numpy.ndarray of the processed spectra.

  • "array"

    Processes batches of spectra as a 2D array.

    The function must:

    Accept a 2D numpy.ndarray as its only required input, where each row represents a spectrum of a pixel. Return a 2D numpy.ndarray with the same number of rows (processed spectra).

  • "tensor"

    Processes data as a 3D torch.Tensor, used for multispectral data with limited number of bands.

    “cuda” is applied if available.

    The function must:

    Accept a 3D tensor as its only required input (shape: [Channels, Height, Width]). Return a 3D tensor with identical Height and Width. Compute operations along the Channel dimension (axis 0).

  • "tensor_hyper"

    Processes data as a 3D PyTorch tensor, optimized for hyperspectral data with large number of bands.

    “cuda” is applied if available.

    The function must:

    Accept a 3D tensor as its only required input (shape: [Channels, Height, Width]). Return a 3D tensor with identical Height and Width. Compute operations along the Height dimension (axis 1).

output_pathstr

Output raster image path in string. Defaults to image_path combined with “_px_app_” and function name.

dtypetype or str, optional

Value data type of output array. The default is "float32".

tile_sizeint, optional

Size of the processing tiles in pixels.

Larger values may improve performance, but require more memory.

The default is -1, in which case the value is determined by function_type as follows:

  • "spec": 32

  • "array": 32

  • "tensor": 64

  • "tensor_hyper": 4

progressbool, optional

Whether to show progress bar.

If True, enables progress bar.

If False, suppresses progress messages.

Default is True.

return_output_pathbool, optional

Whether path of processed image is returned. Default is True.

overridebool, optional

Whether existed image of output_path is override.

If False, function will not be applied if output_path existed. The default is True.

Returns:
str

Path of processed image.

Return type:

Optional[str]

Examples

Apply function accepting 2D array:

>>> pixel_apply("/image1.tif", array_function, "array")

Save to custom output path:

>>> pixel_apply("/image1.tif", array_function, "array", output_path="/image1_processed.tif")

Apply function accepting 3D hyperspectral tensor and computing along axis 0:

>>> pixel_apply("/image1.tif", tensor_function, "tensor")

Apply function accepting 3D hyperspectral tensor and computing along axis 1:

>>> pixel_apply("/image1.tif", hypertensor_function, "tensor_hyper")

Customize tile size:

>>> pixel_apply("/image1.tif", array_function, "array", tile_size=128)