<div dir="ltr">Hi,<div><br></div><div>I am trying to do a slding window on a cube (3D array) to get the average over a block of vertical 1D arrays. I have achieved this using the stride_tricks.asstrided but this will load the whole cube into memory at once and is not suitable for large cubes. I have also achieved it using an nditer but this is a lot slower:</div><div><br></div><div><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold">def </span>trace_block(vol, x, y, half_window):<br>    <span style="color:rgb(0,0,128);font-weight:bold">return </span>vol[x - half_window:x + half_window + <span style="color:rgb(0,0,255)">1</span>, y - half_window:y + half_window + <span style="color:rgb(0,0,255)">1</span>]<br><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic">    </span>vol = np.linspace(<span style="color:rgb(0,0,255)">1</span>, <span style="color:rgb(0,0,255)">125</span>, <span style="color:rgb(0,0,255)">125</span>, <span style="color:rgb(102,0,153)">dtype</span>=np.int32).reshape(<span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>)<br>    window_size = <span style="color:rgb(0,0,255)">3</span><br><br>    x, y, z = vol.shape<br>    half_window = (window_size - <span style="color:rgb(0,0,255)">1</span>) // <span style="color:rgb(0,0,255)">2<br></span><span style="color:rgb(0,0,255)">    </span>xs = np.arange(half_window, x - half_window, <span style="color:rgb(102,0,153)">dtype</span>=np.int16)<br>    ys = np.arange(half_window, y - half_window, <span style="color:rgb(102,0,153)">dtype</span>=np.int16)<br>    averaged = np.zeros((<span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>))<br>    <span style="color:rgb(0,0,128);font-weight:bold">for </span>x, y <span style="color:rgb(0,0,128);font-weight:bold">in </span>np.nditer(np.ix_(xs, ys)):<br>        averaged[x, y] = np.mean(trace_block(vol, x, y, half_window), (<span style="color:rgb(0,0,255)">0</span>, <span style="color:rgb(0,0,255)">1</span>))</pre></div><div><br></div><div>My attempt at using numpy vectorisation to avoid the for loop throws the error in the subject:</div><div><br></div><div><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:9pt"><span style="background-color:rgb(255,228,255)">vol</span> = np.linspace(<span style="color:rgb(0,0,255)">1</span>, <span style="color:rgb(0,0,255)">125</span>, <span style="color:rgb(0,0,255)">125</span>, <span style="color:rgb(102,0,153)">dtype</span>=np.int32).reshape(<span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>)<br>window_size = <span style="color:rgb(0,0,255)">3</span><br><br>x, y, z = <span style="background-color:rgb(228,228,255)">vol</span>.shape<br>half_window = (window_size - <span style="color:rgb(0,0,255)">1</span>) // <span style="color:rgb(0,0,255)">2<br></span>xs = np.arange(half_window, x - half_window, <span style="color:rgb(102,0,153)">dtype</span>=np.int16)<br>ys = np.arange(half_window, y - half_window, <span style="color:rgb(102,0,153)">dtype</span>=np.int16)<br>averaged = np.zeros((<span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>, <span style="color:rgb(0,0,255)">5</span>))<br>xi, yi = np.ix_(xs, ys)<br>averaged[xi, yi] = np.mean(trace_block(<span style="background-color:rgb(228,228,255)">vol</span>, xi, yi, half_window), (<span style="color:rgb(0,0,255)">0</span>, <span style="color:rgb(0,0,255)">1</span>))</pre></div><div>Is there any way to do slicing as shown in the trace_block function to support the xi and yi grid arrays?</div><div><br></div><div>Any help you can provide will be greatly appreciated.</div><div><br></div><div><br></div></div>