Skip to content

Commit

Permalink
ENH: Implement force-cast while loading mask
Browse files Browse the repository at this point in the history
PyRadiomics requires the mask to be of an integer data type. Force type to UInt32 upon load from file (string input), or apply a Cast (SimpleITK.Image input).

Remove now unnecessary cast in `imageoperations.cropToTumorMask`
  • Loading branch information
JoostJM committed Oct 2, 2018
1 parent 6542181 commit 5538420
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 3 additions & 0 deletions radiomics/featureextractor.py
Expand Up @@ -508,6 +508,9 @@ def loadImage(self, ImageFilePath, MaskFilePath):
# It is therefore possible that image and mask do not align, or even have different sizes.
self.generalInfo.addMaskElements(None, mask, label)

# Force the mask to unsigned 32 bits integer
mask = sitk.Cast(mask, sitk.sitkUInt32)

# This point is only reached if image and mask loaded correctly
if normalize:
image = imageoperations.normalizeImage(image, **self.settings)
Expand Down
4 changes: 0 additions & 4 deletions radiomics/imageoperations.py
Expand Up @@ -337,8 +337,6 @@ def cropToTumorMask(imageNode, maskNode, boundingBox, **kwargs):
global logger
padDistance = kwargs.get('padDistance', 0)

oldMaskID = maskNode.GetPixelID()
maskNode = sitk.Cast(maskNode, sitk.sitkInt32)
size = numpy.array(maskNode.GetSize())

ijkMinBounds = boundingBox[0::2] - padDistance
Expand All @@ -361,8 +359,6 @@ def cropToTumorMask(imageNode, maskNode, boundingBox, **kwargs):
croppedImageNode = cif.Execute(imageNode)
croppedMaskNode = cif.Execute(maskNode)

croppedMaskNode = sitk.Cast(croppedMaskNode, oldMaskID)

return croppedImageNode, croppedMaskNode


Expand Down
2 changes: 2 additions & 0 deletions tests/testUtils.py
Expand Up @@ -173,6 +173,8 @@ class or test case is changed, function returns True.
if 'MaskHash' in self._current_config:
assert sitk.Hash(self._mask) == self._current_config['MaskHash']

self._mask = sitk.Cast(self._mask, sitk.sitkUInt32)

settings = self._current_config.get('Settings', {})

interpolator = settings.get('interpolator', sitk.sitkBSpline)
Expand Down

0 comments on commit 5538420

Please sign in to comment.