xrspatial.focal.mean#

xrspatial.focal.mean(agg, passes=1, excludes=[nan], name='mean')[source]#

Returns Mean filtered array using a 3x3 window. Default behaviour to ‘mean’ is to exclude NaNs from calculations.

Parameters
  • agg (xarray.DataArray) – 2D array of input values to be filtered.

  • passes (int, default=1) – Number of times to run mean.

  • name (str, default='mean') – Output xr.DataArray.name property.

Returns

mean_agg – 2D aggregate array of filtered values.

Return type

xarray.DataArray of same type as agg

Examples

Focal mean works with NumPy backed xarray DataArray .. sourcecode:: python

>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial.focal import mean
>>> data = np.array([
    [0., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.],
    [0., 0., 9., 0., 0.],
    [0., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.]])
>>> raster = xr.DataArray(data)
>>> mean_agg = mean(raster)
>>> print(mean_agg)
<xarray.DataArray 'mean' (dim_0: 5, dim_1: 5)>
array([[0., 0., 0., 0., 0.],
       [0., 1., 1., 1., 0.],
       [0., 1., 1., 1., 0.],
       [0., 1., 1., 1., 0.],
       [0., 0., 0., 0., 0.]])
Dimensions without coordinates: dim_0, dim_1

Focal mean works with Dask with NumPy backed xarray DataArray. Increase number of runs by setting a specific value for parameter passes .. sourcecode:: python

>>> import dask.array as da
>>> data_da = da.from_array(data, chunks=(3, 3))
>>> raster_da = xr.DataArray(data_da, dims=['y', 'x'], name='raster_da')  # noqa
>>> print(raster_da)
<xarray.DataArray 'raster_da' (y: 5, x: 5)>
dask.array<array, shape=(5, 5), dtype=int64, chunksize=(3, 3), chunktype=numpy.ndarray>  # noqa
Dimensions without coordinates: y, x
>>> mean_da = mean(raster_da, passes=2)
>>> print(mean_da)
<xarray.DataArray 'mean' (y: 5, x: 5)>
dask.array<_trim, shape=(5, 5), dtype=float64, chunksize=(3, 3), chunktype=numpy.ndarray>  # noqa
Dimensions without coordinates: y, x
>>> print(mean_da.compute())
<xarray.DataArray 'mean' (y: 5, x: 5)>
array([[0.25      , 0.33333333, 0.5       , 0.33333333, 0.25      ],
       [0.33333333, 0.44444444, 0.66666667, 0.44444444, 0.33333333],
       [0.5       , 0.66666667, 1.        , 0.66666667, 0.5       ],
       [0.33333333, 0.44444444, 0.66666667, 0.44444444, 0.33333333],
       [0.25      , 0.33333333, 0.5       , 0.33333333, 0.25      ]])
Dimensions without coordinates: y, x

Focal mean works with CuPy backed xarray DataArray. In this example, we set passes to the number of elements of the array, we’ll get a mean array where every element has the same value. .. sourcecode:: python

>>> import cupy
>>> raster_cupy = xr.DataArray(cupy.asarray(data), name='raster_cupy')
>>> mean_cupy = mean(raster_cupy, passes=25)
>>> print(type(mean_cupy.data))
<class 'cupy.core.core.ndarray'>
>>> print(mean_cupy)
<xarray.DataArray 'mean' (dim_0: 5, dim_1: 5)>
array([[0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
       [0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
       [0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
       [0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
       [0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995]])
Dimensions without coordinates: dim_0, dim_1