Re: Using ndimage.gaussian_filter for 3d array

Gregory Lee <[email protected]> Tue, 26 Oct 2021 15:32:30 -0400
Newsgroups gmane.comp.python.scientific.user
Message-ID <CAJR3sXc4fTmo7z30veDmKiaxT0cx2S8QZB9Zj2VOn0bB_hvZLg@mail.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 <[email protected]> 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 <[email protected]>
> 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_filter.html
>>
>> 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 -- [email protected]
>> To unsubscribe send an email to [email protected]
>> https://mail.python.org/mailman3/lists/scipy-user.python.org/
>> Member address: [email protected]
>>
>>
>> _______________________________________________
>> SciPy-User mailing list -- [email protected]
>> To unsubscribe send an email to [email protected]
>> https://mail.python.org/mailman3/lists/scipy-user.python.org/
>> Member address: [email protected]
>>
> _______________________________________________
> SciPy-User mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/scipy-user.python.org/
> Member address: [email protected]
>

_______________________________________________
SciPy-User mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/scipy-user.python.org/
Member address: [email protected]