Skip to content

Commit a9f6888

Browse files
committed
Skip redundant full-image mask on StdShiftIntensity nonzero=False path
On the default nonzero=False path the boolean mask is all-True, so img[slices] just gathers and scatters the whole image. Shift the image directly instead, avoiding the mask allocation, .any(), gather and scatter. Output is unchanged. Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent f1dcac4 commit a9f6888

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

monai/transforms/intensity/array.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,15 @@ def __init__(
354354
self.dtype = dtype
355355

356356
def _stdshift(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
357-
ones: Callable
358357
std: Callable
359358
if isinstance(img, torch.Tensor):
360-
ones = torch.ones
361359
std = partial(torch.std, unbiased=False)
362360
else:
363-
ones = np.ones
364361
std = np.std
365362

366-
slices = (img != 0) if self.nonzero else ones(img.shape, dtype=bool)
363+
if not self.nonzero:
364+
return img + self.factor * std(img)
365+
slices = img != 0
367366
if slices.any():
368367
offset = self.factor * std(img[slices])
369368
img[slices] = img[slices] + offset

0 commit comments

Comments
 (0)