0

I want to fill in an empty 4D array. I have created a pre-allocated array (data_4d_smoothed) with 80 x 80 x 44 x 50. I want to loop through all (50) volumes of the data (data_4d), smooth them separately and store the results in data_4d_smoothed. Basically:

data_4d_smoothed = np.zeros(data_4d.shape)

sigma = 0.7
for i in data_4d[:, :, :, i]:
    smoothed_vol = gaussian_filter(i, sigma=sigma)
    data_4d_smoothed.append(smoothed_vol)

The gaussian_filter should take every volume (the last dimension of the 4d array), do the operation, and save it into data_4d_smoothed. But obviously, this is not a 2D array and I think I need a nested loop to fill this empty list.

1 Answer 1

1

I think this should work without looping:

from scipy.ndimage import gaussian_filter
data_4d = np.random.rand(80,80,44,50)
data_4d_smoothed = gaussian_filter(data_4d, sigma = (sigma, sigma, sigma, 0))

Basically make the last dimension's sigma = 0, so that it doesn't do the convolution in that dimension.

Checking:

data_4d_0 = gaussian_filter(data_4d[..., 0], sigma = sigma)  #filter first image

np.allclose(data_4d_0, data_4d_smoothed[..., 0])             #first image from global filter

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

4 Comments

This gives me False. Sigma is a property of the function - irrespective of the dimentions (gaussian_filter). So I don't think this fits :( the function should take 50 volumes one-by-one - the last dimension - apply the function property, and then save it in the output again as a 4D array.
You're not using scipy.ndimage.gaussian_filter? Then what are you using?
SORRY IT WORKED! can you explain what happens here?
A convolution with a sigma = 0 gaussian kernel just returns the original array, so by using 0 as the sigma in the 4th dimension, you only convolve over the first three dimensions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.