Skip to content

Instantly share code, notes, and snippets.

@syaffers
Last active June 17, 2020 16:57
Show Gist options
  • Select an option

  • Save syaffers/4b4c3b2c17c3f53cdf4d2d1df6ab4f9e to your computer and use it in GitHub Desktop.

Select an option

Save syaffers/4b4c3b2c17c3f53cdf4d2d1df6ab4f9e to your computer and use it in GitHub Desktop.
Vectorized striding window extraction
def vectorized_stride_v1(array, clearing_time_index, max_time, sub_window_size,
stride_size):
start = clearing_time_index + 1 - sub_window_size + 1
sub_windows = (
start +
np.expand_dims(np.arange(sub_window_size), 0) +
np.expand_dims(np.arange(max_time + 1), 0).T
)
# Fancy indexing to select every V rows.
return array[sub_windows[::stride_size]]
def vectorized_stride_v2(array, clearing_time_index, max_time, sub_window_size,
stride_size):
start = clearing_time_index + 1 - sub_window_size + 1
sub_windows = (
start +
np.expand_dims(np.arange(sub_window_size), 0) +
# Create a rightmost vector as [0, V, 2V, ...].
np.expand_dims(np.arange(max_time + 1, step=stride_size), 0).T
)
return array[sub_windows]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment