Repository: Project-MONAI/MONAI

Issue:
`RandKSpaceSpikeNoise` got an unexpected keyword argument 'dim'
**Describe the bug**
`RandKSpaceSpikeNoise` does not work with NumPy array if `intensity_range` is not set manually.

In case a NumPy array is provided, the linked part will call `np.mean(.., dim=...)` but NumPy does not support dim arguments (should be `axis`). The implementation works fine for `torch.Tensors`.

https://github.com/Project-MONAI/MONAI/blob/d6327a765cf23dafd680b2a4b472edf9639e1087/monai/transforms/intensity/array.py#L1816-L1818

**To Reproduce**
Call transform on NumPy array with default values (specifically `intensity_range =None`).

**Expected behavior**
No crash.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environment**

Ensuring you use the relevant python executable, please paste the output of:

```
python -c 'import monai; monai.config.print_debug_info()'
```

**Additional context**
Add any other context about the problem here.



Discussion:
Hi @mibaumgartner ,

Thanks for your feedback!
@yiheng-wang-nv , could you please help confirm and fix it?
I think changing it to `mod.mean(log_abs, tuple(range(-n_dims, 0))) * 2.5` may help.

Thanks in advance.


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
@@ -1815,7 +1815,9 @@ def _set_default_range(self, img: NdarrayOrTensor) -> Sequence[Sequence[float]]:
         k = self.shift_fourier(img, n_dims)
         mod = torch if isinstance(k, torch.Tensor) else np
         log_abs = mod.log(mod.absolute(k) + 1e-10)
-        shifted_means = mod.mean(log_abs, dim=tuple(range(-n_dims, 0))) * 2.5
+        shifted_means = mod.mean(log_abs, tuple(range(-n_dims, 0))) * 2.5
+        if isinstance(shifted_means, torch.Tensor):
+            shifted_means = shifted_means.to("cpu")
         return tuple((i * 0.95, i * 1.1) for i in shifted_means)
 
 


Test patch:
diff --git a/tests/test_k_space_spike_noise.py b/tests/test_k_space_spike_noise.py
--- a/tests/test_k_space_spike_noise.py
+++ b/tests/test_k_space_spike_noise.py
@@ -25,7 +25,8 @@
 TESTS = []
 for shape in ((128, 64), (64, 48, 80)):
     for p in TEST_NDARRAYS:
-        TESTS.append((shape, p))
+        for intensity in [10, None]:
+            TESTS.append((shape, p, intensity))
 
 
 class TestKSpaceSpikeNoise(unittest.TestCase):
@@ -43,11 +44,10 @@ def get_data(im_shape, im_type):
         return im_type(im[None])
 
     @parameterized.expand(TESTS)
-    def test_same_result(self, im_shape, im_type):
+    def test_same_result(self, im_shape, im_type, k_intensity):
 
         im = self.get_data(im_shape, im_type)
         loc = [0, int(im.shape[1] / 2), 0] if len(im_shape) == 2 else [0, int(im.shape[1] / 2), 0, 0]
-        k_intensity = 10
         t = KSpaceSpikeNoise(loc, k_intensity)
 
         out1 = t(deepcopy(im))
@@ -62,11 +62,10 @@ def test_same_result(self, im_shape, im_type):
         np.testing.assert_allclose(out1, out2)
 
     @parameterized.expand(TESTS)
-    def test_highlighted_kspace_pixel(self, im_shape, as_tensor_input):
+    def test_highlighted_kspace_pixel(self, im_shape, as_tensor_input, k_intensity):
 
         im = self.get_data(im_shape, as_tensor_input)
         loc = [0, int(im.shape[1] / 2), 0] if len(im_shape) == 2 else [0, int(im.shape[1] / 2), 0, 0]
-        k_intensity = 10
         t = KSpaceSpikeNoise(loc, k_intensity)
         out = t(im)
 
@@ -75,10 +74,11 @@ def test_highlighted_kspace_pixel(self, im_shape, as_tensor_input):
             self.assertEqual(im.device, out.device)
             out = out.cpu()
 
-        n_dims = len(im_shape)
-        out_k = fftshift(fftn(out, axes=tuple(range(-n_dims, 0))), axes=tuple(range(-n_dims, 0)))
-        log_mag = np.log(np.absolute(out_k))
-        np.testing.assert_allclose(k_intensity, log_mag[tuple(loc)], 1e-4)
+        if k_intensity is not None:
+            n_dims = len(im_shape)
+            out_k = fftshift(fftn(out, axes=tuple(range(-n_dims, 0))), axes=tuple(range(-n_dims, 0)))
+            log_mag = np.log(np.absolute(out_k))
+            np.testing.assert_allclose(k_intensity, log_mag[tuple(loc)], 1e-4)
 
 
 if __name__ == "__main__":
diff --git a/tests/test_rand_k_space_spike_noise.py b/tests/test_rand_k_space_spike_noise.py
--- a/tests/test_rand_k_space_spike_noise.py
+++ b/tests/test_rand_k_space_spike_noise.py
@@ -92,6 +92,13 @@ def test_intensity(self, im_shape, im_type, channel_wise):
         self.assertGreaterEqual(t.sampled_k_intensity[0], 14)
         self.assertLessEqual(t.sampled_k_intensity[0], 14.1)
 
+    @parameterized.expand(TESTS)
+    def test_default_intensity(self, im_shape, im_type, channel_wise):
+        im = self.get_data(im_shape, im_type)
+        t = RandKSpaceSpikeNoise(1.0, intensity_range=None, channel_wise=channel_wise)
+        out = t(deepcopy(im))
+        self.assertEqual(out.shape, im.shape)
+
 
 if __name__ == "__main__":
     unittest.main()