Skip to content

Commit 2713ebc

Browse files
committed
ENH: to_itk_image does not return None with itk.Image
So this function can be used to generally return an itk.Image from something that is convertible.
1 parent cf486f1 commit 2713ebc

File tree

5 files changed

+11
-18
lines changed

5 files changed

+11
-18
lines changed

itkwidgets/_transform_types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _numpy_array_to_point_set(point_set_like):
161161
def to_itk_image(image_like):
162162

163163
if isinstance(image_like, itk.Image):
164-
return None
164+
return image_like
165165

166166
if is_arraylike(image_like):
167167
array = np.asarray(image_like)
@@ -201,6 +201,8 @@ def to_itk_image(image_like):
201201
array = imglyb.to_numpy(image_like)
202202
image_from_array = itk.image_view_from_array(array)
203203
return image_from_array
204+
elif isinstance(image_like, itk.ProcessObject):
205+
return itk.output(image_like)
204206

205207
return None
206208

itkwidgets/trait_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class ITKImage(traitlets.TraitType):
3434
def validate(self, obj, value):
3535
self._source_object = value
3636

37-
image_from_array = to_itk_image(value)
38-
if image_from_array:
37+
if not isinstance(value, itk.Image) and not isinstance(value,
38+
itk.ProcessObject):
39+
image_from_array = to_itk_image(value)
3940
return image_from_array
4041

4142
try:

itkwidgets/widget_checkerboard.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ def checkerboard(image1, image2, pattern=3, invert=False, **viewer_kwargs): # n
3636
"""
3737

3838
itk_image1 = to_itk_image(image1)
39-
if not itk_image1:
40-
itk_image1 = itk.output(image1)
4139
itk_image2 = to_itk_image(image2)
42-
if not itk_image2:
43-
itk_image2 = itk.output(image2)
4440
input1 = itk_image1
4541
input2 = itk_image2
4642

itkwidgets/widget_compare.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from itkwidgets.widget_viewer import Viewer
44
from traitlets import CBool
55
import IPython
6-
from itkwidgets._transform_types import to_itk_image
7-
86

97
def compare(image1, image2,
108
link_cmap=False, link_gradient_opacity=False,

itkwidgets/widget_line_profiler.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,14 @@ def get_profile(self, image_or_array=None,
8686
point2 = self.point2
8787
if order is None:
8888
order = self.order
89-
image_from_array = to_itk_image(image_or_array)
90-
if image_from_array:
91-
image_ = image_from_array
92-
else:
93-
image_ = image_or_array
94-
image_array = itk.array_view_from_image(image_)
95-
dimension = image_.GetImageDimension()
89+
image = to_itk_image(image_or_array)
90+
image_array = itk.array_view_from_image(image)
91+
dimension = image.GetImageDimension()
9692
distance = np.sqrt(
9793
sum([(point1[ii] - point2[ii])**2 for ii in range(dimension)]))
98-
index1 = tuple(image_.TransformPhysicalPointToIndex(
94+
index1 = tuple(image.TransformPhysicalPointToIndex(
9995
tuple(point1[:dimension])))
100-
index2 = tuple(image_.TransformPhysicalPointToIndex(
96+
index2 = tuple(image.TransformPhysicalPointToIndex(
10197
tuple(point2[:dimension])))
10298
num_points = int(np.round(
10399
np.sqrt(sum([(index1[ii] - index2[ii])**2 for ii in range(dimension)])) * 2.1))

0 commit comments

Comments
 (0)