|
| 1 | +# ========================================================================== |
| 2 | +# |
| 3 | +# Copyright NumFOCUS |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# ========================================================================== |
| 18 | + |
| 19 | +"""ITK's half of the SimpleITK interop contract, without SimpleITK. |
| 20 | +
|
| 21 | +SimpleITK builds ITK, so an ITK test that imports SimpleITK would close a |
| 22 | +cycle in the ecosystem build graph. The contract ITK owns is duck-typed -- |
| 23 | +"read xyz-ordered geometry from an image-like object" -- so it is exercised |
| 24 | +here with a stub. Round-trip fidelity between two independently built ITK |
| 25 | +libraries is the business of a standalone suite that depends on neither |
| 26 | +project's build. |
| 27 | +""" |
| 28 | + |
| 29 | +import itk |
| 30 | +import numpy as np |
| 31 | + |
| 32 | +from itk.support.extras import _spatial_from_order_explicit |
| 33 | + |
| 34 | +SPACING_XYZ = (1.0, 2.0, 3.0) |
| 35 | + |
| 36 | + |
| 37 | +class ImageLike: |
| 38 | + """Stands in for a SimpleITK Image: Get*() accessors, optional keys.""" |
| 39 | + |
| 40 | + def __init__(self, spacing_xyz=SPACING_XYZ, keys=None): |
| 41 | + self._spacing = spacing_xyz |
| 42 | + self._keys = {} if keys is None else keys |
| 43 | + |
| 44 | + def __getitem__(self, key): |
| 45 | + try: |
| 46 | + return self._keys[key] |
| 47 | + except KeyError: |
| 48 | + raise KeyError(f'"{key}" not in meta-data dictionary') from None |
| 49 | + |
| 50 | + def GetSpacing(self): |
| 51 | + return self._spacing |
| 52 | + |
| 53 | + |
| 54 | +# -------------------------------------------------------------------------- |
| 55 | +# The order-explicit key is preferred when present |
| 56 | +# -------------------------------------------------------------------------- |
| 57 | +explicit = ImageLike(keys={"spacing_xyz": (7.0, 8.0, 9.0)}) |
| 58 | +assert _spatial_from_order_explicit(explicit, "spacing") == ( |
| 59 | + 7.0, |
| 60 | + 8.0, |
| 61 | + 9.0, |
| 62 | +), "the _xyz key must win over the accessor when both are available" |
| 63 | +print("order-explicit key preferred over the accessor") |
| 64 | + |
| 65 | + |
| 66 | +# -------------------------------------------------------------------------- |
| 67 | +# Fall back to the accessor: SimpleITK exposes no _xyz keys today |
| 68 | +# -------------------------------------------------------------------------- |
| 69 | +# Verified against SimpleITK 2.5.4: image['spacing_xyz'] raises KeyError and |
| 70 | +# the class has no keys(). The accessor path therefore carries every real |
| 71 | +# conversion, so it is not a decorative fallback. |
| 72 | +accessor_only = ImageLike() |
| 73 | +assert ( |
| 74 | + _spatial_from_order_explicit(accessor_only, "spacing") == SPACING_XYZ |
| 75 | +), "must fall back to GetSpacing() when no order-explicit key exists" |
| 76 | +print("accessor fallback used when no order-explicit key is present") |
| 77 | + |
| 78 | + |
| 79 | +# -------------------------------------------------------------------------- |
| 80 | +# The bare key is never consulted, even when it is the only key |
| 81 | +# -------------------------------------------------------------------------- |
| 82 | +# This is the #6706 conflict: a bare 'spacing' means (z,y,x) on an itk.Image |
| 83 | +# and (x,y,z) on a SimpleITK Image. Reading it would silently reverse the |
| 84 | +# spacing. Give the stub a bare key that disagrees with its accessor and |
| 85 | +# require the accessor to win. |
| 86 | +trap = ImageLike(keys={"spacing": SPACING_XYZ[::-1]}) |
| 87 | +assert ( |
| 88 | + _spatial_from_order_explicit(trap, "spacing") == SPACING_XYZ |
| 89 | +), "the order-ambiguous bare key must never be read (#6706)" |
| 90 | +print("order-ambiguous bare key is never read") |
| 91 | + |
| 92 | + |
| 93 | +# -------------------------------------------------------------------------- |
| 94 | +# Absent entirely: report nothing rather than guess a default |
| 95 | +# -------------------------------------------------------------------------- |
| 96 | +class Empty: |
| 97 | + def __getitem__(self, key): |
| 98 | + raise KeyError(key) |
| 99 | + |
| 100 | + |
| 101 | +assert ( |
| 102 | + _spatial_from_order_explicit(Empty(), "spacing") is None |
| 103 | +), "must return None when neither the key nor the accessor exists" |
| 104 | +print("missing geometry reported as None rather than defaulted") |
| 105 | + |
| 106 | + |
| 107 | +# -------------------------------------------------------------------------- |
| 108 | +# The index the converter records for a non-zero buffered region |
| 109 | +# -------------------------------------------------------------------------- |
| 110 | +offset_image = itk.Image[itk.F, 3].New() |
| 111 | +region = itk.ImageRegion[3]() |
| 112 | +region.SetIndex([2, 3, 4]) |
| 113 | +region.SetSize([4, 5, 6]) |
| 114 | +offset_image.SetRegions(region) |
| 115 | +offset_image.Allocate(True) |
| 116 | +assert tuple(offset_image["index_xyz"]) == (2, 3, 4), "index_xyz must be xyz" |
| 117 | +assert tuple(offset_image["index_zyx"]) == (4, 3, 2), "index_zyx must be zyx" |
| 118 | +print("non-zero start index is readable in both orders (#6710)") |
| 119 | + |
| 120 | + |
| 121 | +# -------------------------------------------------------------------------- |
| 122 | +# ITK's own order-explicit keys, which the converter writes against |
| 123 | +# -------------------------------------------------------------------------- |
| 124 | +image = itk.Image[itk.F, 3].New() |
| 125 | +image.SetRegions([4, 5, 6]) |
| 126 | +image.Allocate(True) |
| 127 | +image.SetSpacing(SPACING_XYZ) |
| 128 | +assert np.allclose(image["spacing_xyz"], SPACING_XYZ), "itk spacing_xyz is not xyz" |
| 129 | +assert np.allclose( |
| 130 | + image["spacing_zyx"], SPACING_XYZ[::-1] |
| 131 | +), "itk spacing_zyx is not zyx" |
| 132 | +print("itk.Image exposes both spatial key orders (#6710)") |
| 133 | + |
| 134 | +print("simpleitk_protocol test passed") |
0 commit comments