median#

Median filter.

DEFAULT_DIMS: list[Hashable] = ['lat', 'lon']#

Default dimensions names.

Used for Xarray input where the dims argument is None and window_size is not a Mapping.

median_filter_core(input_field, window_size, mode, cval, **kwargs)#

Apply median filter.

Use scipy.signal.medfilt2d() in the conditions where it is faster, otherwise use scipy.ndimage.median_filter().

Warning

Internal function.

Users should rather use median_filter_numpy().

Parameters:
Return type:

ndarray[_Size, _DT]

median_filter_dask(input_field, window_size=3, mode='constant', cval=0.0, axes=None, **kwargs)#

Apply median filter.

If the mode is constant with cval=0 and if the input array dtype is uint8, float32, or float64, it will use the faster scipy.signal.medfilt2d(). Otherwise it will use scipy.ndimage.median_filter().

Parameters:
  • input_field (Array) – Array to filter

  • window_size (int | Sequence[int]) – Size of the moving window

  • mode (str) –

    The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’. Behavior for each valid value is as follows:

    ‘constant’ (k k k k | a b c d | k k k k)

    The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

    ‘reflect’ (d c b a | a b c d | d c b a)

    The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.

    ‘nearest’ (a a a a | a b c d | d d d d)

    The input is extended by replicating the last pixel.

    ‘mirror’ (d c b | a b c d | c b a)

    The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.

    ‘wrap’ (a b c d | a b c d | a b c d)

    The input is extended by wrapping around to the opposite edge

  • cval (float) – Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

  • axes (Sequence[int] | None) – Indices of the the y/lat and x/lon axes on which to work. If None (default), the last two axes are used.

  • kwargs – Arguments passed to either medfilt2d() or median_filter().

Returns:

Filtered array.

Return type:

Array

median_filter_numpy(input_field, window_size=3, mode='constant', cval=0.0, axes=None, **kwargs)#

Apply median filter.

If the mode is constant with cval=0 and if the input array dtype is uint8, float32, or float64, it will use the faster scipy.signal.medfilt2d(). Otherwise it will use scipy.ndimage.median_filter().

Parameters:
  • input_field (ndarray[_Size, _DT]) – Array to filter

  • window_size (int | Sequence[int]) – Size of the moving window

  • mode (str) –

    The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’. Behavior for each valid value is as follows:

    ‘constant’ (k k k k | a b c d | k k k k)

    The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

    ‘reflect’ (d c b a | a b c d | d c b a)

    The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.

    ‘nearest’ (a a a a | a b c d | d d d d)

    The input is extended by replicating the last pixel.

    ‘mirror’ (d c b | a b c d | c b a)

    The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.

    ‘wrap’ (a b c d | a b c d | a b c d)

    The input is extended by wrapping around to the opposite edge

  • cval (float) – Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

  • axes (Sequence[int] | None) – Indices of the the y/lat and x/lon axes on which to work. If None (default), the last two axes are used.

  • kwargs – Arguments passed to either medfilt2d() or median_filter().

Returns:

Filtered array.

Return type:

ndarray[_Size, _DT]

median_filter_xarray(input_field, window_size=3, mode='constant', cval=0.0, dims=None, **kwargs)#

Apply median filter.

If the mode is constant with cval=0 and if the input array dtype is uint8, float32, or float64, it will use the faster scipy.signal.medfilt2d(). Otherwise it will use scipy.ndimage.median_filter().

Parameters:
  • input_field (DataArray) – Array to filter

  • window_size (int | Mapping[Hashable, int]) – Size of the moving window

  • mode (str) –

    The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’. Behavior for each valid value is as follows:

    ‘constant’ (k k k k | a b c d | k k k k)

    The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

    ‘reflect’ (d c b a | a b c d | d c b a)

    The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.

    ‘nearest’ (a a a a | a b c d | d d d d)

    The input is extended by replicating the last pixel.

    ‘mirror’ (d c b | a b c d | c b a)

    The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.

    ‘wrap’ (a b c d | a b c d | a b c d)

    The input is extended by wrapping around to the opposite edge

  • cval (float) – Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

  • kwargs – Arguments passed to either medfilt2d() or median_filter().

  • dims (Collection[Hashable] | None) – Names of the dimensions along which to apply the algorithm. Order is irrelevant, no reordering will be made between the two dimensions. If the window_size argument is given as a mapping, its keys are used instead. If not specified, is taken as module-wide variable DEFAULT_DIMS which defaults to {'lat', 'lon'}.

Returns:

Filtered array.

Return type:

DataArray