Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Wrapping/Generators/Python/Tests/test_ctype.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ def test_ctype_aliases_for_64_bit_integers(self) -> None:
itk.UL,
)

def test_itk_ctype_numpy_dtype_roundtrip(self) -> None:
"""Tests round-trip from itkCType to NumPy dtype and back, for the itkCType aliases with a specified size"""

for itk_ctype in (
itk.float32_t,
itk.float64_t,
itk.uint8_t,
itk.uint16_t,
itk.uint32_t,
itk.uint64_t,
itk.int8_t,
itk.int16_t,
itk.int32_t,
itk.int64_t,
):
self.assertEqual(itk.itkCType.GetCTypeForDType(itk_ctype.dtype), itk_ctype)


if __name__ == "__main__":
unittest.main()
10 changes: 7 additions & 3 deletions Wrapping/Generators/Python/itk/support/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,26 @@ def initialize_c_types_once(cls) -> tuple[Self, ...]:
"""
import numpy as np

# Note: The order in which the following itkCType objects are created
# is relevant! For each NumPy `dtype`, only the last created itkCType
# object for that particular `dtype` is stored in the mapping from
# `dtype` to itkCType (using the Python dict `__c_types_for_dtype__`).
_F: itkCType = cls("float", "F", np.dtype(np.float32))
_D: itkCType = cls("double", "D", np.dtype(np.float64))
_UC: itkCType = cls("unsigned char", "UC", np.dtype(np.uint8))
_US: itkCType = cls("unsigned short", "US", np.dtype(np.uint16))
_UI: itkCType = cls("unsigned int", "UI", np.dtype(np.uint32))
_ULL: itkCType = cls("unsigned long long", "ULL", np.dtype(np.uint64))
_SLL: itkCType = cls("signed long long", "SLL", np.dtype(np.int64))
if os.name == "nt":
_UL: itkCType = cls("unsigned long", "UL", np.dtype(np.uint32))
_SL: itkCType = cls("signed long", "SL", np.dtype(np.int32))
else:
_UL: itkCType = cls("unsigned long", "UL", np.dtype(np.uint64))
_SL: itkCType = cls("signed long", "SL", np.dtype(np.int64))
_ULL: itkCType = cls("unsigned long long", "ULL", np.dtype(np.uint64))
_UI: itkCType = cls("unsigned int", "UI", np.dtype(np.uint32))
_SC: itkCType = cls("signed char", "SC", np.dtype(np.int8))
_SS: itkCType = cls("signed short", "SS", np.dtype(np.int16))
_SI: itkCType = cls("signed int", "SI", np.dtype(np.int32))
_SLL: itkCType = cls("signed long long", "SLL", np.dtype(np.int64))
_B: itkCType = cls("bool", "B", np.dtype(np.bool_))
return _F, _D, _UC, _US, _UI, _UL, _SL, _ULL, _SC, _SS, _SI, _SLL, _B

Expand Down
Loading