Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 16 additions & 27 deletions patchify/view_as_windows.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
# The code is copied from https://github.com/scikit-image/scikit-image/blob/main/skimage/util/shape.py
#
# Copyright (C) 2011, the scikit-image team All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# Neither the name of skimage nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numbers
import numpy as np
from numpy.lib.stride_tricks import as_strided


def view_as_windows(arr_in, window_shape, step=1):

# -- basic checks on arguments
"""
Rolling window view of the input n-dimensional array.
Uses sliding_window_view for safety and compatibility with NumPy 2.x.
"""
if not isinstance(arr_in, np.ndarray):
raise TypeError("`arr_in` must be a numpy ndarray")

Expand All @@ -40,21 +29,21 @@ def view_as_windows(arr_in, window_shape, step=1):
if ((arr_shape - window_shape) < 0).any():
raise ValueError("`window_shape` is too large")

# This is technically redundant — values < 1 would already fail above check,
# but we keep it for extra safety.
if ((window_shape - 1) < 0).any():
raise ValueError("`window_shape` is too small")

# -- build rolling window view
slices = tuple(slice(None, None, st) for st in step)
window_strides = np.array(arr_in.strides)

indexing_strides = arr_in[slices].strides
# Use sliding_window_view for safety in NumPy 2.x
try:
from numpy.lib.stride_tricks import sliding_window_view
except ImportError:
raise ImportError("sliding_window_view is required for NumPy 2.x compatibility.")

win_indices_shape = (
(np.array(arr_in.shape) - np.array(window_shape)) // np.array(step)
) + 1
arr_out = sliding_window_view(arr_in, window_shape)

new_shape = tuple(list(win_indices_shape) + list(window_shape))
strides = tuple(list(indexing_strides) + list(window_strides))
# Apply step if needed
slices = tuple(slice(None, None, st) for st in step)
arr_out = arr_out[slices]

arr_out = as_strided(arr_in, shape=new_shape, strides=strides)
return arr_out
return arr_out