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

Method _apply_mask

monai/transforms/intensity/array.py:1968–1999  ·  view source on GitHub ↗

Builds and applies a mask on the spatial dimensions. Args: k: k-space version of the image. Returns: masked version of the k-space image.

(self, k: NdarrayOrTensor)

Source from the content-addressed store, hash-verified

1966 return img
1967
1968 def _apply_mask(self, k: NdarrayOrTensor) -> NdarrayOrTensor:
1969 """Builds and applies a mask on the spatial dimensions.
1970
1971 Args:
1972 k: k-space version of the image.
1973 Returns:
1974 masked version of the k-space image.
1975 """
1976 shape = k.shape[1:]
1977
1978 # compute masking radius and center
1979 r = (1 - self.alpha) * np.max(shape) * np.sqrt(2) / 2.0
1980 center = (np.array(shape) - 1) / 2
1981
1982 # gives list w/ len==self.dim. Each dim gives coordinate in that dimension
1983 coords = np.ogrid[tuple(slice(0, i) for i in shape)]
1984
1985 # need to subtract center coord and then square for Euc distance
1986 coords_from_center_sq = [(coord - c) ** 2 for coord, c in zip(coords, center)]
1987 dist_from_center = np.sqrt(sum(coords_from_center_sq))
1988 mask = dist_from_center <= r
1989
1990 # add channel dimension into mask
1991 mask = np.repeat(mask[None], k.shape[0], axis=0)
1992
1993 if isinstance(k, torch.Tensor):
1994 mask, *_ = convert_data_type(mask, torch.Tensor, device=k.device)
1995
1996 # apply binary mask
1997 k_masked: NdarrayOrTensor
1998 k_masked = k * mask
1999 return k_masked
2000
2001
2002class RandGibbsNoise(RandomizableTransform):

Callers 1

__call__Method · 0.95

Calls 3

convert_data_typeFunction · 0.90
sumFunction · 0.85
arrayMethod · 0.80

Tested by

no test coverage detected