Using ndimage.gaussian_filter for 3d array
Hello, I am wanting to use ndimage.gaussian_filter for a 3d array in order to smooth the data . When I do this - abc[:,:,:] = ndimage.gaussian_filter(abc[:,:,:]*1e2,sigma=2,order=0) I get unreal values. However the function call when done within a loop like this for k in range(0,N): abc[k,:,:] = ndimage.gaussian_filter(abc[k,:,:]*1e2,sigma=2,order=0) gives reasonable smoothed values. From the docs - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_... the input array does not seem to have any restriction on the dimensionality . So where am I going wrong ? Best regards, Ashwin.
A 3D gaussian filter is smoothing across all axes, so in abc[k, :, :] you are getting some of abc[k-1, :, :] and abc[k+1, :, :], and so on, according to the kernel weights in 3D. To smooth in 2D across all slices of a 3D array, specify a different sigma per axis, like so: abc = ndimage.gaussian_filter(abc * 1e2, sigma=(0, 2, 2), output=abc, order=0) Note that you don't need the [:, :, :]: using output=array is how to ensure that the array is modified in-place (if you are trying to avoid an extra allocation). Juan. On Tue, 26 Oct 2021, at 4:59 AM, ashwin .D wrote:
Hello, I am wanting to use ndimage.gaussian_filter for a 3d array in order to smooth the data .
When I do this -
abc[:,:,:] = ndimage.gaussian_filter(abc[:,:,:]*1e2,sigma=2,order=0) I get unreal values.
However the function call when done within a loop like this
for k in range(0,N): abc[k,:,:] = ndimage.gaussian_filter(abc[k,:,:]*1e2,sigma=2,order=0) gives reasonable smoothed values.
From the docs - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_...
the input array does not seem to have any restriction on the dimensionality . So where am I going wrong ?
Best regards, Ashwin.
_______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: jni@fastmail.com
Hello Juan, Thanks for your prompt response. You are right - I only want to smooth along 2d slices along the first axis. args = [] #abc shape is (10,100,100) for abc2d in abc: ndimage.gaussian_filter(abc2d*1e2,sigma=2,output=abc2d,order=0) b = abc2d[newaxis,:,:] args.append(b) smoothedABC = np.concatenate(args,axis=0) Is this the best vectorized approach that I can take forward ? On Tue, Oct 26, 2021 at 3:43 PM Juan Nunez-Iglesias <jni@fastmail.com> wrote:
A 3D gaussian filter is smoothing across all axes, so in abc[k, :, :] you are getting some of abc[k-1, :, :] and abc[k+1, :, :], and so on, according to the kernel weights in 3D. To smooth in 2D across all slices of a 3D array, specify a different sigma per axis, like so:
abc = ndimage.gaussian_filter(abc * 1e2, sigma=(0, 2, 2), output=abc, order=0)
Note that you don't need the [:, :, :]: using output=array is how to ensure that the array is modified in-place (if you are trying to avoid an extra allocation).
Juan.
On Tue, 26 Oct 2021, at 4:59 AM, ashwin .D wrote:
Hello, I am wanting to use ndimage.gaussian_filter for a 3d array in order to smooth the data .
When I do this -
abc[:,:,:] = ndimage.gaussian_filter(abc[:,:,:]*1e2,sigma=2,order=0) I get unreal values.
However the function call when done within a loop like this
for k in range(0,N): abc[k,:,:] = ndimage.gaussian_filter(abc[k,:,:]*1e2,sigma=2,order=0) gives reasonable smoothed values.
From the docs - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_...
the input array does not seem to have any restriction on the dimensionality . So where am I going wrong ?
Best regards, Ashwin.
_______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: jni@fastmail.com
_______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: winash12@gmail.com
Ashwin, As Juan mentioned, you can just use *sigma=(0, 2, 2)* on the 3D array and you will not need any for loop. By setting sigma=0 on the first axis, there will be no smoothing along that dimension. It will be equivalent to looping over the ten separate 2D images, using sigma=(2, 2) on each. Specifically, the suggestion is: *smoothed_abc = ndimage.gaussian_filter(abc, sigma=(0, 2, 2))* Cheers, Greg On Tue, Oct 26, 2021 at 7:04 AM ashwin .D <winash12@gmail.com> wrote:
Hello Juan, Thanks for your prompt response. You are right - I only want to smooth along 2d slices along the first axis. args = [] #abc shape is (10,100,100) for abc2d in abc: ndimage.gaussian_filter(abc2d*1e2,sigma=2,output=abc2d,order=0) b = abc2d[newaxis,:,:] args.append(b) smoothedABC = np.concatenate(args,axis=0)
Is this the best vectorized approach that I can take forward ?
On Tue, Oct 26, 2021 at 3:43 PM Juan Nunez-Iglesias <jni@fastmail.com> wrote:
A 3D gaussian filter is smoothing across all axes, so in abc[k, :, :] you are getting some of abc[k-1, :, :] and abc[k+1, :, :], and so on, according to the kernel weights in 3D. To smooth in 2D across all slices of a 3D array, specify a different sigma per axis, like so:
abc = ndimage.gaussian_filter(abc * 1e2, sigma=(0, 2, 2), output=abc, order=0)
Note that you don't need the [:, :, :]: using output=array is how to ensure that the array is modified in-place (if you are trying to avoid an extra allocation).
Juan.
On Tue, 26 Oct 2021, at 4:59 AM, ashwin .D wrote:
Hello, I am wanting to use ndimage.gaussian_filter for a 3d array in order to smooth the data .
When I do this -
abc[:,:,:] = ndimage.gaussian_filter(abc[:,:,:]*1e2,sigma=2,order=0) I get unreal values.
However the function call when done within a loop like this
for k in range(0,N): abc[k,:,:] = ndimage.gaussian_filter(abc[k,:,:]*1e2,sigma=2,order=0) gives reasonable smoothed values.
From the docs - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_...
the input array does not seem to have any restriction on the dimensionality . So where am I going wrong ?
Best regards, Ashwin.
_______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: jni@fastmail.com
_______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: winash12@gmail.com
_______________________________________________ SciPy-User mailing list -- scipy-user@python.org To unsubscribe send an email to scipy-user-leave@python.org https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: grlee77@gmail.com
participants (3)
-
ashwin .D
-
Gregory Lee
-
Juan Nunez-Iglesias