Skip to content

Commit

Permalink
fix: Disabled saving support for 4D images due to SimpleITK bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol-G committed Aug 19, 2024
1 parent cc8df4c commit 8335e61
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
8 changes: 0 additions & 8 deletions examples/example_read_write_4d_nrrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ def example_read_write_4d(load_filepath, save_filepath):
direction1 = image.direction
affine1 = image.affine

# SimpleITK cannot store 4D metadata, only 2D and 3D metadata, but this image was created via MITK which can store 4D metadata
# Need to manually remove the additional 4D information
spacing1[3] = 1
origin1[3] = 0
direction1[3, 3] = 1
affine1[3, 3] = 1
affine1[3, 4] = 0

# Write as new 4D image
image.save(save_filepath)

Expand Down
2 changes: 1 addition & 1 deletion medvol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.0.6"
__version__ = "0.0.7"

from medvol.medvol import MedVol
40 changes: 12 additions & 28 deletions medvol/medvol.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,16 @@ def _load(self, filepath):
array = sitk.GetArrayFromImage(image_sitk)
ndims = len(array.shape)
metadata_ndims = len(image_sitk.GetSpacing())
spacing = np.array(image_sitk.GetSpacing()[::-1])
origin = np.array(image_sitk.GetOrigin()[::-1])
direction = np.array(image_sitk.GetDirection()[::-1])
header = {key: image_sitk.GetMetaData(key) for key in image_sitk.GetMetaDataKeys()}

if ndims == metadata_ndims:
direction = direction.reshape(ndims, ndims)
elif ndims == 4 and metadata_ndims == 3: # Some 4D Nifti images might have 3D metadata as SimpleITK can only store 2D and 3D metadata
# Expand metadata from 3D to 4D
spacing_expanded = np.zeros((ndims,))
spacing_expanded[:ndims-1] = spacing
spacing_expanded[ndims-1] = 1
spacing = spacing_expanded

origin_expanded = np.zeros((ndims,))
origin_expanded[:ndims-1] = origin
origin_expanded[ndims-1] = 0
origin = origin_expanded

direction_expanded = np.zeros((ndims, ndims))
direction_expanded[:ndims-1, :ndims-1] = direction.reshape(ndims-1, ndims-1)
direction_expanded[ndims-1, ndims-1] = 1
direction = direction_expanded
else:
if ndims == 4 and np.argmin(array.shape) != 0:
raise RuntimeError("MedVol expect 4D images to be channel-first.")
if ndims != metadata_ndims:
raise RuntimeError("Cannot interpret image metadata. Something is wrong with the dimensionality.")

spacing = np.array(image_sitk.GetSpacing()[::-1])
origin = np.array(image_sitk.GetOrigin()[::-1])
direction = np.array(image_sitk.GetDirection()[::-1]).reshape(ndims, ndims)
header = {key: image_sitk.GetMetaData(key) for key in image_sitk.GetMetaDataKeys()}
is_seg = None
if "intent_name" in header and header["intent_name"] == "medvol_seg":
is_seg = True
Expand All @@ -160,16 +144,16 @@ def _load(self, filepath):
return array, spacing, origin, direction, header, is_seg

def save(self, filepath):
if filepath[-5:] == ".nrrd" and self.ndims == 4:
raise RuntimeError("Saving a 4D image as NRRD is currently not supported. Save the image as Nifti instead.")
if self.ndims == 4:
raise RuntimeError("Saving a 4D image is currently not supported.")
image_sitk = sitk.GetImageFromArray(self.array)
# SimpleITK cannot store 4D metadata, only 2D and 3D metadata
if self.spacing is not None:
image_sitk.SetSpacing(self.spacing[:3].tolist()[::-1])
image_sitk.SetSpacing(self.spacing.tolist()[::-1])
if self.origin is not None:
image_sitk.SetOrigin(self.origin[:3].tolist()[::-1])
image_sitk.SetOrigin(self.origin.tolist()[::-1])
if self.direction is not None:
image_sitk.SetDirection(self.direction[:3, :3].flatten().tolist()[::-1])
image_sitk.SetDirection(self.direction.flatten().tolist()[::-1])
if self.is_seg is not None:
if self.is_seg:
self.header["intent_name"] = "medvol_seg"
Expand Down

0 comments on commit 8335e61

Please sign in to comment.