fosanalysis
A framework to evaluate distributed fiber optic sensor data
Loading...
Searching...
No Matches
fosanalysis.utils.windows Namespace Reference

Contains functions to shift a (partly) view of an array across the full array. More...

Functions

list determine_moving_parameters (np.array data_array, tuple radius, tuple start_pixel=None, tuple step_size=None)
 Generate indices for a moving window and check the other parameters.
 
 moving (np.array data_array, tuple radius, tuple start_pixel=None, tuple step_size=None)
 Generates a symmetric moving window over an array.
 
 sliding (np.array data_array, radius)
 Generates a sliding window over an array.
 
np.array sliding_window_function (np.array arr, radius, fn, str pad_mode=None, *args, **kwargs)
 Applies the function fn to a sliding window over the array arr.
 

Detailed Description

Contains functions to shift a (partly) view of an array across the full array.

Author
Bertram Richter
Date
2024

Function Documentation

◆ determine_moving_parameters()

list fosanalysis.utils.windows.determine_moving_parameters ( np.array data_array,
tuple radius,
tuple start_pixel = None,
tuple step_size = None )

Generate indices for a moving window and check the other parameters.

Parameters
data_arrayArray of data over which the window should move.
radiusInradius of the window's rectangle. If radius is an int, all axes will use this radius and the window is a square. For non-square windows, pass a tuple with a radius for each dimension of data_array. Along an axis, the window has a width of \(2r + 1\) for each element \(r\) of radius.
start_pixelIndex of the first window's central pixel. If start_pixel is an int, it is used for all dimensions of data_array. To specify a custom starting element, pass a tuple with a step size for each dimension of data_array. If None, it defaults to radius, the moving window starts with a full slice.
step_sizeStep size for estimation. If step_size is an int, it is used for all dimensions of data_array. If None, it defaults to \(2r + 1\) for each element \(r\) of radius, which is equivalent to a rolling window.
Returns
Retuns a tuple like (orig_index_lists, radius, start_pixel, step_size):
Return values
orig_index_listsList of lists, containing the indices along each axis.
radiusA tuple, see above.
start_pixelA tuple, see above.
step_sizeA tuple, see above.

Definition at line 158 of file windows.py.

Here is the caller graph for this function:

◆ moving()

fosanalysis.utils.windows.moving ( np.array data_array,
tuple radius,
tuple start_pixel = None,
tuple step_size = None )

Generates a symmetric moving window over an array.

This function returns a generator that yields information about the window_center_orig, target_pixel, and the content of the window. Converts data_array to a Numpy array. Ensuring radius as a tuple. do this similar for stepsize and the start pixel (start pixel if not given equal to radius). Determine orig_index_lists based on array dimensions and provided indices. It generates combinations of indices. It iterates over combinations and yields window information.

Parameters
data_arrayArray of data over which the window should move.
radiusInradius of the window's rectangle. If radius is an int, all axes will use this radius and the window is a square. For non-square windows, pass a tuple with a radius for each dimension of data_array. Along an axis, the window has a width of \(2r + 1\) for each element \(r\) of radius.
start_pixelIndex of the first window's central pixel. If start_pixel is an int, it is used for all dimensions of data_array. To specify a custom starting element, pass a tuple with a step size for each dimension of data_array. If None, it defaults to radius, the moving window starts with a full slice.
step_sizeStep size how far the window moves in one step. If step_size is an int, it is used for all dimensions of data_array. If None, it defaults to \(2r + 1\) for each element \(r\) of radius, which is equivalent to a rolling window.
Returns
Generator yielding in each iteration a tuple like (orig_pixel, target_pixel, window_content).
Return values
orig_pixelIndex of the window's center in data_array.
target_pixelIndex of the entry in a new array, in which the aggregated result is to be stored.
window_contentA view of the data_array, around the orig_pixel.

Definition at line 109 of file windows.py.

Here is the call graph for this function:

◆ sliding()

fosanalysis.utils.windows.sliding ( np.array data_array,
radius )

Generates a sliding window over an array.

This function returns a generator, hence, it should be use like:

for pixel, window in sliding(<array>, <radius>):
# do something
pass

In contrast to other function like numpy.lib.stride_tricks.sliding_window_view(), this window slides over each pixel, even in the margin areas of data_array. So, for each entry in data_array, a view to the window surrounding it is yielded. In the margins, the window contains fewer entries. Thus, boundary effects are to be expected, when using this function.

Remarks
Note, that a views of the original array are yielded, not copies. Changing a pixel's value in the window will change the original array.
Parameters
data_arrayArray of data, over which the window should slide.
radiusInradius of the window, sets the window's widths. Along an axis, the window has a width of \((2r+1)\). It is expected to be an int or iterable. If radius is an integer, it is used for all axes. The window will contain (up to) \((2r+1)^2\) entries. To set different widths for axes, use an iterable, such as a tuple. This tuple's length has to match the dimension of data_array.
Returns
In each iteration, a tuple like (pixel, window) is yielded.
Return values
pixelIndex of the window's central pixel in data_array.
windowSub-array view of the data_array centered around pixel.

Definition at line 63 of file windows.py.

◆ sliding_window_function()

np.array fosanalysis.utils.windows.sliding_window_function ( np.array arr,
radius,
fn,
str pad_mode = None,
* args,
** kwargs )

Applies the function fn to a sliding window over the array arr.

Note
Results in the margins (the first and last entries closer than radius to the edge of the array in each direction) are not reliable, as they suffer from boundary effects. This is caused by the sliding window only visit "complete" windows. In the margins, the values of the edge is repeated. The padding behavior can be changed with pad_mode.
Parameters
arrArray of data, over which the window should slide.
radiusInradius of the window, sets the window's widths. Along an axis, the window has a width of \((2r+1)\). It is expected to be an int a tuple. If radius is an integer, it is used for all axes. The windows will contain \((2r+1)^n\) entries. To set indiviual widths along for each dimension, use a tuple. This tuple's length has to match the dimension of arr.
fnA function object (type: callable), taking a np.array as input and returning a float.
pad_modeMode for padding the edges of the result array. Defaults to "edge", which repeats the result value on the edge. For more, options, see numpy.pad()
*argsAdditional positional arguments; ignored.
**kwargsAdditional keyword arguments; ignored.
Returns
Return a np.array with the same shape as arr. Each entry is the result of applying fn to a window reaching radius into each direction.

Definition at line 14 of file windows.py.