Skip to content

Commit 86f75a9

Browse files
hjmjohnsonclaude
andcommitted
ENH: Add comprehensive tests for itk.Image buffer protocol and lifetime
Add three test suites with automatic feature detection so they can be cherry-picked into ITKv5.4.5 — tests for unavailable features are skipped with descriptive messages rather than failing. Feature detection at runtime: - hasattr(image, '__buffer__'): PEP 688 (ITK 6.x) - hasattr(image, '__array_interface__'): NumPy v3 interface (ITK 6.x) - image.__array__(copy=None): NumPy 2.0 copy param (ITK 6.x) - np.asarray zero-copy mutation test: zero-copy support (ITK 6.x) itkImageTest.py (25-27 assertions depending on Python version): - Basic __array__() and np.asarray() [all ITK versions] - __buffer__() PEP 688: 2D/3D, UC/SS/F [skip if unavailable] - memoryview(image) auto-dispatch [skip if Python < 3.12] - np.asarray zero-copy mutation [skip if unavailable] - __array__(copy=None/True/False) [skip if unavailable] itkImageLifetimeTest.py (54-56 assertions): - Deep copy: np.array(copy=True), array_from_image [all versions] - __array__() zero-copy lifetime [skip if unavailable] - np.asarray del-image safety [skip if unavailable] - __buffer__/memoryview lifetime [skip if unavailable] - Chained refs: slice, transpose, ravel [skip if unavailable] - Filter pipeline output lifetime [skip if unavailable] - Weak reference GC verification [skip if unavailable] - Reference count verification [skip if unavailable] - Circular reference detection [all versions] - RSS memory growth (200x cycles) [all versions] itkImageInteropTest.py (37 assertions when fully supported): - NumPy: CT/MRI/DWI clinical sizes [all versions] - Zero-copy assertions gated on HAS_ZEROCOPY [skip if unavailable] - __array__(copy=...) protocol gated on HAS_ARRAY_COPY_PARAM - PyTorch: 14 tests [optional, skip if not installed] - Dask: 9 tests [optional, skip if not installed] Co-Authored-By: Hans J. Johnson <hans-johnson@uiowa.edu> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1e73a2b commit 86f75a9

4 files changed

Lines changed: 1298 additions & 6 deletions

File tree

Modules/Core/Common/wrapping/test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ if(ITK_WRAP_PYTHON)
5656
COMMAND
5757
${CMAKE_CURRENT_SOURCE_DIR}/itkSpatialOrientationAdapterTest.py
5858
)
59+
itk_python_add_test(
60+
NAME itkImageInteropPythonTest
61+
COMMAND
62+
${CMAKE_CURRENT_SOURCE_DIR}/itkImageInteropTest.py
63+
)
64+
itk_python_add_test(
65+
NAME itkImageLifetimePythonTest
66+
COMMAND
67+
${CMAKE_CURRENT_SOURCE_DIR}/itkImageLifetimeTest.py
68+
)
5969
endif()

0 commit comments

Comments
 (0)