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

Forced Fourier class to output contiguous tensors. #7969

Merged
merged 5 commits into from
Sep 3, 2024
Merged
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
12 changes: 8 additions & 4 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,14 +1863,15 @@ class Fourier:
"""

@staticmethod
def shift_fourier(x: NdarrayOrTensor, spatial_dims: int) -> NdarrayOrTensor:
def shift_fourier(x: NdarrayOrTensor, spatial_dims: int, as_contiguous: bool = False) -> NdarrayOrTensor:
"""
Applies fourier transform and shifts the zero-frequency component to the
center of the spectrum. Only the spatial dimensions get transformed.

Args:
x: Image to transform.
spatial_dims: Number of spatial dimensions.
as_contiguous: Whether to convert the cached NumPy array or PyTorch tensor to be contiguous.

Returns
k: K-space data.
Expand All @@ -1885,17 +1886,20 @@ def shift_fourier(x: NdarrayOrTensor, spatial_dims: int) -> NdarrayOrTensor:
k = np.fft.fftshift(np.fft.fftn(x.cpu().numpy(), axes=dims), axes=dims)
else:
k = np.fft.fftshift(np.fft.fftn(x, axes=dims), axes=dims)
return k
return ascontiguousarray(k) if as_contiguous else k

@staticmethod
def inv_shift_fourier(k: NdarrayOrTensor, spatial_dims: int, n_dims: int | None = None) -> NdarrayOrTensor:
def inv_shift_fourier(
k: NdarrayOrTensor, spatial_dims: int, n_dims: int | None = None, as_contiguous: bool = False
) -> NdarrayOrTensor:
"""
Applies inverse shift and fourier transform. Only the spatial
dimensions are transformed.

Args:
k: K-space data.
spatial_dims: Number of spatial dimensions.
as_contiguous: Whether to convert the cached NumPy array or PyTorch tensor to be contiguous.

Returns:
x: Tensor in image space.
Expand All @@ -1910,7 +1914,7 @@ def inv_shift_fourier(k: NdarrayOrTensor, spatial_dims: int, n_dims: int | None
out = np.fft.ifftn(np.fft.ifftshift(k.cpu().numpy(), axes=dims), axes=dims).real
else:
out = np.fft.ifftn(np.fft.ifftshift(k, axes=dims), axes=dims).real
return out
return ascontiguousarray(out) if as_contiguous else out


def get_number_image_type_conversions(transform: Compose, test_data: Any, key: Hashable | None = None) -> int:
Expand Down
Loading