Skip to content

Commit 27d3ee8

Browse files
committed
ENH: Propagate needs_contiguous option, docs up from PyBuffer
Into extras.py so it can be used and the description is available in itk.image_view_from_array. Also propagage other related documentation.
1 parent b07aaff commit 27d3ee8

1 file changed

Lines changed: 65 additions & 8 deletions

File tree

  • Wrapping/Generators/Python/itk/support

Wrapping/Generators/Python/itk/support/extras.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,15 @@ def GetArrayFromImage(
323323
update: bool = True,
324324
ttype=None,
325325
) -> np.ndarray:
326-
"""Get an array with the content of the image buffer"""
326+
"""Get an array with the content of the image buffer.
327+
328+
When *keep_axes* is *False*, the NumPy array will have C-order
329+
indexing. This is the reverse of how indices are specified in ITK,
330+
i.e. k,j,i versus i,j,k. However C-order indexing is expected by most
331+
algorithms in NumPy / SciPy.
332+
333+
This is a deep copy of the image buffer and is completely safe and without potential side effects.
334+
"""
327335
return _GetArrayFromImage(
328336
image_or_filter, "GetArrayFromImage", keep_axes, update, ttype
329337
)
@@ -338,7 +346,13 @@ def GetArrayViewFromImage(
338346
update: bool = True,
339347
ttype=None,
340348
) -> np.ndarray:
341-
"""Get an array view with the content of the image buffer"""
349+
"""Get an array view with the content of the image buffer.
350+
351+
When *keep_axes* is *False*, the NumPy array will have C-order
352+
indexing. This is the reverse of how indices are specified in ITK,
353+
i.e. k,j,i versus i,j,k. However C-order indexing is expected by most
354+
algorithms in NumPy / SciPy.
355+
"""
342356
return _GetArrayFromImage(
343357
image_or_filter, "GetArrayViewFromImage", keep_axes, update, ttype
344358
)
@@ -347,7 +361,8 @@ def GetArrayViewFromImage(
347361
array_view_from_image = GetArrayViewFromImage
348362

349363

350-
def _GetImageFromArray(arr: ArrayLike, function_name: str, is_vector: bool, ttype):
364+
def _GetImageFromArray(arr: ArrayLike, function_name: str, is_vector: bool,
365+
ttype, need_contiguous:bool = True):
351366
"""Get an ITK image from a Python array."""
352367
import itk
353368

@@ -398,24 +413,66 @@ def _GetImageFromArray(arr: ArrayLike, function_name: str, is_vector: bool, ttyp
398413
Please specify an output type via the 'ttype' keyword parameter."""
399414
)
400415
templatedFunction = getattr(itk.PyBuffer[keys[0]], function_name)
401-
return templatedFunction(arr, is_vector)
416+
if function_name == "GetImageViewFromArray":
417+
return templatedFunction(arr, is_vector, need_contiguous)
418+
else:
419+
return templatedFunction(arr, is_vector)
420+
402421

403422

404423
def GetImageFromArray(
405424
arr: ArrayLike, is_vector: bool = False, ttype=None
406425
) -> "itkt.ImageBase":
407-
"""Get an ITK image from a Python array."""
426+
"""Get an ITK image from a Python array.
427+
428+
This is a deep copy of the NumPy array buffer and is completely safe without potential
429+
side effects.
430+
431+
If is_vector is True, then a 3D array will be treated as a 2D vector image,
432+
otherwise it will be treated as a 3D image.
433+
434+
If the array uses Fortran-order indexing, i.e. i,j,k, the Image Size
435+
will have the same dimensions as the array shape. If the array uses
436+
C-order indexing, i.e. k,j,i, the image Size will have the dimensions
437+
reversed from the array shape.
438+
439+
Therefore, since the *np.transpose* operator on a 2D array simply
440+
inverts the indexing scheme, the Image representation will be the
441+
same for an array and its transpose. If flipping is desired, see
442+
*np.reshape*.
443+
444+
ttype can be used te specify a specific itk.Image type.
445+
"""
408446
return _GetImageFromArray(arr, "GetImageFromArray", is_vector, ttype)
409447

410448

411449
image_from_array = GetImageFromArray
412450

413451

414452
def GetImageViewFromArray(
415-
arr: ArrayLike, is_vector: bool = False, ttype=None
453+
arr: ArrayLike, is_vector: bool = False, ttype=None, need_contiguous=True
416454
) -> "itkt.ImageBase":
417-
"""Get an ITK image view from a Python array."""
418-
return _GetImageFromArray(arr, "GetImageViewFromArray", is_vector, ttype)
455+
"""Get an ITK image view (shared pixel buffer memory) from a Python array.
456+
457+
If is_vector is True, then a 3D array will be treated as a 2D vector image,
458+
otherwise it will be treated as a 3D image.
459+
460+
If the array uses Fortran-order indexing, i.e. i,j,k, the Image Size
461+
will have the same dimensions as the array shape. If the array uses
462+
C-order indexing, i.e. k,j,i, the image Size will have the dimensions
463+
reversed from the array shape.
464+
465+
Therefore, since the *np.transpose* operator on a 2D array simply
466+
inverts the indexing scheme, the Image representation will be the
467+
same for an array and its transpose. If flipping is desired, see
468+
*np.reshape*.
469+
470+
By default, a warning is issued if this function is called on a non-contiguous
471+
array, since a copy is performed and care must be taken to keep a reference
472+
to the copied array. This warning can be suppressed with need_contiguous=False
473+
"""
474+
return _GetImageFromArray(arr, "GetImageViewFromArray", is_vector, ttype,
475+
need_contiguous=need_contiguous)
419476

420477

421478
image_view_from_array = GetImageViewFromArray

0 commit comments

Comments
 (0)