MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / StdShiftIntensity

Class StdShiftIntensity

monai/transforms/intensity/array.py:330–381  ·  view source on GitHub ↗

Shift intensity for the image with a factor and the standard deviation of the image by: ``v = v + factor * std(v)``. This transform can focus on only non-zero values or the entire image, and can also calculate the std on each channel separately. Args: factor: factor shi

Source from the content-addressed store, hash-verified

328
329
330class StdShiftIntensity(Transform):
331 """
332 Shift intensity for the image with a factor and the standard deviation of the image
333 by: ``v = v + factor * std(v)``.
334 This transform can focus on only non-zero values or the entire image,
335 and can also calculate the std on each channel separately.
336
337 Args:
338 factor: factor shift by ``v = v + factor * std(v)``.
339 nonzero: whether only count non-zero values.
340 channel_wise: if True, calculate on each channel separately. Please ensure
341 that the first dimension represents the channel of the image if True.
342 dtype: output data type, if None, same as input image. defaults to float32.
343 """
344
345 backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
346
347 def __init__(
348 self, factor: float, nonzero: bool = False, channel_wise: bool = False, dtype: DtypeLike = np.float32
349 ) -> None:
350 self.factor = factor
351 self.nonzero = nonzero
352 self.channel_wise = channel_wise
353 self.dtype = dtype
354
355 def _stdshift(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
356 ones: Callable
357 std: Callable
358 if isinstance(img, torch.Tensor):
359 ones = torch.ones
360 std = partial(torch.std, unbiased=False)
361 else:
362 ones = np.ones
363 std = np.std
364
365 slices = (img != 0) if self.nonzero else ones(img.shape, dtype=bool)
366 if slices.any():
367 offset = self.factor * std(img[slices])
368 img[slices] = img[slices] + offset
369 return img
370
371 def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
372 """
373 Apply the transform to `img`.
374 """
375 img = convert_to_tensor(img, track_meta=get_track_meta(), dtype=self.dtype)
376 if self.channel_wise:
377 for i, d in enumerate(img):
378 img[i] = self._stdshift(d) # type: ignore
379 else:
380 img = self._stdshift(img)
381 return img
382
383
384class RandStdShiftIntensity(RandomizableTransform):

Callers 7

__init__Method · 0.90
test_valueMethod · 0.90
test_zerostdMethod · 0.90
test_nonzeroMethod · 0.90
test_channel_wiseMethod · 0.90
test_dtypeMethod · 0.90
__call__Method · 0.85

Calls

no outgoing calls

Tested by 5

test_valueMethod · 0.72
test_zerostdMethod · 0.72
test_nonzeroMethod · 0.72
test_channel_wiseMethod · 0.72
test_dtypeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…