Skip to content

Commit 643266f

Browse files
committed
DOC: rewrite with doctests
Make get_data examples into doctests.
1 parent 6adeb15 commit 643266f

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

nibabel/spatialimages.py

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
''' Very simple spatial image class
9+
''' A simple spatial image class
1010
1111
The image class maintains the association between a 3D (or greater)
1212
array, and an affine transform that maps voxel coordinates to some world space.
@@ -27,7 +27,7 @@
2727
``fname``, where the derivation may differ between formats.
2828
* to_file_map() - save image to files with which the image is already
2929
associated.
30-
* .get_shape() (Deprecated)
30+
* .get_shape() (deprecated)
3131
3232
properties:
3333
@@ -451,16 +451,16 @@ def __str__(self):
451451
def get_data(self, caching='fill'):
452452
""" Return image data from image with any necessary scalng applied
453453
454-
If the image data is a array proxy (an object that knows how to load
454+
If the image data is an array proxy (an object that knows how to load
455455
the image data from disk) then the default behavior (`caching` ==
456456
"fill") is to read the data from the proxy, and store in an internal
457-
cache. Future calls to ``get_data`` will return the cached copy.
457+
cache. Future calls to ``get_data`` will return the cached array.
458458
459-
Once the data has been cached and returned from a proxy array, the
460-
cached array can be modified by modifying the returned array, because
461-
the returned array is a reference to the array in the cache. Regardless
462-
of the `caching` flag, this is always true of an in-memory image (where
463-
the image data is an array rather than an array proxy).
459+
Once the data has been cached and returned from an image array proxy,
460+
if you modify the returned array, you will also modify the cached
461+
array (because they are the same array). Regardless of the `caching`
462+
flag, this is always true of an in-memory image (where the image data
463+
is an array rather than an array proxy).
464464
465465
Parameters
466466
----------
@@ -522,27 +522,56 @@ def get_data(self, caching='fill'):
522522
will modify the result of future calls to ``get_data()``. For example
523523
you might do this:
524524
525-
img = load('my_image.nii') # a proxy image
526-
data = img.get_data()
527-
data[0, 0, 0] = 99
525+
>>> import os
526+
>>> import nibabel as nib
527+
>>> from nibabel.testing import data_path
528+
>>> img_fname = os.path.join(data_path, 'example4d.nii.gz')
529+
530+
>>> img = nib.load(img_fname) # This is a proxy image
531+
>>> nib.is_proxy(img.dataobj)
532+
True
533+
534+
The array is not yet cached by a call to "get_data", so:
535+
>>> img.in_memory
536+
False
537+
538+
After we call ``get_data`` using the default `caching='fill', the cache
539+
contains a reference to the returned array ``data``:
528540
529-
In this case the cache is full (default `caching='fill'), and the cache
530-
contains a reference to the returned array ``data``, so the next time
531-
you call ``get_data()``:
541+
>>> data = img.get_data()
542+
>>> img.in_memory
543+
True
544+
545+
We modify an element in the returned data array:
546+
547+
>>> data[0, 0, 0, 0]
548+
0
549+
>>> data[0, 0, 0, 0] = 99
550+
>>> data[0, 0, 0, 0]
551+
99
532552
533-
data_again = img.get_data()
534-
data_again is data # will be True
535-
data_again[0, 0, 0] == 99 # will be True
553+
The next time we call 'get_data', the method returns the cached
554+
reference to the (modified) array:
555+
556+
>>> data_again = img.get_data()
557+
>>> data_again is data
558+
True
559+
>>> data_again[0, 0, 0, 0]
560+
99
536561
537562
If you had *initially* used `caching` == 'unchanged' then the returned
538-
``data`` array is loaded from file, but not cached, and:
539-
540-
img = load('my_image.nii') # a proxy image
541-
data = img.get_data(caching='unchanged')
542-
data[0, 0, 0] = 99
543-
data_again = img.get_data(caching='unchanged')
544-
data_again is data # will be False
545-
data_again[0, 0, 0] == 99 # will be False
563+
``data`` array would have been loaded from file, but not cached, and:
564+
565+
>>> img = nib.load(img_fname) # a proxy image again
566+
>>> data = img.get_data(caching='unchanged')
567+
>>> img.in_memory
568+
False
569+
>>> data[0, 0, 0] = 99
570+
>>> data_again = img.get_data(caching='unchanged')
571+
>>> data_again is data
572+
False
573+
>>> data_again[0, 0, 0, 0]
574+
0
546575
"""
547576
if caching not in ('fill', 'unchanged'):
548577
raise ValueError('caching value should be "fill" or "unchanged"')

0 commit comments

Comments
 (0)