I am in the process of converting some matlab code to python. I working with a 3d volume h x w x d
represented as an numpy array, I am extracting smaller 3d patches from this volume using the function from SO here. So if I have 32x32x32
array and extract 16x16x16
patches I end up with a shape (2, 2, 2, 16, 16, 16)
After processing each patch I would like to put it back into shape h x w x d
basically reverse window_nd
What would be the idiomatic numpy way without looping each dimension? Since I also need to work with 2d and 4d data I would like to avoid creating a function for each dimension.
Asked
Active
Viewed 67 times
2

Daniel F
- 13,620
- 2
- 29
- 55

Hamza Yerlikaya
- 49,047
- 44
- 147
- 241
-
Do these patches overlap? `as_strided` creates a `view` of the source array, so it does not reorder anything. What kind of processing are you doing? Depending on the processing the changes might already appear in the source. Or reshaping might be enough. Sometimes a tranpose is need as well, though I'm not sure that's ever the case with `as_strided` arrays. – hpaulj Feb 11 '21 at 00:44
-
I imagine the downvotes are because the question too general. Good answer have working examples. It's easier to construct such examples if the OP has provided a [mcve]. – hpaulj Feb 11 '21 at 00:49
-
As @hpaulj says, normally you'll get better answers if you have a reproducible example. You won't often get the person who wrote the original code (and thus can intuit your problem) to answer you :) – Daniel F Feb 11 '21 at 09:29
-
@hpaulj basically each patch is sent to a segmentation algorithm that returns a new patch of the same size segmented. So original patch is untouched used as read only. – Hamza Yerlikaya Feb 11 '21 at 10:48
-
So you have new array that matches the windowed view in shape, but not in strides. We've shown in lots of other questions that it's possible to divide an array into blocks with a mix `reshape` and `transpose`, and do the reverse with a complementary mix. Most examples work with 2d arrays (e.g. images) but the ideas extend to 3d. – hpaulj Feb 11 '21 at 15:36
1 Answers
1
Normally, writing back to as_strided
views is not advised because it can cause race conditions, but since you only made blocks, this should work:
original_shaped_array = windowed_array.transpose(0,3,1,4,2,5).reshape(32,32,32)
Additionally, if you never copied the windowed array, and do calculations in-place, the data should be changed in the original array - a windowed view is simply a new view into the same data. Don't do this if there is any overlap

Daniel F
- 13,620
- 2
- 29
- 55
-
basically each patch is sent to a segmentation algorithm that returns a new patch of the same size segmented. So original patch is untouched used as read only. I am actually tring to combine these segmented patches back into original shape – Hamza Yerlikaya Feb 11 '21 at 10:49
-
You may also be able to use [`np.block`](https://numpy.org/doc/stable/reference/generated/numpy.block.html) to reconstruct in that case – Daniel F Feb 11 '21 at 12:21