eolearn.ml_tools.utils
The utilities module is a collection of methods used across the eolearn package, such as checking whether two objects are deeply equal, padding of an image, etc.
- eolearn.ml_tools.utils.rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True)[source]
Create a view of array which for every point gives the n-dimensional neighbourhood of size window. New dimensions are added at the end of array or after the corresponding original dimension.
Examples:
>>> a = np.arange(9).reshape(3,3) >>> rolling_window(a, (2,2)) array([[[[0, 1], [3, 4]], [[1, 2], [4, 5]]], [[[3, 4], [6, 7]], [[4, 5], [7, 8]]]])
Or to create non-overlapping windows, but only along the first dimension:
>>> rolling_window(a, (2,0), asteps=(2,1)) array([[[0, 3], [1, 4], [2, 5]]])
Note that the 0 is discared, so that the output dimension is 3:
>>> rolling_window(a, (2,0), asteps=(2,1)).shape (1, 3, 2)
This is useful for example to calculate the maximum in all (overlapping) 2x2 submatrixes:
>>> rolling_window(a, (2,2)).max((2,3)) array([[4, 5], [7, 8]])
Or delay embedding (3D embedding with delay 2):
>>> x = np.arange(10) >>> rolling_window(x, 3, wsteps=2) array([[0, 2, 4], [1, 3, 5], [2, 4, 6], [3, 5, 7], [4, 6, 8], [5, 7, 9]])
- Parameters
array (np.ndarray) – Array to which the rolling window is applied
window (int or (int, int)) – Either a single integer to create a window of only the last axis or a tuple to create it for the last len(window) axes. 0 can be used to ignore a dimension in the window.
asteps (tuple) – Aligned at the last axis, new steps for the original array, i.e. for creation of non-overlapping windows. (Equivalent to slicing result)
wsteps (int or tuple (same size as window) or None) – Steps for the added window dimensions. These can be 0 to repeat values along the axis.
axes (int or tuple) – If given, must have the same size as window. In this case window is interpreted as the size in the dimension given by axes. E.g. a window of (2, 1) is equivalent to window=2 and axis=-2.
toend (bool) – If False, the new dimensions are right after the corresponding original dimension, instead of at the end of the array. Adding the new axes at the end makes it easier to get the neighborhood, however toend=False will give a more intuitive result if you view the whole array.
- Returns
A view on array which is smaller to fit the windows and has windows added dimensions (0s not counting), i.e. every point of array is an array of size window.
- Return type
np.ndarray