|
6 | 6 | # copyright and license terms.
|
7 | 7 | #
|
8 | 8 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
9 |
| -''' Very simple spatial image class |
| 9 | +''' A simple spatial image class |
10 | 10 |
|
11 | 11 | The image class maintains the association between a 3D (or greater)
|
12 | 12 | array, and an affine transform that maps voxel coordinates to some world space.
|
|
27 | 27 | ``fname``, where the derivation may differ between formats.
|
28 | 28 | * to_file_map() - save image to files with which the image is already
|
29 | 29 | associated.
|
30 |
| - * .get_shape() (Deprecated) |
| 30 | + * .get_shape() (deprecated) |
31 | 31 |
|
32 | 32 | properties:
|
33 | 33 |
|
@@ -451,16 +451,16 @@ def __str__(self):
|
451 | 451 | def get_data(self, caching='fill'):
|
452 | 452 | """ Return image data from image with any necessary scalng applied
|
453 | 453 |
|
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 |
455 | 455 | the image data from disk) then the default behavior (`caching` ==
|
456 | 456 | "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. |
458 | 458 |
|
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). |
464 | 464 |
|
465 | 465 | Parameters
|
466 | 466 | ----------
|
@@ -522,27 +522,56 @@ def get_data(self, caching='fill'):
|
522 | 522 | will modify the result of future calls to ``get_data()``. For example
|
523 | 523 | you might do this:
|
524 | 524 |
|
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``: |
528 | 540 |
|
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 |
532 | 552 |
|
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 |
536 | 561 |
|
537 | 562 | 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 |
546 | 575 | """
|
547 | 576 | if caching not in ('fill', 'unchanged'):
|
548 | 577 | raise ValueError('caching value should be "fill" or "unchanged"')
|
|
0 commit comments