MCPcopy Create free account
hub / github.com/dask/dask / sliding_window_view

Function sliding_window_view

dask/array/overlap.py:825–896  ·  view source on GitHub ↗
(x, window_shape, axis=None, automatic_rechunk=True)

Source from the content-addressed store, hash-verified

823
824@derived_from(np.lib.stride_tricks)
825def sliding_window_view(x, window_shape, axis=None, automatic_rechunk=True):
826 window_shape = tuple(window_shape) if np.iterable(window_shape) else (window_shape,)
827
828 window_shape_array = np.array(window_shape)
829 if np.any(window_shape_array <= 0):
830 raise ValueError("`window_shape` must contain values > 0")
831
832 if axis is None:
833 axis = tuple(range(x.ndim))
834 if len(window_shape) != len(axis):
835 raise ValueError(
836 f"Since axis is `None`, must provide "
837 f"window_shape for all dimensions of `x`; "
838 f"got {len(window_shape)} window_shape elements "
839 f"and `x.ndim` is {x.ndim}."
840 )
841 else:
842 axis = normalize_axis_tuple(axis, x.ndim, allow_duplicate=True)
843 if len(window_shape) != len(axis):
844 raise ValueError(
845 f"Must provide matching length window_shape and "
846 f"axis; got {len(window_shape)} window_shape "
847 f"elements and {len(axis)} axes elements."
848 )
849
850 depths = [0] * x.ndim
851 for ax, window in zip(axis, window_shape):
852 depths[ax] += window - 1
853
854 # Ensure that each chunk is big enough to leave at least a size-1 chunk
855 # after windowing (this is only really necessary for the last chunk).
856 safe_chunks = list(
857 ensure_minimum_chunksize(d + 1, c) for d, c in zip(depths, x.chunks)
858 )
859 if automatic_rechunk:
860 safe_chunks = [
861 s if d != 0 else c for d, c, s in zip(depths, x.chunks, safe_chunks)
862 ]
863 # safe chunks is our output chunks, so add the new dimensions
864 safe_chunks.extend([(w,) for w in window_shape])
865 max_chunk = reduce(mul, map(max, x.chunks))
866 new_chunks = _calculate_new_chunksizes(
867 x.chunks,
868 safe_chunks.copy(),
869 {i for i, d in enumerate(depths) if d == 0},
870 max_chunk,
871 )
872 x = x.rechunk(tuple(new_chunks))
873 else:
874 x = x.rechunk(tuple(safe_chunks))
875
876 # result.shape = x_shape_trimmed + window_shape,
877 # where x_shape_trimmed is x.shape with every entry
878 # reduced by one less than the corresponding window size.
879 # trim chunks to match x_shape_trimmed
880 newchunks = tuple(c[:-1] + (c[-1] - d,) for d, c in zip(depths, x.chunks)) + tuple(
881 (window,) for window in window_shape
882 )

Callers

nothing calls this directly

Calls 6

ensure_minimum_chunksizeFunction · 0.70
map_overlapFunction · 0.70
anyMethod · 0.45
copyMethod · 0.45
rechunkMethod · 0.45

Tested by

no test coverage detected