xrspatial.focal.focal_stats#
- xrspatial.focal.focal_stats(agg, kernel, stats_funcs=['mean', 'max', 'min', 'range', 'std', 'var', 'sum'])[source]#
Calculates statistics of the values within a specified focal neighborhood for each pixel in an input raster. The statistics types are Mean, Maximum, Minimum, Range, Standard deviation, Variation and Sum.
- Parameters
agg (xarray.DataArray) – 2D array of input values to be analysed. Can be a NumPy backed, Cupy backed, or Dask with NumPy backed DataArray.
kernel (numpy.array) – 2D array where values of 1 indicate the kernel.
stats_funcs (list of string) – List of statistics types to be calculated. Default set to [‘mean’, ‘max’, ‘min’, ‘range’, ‘std’, ‘var’, ‘sum’].
- Returns
stats_agg – 3D array with dimensions of (stat, y, x) and with values indicating the focal stats.
- Return type
xarray.DataArray of same type as agg
Examples
>>> import numpy as np >>> import xarray as xr >>> from xrspatial.convolution import circle_kernel >>> kernel = circle_kernel(1, 1, 1) >>> kernel array([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]]) >>> data = np.array([ [0, 0, 0, 0, 0, 0], [1, 1, 2, 2, 1, 1], [2, 2, 1, 1, 2, 2], [3, 3, 0, 0, 3, 3], ]) >>> from xrspatial.focal import focal_stats >>> focal_stats(xr.DataArray(data), kernel, stats_funcs=['min', 'sum']) <xarray.DataArray 'focal_apply' (stats: 2, dim_0: 4, dim_1: 6)> array([[[0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [1., 1., 0., 0., 1., 1.], [2., 0., 0., 0., 0., 2.]], [[1., 1., 2., 2., 1., 1.], [4., 6., 6., 6., 6., 4.], [8., 9., 6., 6., 9., 8.], [8., 8., 4., 4., 8., 8.]]]) Coordinates: * stats (stats) object 'min' 'sum' Dimensions without coordinates: dim_0, dim_1