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

Function sliding_window_view

dask/array/_array_expr/_overlap.py:838–909  ·  view source on GitHub ↗
(x, window_shape, axis=None, automatic_rechunk=True)

Source from the content-addressed store, hash-verified

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

Calls 6

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…