@@ -23,81 +23,86 @@ familiarize oneself with the basic design of the API.
23
23
24
24
When loading an image, NiBabel aims to figure out the image format from the
25
25
filename. An image in a known format can easily be loaded by simply passing its
26
- name to the ``load `` function:
26
+ name to the ``load `` function. First let's get the nibabel example data
27
+ directory:
27
28
28
- >>> import nibabel as nb
29
- >>> import os
30
- >>> import numpy as np
31
- >>> img = nb.load(os.path.join(' nibabel' , ' tests' , ' data' ,
32
- ... ' example4d.nii.gz' ))
29
+ >>> import os
30
+ >>> from nibabel.testing import data_path
33
31
34
- A NiBabel image knows about its shape
32
+ Now we can load an image:
35
33
36
- >>> img.get_shape()
37
- (128, 96, 24, 2 )
34
+ >>> import nibabel as nib
35
+ >>> img = nib.load(os.path.join(data_path, ' example4d.nii.gz ' ) )
38
36
39
- its data type
37
+ A NiBabel image knows about its shape:
40
38
41
- >>> img.get_data_dtype()
42
- dtype('int16')
39
+ >>> img.get_shape()
40
+ (128, 96, 24, 2)
41
+
42
+ and its data type:
43
+
44
+ >>> img.get_data_dtype()
45
+ dtype('int16')
43
46
44
47
and an affine transformation that determines the world-coordinates of the image
45
48
elements.
46
49
47
- >>> img.get_affine().shape
48
- (4, 4)
50
+ >>> img.get_affine().shape
51
+ (4, 4)
49
52
50
53
This information is available without the need to load anything of the main
51
54
image data into the memory. Of course there is also access to the image data as
52
55
a NumPy _ array
53
56
54
- >>> data = img.get_data()
55
- >>> data.shape
56
- (128, 96, 24, 2)
57
- >>> type (data)
58
- <type 'numpy.ndarray'>
57
+ >>> data = img.get_data()
58
+ >>> data.shape
59
+ (128, 96, 24, 2)
60
+ >>> type (data)
61
+ <type 'numpy.ndarray'>
59
62
60
63
The complete information embedded in an image header is available via a
61
64
format-specific header object.
62
65
63
- >>> hdr = img.get_header()
66
+ >>> hdr = img.get_header()
64
67
65
68
In case of this NIfTI _ file it allows accessing all NIfTI-specific information,
66
69
e.g.
67
70
68
- >>> hdr.get_xyzt_units()
69
- ('mm', 'sec')
71
+ >>> hdr.get_xyzt_units()
72
+ ('mm', 'sec')
70
73
71
74
Corresponding "setter" methods allow modifying a header, while ensuring its
72
75
compliance with the file format specifications.
73
76
74
77
In some situations even more flexibility is required and for ultimate experts
75
78
NiBabel also offers access to the raw header information
76
79
77
- >>> raw = hdr.structarr
78
- >>> raw[' xyzt_units' ]
79
- array(10, dtype=uint8)
80
+ >>> raw = hdr.structarr
81
+ >>> raw[' xyzt_units' ]
82
+ array(10, dtype=uint8)
80
83
81
84
This lowest level of the API is only for people that know what they are doing
82
- comes without any safety-net.
85
+ and comes without any safety-net.
83
86
84
87
Creating a new image in some file format is also easy. At a minimum it only
85
88
needs some image data and an image coordinate transformation.
86
89
87
- >>> data = np.ones((32 , 32 , 15 , 100 ), dtype = np.int16)
88
- >>> img = nb.Nifti1Image(data, np.eye(4 ))
89
- >>> img.get_data_dtype()
90
- dtype('int16')
91
- >>> img.get_header().get_xyzt_units()
92
- ('unknown', 'unknown')
90
+ >>> import numpy as np
91
+ >>> data = np.ones((32 , 32 , 15 , 100 ), dtype = np.int16)
92
+ >>> img = nib.Nifti1Image(data, np.eye(4 ))
93
+ >>> img.get_data_dtype()
94
+ dtype('int16')
95
+ >>> img.get_header().get_xyzt_units()
96
+ ('unknown', 'unknown')
93
97
94
98
In this case, identity is used as the affine transformation. The image header
95
99
is initialized from the provided data array (i.e. shape, dtype) and all other
96
100
values are set to resonable defaults.
97
101
98
- Saving this new image to a file is trivial
102
+ Saving this new image to a file is trivial. We won't do it here, but it looks
103
+ like::
99
104
100
- >>> img.to_filename(os.path.join(' build' ,' test4d.nii.gz' ))
105
+ img.to_filename(os.path.join('build','test4d.nii.gz'))
101
106
102
107
This short introduction only gave a quick overview of NiBabel's capabilities.
103
108
Please have a look at the :ref: `api ` for more details about supported file
0 commit comments