def EnsureDim(data: NdarrayTensor): if data.ndim == 0: if isinstance(data, np.ndarray): return np.expand_dims(data, 0) else: return data.unsqueeze(0) return data
def default_normalizer(x: NdarrayTensor) -> NdarrayTensor: """ A linear intensity scaling by mapping the (min, max) to (1, 0). If the input data is PyTorch Tensor, the output data will be Tensor on the same device, otherwise, output data will be numpy array. Note: This will flip magnitudes (i.e., smallest will become biggest and vice versa). """ def _compute(data: np.ndarray) -> np.ndarray: scaler = ScaleIntensity(minv=1.0, maxv=0.0) return np.stack([scaler(i) for i in data], axis=0) if isinstance(x, torch.Tensor): return torch.as_tensor(_compute(x.detach().cpu().numpy()), device=x.device) return _compute(x)
def __call__(self, img: NdarrayTensor) -> NdarrayTensor: """ Args: img: numpy arrays with required dimension `dim` removed """ return img.squeeze(self.dim) # type: ignore