From cf202c9fc09908c497c9d609642532fa707d3047 Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Sat, 9 Aug 2025 02:28:27 +0530 Subject: [PATCH] Update view_as_windows.py --- patchify/view_as_windows.py | 43 ++++++++++++++----------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/patchify/view_as_windows.py b/patchify/view_as_windows.py index ddfe20c..c7e71c9 100644 --- a/patchify/view_as_windows.py +++ b/patchify/view_as_windows.py @@ -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") @@ -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 \ No newline at end of file + return arr_out