Repository: Project-MONAI/MONAI

Issue:
error from `convert_to_tensor` 
**Describe the bug**
from the cron logs:
```
======================================================================
ERROR: test_dict_matches_4 (tests.test_k_space_spike_noised.TestKSpaceSpikeNoised)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/parameterized/parameterized.py", line 533, in standalone_func
    return func(*(a + p.args), **p.kwargs)
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_k_space_spike_noised.py", line 97, in test_dict_matches
    out = t(deepcopy(data))
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/transforms/intensity/dictionary.py", line 1324, in __call__
    d[key] = self.transform(d[key])
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/transforms/intensity/array.py", line 1499, in __call__
    k = self.shift_fourier(img, n_dims)
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/utils/deprecated.py", line 204, in _wrapper
    return func(*args, **kwargs)
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/transforms/utils.py", line 1280, in shift_fourier
    k, *_ = convert_to_dst_type(k, x)
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/utils/type_conversion.py", line 284, in convert_to_dst_type
    return convert_data_type(data=src, output_type=output_type, device=device, dtype=dtype)
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/utils/type_conversion.py", line 251, in convert_data_type
    data = convert_to_tensor(data, dtype=dtype_, device=device)
  File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/utils/type_conversion.py", line 121, in convert_to_tensor
    return torch.as_tensor(data, dtype=dtype, device=device)  # type: ignore
TypeError: can't convert np.ndarray of type numpy.complex128. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool.
```


Discussion:

Resolving patch:
diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py
--- a/monai/transforms/intensity/array.py
+++ b/monai/transforms/intensity/array.py
@@ -41,7 +41,6 @@
 )
 from monai.utils.deprecated import deprecated_arg
 from monai.utils.enums import TransformBackends
-from monai.utils.misc import is_module_ver_at_least
 from monai.utils.type_conversion import convert_to_tensor, get_equivalent_dtype
 
 __all__ = [
@@ -1493,11 +1492,10 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
 
         n_dims = len(img.shape[1:])
 
-        lib = np if isinstance(img, np.ndarray) else torch
-
         # FT
         k = self.shift_fourier(img, n_dims)
-        log_abs = lib.log(lib.absolute(k) + 1e-10)  # type: ignore
+        lib = np if isinstance(k, np.ndarray) else torch
+        log_abs = lib.log(lib.abs(k) + 1e-10)  # type: ignore
         phase = lib.angle(k)  # type: ignore
 
         k_intensity = self.k_intensity
@@ -1512,13 +1510,8 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
         else:
             self._set_spike(log_abs, self.loc, k_intensity)
         # map back
-        # complex exponential not implemented for older pytorch
-        if isinstance(phase, torch.Tensor) and not is_module_ver_at_least(torch, (1, 6, 0)):
-            phase = phase.cpu()
-            k = torch.exp(log_abs) * torch.exp(1j * phase.cpu()).to(log_abs.device)
-        else:
-            k = lib.exp(log_abs) * lib.exp(1j * phase)  # type: ignore
-        img = self.inv_shift_fourier(k, n_dims)
+        k = lib.exp(log_abs) * lib.exp(1j * phase)  # type: ignore
+        img, *_ = convert_to_dst_type(self.inv_shift_fourier(k, n_dims), dst=img)
 
         return img
 
@@ -1695,7 +1688,7 @@ def _set_default_range(self, img: NdarrayOrTensor) -> Sequence[Sequence[float]]:
         n_dims = len(img.shape[1:])
 
         k = self.shift_fourier(img, n_dims)
-        mod = torch if isinstance(img, torch.Tensor) else np
+        mod = torch if isinstance(k, torch.Tensor) else np
         log_abs = mod.log(mod.absolute(k) + 1e-10)  # type: ignore
         shifted_means = mod.mean(log_abs, dim=tuple(range(-n_dims, 0))) * 2.5  # type: ignore
         return tuple((i * 0.95, i * 1.1) for i in shifted_means)
diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py
--- a/monai/transforms/utils.py
+++ b/monai/transforms/utils.py
@@ -1276,8 +1276,8 @@ def shift_fourier(x: NdarrayOrTensor, spatial_dims: int, n_dims: Optional[int] =
             if hasattr(torch.fft, "fftshift"):
                 k = torch.fft.fftshift(torch.fft.fftn(x, dim=dims), dim=dims)
             else:
+                # if using old PyTorch, will convert to numpy array and return
                 k = np.fft.fftshift(np.fft.fftn(x.cpu().numpy(), axes=dims), axes=dims)
-                k, *_ = convert_to_dst_type(k, x)
         else:
             k = np.fft.fftshift(np.fft.fftn(x, axes=dims), axes=dims)
         return k
@@ -1309,8 +1309,8 @@ def inv_shift_fourier(k: NdarrayOrTensor, spatial_dims: int, n_dims: Optional[in
             if hasattr(torch.fft, "ifftshift"):
                 out = torch.fft.ifftn(torch.fft.ifftshift(k, dim=dims), dim=dims, norm="backward").real
             else:
+                # if using old PyTorch, will convert to numpy array and return
                 out = np.fft.ifftn(np.fft.ifftshift(k.cpu().numpy(), axes=dims), axes=dims).real
-                out, *_ = convert_to_dst_type(out, k)
         else:
             out = np.fft.ifftn(np.fft.ifftshift(k, axes=dims), axes=dims).real
         return out


Test patch: