Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warning to truncate affine #6819

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ def __call__(
warnings.warn("`data_array` is not of type MetaTensor, assuming affine to be identity.")
# default to identity
input_affine = np.eye(sr + 1, dtype=np.float64)
input_affine_shape = input_affine.shape[0]
if input_affine_shape != sr +1 :
warnings.warn(f"Expected `affine_matrix` of size ({sr+1},{sr+1}) for {sr}D input got ({input_affine_shape},{input_affine_shape}) . The affine matrix will be truncated for {sr}D input.")
affine_ = to_affine_nd(sr, convert_data_type(input_affine, np.ndarray)[0])

out_d = self.pixdim[:sr].copy()
Expand Down Expand Up @@ -3549,3 +3552,12 @@ def __call__(self, img: torch.Tensor, randomize: bool = True) -> torch.Tensor:

else:
return img


img = MetaTensor(torch.rand(10,10,5), affine=torch.eye(4), meta={"original_channel_dim": torch.nan})
sr = Spacing(pixdim=(2,2,2))

double_spacing = sr(img)

#expected shape (6,6,3) or an exception, but the results is (10,6,3), so first dim is treated as channel
print(double_spacing.shape)
2 changes: 2 additions & 0 deletions monai/transforms/spatial/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def spatial_resample(
spatial_rank = min(len(img.shape) - 1, src_affine.shape[0] - 1, 3)
if (not isinstance(spatial_size, int) or spatial_size != -1) and spatial_size is not None:
spatial_rank = min(len(ensure_tuple(spatial_size)), 3) # infer spatial rank based on spatial_size
if src_affine.shape[0] != spatial_rank +1 :
warnings.warn(f"Expected `affine_matrix` of size ({spatial_rank+1},{spatial_rank+1}) for {spatial_rank}D input got ({src_affine.shape[0]},{src_affine.shape[0]}) . The affine matrix will be truncated for {spatial_rank}D input.")
src_affine = to_affine_nd(spatial_rank, src_affine).to(torch.float64)
dst_affine = to_affine_nd(spatial_rank, dst_affine) if dst_affine is not None else src_affine
dst_affine = convert_to_dst_type(dst_affine, src_affine)[0]
Expand Down