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 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
7 changes: 7 additions & 0 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@ 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we can compare the length of self.pixdim and sr+1; otherwise, there might always be a WARNING for 2D data. Also, users may not modify the affine_matrix, perhaps we should just mention it in the warning message for users who are unfamiliar with MONAI and don't know they need to add a channel.
What do you think?

warnings.warn(
f"Expected `affine_matrix` of size ({sr+1},{sr+1}) for {sr}D input got "
f"({input_affine_shape},{input_affine_shape}). "
f"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
6 changes: 6 additions & 0 deletions monai/transforms/spatial/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ 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}) "
f"for {spatial_rank}D input got ({src_affine.shape[0]},{src_affine.shape[0]}). "
f"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