Skip to content

Commit

Permalink
Fix error when normalization mask is empty
Browse files Browse the repository at this point in the history
Fixes #655.
  • Loading branch information
fepegar committed Sep 8, 2021
1 parent 449bd8f commit 3b67e08
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions torchio/transforms/preprocessing/intensity/rescale.py
Expand Up @@ -76,22 +76,29 @@ def rescale(
# The tensor is cloned as in-place operations will be used
array = tensor.clone().float().numpy()
mask = mask.numpy()
if not mask.any():
message = (
f'Rescaling image "{image_name}" not possible'
' because the mask to compute the statistics is empty'
)
warnings.warn(message, RuntimeWarning)
return tensor
values = array[mask]
cutoff = np.percentile(values, self.percentiles)
np.clip(array, *cutoff, out=array)
if self.in_min_max is None:
in_min, in_max = array.min(), array.max()
else:
in_min, in_max = self.in_min_max
array -= in_min
in_range = in_max - in_min
if in_range == 0: # should this be compared using a tolerance?
message = (
f'Rescaling image "{image_name}" not possible'
' due to division by zero'
' because all the intensity values are the same'
)
warnings.warn(message, RuntimeWarning)
return tensor
array -= in_min
array /= in_range
out_range = self.out_max - self.out_min
array *= out_range
Expand Down

0 comments on commit 3b67e08

Please sign in to comment.