SlidingWindows#

class SlidingWindows[source]#

Bases: Sequence[Box]

Lazy representation of a list of sliding windows.

Instead of storing a list of all windows in memory, this class dynamically computes the coordinates of windows as they are retrieved. Supports iteration and basic slicing.

__init__(box: Box, *, size: Union[int, tuple[int, int]], stride: Union[int, tuple[int, int]], padding: Optional[Union[int, tuple[int, int]]] = None, pad_direction: Literal['both', 'start', 'end'] = 'end')[source]#

Constructor.

Each of size, stride, and padding can be either a positive int or a tuple (vertical-component, horizontal-component) of positive ints.

If padding is not specified and stride <= size, it will be automatically calculated such that the windows cover the entire extent.

Parameters:
  • box (Box) – Outer box within which to generate sliding windows.

  • size (Union[int, tuple[int, int]]) – Size (h, w) of the windows.

  • stride (Union[int, tuple[int, int]]) – Step size between windows. Can be a (h_step, w_step) tuple or positive int.

  • padding (Optional[Union[int, tuple[int, int]]]) – Optional padding to accommodate windows that overflow the extent. Can be a (h_pad, w_pad) tuple or a non-negative int. If None, will be automatically calculated such that the windows cover the entire extent. Defaults to None.

  • pad_direction (Literal['both', 'start', 'end']) – Directions to add padding to. If 'end', only add padding to bottom and right. If 'start', only add padding to top and left. If 'both', add padding to all sides. Has no effect if padding is zero. Defaults to 'end'.

Methods

__init__(box, *, size, stride[, padding, ...])

Constructor.

count(value)

get_by_rowcol(row, col)

Get window at given row and column indices.

index(value, [start, [stop]])

Raises ValueError if the value is not present.

index_to_rowcol(i)

Get row and column indices of the i-th window.

__contains__(value)#
__getitem__(i: int | numpy.integer) Box[source]#
__getitem__(s: slice) list[rastervision.core.box.Box]
__getitem__(inds: Sequence[int]) list[rastervision.core.box.Box]
__init__(box: Box, *, size: Union[int, tuple[int, int]], stride: Union[int, tuple[int, int]], padding: Optional[Union[int, tuple[int, int]]] = None, pad_direction: Literal['both', 'start', 'end'] = 'end')[source]#

Constructor.

Each of size, stride, and padding can be either a positive int or a tuple (vertical-component, horizontal-component) of positive ints.

If padding is not specified and stride <= size, it will be automatically calculated such that the windows cover the entire extent.

Parameters:
  • box (Box) – Outer box within which to generate sliding windows.

  • size (Union[int, tuple[int, int]]) – Size (h, w) of the windows.

  • stride (Union[int, tuple[int, int]]) – Step size between windows. Can be a (h_step, w_step) tuple or positive int.

  • padding (Optional[Union[int, tuple[int, int]]]) – Optional padding to accommodate windows that overflow the extent. Can be a (h_pad, w_pad) tuple or a non-negative int. If None, will be automatically calculated such that the windows cover the entire extent. Defaults to None.

  • pad_direction (Literal['both', 'start', 'end']) – Directions to add padding to. If 'end', only add padding to bottom and right. If 'start', only add padding to top and left. If 'both', add padding to all sides. Has no effect if padding is zero. Defaults to 'end'.

count(value) integer -- return number of occurrences of value#
get_by_rowcol(row: int, col: int) Box[source]#

Get window at given row and column indices.

Parameters:
Return type:

Box

index(value[, start[, stop]]) integer -- return first index of value.#

Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.

index_to_rowcol(i: int) tuple[int, int][source]#

Get row and column indices of the i-th window.

Parameters:

i (int) –

Return type:

tuple[int, int]