[Neuroimaging] Nibabel get_fdata()

Christopher Markiewicz markiewicz at stanford.edu
Wed Oct 17 10:26:34 EDT 2018

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.


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


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 ?


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


Can you post an image somewhere for us to try?

Are your images on your local hard disk?


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?

Neuroimaging mailing list
Neuroimaging at python.org<mailto:Neuroimaging at python.org>
Neuroimaging mailing list
Neuroimaging at python.org<mailto:Neuroimaging at python.org>

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

More information about the Neuroimaging mailing list