Skip to content

Commit 7c99db6

Browse files
committed
DOC: Docstrings
1 parent d0700db commit 7c99db6

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

ants/utils/nifti_utils.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def get_nifti_sform_shear(metadata):
161161
Returns
162162
-------
163163
[shear_xy, shear_xz, shear_yz] (cosines between axes; 0 means orthogonal)
164+
165+
Example
166+
-------
167+
>>> import ants
168+
>>> metadata = ants.read_image_metadata( ants.get_ants_data('mni') )
169+
>>> ants.get_nifti_sform_shear(metadata)
164170
"""
165171
A = _as_affine_4x4_from_srow(metadata)
166172
if A is None:
@@ -175,6 +181,28 @@ def deshear_nifti_sform(metadata, shear_threshold=1e-6, max_angle_deviation=0.5)
175181
Returns a new 4x4 affine (index->RAS) with shear removed (direction orthonormal),
176182
preserving spacings and translation. Raises ValueError if the orthonormalized
177183
directions deviate from the originals by more than max_angle_deviation degrees.
184+
185+
Arguments
186+
---------
187+
metadata : dict
188+
The NIfTI header metadata dictionary.
189+
shear_threshold : float
190+
Shear threshold for deshearing sform. Shear below this value is considered negligible, and the original sform is
191+
returned.
192+
max_angle_deviation : float
193+
Maximum angle deviation for directions after deshearing sform. If the desheared directions deviate from the original
194+
directions by more than this value (in degrees), a ValueError is raised.
195+
196+
Returns
197+
-------
198+
numpy.ndarray array
199+
The desheared affine matrix.
200+
201+
Example
202+
-------
203+
>>> import ants
204+
>>> metadata = ants.read_image_metadata( ants.get_ants_data('mni') )
205+
>>> A = ants.deshear_nifti_sform(metadata)
178206
"""
179207
A = _as_affine_4x4_from_srow(metadata)
180208
if A is None:
@@ -216,9 +244,10 @@ def get_nifti_sform_spatial_info(metadata, shear_threshold=1e-6, max_angle_devia
216244
metadata : dict
217245
The NIfTI header metadata dictionary.
218246
shear_threshold : float
219-
Shear threshold for deshearing sform.
247+
Shear threshold for deshearing sform, if the shear is beneath this value, the sform is not modified.
220248
max_angle_deviation : float
221-
Maximum angle deviation for directions after deshearing sform.
249+
Maximum angle deviation for directions after deshearing sform. If the desheared directions deviate from the original
250+
directions by more than this value (in degrees), deshearing fails and the return value is None.
222251
223252
Returns
224253
-------
@@ -229,6 +258,14 @@ def get_nifti_sform_spatial_info(metadata, shear_threshold=1e-6, max_angle_devia
229258
direction : 3x3 list (direction cosines)
230259
desheared : bool
231260
original_shear : [shear_xy, shear_xz, shear_yz]
261+
262+
Returns None if no sform is present or if deshearing fails.
263+
264+
Example
265+
-------
266+
>>> import ants
267+
>>> metadata = ants.read_image_metadata( ants.get_ants_data('mni') )
268+
>>> ants.get_nifti_sform_spatial_info(metadata)
232269
"""
233270
A = _as_affine_4x4_from_srow(metadata)
234271
if A is None:
@@ -279,6 +316,12 @@ def get_nifti_qform_spatial_info(metadata):
279316
transform_spacing : [sx, sy, sz]
280317
origin : [ox, oy, oz]
281318
direction : 3x3 list (direction cosines)
319+
320+
Example
321+
-------
322+
>>> import ants
323+
>>> metadata = ants.read_image_metadata( ants.get_ants_data('mni') )
324+
>>> ants.get_nifti_qform_spatial_info(metadata)
282325
"""
283326
A = _as_affine_4x4_from_qto(metadata)
284327
if A is None:
@@ -337,6 +380,12 @@ def get_nifti_spatial_transform_from_metadata(metadata, prefer_sform=True, shear
337380
"direction": [[...],[...],[...]],
338381
"transform_source": "sform" | "qform"
339382
}
383+
384+
Example
385+
-------
386+
>>> import ants
387+
>>> metadata = ants.read_image_metadata( ants.get_ants_data('mni') )
388+
>>> ants.get_nifti_spatial_transform_from_metadata(metadata)
340389
"""
341390
have_sform = all(k in metadata for k in ("srow_x", "srow_y", "srow_z")) and int(metadata.get("sform_code", 0)) > 0
342391
have_qform = ("qto_xyz" in metadata) and int(metadata.get("qform_code", 0)) > 0
@@ -403,7 +452,8 @@ def set_nifti_spatial_transform_from_metadata(image, metadata, prefer_sform=True
403452
max_angle_deviation=0.5, use_pixdim_spacing=True, verbose=False):
404453
"""
405454
Set the spatial transform of an ANTsImage from NIfTI header metadata. This sets the 3D spatial transform but does
406-
not modify spacing or the fourth row / column of the direction matrix for 4D images.
455+
not modify spacing or the fourth row / column of the direction matrix for 4D images. This function does not support 2D
456+
images, because the projection of a 3D spatial transform to 2D is not preserved in the ANTsImage.
407457
408458
The spacing is not modified but it is checked for consistency with the pixdim fields in the NIfTI header,
409459
as is standard in ITK. If the spacing does not match the pixdim, an error is raised.
@@ -417,16 +467,24 @@ def set_nifti_spatial_transform_from_metadata(image, metadata, prefer_sform=True
417467
prefer_sform : bool
418468
Whether to prefer sform over qform if both are available.
419469
shear_threshold : float
420-
Shear threshold for deshearing sform.
470+
Shear threshold for deshearing sform, shear below this will be ignored.
421471
max_angle_deviation : float
422-
Maximum angle deviation for directions after deshearing sform.
472+
Maximum angle deviation for directions after deshearing sform. If the desheared directions deviate from the original
473+
directions by more than this value (in degrees), deshearing fails, and qform is used if available.
423474
verbose : bool
424475
Whether to print verbose output.
425476
426477
Returns
427478
-------
428479
ants.ANTsImage
429480
The modified image with updated spatial transform.
481+
482+
Example
483+
-------
484+
>>> import ants
485+
>>> img = ants.image_read( ants.get_ants_data('mni') )
486+
>>> metadata = ants.read_image_metadata( ants.get_ants_data('mni') )
487+
>>> ants.set_nifti_spatial_transform_from_metadata(img, metadata)
430488
"""
431489
if image.dimension == 2:
432490
raise ValueError("Projection of NIFTI spatial orientation to 2D is not supported")

0 commit comments

Comments
 (0)