Repository: Project-MONAI/MONAI

Issue:
integration error monai/metrics/cumulative_average.py 
```
12:47:57  Traceback (most recent call last):
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/utils/module.py", line 199, in load_submodules
12:47:57      mod = import_module(name)
12:47:57    File "/opt/conda/lib/python3.8/importlib/__init__.py", line 127, in import_module
12:47:57      return _bootstrap._gcd_import(name[level:], package, level)
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/apps/pathology/__init__.py", line 15, in <module>
12:47:57      from .metrics import LesionFROC
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/apps/pathology/metrics/__init__.py", line 12, in <module>
12:47:57      from .lesion_froc import LesionFROC
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/apps/pathology/metrics/lesion_froc.py", line 18, in <module>
12:47:57      from monai.metrics import compute_fp_tp_probs, compute_froc_curve_data, compute_froc_score
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/metrics/__init__.py", line 14, in <module>
12:47:57      from .cumulative_average import CumulativeAverage
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/metrics/cumulative_average.py", line 17, in <module>
12:47:57      from numpy.typing import NDArray
12:47:57  ImportError: cannot import name 'NDArray' from 'numpy.typing' (/opt/conda/lib/python3.8/site-packages/numpy/typing/__init__.py)
12:47:57  
12:47:57  The above exception was the direct cause of the following exception:
12:47:57  
12:47:57  Traceback (most recent call last):
12:47:57    File "/opt/conda/lib/python3.8/runpy.py", line 194, in _run_module_as_main
12:47:57      return _run_code(code, main_globals, None,
12:47:57    File "/opt/conda/lib/python3.8/runpy.py", line 87, in _run_code
12:47:57      exec(code, run_globals)
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/tests/utils.py", line 38, in <module>
12:47:57      from monai.apps.utils import download_url
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/__init__.py", line 55, in <module>
12:47:57      load_submodules(sys.modules[__name__], False, exclude_pattern=excludes)
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/utils/module.py", line 209, in load_submodules
12:47:57      raise type(e)(f"{e}\n{msg}").with_traceback(e.__traceback__) from e  # raise with modified message
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/utils/module.py", line 199, in load_submodules
12:47:57      mod = import_module(name)
12:47:57    File "/opt/conda/lib/python3.8/importlib/__init__.py", line 127, in import_module
12:47:57      return _bootstrap._gcd_import(name[level:], package, level)
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/apps/pathology/__init__.py", line 15, in <module>
12:47:57      from .metrics import LesionFROC
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/apps/pathology/metrics/__init__.py", line 12, in <module>
12:47:57      from .lesion_froc import LesionFROC
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/apps/pathology/metrics/lesion_froc.py", line 18, in <module>
12:47:57      from monai.metrics import compute_fp_tp_probs, compute_froc_curve_data, compute_froc_score
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/metrics/__init__.py", line 14, in <module>
12:47:57      from .cumulative_average import CumulativeAverage
12:47:57    File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/metrics/cumulative_average.py", line 17, in <module>
12:47:57      from numpy.typing import NDArray
12:47:57  ImportError: cannot import name 'NDArray' from 'numpy.typing' (/opt/conda/lib/python3.8/site-packages/numpy/typing/__init__.py)
```


Discussion:

Resolving patch:
diff --git a/monai/data/meta_tensor.py b/monai/data/meta_tensor.py
--- a/monai/data/meta_tensor.py
+++ b/monai/data/meta_tensor.py
@@ -321,7 +321,7 @@ def as_tensor(self) -> torch.Tensor:
         Return the `MetaTensor` as a `torch.Tensor`.
         It is OS dependent as to whether this will be a deep copy or not.
         """
-        return self.as_subclass(torch.Tensor)  # type: ignore
+        return self.as_subclass(torch.Tensor)
 
     def get_array(self, output_type=np.ndarray, dtype=None, device=None, *_args, **_kwargs):
         """
diff --git a/monai/metrics/cumulative_average.py b/monai/metrics/cumulative_average.py
--- a/monai/metrics/cumulative_average.py
+++ b/monai/metrics/cumulative_average.py
@@ -10,11 +10,12 @@
 # limitations under the License.
 
 import warnings
-from typing import Any, Optional, Union
+from typing import Any, Optional
 
 import torch
 import torch.distributed as dist
-from numpy.typing import NDArray
+
+from monai.config import NdarrayOrTensor
 
 
 class CumulativeAverage:
@@ -52,7 +53,7 @@ def reset(self) -> None:
         self.count = torch.tensor(0, dtype=torch.float)
         self.is_distributed = dist.is_available() and dist.is_initialized()
 
-    def get_current(self, to_numpy: bool = True) -> Union[NDArray, torch.Tensor]:
+    def get_current(self, to_numpy: bool = True) -> NdarrayOrTensor:
         """
         returns the most recent value (averaged across processes)
 
@@ -74,7 +75,7 @@ def get_current(self, to_numpy: bool = True) -> Union[NDArray, torch.Tensor]:
 
         return val
 
-    def aggregate(self, to_numpy: bool = True) -> Union[NDArray, torch.Tensor]:
+    def aggregate(self, to_numpy: bool = True) -> NdarrayOrTensor:
         """
         returns the total average value (averaged across processes)
 


Test patch:
diff --git a/tests/test_nifti_rw.py b/tests/test_nifti_rw.py
--- a/tests/test_nifti_rw.py
+++ b/tests/test_nifti_rw.py
@@ -181,8 +181,8 @@ def test_write_3d(self):
                 writer_obj.set_metadata({"affine": np.diag([1, 1, 1, 1]), "original_affine": np.diag([1.4, 1, 1, 1])})
                 writer_obj.write(image_name, verbose=True)
                 out = nib.load(image_name)
-                np.testing.assert_allclose(out.get_fdata(), [[[0, 1, 2], [3, 4, 5]]])
-                np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))
+                np.testing.assert_allclose(out.get_fdata(), [[[0, 1, 2], [3, 4, 5]]], atol=1e-4, rtol=1e-4)
+                np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]), atol=1e-4, rtol=1e-4)
 
                 image_name = os.path.join(out_dir, "test1.nii.gz")
                 img = p(np.arange(5).reshape((1, 1, 5)))
@@ -205,8 +205,8 @@ def test_write_4d(self):
                 writer_obj.set_metadata({"affine": np.diag([1.4, 1, 1, 1]), "original_affine": np.diag([1, 1.4, 1, 1])})
                 writer_obj.write(image_name, verbose=True)
                 out = nib.load(image_name)
-                np.testing.assert_allclose(out.get_fdata(), [[[[0, 1], [2, 3], [4, 5]]]])
-                np.testing.assert_allclose(out.affine, np.diag([1, 1.4, 1, 1]))
+                np.testing.assert_allclose(out.get_fdata(), [[[[0, 1], [2, 3], [4, 5]]]], atol=1e-4, rtol=1e-4)
+                np.testing.assert_allclose(out.affine, np.diag([1, 1.4, 1, 1]), atol=1e-4, rtol=1e-4)
 
                 image_name = os.path.join(out_dir, "test1.nii.gz")
                 img = p(np.arange(5).reshape((1, 1, 5, 1)))
@@ -232,8 +232,10 @@ def test_write_5d(self):
                 np.testing.assert_allclose(
                     out.get_fdata(),
                     np.array([[[[[0.0, 1.0], [2.0, 3.0]], [[4.0, 5.0], [6.0, 7.0]], [[8.0, 9.0], [10.0, 11.0]]]]]),
+                    atol=1e-4,
+                    rtol=1e-4,
                 )
-                np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))
+                np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]), atol=1e-4, rtol=1e-4)
 
                 image_name = os.path.join(out_dir, "test1.nii.gz")
                 img = p(np.arange(10).reshape((1, 1, 5, 1, 2)))
diff --git a/tests/test_to_tensord.py b/tests/test_to_tensord.py
--- a/tests/test_to_tensord.py
+++ b/tests/test_to_tensord.py
@@ -20,7 +20,6 @@
 
 cp, _ = optional_import("cupy")
 
-
 im = [[1, 2], [3, 4]]
 
 TESTS = [(im, (2, 2))]