@@ -151,10 +151,10 @@ You can get and set individual fields in the header using dict (mapping-type)
151
151
item access. For example:
152
152
153
153
>>> n1_header[' cal_max' ]
154
- array(1162.0 , dtype=float32)
154
+ array(1162., dtype=float32)
155
155
>>> n1_header[' cal_max' ] = 1200
156
156
>>> n1_header[' cal_max' ]
157
- array(1200.0 , dtype=float32)
157
+ array(1200., dtype=float32)
158
158
159
159
Check the attributes of the header for ``get_ `` / ``set_ `` methods to get and
160
160
set various combinations of NIfTI header fields.
@@ -173,10 +173,10 @@ Like other nibabel image types, NIfTI images have an affine relating the voxel
173
173
coordinates to world coordinates in RAS+ space:
174
174
175
175
>>> n1_img.affine
176
- array([[ -2. , 0. , 0. , 117.86],
177
- [ -0. , 1.97, -0.36, -35.72],
178
- [ 0. , 0.32, 2.17, -7.25],
179
- [ 0. , 0. , 0. , 1. ]])
176
+ array([[ -2. , 0. , 0. , 117.86],
177
+ [ -0. , 1.97, -0.36, -35.72],
178
+ [ 0. , 0.32, 2.17, -7.25],
179
+ [ 0. , 0. , 0. , 1. ]])
180
180
181
181
Unlike other formats, the NIfTI header format can specify this affine in one
182
182
of three ways |-- | the *sform * affine, the *qform * affine and the *fall-back
@@ -199,16 +199,16 @@ the image or the header.
199
199
For example:
200
200
201
201
>>> print (n1_header[' srow_x' ])
202
- [ -2. 0. 0. 117.86]
202
+ [ -2. 0. 0. 117.86]
203
203
>>> print (n1_header[' srow_y' ])
204
204
[ -0. 1.97 -0.36 -35.72]
205
205
>>> print (n1_header[' srow_z' ])
206
206
[ 0. 0.32 2.17 -7.25]
207
207
>>> print (n1_header.get_sform())
208
- [[ -2. 0. 0. 117.86]
209
- [ -0. 1.97 -0.36 -35.72]
210
- [ 0. 0.32 2.17 -7.25]
211
- [ 0. 0. 0. 1. ]]
208
+ [[ -2. 0. 0. 117.86]
209
+ [ -0. 1.97 -0.36 -35.72]
210
+ [ 0. 0.32 2.17 -7.25]
211
+ [ 0. 0. 0. 1. ]]
212
212
213
213
This affine is valid only if the ``sform_code `` is not zero.
214
214
@@ -234,29 +234,29 @@ You can get the affine and the code using the ``coded=True`` argument to
234
234
``get_sform() ``:
235
235
236
236
>>> print (n1_header.get_sform(coded = True ))
237
- (array([[ -2. , 0. , 0. , 117.86],
238
- [ -0. , 1.97, -0.36, -35.72],
239
- [ 0. , 0.32, 2.17, -7.25],
240
- [ 0. , 0. , 0. , 1. ]]), 1)
237
+ (array([[ -2. , 0. , 0. , 117.86],
238
+ [ -0. , 1.97, -0.36, -35.72],
239
+ [ 0. , 0.32, 2.17, -7.25],
240
+ [ 0. , 0. , 0. , 1. ]]), 1)
241
241
242
242
You can set the sform with the ``set_sform() `` method of the header and
243
243
the image.
244
244
245
245
>>> n1_header.set_sform(np.diag([2 , 3 , 4 , 1 ]))
246
246
>>> n1_header.get_sform()
247
- array([[ 2., 0., 0., 0.],
248
- [ 0., 3., 0., 0.],
249
- [ 0., 0., 4., 0.],
250
- [ 0., 0., 0., 1.]])
247
+ array([[2., 0., 0., 0.],
248
+ [0., 3., 0., 0.],
249
+ [0., 0., 4., 0.],
250
+ [0., 0., 0., 1.]])
251
251
252
252
Set the affine and code using the ``code `` parameter to ``set_sform() ``:
253
253
254
254
>>> n1_header.set_sform(np.diag([3 , 4 , 5 , 1 ]), code = ' mni' )
255
255
>>> n1_header.get_sform(coded = True )
256
- (array([[ 3., 0., 0., 0.],
257
- [ 0., 4., 0., 0.],
258
- [ 0., 0., 5., 0.],
259
- [ 0., 0., 0., 1.]]), 4)
256
+ (array([[3., 0., 0., 0.],
257
+ [0., 4., 0., 0.],
258
+ [0., 0., 5., 0.],
259
+ [0., 0., 0., 1.]]), 4)
260
260
261
261
The qform affine
262
262
================
@@ -273,10 +273,10 @@ You can get and set the qform affine using the equivalent methods to those for
273
273
the sform: ``get_qform() ``, ``set_qform() ``.
274
274
275
275
>>> n1_header.get_qform(coded = True )
276
- (array([[ -2. , 0. , 0. , 117.86],
277
- [ -0. , 1.97, -0.36, -35.72],
278
- [ 0. , 0.32, 2.17, -7.25],
279
- [ 0. , 0. , 0. , 1. ]]), 1)
276
+ (array([[ -2. , 0. , 0. , 117.86],
277
+ [ -0. , 1.97, -0.36, -35.72],
278
+ [ 0. , 0.32, 2.17, -7.25],
279
+ [ 0. , 0. , 0. , 1. ]]), 1)
280
280
281
281
The qform also has a corresponding ``qform_code `` with the same interpretation
282
282
as the `sform_code `.
@@ -295,10 +295,10 @@ Again like SPM, we prefer to assume LAS+ voxel orientation by default.
295
295
You can always get the fall-back affine with ``get_base_affine() ``:
296
296
297
297
>>> n1_header.get_base_affine()
298
- array([[ -2. , 0. , 0. , 127. ],
299
- [ 0. , 2. , 0. , -95. ],
300
- [ 0. , 0. , 2.2, -25.3],
301
- [ 0. , 0. , 0. , 1. ]])
298
+ array([[ -2. , 0. , 0. , 127. ],
299
+ [ 0. , 2. , 0. , -95. ],
300
+ [ 0. , 0. , 2.2, -25.3],
301
+ [ 0. , 0. , 0. , 1. ]])
302
302
303
303
.. _choosing-image-affine :
304
304
@@ -329,10 +329,10 @@ The sform and qform codes will be initialised to 2 (aligned) and 0 (unknown)
329
329
respectively:
330
330
331
331
>>> img.get_sform(coded = True ) # doctest: +NORMALIZE_WHITESPACE
332
- (array([[ 2., 0., 0., 0.],
333
- [ 0., 2., 0., 0.],
334
- [ 0., 0., 2., 0.],
335
- [ 0., 0., 0., 1.]]), 2)
332
+ (array([[2., 0., 0., 0.],
333
+ [ 0., 2., 0., 0.],
334
+ [ 0., 0., 2., 0.],
335
+ [ 0., 0., 0., 1.]]), 2)
336
336
>>> img.get_qform(coded = True )
337
337
(None, 0)
338
338
@@ -434,21 +434,21 @@ image is written. We can do this by setting the fields directly, or with
434
434
>>> array_header.get_slope_inter()
435
435
(2.0, 10.0)
436
436
>>> array_header[' scl_slope' ]
437
- array(2.0 , dtype=float32)
437
+ array(2., dtype=float32)
438
438
>>> array_header[' scl_inter' ]
439
- array(10.0 , dtype=float32)
439
+ array(10., dtype=float32)
440
440
441
441
Setting the scale factors in the header has no effect on the image data before
442
442
we save and load again:
443
443
444
- >>> array_img.get_data ()
445
- array([[[ 0, 1, 2, 3],
446
- [ 4, 5, 6, 7],
447
- [ 8, 9, 10, 11]],
444
+ >>> array_img.get_fdata ()
445
+ array([[[ 0. , 1. , 2. , 3. ],
446
+ [ 4. , 5. , 6. , 7. ],
447
+ [ 8. , 9. , 10. , 11. ]],
448
448
<BLANKLINE>
449
- [[12, 13, 14, 15],
450
- [16, 17, 18, 19],
451
- [20, 21, 22, 23]]], dtype=int16 )
449
+ [[12. , 13. , 14. , 15. ],
450
+ [16. , 17. , 18. , 19. ],
451
+ [20. , 21. , 22. , 23. ]]])
452
452
453
453
Now we save the image and load it again:
454
454
@@ -457,14 +457,14 @@ Now we save the image and load it again:
457
457
458
458
The data array has the scaling applied:
459
459
460
- >>> scaled_img.get_data ()
461
- ... ([[[ 10., 12., 14., 16.],
462
- [ 18., 20., 22., 24.],
463
- [ 26., 28., 30., 32.]],
460
+ >>> scaled_img.get_fdata ()
461
+ array ([[[10., 12., 14., 16.],
462
+ [18., 20., 22., 24.],
463
+ [26., 28., 30., 32.]],
464
464
<BLANKLINE>
465
- [[ 34., 36., 38., 40.],
466
- [ 42., 44., 46., 48.],
467
- [ 50., 52., 54., 56.]]])
465
+ [[34., 36., 38., 40.],
466
+ [42., 44., 46., 48.],
467
+ [50., 52., 54., 56.]]])
468
468
469
469
The header for the loaded image has had the scaling reset to undefined, to
470
470
mark the fact that the scaling has been "consumed" by the load:
0 commit comments