Skip to content

dissection_index

darts_preprocessing.engineering.dissection_index

RADIAN module-attribute

RADIAN = 180 / numpy.pi

_run_cupy

_run_cupy(data, d: int)
Source code in darts-preprocessing/src/darts_preprocessing/engineering/dissection_index.py
def _run_cupy(data, d: int):
    data = data.astype(cupy.float32)
    griddim, blockdim = cuda_args(data.shape)
    out = cupy.empty(data.shape, dtype="float32")
    out[:] = cupy.nan
    _run_gpu[griddim, blockdim](data, d, out)
    return out

_run_dask_numpy

_run_dask_numpy(
    data: dask.array.Array, d: int
) -> dask.array.Array
Source code in darts-preprocessing/src/darts_preprocessing/engineering/dissection_index.py
def _run_dask_numpy(data: da.Array, d: int) -> da.Array:
    data = data.astype(np.float32)
    _func = partial(_run_numpy, d=d)  # noqa: RUF052
    out = data.map_overlap(_func, depth=(d, d), boundary=np.nan, meta=np.array(()))
    return out

_run_gpu

_run_gpu(arr, d, out)
Source code in darts-preprocessing/src/darts_preprocessing/engineering/dissection_index.py
@cuda.jit
def _run_gpu(arr, d, out):
    i, j = cuda.grid(2)
    di = d
    dj = d
    if i - di >= 0 and i + di < out.shape[0] and j - dj >= 0 and j + dj < out.shape[1]:
        # aoi = arr[i - di : i + di + 1, j - dj : j + dj + 1]
        aoi_min = np.inf
        aoi_max = -np.inf
        for y in range(i - di, i + di + 1):
            for x in range(j - dj, j + dj + 1):
                v = arr[y, x]
                if v < aoi_min:
                    aoi_min = v
                if v > aoi_max:
                    aoi_max = v
        if aoi_max != 0:
            out[i, j] = (aoi_max - aoi_min) / aoi_max

_run_numpy

_run_numpy(data: numpy.ndarray, d: int)
Source code in darts-preprocessing/src/darts_preprocessing/engineering/dissection_index.py
@ngjit
def _run_numpy(data: np.ndarray, d: int):
    data = data.astype(np.float32)
    out = np.zeros_like(data, dtype=np.float32)
    out[:] = np.nan
    rows, cols = data.shape
    for y in range(d, rows - d):
        for x in range(d, cols - d):
            aoi = data[y - d : y + d + 1, x - d : x + d + 1]
            aoi_min = aoi.min()
            aoi_max = aoi.max()
            if aoi_max == aoi_min:
                out[y, x] = 0
            elif aoi_max == 0:
                out[y, x] = float("nan")
            else:
                out[y, x] = (aoi_max - aoi_min) / (aoi_max)
    return out

dissection_index

dissection_index(
    agg: xarray.DataArray,
    window_size: int = 3,
    name: str | None = "dissection_index",
) -> xarray.DataArray

Compute the dissection index of a 2D array.

Parameters:

  • agg (xarray.DataArray) –

    The input data array.

  • window_size (int, default: 3 ) –

    The size of the window to use for the computation. Defaults to 3.

  • name (str | None, default: 'dissection_index' ) –

    The name of the output data array. Defaults to "dissection_index".

Returns:

  • xarray.DataArray

    xr.DataArray: The dissection index of the input data array.

Source code in darts-preprocessing/src/darts_preprocessing/engineering/dissection_index.py
def dissection_index(agg: xr.DataArray, window_size: int = 3, name: str | None = "dissection_index") -> xr.DataArray:
    """Compute the dissection index of a 2D array.

    Args:
        agg (xr.DataArray): The input data array.
        window_size (int, optional): The size of the window to use for the computation. Defaults to 3.
        name (str | None, optional): The name of the output data array. Defaults to "dissection_index".

    Returns:
        xr.DataArray: The dissection index of the input data array.

    """
    mapper = ArrayTypeFunctionMapping(
        numpy_func=_run_numpy,
        dask_func=_run_dask_numpy,
        cupy_func=_run_cupy,
        dask_cupy_func=lambda *args: not_implemented_func(
            *args, messages="dissection_index() does not support dask with cupy backed DataArray"
        ),
    )

    out = mapper(agg)(agg.data, (window_size - 1) // 2)

    return xr.DataArray(out, name=name, coords=agg.coords, dims=agg.dims, attrs=agg.attrs)