window for filters

Creates a sliding window of `window_size` that slide by n rows/elements across input.

Signature

> window (window_size) --stride --remainder

Parameters

  • window_size: the size of each window
  • --stride {int}: the number of rows to slide over between windows
  • --remainder (-r): yield last chunks even if they have fewer elements than size

Examples

A sliding window of two elements

> [1 2 3 4] | window 2
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 01 │ │
│   │ │ 12 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 02 │ │
│   │ │ 13 │ │
│   │ ╰───┴───╯ │
│ 2 │ ╭───┬───╮ │
│   │ │ 03 │ │
│   │ │ 14 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

A sliding window of two elements, with a stride of 3

> [1, 2, 3, 4, 5, 6, 7, 8] | window 2 --stride 3
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 01 │ │
│   │ │ 12 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 04 │ │
│   │ │ 15 │ │
│   │ ╰───┴───╯ │
│ 2 │ ╭───┬───╮ │
│   │ │ 07 │ │
│   │ │ 18 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

A sliding window of equal stride that includes remainder. Equivalent to chunking

> [1, 2, 3, 4, 5] | window 3 --stride 3 --remainder
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 01 │ │
│   │ │ 12 │ │
│   │ │ 23 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 04 │ │
│   │ │ 15 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯