The ITK Python package allows specifying the pixel type of an image by passing an itkCType object, for example itk.D for the C type double, itk.US for unsigned short and itk.SLL for signed long long. As in itk.Image[itk.D, 2], itk.Image[itk.US, 2], itk.Image[itk.SLL, 2]. While this notation is very concise, I believe it would be handy for ITK users to allow directly specifying the number of bits of the C type, just like in C and C++ with int8_t, int16_t, int32_t, etc. (See Fixed width integer types (since C++11) - cppreference.com and Fixed width floating-point types (since C++23) - cppreference.com ).
Pull request ENH: Add ctype aliases for numeric types of specific sizes to Python by N-Dekker · Pull Request #6762 · InsightSoftwareConsortium/ITK · GitHub proposes this feature, simply by adding the following aliases to ITK Python:
float32_t = F
float64_t = D
uint8_t = UC
uint16_t = US
uint32_t = UI
uint64_t = UL if UL.dtype.itemsize == 8 else ULL
int8_t = SC
int16_t = SS
int32_t = SI
int64_t = SL if SL.dtype.itemsize == 8 else SLL
To be used, for example, in itk.Image[itk.float64_t, 2], itk.Image[itk.uint16, 2], or itk.Image[itk.int64_t, 2].
Please approve this pull request if you like it!
main ← N-Dekker:ctype-aliases-for-numeric-types-of-specific-sizes
opened 10:24PM - 12 Aug 26 UTC
Added the following aliases:
float32_t
float64_t
uint8_t
u… int16_t
uint32_t
uint64_t
int8_t
int16_t
int32_t
int64_t
Aims to provide a more human-readable alternative to F, D, UC, US, UI, ULL, SC, SS, SI, and SLL. Eases writing code for which the specific size of numeric types should be platform-independent.
Typical use cases, specifying the pixel type of an image:
- `itk.Image[itk.uint8_t, 2]` (equivalent to `itk.Image[itk.UC, 2]`)
- `itk.Image[itk.float64_t, 2]` (equivalent to `itk.Image[itk.D, 2]`)
- `itk.Image[itk.int64_t, 2]` (equivalent to `itk.Image[itk.SL, 2]` on Linux and `itk.Image[itk.SLL, 2]` on Windows)
----
For the record, this comment is updated, following the replacement of the `_ctype` postfixes with `_t` (see https://github.com/InsightSoftwareConsortium/ITK/pull/6762#issuecomment-5301549951 and [this force-pushed amend](https://github.com/InsightSoftwareConsortium/ITK/compare/faaacd488b169cf3a2f7283b4e796ccb620d812d..1d06a176362535d3461e0eba691c2ba501f2e34a))
1 Like
Thanks for approving and merging ENH: Add ctype aliases for numeric types of specific sizes to Python by N-Dekker · Pull Request #6762 · InsightSoftwareConsortium/ITK · GitHub !
Here is a proposed use case of itk.int64_t and itk.uint64_t:
main ← N-Dekker:Use-int64_t-instead-of-if-os-name
opened 02:11PM - 19 Aug 26 UTC
Replaced `if os.name == "nt"` statements which used `itk.SL`, `itk.SLL`, `itk.UL… `, or `itk.ULL` with the equivalent code, using just `itk.int64_t` or `itk.uint64_t`.
Aims to improve code readability and remove OS-specific code.
- Follow-up to pull request #6762 commit dc0ea28fe9af0503ac572c44839506daaf0329b1
1 Like