2

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.

5
  • 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. Commented Feb 11, 2021 at 0: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 minimal reproducible example. Commented Feb 11, 2021 at 0: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 :) Commented Feb 11, 2021 at 9: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. Commented Feb 11, 2021 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. Commented Feb 11, 2021 at 15:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
You may also be able to use np.block to reconstruct in that case

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.