[Neuroimaging] Nibabel get_fdata()

serafim loukas seralouk at hotmail.com
Wed Oct 17 15:17:45 EDT 2018


Thank you for this detailed answer.

What about data = np.asanyarray(data.dataobj) ?

I think this is what I need right now.



On 17 Oct 2018, at 16:26, Christopher Markiewicz <markiewicz at stanford.edu<mailto:markiewicz at stanford.edu>> wrote:

Hi Serafim,

Matthew's analysis is correct, but just to let you know, get_data will be deprecated in the near future, so it's worth figuring out how to do what you want with the more consistent APIs.

get_fdata will by default return 64-bit floats, but you can provide any dtype you like. If you have 32-bit float data, you can use get_fdata(np.float32). I believe that will simply return the memory-mapped array without loading the whole thing into memory.

Another approach is to slice the image, to ensure that you retain any scale factors, before calling get_fdata():


import numpy as np
import nibabel as nib

data = nib.load('wBOLD.nii')
fast_fdata = data.slicer[..., 0].get_fdata()

This takes advantage of the optimizations that have been built into the data objects, while preserving the image API, but you can also directly interact with the data object, which allows you to slice efficiently while performing any necessary scaling:

flexible = data.dataobj[..., 0]
flexible_and_typed = data.dataobj[..., 0].astype(np.float32)

For more details, please see: http://nipy.org/nibabel/nibabel_images.html#array-proxies-and-proxy-images

And let us know if there's anything unclear in the docs.

Chris

________________________________
From: Neuroimaging <neuroimaging-bounces+markiewicz=stanford.edu at python.org<mailto:neuroimaging-bounces+markiewicz=stanford.edu at python.org>> on behalf of serafim loukas <seralouk at hotmail.com<mailto:seralouk at hotmail.com>>
Sent: Wednesday, October 17, 2018 8:19:40 AM
To: Neuroimaging analysis in Python
Subject: Re: [Neuroimaging] Nibabel get_fdata()

Hello,


The images are on my local HD.
The file is around 60GB.

Here is my code to demonstrate this:

import nibabel as nib

data = nib.load('wBOLD.nii')
slices_fast = data.get_data()[:,:,:,0]
slices_slow = data.get_fdata()[:,:,:,0]

And here you can find the file compressed: https://drive.google.com/open?id=1BkClOxytqUKtv5Zt7fMr2P-kvzJ5OKS0


I was thinking to use get_data() instead of get_fdata() and then conduct some analysis.
In this case, what is the difference between the 2 commands ?


Best,
Serafim


On 17 Oct 2018, at 12:49, Matthew Brett <matthew.brett at gmail.com<mailto:matthew.brett at gmail.com>> wrote:

Hi,

Can you post an image somewhere for us to try?

Are your images on your local hard disk?

Cheers,

Matthew
On Wed, Oct 17, 2018 at 10:07 AM serafim loukas <seralouk at hotmail.com<mailto:seralouk at hotmail.com>> wrote:

Dear Experts,



I have some neuroimaging data and I would like to use Nibabel instead of SPM (matlab).
My goal is to load brain images, then vectorise them and finally do some analysis on these vectors.

When I use the get_fdata() command, it takes a lot of time to load the image.
On the other hand, SPM is faster (spm_read_vols(spm_vol(my_image_path))) in terms of loading the data.

Am I missing something? Is there any way to make get_fdata() faster?



Best,
Serafim
_______________________________________________
Neuroimaging mailing list
Neuroimaging at python.org<mailto:Neuroimaging at python.org>
https://mail.python.org/mailman/listinfo/neuroimaging
_______________________________________________
Neuroimaging mailing list
Neuroimaging at python.org<mailto:Neuroimaging at python.org>
https://mail.python.org/mailman/listinfo/neuroimaging

_______________________________________________
Neuroimaging mailing list
Neuroimaging at python.org<mailto:Neuroimaging at python.org>
https://mail.python.org/mailman/listinfo/neuroimaging

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20181017/d0061994/attachment.html>


More information about the Neuroimaging mailing list