Skip to content
Open
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
6 changes: 6 additions & 0 deletions Wrapping/Generators/Python/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,9 @@ itk_python_add_test(
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/test_gil_release_safety.py
)

itk_python_add_test(
NAME CTypeTest
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/test_ctype.py
)
75 changes: 75 additions & 0 deletions Wrapping/Generators/Python/Tests/test_ctype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# ==========================================================================
#
# Copyright NumFOCUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==========================================================================

import itk
import unittest
import numpy as np
import os


class CTypeTestCase(unittest.TestCase):
"""Tests itkCType"""

def test_dtype_of_ctype_aliases_for_specific_sizes(self) -> None:
"""Tests that each `ctype` alias for a specific size has the corresponding `dtype`"""

# Check uint8 to uint64 and int8 to int64:
for number_of_bits in (8, 16, 32, 64):
self.assertEqual(
getattr(itk, f"int{number_of_bits}_ctype").dtype,
getattr(np, f"int{number_of_bits}"),
)
self.assertEqual(
getattr(itk, f"uint{number_of_bits}_ctype").dtype,
getattr(np, f"uint{number_of_bits}"),
)

# Check float32 and float64:
for number_of_bits in (32, 64):
self.assertEqual(
getattr(itk, f"float{number_of_bits}_ctype").dtype,
getattr(np, f"float{number_of_bits}"),
)

def test_ctype_aliases_for_64_bit_integers(self) -> None:
"""Tests int64_ctype and uint64_type, for both Windows and non-Windows"""

if os.name == "nt":
# On Windows, only `long long` integer types (SLL and ULL) are 64-bit.
self.assertEqual(
itk.int64_ctype,
itk.SLL,
)
self.assertEqual(
itk.uint64_ctype,
itk.ULL,
)
else:
# On Linux and MacOS, `long` integer types (SL and UL) are already 64-bit.
self.assertEqual(
itk.int64_ctype,
itk.SL,
)
self.assertEqual(
itk.uint64_ctype,
itk.UL,
)


if __name__ == "__main__":
unittest.main()
10 changes: 10 additions & 0 deletions Wrapping/Generators/Python/itk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
SI,
SLL,
B,
float32_ctype,
float64_ctype,
uint8_ctype,
uint16_ctype,
uint32_ctype,
uint64_ctype,
int8_ctype,
int16_ctype,
int32_ctype,
int64_ctype,
ST,
IT,
OT,
Expand Down
12 changes: 12 additions & 0 deletions Wrapping/Generators/Python/itk/support/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ def initialize_c_types_once(cls) -> tuple[Self, ...]:
B,
) = itkCType.initialize_c_types_once()

# Aliases for numeric types of specific sizes (specified by their number of bits)
float32_ctype = F
float64_ctype = D
uint8_ctype = UC
uint16_ctype = US
uint32_ctype = UI
uint64_ctype = UL if UL.dtype.itemsize == 8 else ULL
int8_ctype = SC
int16_ctype = SS
int32_ctype = SI
int64_ctype = SL if SL.dtype.itemsize == 8 else SLL

# Aliases for SizeValueType, IdentifierType, OffsetType
ST = UL
IT = UL
Expand Down
Loading