Skip to content

Commit 387a049

Browse files
committed
DOC - fix up getting started doctests
1 parent 026e52c commit 387a049

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

doc/source/gettingstarted.rst

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,81 +23,86 @@ familiarize oneself with the basic design of the API.
2323

2424
When loading an image, NiBabel aims to figure out the image format from the
2525
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:
2728

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
3331

34-
A NiBabel image knows about its shape
32+
Now we can load an image:
3533

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'))
3836

39-
its data type
37+
A NiBabel image knows about its shape:
4038

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')
4346

4447
and an affine transformation that determines the world-coordinates of the image
4548
elements.
4649

47-
>>> img.get_affine().shape
48-
(4, 4)
50+
>>> img.get_affine().shape
51+
(4, 4)
4952

5053
This information is available without the need to load anything of the main
5154
image data into the memory. Of course there is also access to the image data as
5255
a NumPy_ array
5356

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'>
5962

6063
The complete information embedded in an image header is available via a
6164
format-specific header object.
6265

63-
>>> hdr = img.get_header()
66+
>>> hdr = img.get_header()
6467

6568
In case of this NIfTI_ file it allows accessing all NIfTI-specific information,
6669
e.g.
6770

68-
>>> hdr.get_xyzt_units()
69-
('mm', 'sec')
71+
>>> hdr.get_xyzt_units()
72+
('mm', 'sec')
7073

7174
Corresponding "setter" methods allow modifying a header, while ensuring its
7275
compliance with the file format specifications.
7376

7477
In some situations even more flexibility is required and for ultimate experts
7578
NiBabel also offers access to the raw header information
7679

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)
8083

8184
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.
8386

8487
Creating a new image in some file format is also easy. At a minimum it only
8588
needs some image data and an image coordinate transformation.
8689

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')
9397

9498
In this case, identity is used as the affine transformation. The image header
9599
is initialized from the provided data array (i.e. shape, dtype) and all other
96100
values are set to resonable defaults.
97101

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::
99104

100-
>>> img.to_filename(os.path.join('build','test4d.nii.gz'))
105+
img.to_filename(os.path.join('build','test4d.nii.gz'))
101106

102107
This short introduction only gave a quick overview of NiBabel's capabilities.
103108
Please have a look at the :ref:`api` for more details about supported file

0 commit comments

Comments
 (0)