@@ -18,40 +18,46 @@ file format format has its own features and pecularities that need to be taken
18
18
care of to get the most out of it. To this end, NiBabel offers both high-level
19
19
format-independent access to neuroimages, as well as an API with various levels
20
20
of format-specific access to all available information in a particular file
21
- format. The following demonstrations show some of NiBabel's capabilities and
22
- familiarize oneself with the basic design of the API.
21
+ format. The following examples show some of NiBabel's capabilities and give
22
+ you an idea of the API.
23
23
24
- When loading an image, NiBabel aims to figure out the image format from the
24
+ For more detail on the API, see :doc: `nibabel_images `.
25
+
26
+ When loading an image, NiBabel tries to figure out the image format from the
25
27
filename. An image in a known format can easily be loaded by simply passing its
26
- name to the ``load `` function.
28
+ filename to the ``load `` function.
27
29
28
- We load some useful libraries:
30
+ To start the code examples, we load some useful libraries:
29
31
30
32
>>> import os
31
33
>>> import numpy as np
32
34
33
- Then we get the nibabel example data directory :
35
+ Then we fine the nibabel directory containing the example data:
34
36
35
37
>>> from nibabel.testing import data_path
36
38
37
- Now we can load an image:
39
+ There is a NIfTI file in this directory called ``example4d.nii.gz ``:
40
+
41
+ >>> example_filename = os.path.join(data_path, ' example4d.nii.gz' )
42
+
43
+ Now we can import nibabel and load the image:
38
44
39
45
>>> import nibabel as nib
40
- >>> img = nib.load(os.path.join(data_path, ' example4d.nii.gz ' ) )
46
+ >>> img = nib.load(example_filename )
41
47
42
48
A NiBabel image knows about its shape:
43
49
44
50
>>> img.shape
45
51
(128, 96, 24, 2)
46
52
47
- and the data type of the data as stored on disk. In this case the data on disk
48
- are 16 bit signed integers:
53
+ It also records the data type of the data as stored on disk. In this case the
54
+ data on disk are 16 bit signed integers:
49
55
50
56
>>> img.get_data_dtype() == np.dtype(np.int16)
51
57
True
52
58
53
- The image also has an affine transformation that determines the
54
- world-coordinates of the image elements.
59
+ The image has an affine transformation that determines the world-coordinates of
60
+ the image elements (see :doc: ` coordinate_systems `):
55
61
56
62
>>> img.affine.shape
57
63
(4, 4)
80
86
Corresponding "setter" methods allow modifying a header, while ensuring its
81
87
compliance with the file format specifications.
82
88
83
- In some situations even more flexibility is required and for ultimate experts
89
+ In some situations we need even more flexibility, and for with great courage,
84
90
NiBabel also offers access to the raw header information
85
91
86
92
>>> raw = hdr.structarr
87
93
>>> raw[' xyzt_units' ]
88
94
array(10, dtype=uint8)
89
95
90
- This lowest level of the API is only for people that know what they are doing
91
- and comes without any safety-net.
96
+ This lowest level of the API is designed for people who know the file format
97
+ well enough to work with its internal data, and comes without any safety-net.
92
98
93
99
Creating a new image in some file format is also easy. At a minimum it only
94
- needs some image data and an image coordinate transformation.
100
+ needs some image data and an image coordinate transformation (affine):
95
101
96
102
>>> import numpy as np
97
103
>>> data = np.ones((32 , 32 , 15 , 100 ), dtype = np.int16)
@@ -101,15 +107,19 @@ True
101
107
>>> img.header.get_xyzt_units()
102
108
('unknown', 'unknown')
103
109
104
- In this case, identity is used as the affine transformation. The image header
105
- is initialized from the provided data array (i.e. shape, dtype) and all other
106
- values are set to resonable defaults.
110
+ In this case, we used the identity matrix as the affine transformation. The
111
+ image header is initialized from the provided data array (i.e. shape, dtype)
112
+ and all other values are set to resonable defaults.
107
113
108
114
Saving this new image to a file is trivial. We won't do it here, but it looks
109
115
like::
110
116
111
117
img.to_filename(os.path.join('build','test4d.nii.gz'))
112
118
119
+ or::
120
+
121
+ nib.save(img, os.path.join('build','test4d.nii.gz'))
122
+
113
123
This short introduction only gave a quick overview of NiBabel's capabilities.
114
124
Please have a look at the :ref: `api ` for more details about supported file
115
125
formats and their features.
0 commit comments