reducers
reshape_windowed #
Reshape an array to support windowed operations. New
dimensions will be added to the array, one for each element of
window_size
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be reshaped. The array must have a |
required |
window_size |
tuple[int, ...]
|
The window size. The length of |
required |
Returns:
Type | Description |
---|---|
The input array reshaped with extra dimensions.
|
E.g., for an |
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import reshape_windowed
>>> data = np.arange(12).reshape(3, 4)
>>> reshaped = reshape_windowed(data, (1, 2))
>>> reshaped.shape
(3, 1, 2, 2)
Source code in src/xarray_multiscale/reducers.py
windowed_mean #
Compute the windowed mean of an array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. The array must have
|
required |
window_size |
tuple[int, ...]
|
The window to use for aggregations. The array is partitioned into
non-overlapping regions with size equal to |
required |
**kwargs |
Any
|
Extra keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
Array - like
|
The result of the windowed mean. The length of each axis of this array
will be a fraction of the input. The datatype is determined by the
behavior of |
Notes
This function works by first reshaping the array to have an extra
axis per element of window_size
, then computing the
mean along those extra axes.
See xarray_multiscale.reductions.reshape_windowed
for the
implementation of the array reshaping routine.
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import windowed_mean
>>> data = np.arange(16).reshape(4, 4)
>>> windowed_mean(data, (2, 2))
array([[ 2.5, 4.5],
[10.5, 12.5]])
Source code in src/xarray_multiscale/reducers.py
windowed_max #
Compute the windowed maximum of an array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. The array must have |
required |
window_size |
tuple[int, ...]
|
The window to use for aggregations. The array is partitioned into
non-overlapping regions with size equal to |
required |
**kwargs |
Any
|
Extra keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
Array - like
|
The result of the windowed max. The length of each axis of this array will be a fraction of the input. The datatype of the return value will will be the same as the input. |
Notes
This function works by first reshaping the array to have an extra
axis per element of window_size
, then computing the
max along those extra axes.
See xarray_multiscale.reductions.reshape_windowed
for
the implementation of the array reshaping routine.
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import windowed_mean
>>> data = np.arange(16).reshape(4, 4)
>>> windowed_max(data, (2, 2))
array([[ 5, 7],
[13, 15]])
Source code in src/xarray_multiscale/reducers.py
windowed_min #
Compute the windowed minimum of an array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. The array must have |
required |
window_size |
tuple[int, ...]
|
The window to use for aggregations. The array is partitioned into
non-overlapping regions with size equal to |
required |
**kwargs |
Any
|
Extra keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
Array - like
|
The result of the windowed min. The length of each axis of this array will be a fraction of the input. The datatype of the return value will will be the same as the input. |
Notes
This function works by first reshaping the array to have an extra
axis per element of window_size
, then computing the
min along those extra axes.
See xarray_multiscale.reductions.reshape_windowed
for the implementation of the array reshaping routine.
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import windowed_mean
>>> data = np.arange(16).reshape(4, 4)
>>> windowed_min(data, (2, 2))
array([[0, 2],
[8, 10]])
Source code in src/xarray_multiscale/reducers.py
windowed_mode #
Compute the windowed mode of an array using either
windowed_mode_countess
or windowed_mode_scipy
Input will be coerced to a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. The array must have a |
required |
window_size |
tuple[int, ...]
|
The window to use for aggregation. The array is partitioned into
non-overlapping regions with size equal to |
required |
Returns:
Type | Description |
---|---|
Numpy array
|
The result of the windowed mode. The length of each axis of this array will be a fraction of the input. |
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import windowed_mode
>>> data = np.arange(16).reshape(4, 4)
>>> windowed_mode(data, (2, 2))
array([[ 0, 2],
[ 8, 10]])
Source code in src/xarray_multiscale/reducers.py
windowed_mode_scipy #
Compute the windowed mode of an array using scipy.stats.mode. Input will be coerced to a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. The array must have a |
required |
window_size |
tuple[int, ...]
|
The window to use for aggregation. The array is partitioned into
non-overlapping regions with size equal to |
required |
Returns:
Type | Description |
---|---|
Numpy array
|
The result of the windowed mode. The length of each axis of this array will be a fraction of the input. |
Notes
This function wraps scipy.stats.mode
.
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import windowed_mode
>>> data = np.arange(16).reshape(4, 4)
>>> windowed_mode(data, (2, 2))
array([[ 0, 2],
[ 8, 10]])
Source code in src/xarray_multiscale/reducers.py
windowed_mode_countless #
countless downsamples labeled images (segmentations) by finding the mode using vectorized instructions. It is ill advised to use this O(2^N-1) time algorithm and O(NCN/2) space for N > about 16 tops. This means it's useful for the following kinds of downsampling. This could be implemented for higher performance in C/Cython more simply, but at least this is easily portable. 2x2x1 (N=4), 2x2x2 (N=8), 4x4x1 (N=16), 3x2x1 (N=6) and various other configurations of a similar nature. c.f. https://medium.com/@willsilversmith/countless-3d-vectorized-2x-downsampling-of-labeled-volume-images-using-python-and-numpy-59d686c2f75
This function has been modified from the original to avoid mutation of the input argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. |
required |
window_size |
tuple[int, ...]
|
The window size. The length of |
required |
Source code in src/xarray_multiscale/reducers.py
windowed_rank #
Compute the windowed rank order filter of an array. Input will be coerced to a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
NDArray[Any]
|
The array to be downscaled. The array must have a |
required |
window_size |
tuple[int, ...]
|
The window to use for aggregation. The array is partitioned into
non-overlapping regions with size equal to |
required |
rank |
int
|
The index to take from the sorted values in each window. If non-negative, then
rank must be between 0 and the product of the elements of |
-1
|
Returns:
Type | Description |
---|---|
Numpy array
|
The result of the windowed rank filter. The length of each axis of this array will be a fraction of the input. |
Examples:
>>> import numpy as np
>>> from xarray_multiscale.reducers import windowed_rank
>>> data = np.arange(16).reshape(4, 4)
>>> windowed_rank(data, (2, 2), -2)
array([[ 4 6]
[12 14]])