Skip to content

Commit 0045ed4

Browse files
authored
Merge pull request #637 from matthew-brett/docs-using-get-fdata
MRG: use get_fdata in docs
2 parents 587a6c7 + 95cb2b4 commit 0045ed4

9 files changed

+76
-62
lines changed

doc/source/coordinate_systems.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ We can load up the EPI image to get the image data array:
3434

3535
>>> import nibabel as nib
3636
>>> epi_img = nib.load('downloads/someones_epi.nii.gz')
37-
>>> epi_img_data = epi_img.get_data()
37+
>>> epi_img_data = epi_img.get_fdata()
3838
>>> epi_img_data.shape
3939
(53, 61, 33)
4040

@@ -64,7 +64,7 @@ and look at slices in the three axes:
6464
:context:
6565

6666
>>> anat_img = nib.load('downloads/someones_anatomy.nii.gz')
67-
>>> anat_img_data = anat_img.get_data()
67+
>>> anat_img_data = anat_img.get_fdata()
6868
>>> anat_img_data.shape
6969
(57, 67, 56)
7070
>>> show_slices([anat_img_data[28, :, :],

doc/source/devel/modified_images.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ flag when anyone asks for the data, on the basis that the user may then
7777
do something to the data and you can't know if they have::
7878

7979
img = nibabel.load('some_image.nii')
80-
data = img.get_data()
80+
data = img.get_fdata()
8181
data[:] = 0
8282
img2 = nibabel.load('some_image.nii')
83-
assert not np.all(img2.get_data() == img.get_data())
83+
assert not np.all(img2.get_fdata() == img.get_fdata())
8484

8585
The image consists of the data, the affine and a header. In order to
8686
keep track of the header and affine, we could cache them when loading
@@ -96,7 +96,7 @@ When we need to know whether the image object and image file correspond, we
9696
could check the current header and current affine (the header may be separate
9797
from the affine for an SPM Analyze image) against their cached copies, if they
9898
are the same and the 'dirty' flag has not been set by a previous call to
99-
``get_data()``, we know that the image file does correspond to the image
99+
``get_fdata()``, we know that the image file does correspond to the image
100100
object.
101101

102102
This may be OK for small bits of memory like the affine and the header,

doc/source/gettingstarted.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ This information is available without the need to load anything of the main
6666
image data into the memory. Of course there is also access to the image data as
6767
a NumPy_ array
6868

69-
>>> data = img.get_data()
69+
>>> data = img.get_fdata()
7070
>>> data.shape
7171
(128, 96, 24, 2)
7272
>>> type(data)

doc/source/images_and_memory.rst

+31-31
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ disk.
1919

2020
Nibabel does not load the image array from the proxy when you ``load`` the
2121
image. It waits until you ask for the array data. The standard way to ask
22-
for the array data is to call the ``get_data()`` method:
22+
for the array data is to call the ``get_fdata()`` method:
2323

24-
>>> data = img.get_data()
24+
>>> data = img.get_fdata()
2525
>>> data.shape
2626
(128, 96, 24, 2)
2727

28-
We also saw in :ref:`proxies-caching` that this call to ``get_data()`` will
28+
We also saw in :ref:`proxies-caching` that this call to ``get_fdata()`` will
2929
(by default) load the array data into an internal image cache. The image
30-
returns the cached copy on the next call to ``get_data()``:
30+
returns the cached copy on the next call to ``get_fdata()``:
3131

32-
>>> data_again = img.get_data()
32+
>>> data_again = img.get_fdata()
3333
>>> data is data_again
3434
True
3535

@@ -64,7 +64,7 @@ in cache, and True when it is in cache:
6464
>>> img = nib.load(example_file)
6565
>>> img.in_memory
6666
False
67-
>>> data = img.get_data()
67+
>>> data = img.get_fdata()
6868
>>> img.in_memory
6969
True
7070

@@ -73,10 +73,10 @@ True
7373
Using ``uncache``
7474
*****************
7575

76-
As y'all know, the proxy image has the array in cache, ``get_data()`` returns
76+
As y'all know, the proxy image has the array in cache, ``get_fdata()`` returns
7777
the cached array:
7878

79-
>>> data_again = img.get_data()
79+
>>> data_again = img.get_fdata()
8080
>>> data_again is data # same array returned from cache
8181
True
8282

@@ -85,34 +85,34 @@ You can uncache a proxy image with the ``uncache()`` method:
8585
>>> img.uncache()
8686
>>> img.in_memory
8787
False
88-
>>> data_once_more = img.get_data()
88+
>>> data_once_more = img.get_fdata()
8989
>>> data_once_more is data # a new copy read from disk
9090
False
9191

9292
``uncache()`` has no effect if the image is an array image, or if the cache is
9393
already empty.
9494

95-
You need to be careful when you modify arrays returned by ``get_data()`` on
95+
You need to be careful when you modify arrays returned by ``get_fdata()`` on
9696
proxy images, because ``uncache`` will then change the result you get back
97-
from ``get_data()``:
97+
from ``get_fdata()``:
9898

9999
>>> proxy_img = nib.load(example_file)
100-
>>> data = proxy_img.get_data() # array cached and returned
100+
>>> data = proxy_img.get_fdata() # array cached and returned
101101
>>> data[0, 0, 0, 0]
102-
0
102+
0.0
103103
>>> data[0, 0, 0, 0] = 99 # modify returned array
104-
>>> data_again = proxy_img.get_data() # return cached array
104+
>>> data_again = proxy_img.get_fdata() # return cached array
105105
>>> data_again[0, 0, 0, 0] # cached array modified
106-
99
106+
99.0
107107

108108
So far the proxy image behaves the same as an array image. ``uncache()`` has
109109
no effect on an array image, but it does have an effect on the returned array
110110
of a proxy image:
111111

112112
>>> proxy_img.uncache() # cached array discarded from proxy image
113-
>>> data_once_more = proxy_img.get_data() # new copy of array loaded
113+
>>> data_once_more = proxy_img.get_fdata() # new copy of array loaded
114114
>>> data_once_more[0, 0, 0, 0] # array modifications discarded
115-
0
115+
0.0
116116

117117
*************
118118
Saving memory
@@ -126,8 +126,8 @@ use the ``uncache()`` method:
126126

127127
>>> img.uncache()
128128

129-
Use the array proxy instead of ``get_data()``
130-
=============================================
129+
Use the array proxy instead of ``get_fdata()``
130+
==============================================
131131

132132
The ``dataobj`` property of a proxy image is an array proxy. We can ask the
133133
proxy to return the array directly by passing ``dataobj`` to the numpy
@@ -145,47 +145,47 @@ This also works for array images, because ``np.asarray`` returns the array:
145145
>>> type(data_array)
146146
<... 'numpy.ndarray'>
147147

148-
If you want to avoid caching you can avoid ``get_data()`` and always use
148+
If you want to avoid caching you can avoid ``get_fdata()`` and always use
149149
``np.asarray(img.dataobj)``.
150150

151-
Use the ``caching`` keyword to ``get_data()``
152-
=============================================
151+
Use the ``caching`` keyword to ``get_fdata()``
152+
==============================================
153153

154-
The default behavior of the ``get_data()`` function is to always fill the
154+
The default behavior of the ``get_fdata()`` function is to always fill the
155155
cache, if it is empty. This corresponds to the default ``'fill'`` value
156156
to the ``caching`` keyword. So, this:
157157

158158
>>> proxy_img = nib.load(example_file)
159-
>>> data = proxy_img.get_data() # default caching='fill'
159+
>>> data = proxy_img.get_fdata() # default caching='fill'
160160
>>> proxy_img.in_memory
161161
True
162162

163163
is the same as this:
164164

165165
>>> proxy_img = nib.load(example_file)
166-
>>> data = proxy_img.get_data(caching='fill')
166+
>>> data = proxy_img.get_fdata(caching='fill')
167167
>>> proxy_img.in_memory
168168
True
169169

170170
Sometimes you may want to avoid filling the cache, if it is empty. In this
171171
case, you can use ``caching='unchanged'``:
172172

173173
>>> proxy_img = nib.load(example_file)
174-
>>> data = proxy_img.get_data(caching='unchanged')
174+
>>> data = proxy_img.get_fdata(caching='unchanged')
175175
>>> proxy_img.in_memory
176176
False
177177

178178
``caching='unchanged'`` will leave the cache full if it is already full.
179179

180-
>>> data = proxy_img.get_data(caching='fill')
180+
>>> data = proxy_img.get_fdata(caching='fill')
181181
>>> proxy_img.in_memory
182182
True
183-
>>> data = proxy_img.get_data(caching='unchanged')
183+
>>> data = proxy_img.get_fdata(caching='unchanged')
184184
>>> proxy_img.in_memory
185185
True
186186

187-
See the :meth:`get_data() docstring
188-
<nibabel.spatialimages.SpatialImage.get_data>` for more detail.
187+
See the :meth:`get_fdata() docstring
188+
<nibabel.spatialimages.SpatialImage.get_fdata>` for more detail.
189189

190190
**********************
191191
Saving time and memory
@@ -202,7 +202,7 @@ For example, let us say you only wanted the second volume from the example
202202
dataset. You could do this:
203203

204204
>>> proxy_img = nib.load(example_file)
205-
>>> data = proxy_img.get_data()
205+
>>> data = proxy_img.get_fdata()
206206
>>> data.shape
207207
(128, 96, 24, 2)
208208
>>> vol1 = data[..., 1]

doc/source/neuro_radio_conventions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ showing the middle slice of :download:`an image
101101
[ 0. , 2.75, 0. , -91. ],
102102
[ 0. , 0. , 2.75, -91. ],
103103
[ 0. , 0. , 0. , 1. ]])
104-
>>> img_data = img.get_data()
104+
>>> img_data = img.get_fdata()
105105
>>> a_slice = img_data[:, :, 28]
106106
>>> # Need transpose to put first axis left-right, second bottom-top
107107
>>> plt.imshow(a_slice.T, cmap="gray", origin="lower") # doctest: +SKIP

doc/source/nibabel_images.rst

+29-11
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,39 @@ False
220220
Getting the image data the easy way
221221
===================================
222222

223-
For either type of image (array or proxy) you can always get the data with
224-
the :meth:`get_data() <nibabel.spatialimages.SpatialImage.get_data>` method.
223+
For either type of image (array or proxy) you can always get the data with the
224+
:meth:`get_fdata() <nibabel.spatialimages.SpatialImage.get_fdata>` method.
225225

226-
For the array image, ``get_data()`` just returns the data array:
226+
For the array image, ``get_fdata()`` just returns the data array, if it's already the required floating point type (default 64-bit float). If it isn't that type, ``get_fdata()`` casts it to one:
227227

228-
>>> image_data = array_img.get_data()
228+
>>> image_data = array_img.get_fdata()
229229
>>> image_data.shape
230230
(2, 3, 4)
231-
>>> image_data is array_data
231+
>>> image_data.dtype == np.dtype(np.float64)
232232
True
233233

234-
For the proxy image, the ``get_data()`` method fetches the array data from
234+
The cast to floating point means the array is not the one attached to the image:
235+
236+
>>> image_data is array_img.dataobj
237+
False
238+
239+
Here's an image backed by a floating point array:
240+
241+
>>> farray_img = nib.Nifti1Image(image_data.astype(np.float64), affine)
242+
>>> farray_data = farray_img.get_fdata()
243+
>>> farray_data.dtype == np.dtype(np.float64)
244+
True
245+
246+
There was no cast, so the array returned is exactly the array attached to the
247+
image:
248+
249+
>>> farray_data is farray_img.dataobj
250+
True
251+
252+
For the proxy image, the ``get_fdata()`` method fetches the array data from
235253
disk using the proxy, and returns the array.
236254

237-
>>> image_data = img.get_data()
255+
>>> image_data = img.get_fdata()
238256
>>> image_data.shape
239257
(128, 96, 24, 2)
240258

@@ -249,12 +267,12 @@ Proxies and caching
249267
===================
250268

251269
You may not want to keep loading the image data off disk every time
252-
you call ``get_data()`` on a proxy image. By default, when you call
253-
``get_data()`` the first time on a proxy image, the image object keeps a
254-
cached copy of the loaded array. The next time you call ``img.get_data()``,
270+
you call ``get_fdata()`` on a proxy image. By default, when you call
271+
``get_fdata()`` the first time on a proxy image, the image object keeps a
272+
cached copy of the loaded array. The next time you call ``img.get_fdata()``,
255273
the image returns the array from cache rather than loading it from disk again.
256274

257-
>>> data_again = img.get_data()
275+
>>> data_again = img.get_fdata()
258276

259277
The returned data is the same (cached) copy we returned before:
260278

doc/source/old/examples.txt

+5-9
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ previously created in a separate file. First, we open the file:
108108
Now we select the first ten volumes and store them to another file, while
109109
preserving as much header information as possible
110110

111-
>>> nim2 = nib.Nifti1Image(nim.get_data()[..., :10],
111+
>>> nim2 = nib.Nifti1Image(nim.get_fdata()[..., :10],
112112
... nim.get_affine(),
113113
... nim.header)
114114
>>> print nim2.header['dim']
@@ -127,7 +127,7 @@ Linear detrending of timeseries (SciPy module is required for this example)
127127
===========================================================================
128128

129129
Let's load another 4d NIfTI file and perform a linear detrending, by fitting
130-
a straight line to the timeseries of each voxel and substract that fit from
130+
a straight line to the timeseries of each voxel and subtract that fit from
131131
the data. Although this might sound complicated at first, thanks to the
132132
excellent SciPy module it is just a few lines of code. For this example we
133133
will first create a NIfTI image with just a single voxel and 50 timepoints
@@ -139,15 +139,11 @@ will first create a NIfTI image with just a single voxel and 50 timepoints
139139
>>> print nim.header['dim']
140140
[ 4 1 1 1 50 1 1 1]
141141

142-
Depending on the datatype of the input image the detrending process might
143-
change the datatype from integer to float. As operations that change the
144-
(binary) size of the NIfTI image are not supported, we need to make a copy
145-
of the data and later create a new NIfTI image. Remember that the array has the
146-
time axis as its first dimension (in contrast to the NIfTI file where it is
147-
the 4th).
142+
Remember that the array has the time axis as its first dimension (in contrast
143+
to the NIfTI file where it is the 4th).
148144

149145
>>> from scipy import signal
150-
>>> data_detrended = signal.detrend(nim.get_data(), axis=0)
146+
>>> data_detrended = signal.detrend(nim.get_fdata(), axis=0)
151147

152148
Finally, create a new NIfTI image using header information from the original
153149
source image.

doc/source/old/orientation.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ the affine after loading, as in::
8585
img = nibabel.load('some_image.img')
8686
aff = img.get_affine()
8787
x_flipper = np.diag([-1,1,1,1])
88-
lr_img = nibabel.Nifti1Image(img.get_data, np.dot(x_flipper, aff), img.header)
88+
lr_img = nibabel.Nifti1Image(img.get_fdata(), np.dot(x_flipper, aff), img.header)
8989

9090
Affines for Analyze, SPM analyze, and NIFTI
9191
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/source/scripts/make_coord_examples.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
t1_img, t2_img = imgs
5151

5252
# Make fake localizer
53-
data = t1_img.get_data()
53+
data = t1_img.get_fdata()
5454
n_x, n_y, n_z = img.shape
5555
mid_x = round(n_x / 2)
5656

@@ -171,7 +171,7 @@ def vx2mm(pts):
171171
# resample, preserving affine
172172
epi_cmap = nca.vox2mni(epi_vox2mm)
173173
epi = rsm.resample(t2_img, epi_cmap, np.eye(4), epi_vox_shape)
174-
epi_data = epi.get_data()
174+
epi_data = epi.get_fdata()
175175
# Do the same kind of thing for the anatomical scan
176176
anat_vox_sizes = [2.75, 2.75, 2.75]
177177
anat_scale = npl.inv(np.diag(anat_vox_sizes + [1]))
@@ -183,7 +183,7 @@ def vx2mm(pts):
183183
[data.shape[0], anat_x_len, anat_y_len], anat_vox_sizes))
184184
anat_cmap = nca.vox2mni(anat_vox2mm)
185185
anat = rsm.resample(t1_img, anat_cmap, np.eye(4), anat_vox_shape)
186-
anat_data = anat.get_data()
186+
anat_data = anat.get_fdata()
187187

188188
save_plot()
189189
nipy.save_image(epi, 'someones_epi.nii.gz', dtype_from='uint8')

0 commit comments

Comments
 (0)