Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions monai/transforms/croppad/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ def __call__(self, img: Union[np.ndarray, torch.Tensor]):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray)
sd = min(len(self.slices), len(img.shape[1:])) # spatial dims
slices = [slice(None)] + self.slices[:sd]
return img[tuple(slices)]
Expand Down Expand Up @@ -449,6 +450,7 @@ def __call__(self, img: np.ndarray):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
roi_size = fall_back_tuple(self.roi_size, img.shape[1:])
center = [i // 2 for i in img.shape[1:]]
cropper = SpatialCrop(roi_center=center, roi_size=roi_size)
Expand All @@ -469,6 +471,7 @@ def __init__(self, roi_scale: Union[Sequence[float], float]):
self.roi_scale = roi_scale

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
img_size = img.shape[1:]
ndim = len(img_size)
roi_size = [ceil(r * s) for r, s in zip(ensure_tuple_rep(self.roi_scale, ndim), img_size)]
Expand Down Expand Up @@ -530,6 +533,7 @@ def __call__(self, img: np.ndarray):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize(img.shape[1:])
if self._size is None:
raise AssertionError
Expand Down Expand Up @@ -576,6 +580,7 @@ def __call__(self, img: np.ndarray):
Apply the transform to `img`, assuming `img` is channel-first and
slicing doesn't apply to the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
img_size = img.shape[1:]
ndim = len(img_size)
self.roi_size = [ceil(r * s) for r, s in zip(ensure_tuple_rep(self.roi_scale, ndim), img_size)]
Expand Down Expand Up @@ -645,6 +650,7 @@ def __call__(self, img: np.ndarray) -> List[np.ndarray]:
Apply the transform to `img`, assuming `img` is channel-first and
cropping doesn't change the channel dim.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
return [self.cropper(img) for _ in range(self.num_samples)]


Expand Down Expand Up @@ -801,12 +807,16 @@ def __call__(self, img: np.ndarray, weight_map: Optional[np.ndarray] = None) ->
Returns:
A list of image patches
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
if weight_map is None:
weight_map = self.weight_map
if weight_map is None:
raise ValueError("weight map must be provided for weighted patch sampling.")
if img.shape[1:] != weight_map.shape[1:]:
raise ValueError(f"image and weight map spatial shape mismatch: {img.shape[1:]} vs {weight_map.shape[1:]}.")

weight_map, *_ = convert_data_type(weight_map, np.ndarray) # type: ignore

self.randomize(weight_map)
_spatial_size = fall_back_tuple(self.spatial_size, weight_map.shape[1:])
results = []
Expand Down Expand Up @@ -942,6 +952,9 @@ def __call__(
if image is None:
image = self.image

image, *_ = convert_data_type(image, np.ndarray) # type: ignore
label, *_ = convert_data_type(label, np.ndarray) # type: ignore

self.randomize(label, fg_indices, bg_indices, image)
results: List[np.ndarray] = []
if self.centers is not None:
Expand Down Expand Up @@ -1075,6 +1088,9 @@ def __call__(
if image is None:
image = self.image

image, *_ = convert_data_type(image, np.ndarray) # type: ignore
label, *_ = convert_data_type(label, np.ndarray) # type: ignore

self.randomize(label, indices, image)
results: List[np.ndarray] = []
if self.centers is not None:
Expand Down Expand Up @@ -1127,6 +1143,7 @@ def __call__(self, img: np.ndarray, mode: Optional[Union[NumpyPadMode, str]] = N
If None, defaults to the ``mode`` in construction.
See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
return self.padder(self.cropper(img), mode=mode)


Expand Down Expand Up @@ -1161,6 +1178,7 @@ def __call__(self, img: np.ndarray) -> np.ndarray:
"""
See also: :py:class:`monai.transforms.utils.generate_spatial_bounding_box`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
bbox = []

for channel in range(img.shape[0]):
Expand Down
16 changes: 15 additions & 1 deletion monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ def __call__(self, img: np.ndarray):
"""
Apply the transform to `img`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize(data=img)
if not self._do_transform:
return img
Expand Down Expand Up @@ -731,6 +732,7 @@ def __call__(self, img: np.ndarray):
"""
Apply the transform to `img`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
epsilon = 1e-7
img_min = img.min()
img_range = img.max() - img_min
Expand Down Expand Up @@ -773,6 +775,7 @@ def __call__(self, img: np.ndarray):
"""
Apply the transform to `img`.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if self.gamma_value is None:
raise ValueError("gamma_value is not set.")
Expand Down Expand Up @@ -910,10 +913,13 @@ def __call__(self, img: np.ndarray, mask_data: Optional[np.ndarray] = None) -> n
- ValueError: When ``mask_data`` and ``img`` channels differ and ``mask_data`` is not single channel.

"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
mask_data = self.mask_data if mask_data is None else mask_data
if mask_data is None:
raise ValueError("must provide the mask_data when initializing the transform or at runtime.")

mask_data, *_ = convert_data_type(mask_data, np.ndarray) # type: ignore

mask_data = np.asarray(self.select_fn(mask_data))
if mask_data.shape[0] != 1 and mask_data.shape[0] != img.shape[0]:
raise ValueError(
Expand All @@ -936,7 +942,7 @@ class SavitzkyGolaySmooth(Transform):
or ``'circular'``. Default: ``'zeros'``. See ``torch.nn.Conv1d()`` for more information.
"""

backend = [TransformBackends.NUMPY]
backend = [TransformBackends.TORCH]

def __init__(self, window_length: int, order: int, axis: int = 1, mode: str = "zeros"):

Expand Down Expand Up @@ -1000,6 +1006,7 @@ def __call__(self, img: np.ndarray):
np.ndarray containing envelope of data in img along the specified axis.

"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
# add one to transform axis because a batch axis will be added at dimension 0
hilbert_transform = HilbertTransform(self.axis + 1, self.n)
# convert to Tensor and add Batch axis expected by HilbertTransform
Expand All @@ -1026,6 +1033,7 @@ def __init__(self, sigma: Union[Sequence[float], float] = 1.0, approx: str = "er
self.approx = approx

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
gaussian_filter = GaussianFilter(img.ndim - 1, self.sigma, approx=self.approx)
input_data = torch.as_tensor(np.ascontiguousarray(img), dtype=torch.float).unsqueeze(0)
return gaussian_filter(input_data).squeeze(0).detach().numpy()
Expand Down Expand Up @@ -1070,6 +1078,7 @@ def randomize(self, data: Optional[Any] = None) -> None:
self.z = self.R.uniform(low=self.sigma_z[0], high=self.sigma_z[1])

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if not self._do_transform:
return img
Expand Down Expand Up @@ -1117,6 +1126,7 @@ def __init__(
self.approx = approx

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
gaussian_filter1 = GaussianFilter(img.ndim - 1, self.sigma1, approx=self.approx)
gaussian_filter2 = GaussianFilter(img.ndim - 1, self.sigma2, approx=self.approx)
input_data = torch.as_tensor(np.ascontiguousarray(img), dtype=torch.float).unsqueeze(0)
Expand Down Expand Up @@ -1183,6 +1193,7 @@ def randomize(self, data: Optional[Any] = None) -> None:
self.a = self.R.uniform(low=self.alpha[0], high=self.alpha[1])

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if not self._do_transform:
return img
Expand Down Expand Up @@ -1227,6 +1238,7 @@ def randomize(self, data: Optional[Any] = None) -> None:
)

def __call__(self, img: np.ndarray) -> np.ndarray:
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize()
if not self._do_transform:
return img
Expand Down Expand Up @@ -1713,6 +1725,7 @@ def _transform_holes(self, img: np.ndarray) -> np.ndarray:
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")

def __call__(self, img: np.ndarray):
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
self.randomize(img.shape[1:])
if self._do_transform:
img = self._transform_holes(img=img)
Expand Down Expand Up @@ -1871,6 +1884,7 @@ def __init__(
self.dtype = dtype

def __call__(self, img: np.ndarray, mask: Optional[np.ndarray] = None) -> np.ndarray:
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
return equalize_hist(
img=img,
mask=mask if mask is not None else self.mask,
Expand Down
6 changes: 6 additions & 0 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def __call__(
data_array (resampled into `self.pixdim`), original affine, current affine.

"""
data_array, *_ = convert_data_type(data_array, np.ndarray) # type: ignore
_dtype = dtype or self.dtype or data_array.dtype
sr = data_array.ndim - 1
if sr <= 0:
Expand Down Expand Up @@ -275,6 +276,7 @@ def __call__(
data_array (reoriented in `self.axcodes`), original axcodes, current axcodes.

"""
data_array, *_ = convert_data_type(data_array, np.ndarray) # type: ignore
sr = data_array.ndim - 1
if sr <= 0:
raise ValueError("data_array must have at least one spatial dimension.")
Expand Down Expand Up @@ -392,6 +394,7 @@ def __call__(
ValueError: When ``self.spatial_size`` length is less than ``img`` spatial dimensions.

"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
if self.size_mode == "all":
input_ndim = img.ndim - 1 # spatial ndim
output_ndim = len(ensure_tuple(self.spatial_size))
Expand Down Expand Up @@ -1098,6 +1101,8 @@ class RandAffineGrid(Randomizable, Transform):

"""

backend = AffineGrid.backend

@deprecated_arg(name="as_tensor_output", since="0.6")
def __init__(
self,
Expand Down Expand Up @@ -1930,6 +1935,7 @@ def __call__(self, img: Union[np.ndarray, torch.Tensor]):
Args:
img: data to be transformed, assuming `img` is channel first.
"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
if max(self.spatial_channels) > img.ndim - 1:
raise ValueError(
f"input has {img.ndim-1} spatial dimensions, cannot add AddCoordinateChannels channel for "
Expand Down
15 changes: 14 additions & 1 deletion monai/transforms/utility/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,9 @@ def __call__(
output_shape: expected shape of output indices. if None, use `self.output_shape` instead.

"""
label, *_ = convert_data_type(label, np.ndarray) # type: ignore
if image is not None:
image, *_ = convert_data_type(image, np.ndarray) # type: ignore
if output_shape is None:
output_shape = self.output_shape
fg_indices, bg_indices = map_binary_to_indices(label, image, self.image_threshold)
Expand Down Expand Up @@ -826,6 +829,10 @@ def __call__(
output_shape: expected shape of output indices. if None, use `self.output_shape` instead.

"""
label, *_ = convert_data_type(label, np.ndarray) # type: ignore
if image is not None:
image, *_ = convert_data_type(image, np.ndarray) # type: ignore

if output_shape is None:
output_shape = self.output_shape
indices = map_classes_to_indices(label, self.num_classes, image, self.image_threshold)
Expand All @@ -846,6 +853,7 @@ class ConvertToMultiChannelBasedOnBratsClasses(Transform):
"""

def __call__(self, img: np.ndarray) -> np.ndarray:
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
# if img has channel dim, squeeze it
if img.ndim == 4 and img.shape[0] == 1:
img = np.squeeze(img, axis=0)
Expand Down Expand Up @@ -912,6 +920,9 @@ def __call__(
if label.shape[0] != 1:
raise ValueError("Only supports single channel labels!")

img, *_ = convert_data_type(img, np.ndarray) # type: ignore
label, *_ = convert_data_type(label, np.ndarray) # type: ignore

# Generate extreme points
self.randomize(label[0, :])

Expand Down Expand Up @@ -948,6 +959,7 @@ def __call__(self, img: torch.Tensor):
img: PyTorch Tensor data for the TorchVision transform.

"""
img, *_ = convert_data_type(img, torch.Tensor) # type: ignore
return self.trans(img)


Expand Down Expand Up @@ -978,7 +990,7 @@ def __init__(self, orig_labels: Sequence, target_labels: Sequence, dtype: DtypeL
self.dtype = dtype

def __call__(self, img: np.ndarray):
img = np.asarray(img)
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
img_flat = img.flatten()
try:
out_flat = np.copy(img_flat).astype(self.dtype)
Expand Down Expand Up @@ -1034,6 +1046,7 @@ def __call__(
mask must have the same shape as input `img`.

"""
img, *_ = convert_data_type(img, np.ndarray) # type: ignore
if meta_data is None:
meta_data = {}

Expand Down