Skip to content

Commit 3da4926

Browse files
committed
BUG: Empty image support in image_from_xarray
Required for map_blocks.
1 parent 2121eed commit 3da4926

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Wrapping/Generators/Python/Tests/extras.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ def custom_callback(name, progress):
387387
assert False
388388
except ValueError:
389389
pass
390+
391+
empty_array = np.array([], dtype=np.uint8)
392+
empty_array.shape = (0,0,0)
393+
empty_image = itk.image_from_array(empty_array)
394+
empty_da = itk.xarray_from_image(empty_image)
395+
empty_image_round = itk.image_from_xarray(empty_da)
390396
except ImportError:
391397
print('xarray not imported. Skipping xarray conversion tests')
392398
pass

Wrapping/Generators/Python/itkExtras.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,10 @@ def image_from_xarray(data_array):
444444
origin = [0.0]*spatial_dimension
445445
spacing = [1.0]*spatial_dimension
446446
for index, dim in enumerate(spatial_dims):
447-
origin[index] = float(data_array.coords[dim][0])
448-
spacing[index] = float(data_array.coords[dim][1]) - float(data_array.coords[dim][0])
447+
coords = data_array.coords[dim]
448+
if coords.shape[0] > 1:
449+
origin[index] = float(coords[0])
450+
spacing[index] = float(coords[1]) - float(coords[0])
449451
spacing.reverse()
450452
itk_image.SetSpacing(spacing)
451453
origin.reverse()

0 commit comments

Comments
 (0)