Repository: Project-MONAI/MONAI

Issue:
test_tciadataset
```
[2023-09-05T13:22:13.399Z] ======================================================================
[2023-09-05T13:22:13.399Z] ERROR: test_values (tests.test_tciadataset.TestTciaDataset)
[2023-09-05T13:22:13.399Z] ----------------------------------------------------------------------
[2023-09-05T13:22:13.399Z] Traceback (most recent call last):
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 141, in apply_transform
[2023-09-05T13:22:13.399Z]     return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 98, in _apply_transform
[2023-09-05T13:22:13.399Z]     return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/io/dictionary.py", line 164, in __call__
[2023-09-05T13:22:13.399Z]     data = self._loader(d[key], reader)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/io/array.py", line 290, in __call__
[2023-09-05T13:22:13.399Z]     img_array, meta_data = reader.get_data(img)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/image_reader.py", line 599, in get_data
[2023-09-05T13:22:13.399Z]     affine = self._get_affine(metadata, self.affine_lps_to_ras)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/image_reader.py", line 666, in _get_affine
[2023-09-05T13:22:13.399Z]     spacing = metadata["00280030"]["Value"]
[2023-09-05T13:22:13.399Z] KeyError: '00280030'
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] The above exception was the direct cause of the following exception:
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] Traceback (most recent call last):
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 141, in apply_transform
[2023-09-05T13:22:13.399Z]     return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 98, in _apply_transform
[2023-09-05T13:22:13.399Z]     return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/compose.py", line 335, in __call__
[2023-09-05T13:22:13.399Z]     result = execute_compose(
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/compose.py", line 111, in execute_compose
[2023-09-05T13:22:13.399Z]     data = apply_transform(
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 171, in apply_transform
[2023-09-05T13:22:13.399Z]     raise RuntimeError(f"applying transform {transform}") from e
[2023-09-05T13:22:13.399Z] RuntimeError: applying transform <monai.transforms.io.dictionary.LoadImaged object at 0x7f449a92b5e0>
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] The above exception was the direct cause of the following exception:
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] Traceback (most recent call last):
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/tests/test_tciadataset.py", line 61, in test_values
[2023-09-05T13:22:13.399Z]     _test_dataset(data)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/tests/test_tciadataset.py", line 43, in _test_dataset
[2023-09-05T13:22:13.399Z]     self.assertTrue("image" in dataset[0])
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/dataset.py", line 112, in __getitem__
[2023-09-05T13:22:13.399Z]     return self._transform(index)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/dataset.py", line 910, in _transform
[2023-09-05T13:22:13.399Z]     return super()._transform(index)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/dataset.py", line 98, in _transform
[2023-09-05T13:22:13.399Z]     return apply_transform(self.transform, data_i) if self.transform is not None else data_i
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 171, in apply_transform
[2023-09-05T13:22:13.399Z]     raise RuntimeError(f"applying transform {transform}") from e
[2023-09-05T13:22:13.399Z] RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7f449a90de20>
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] ----------------------------------------------------------------------
```
test_tciadataset
```
[2023-09-05T13:22:13.399Z] ======================================================================
[2023-09-05T13:22:13.399Z] ERROR: test_values (tests.test_tciadataset.TestTciaDataset)
[2023-09-05T13:22:13.399Z] ----------------------------------------------------------------------
[2023-09-05T13:22:13.399Z] Traceback (most recent call last):
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 141, in apply_transform
[2023-09-05T13:22:13.399Z]     return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 98, in _apply_transform
[2023-09-05T13:22:13.399Z]     return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/io/dictionary.py", line 164, in __call__
[2023-09-05T13:22:13.399Z]     data = self._loader(d[key], reader)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/io/array.py", line 290, in __call__
[2023-09-05T13:22:13.399Z]     img_array, meta_data = reader.get_data(img)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/image_reader.py", line 599, in get_data
[2023-09-05T13:22:13.399Z]     affine = self._get_affine(metadata, self.affine_lps_to_ras)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/image_reader.py", line 666, in _get_affine
[2023-09-05T13:22:13.399Z]     spacing = metadata["00280030"]["Value"]
[2023-09-05T13:22:13.399Z] KeyError: '00280030'
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] The above exception was the direct cause of the following exception:
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] Traceback (most recent call last):
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 141, in apply_transform
[2023-09-05T13:22:13.399Z]     return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 98, in _apply_transform
[2023-09-05T13:22:13.399Z]     return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/compose.py", line 335, in __call__
[2023-09-05T13:22:13.399Z]     result = execute_compose(
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/compose.py", line 111, in execute_compose
[2023-09-05T13:22:13.399Z]     data = apply_transform(
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 171, in apply_transform
[2023-09-05T13:22:13.399Z]     raise RuntimeError(f"applying transform {transform}") from e
[2023-09-05T13:22:13.399Z] RuntimeError: applying transform <monai.transforms.io.dictionary.LoadImaged object at 0x7f449a92b5e0>
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] The above exception was the direct cause of the following exception:
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] Traceback (most recent call last):
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/tests/test_tciadataset.py", line 61, in test_values
[2023-09-05T13:22:13.399Z]     _test_dataset(data)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/tests/test_tciadataset.py", line 43, in _test_dataset
[2023-09-05T13:22:13.399Z]     self.assertTrue("image" in dataset[0])
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/dataset.py", line 112, in __getitem__
[2023-09-05T13:22:13.399Z]     return self._transform(index)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/dataset.py", line 910, in _transform
[2023-09-05T13:22:13.399Z]     return super()._transform(index)
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/data/dataset.py", line 98, in _transform
[2023-09-05T13:22:13.399Z]     return apply_transform(self.transform, data_i) if self.transform is not None else data_i
[2023-09-05T13:22:13.399Z]   File "/home/jenkins/agent/workspace/MONAI-postmerge/monai_github/monai/transforms/transform.py", line 171, in apply_transform
[2023-09-05T13:22:13.399Z]     raise RuntimeError(f"applying transform {transform}") from e
[2023-09-05T13:22:13.399Z] RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7f449a90de20>
[2023-09-05T13:22:13.399Z] 
[2023-09-05T13:22:13.399Z] ----------------------------------------------------------------------
```


Discussion:

Resolving patch:
diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py
--- a/monai/data/image_reader.py
+++ b/monai/data/image_reader.py
@@ -663,7 +663,7 @@ def _get_affine(self, metadata: dict, lps_to_ras: bool = True):
         # "00200032" is the tag of `ImagePositionPatient`
         sx, sy, sz = metadata["00200032"]["Value"]
         # "00280030" is the tag of `PixelSpacing`
-        spacing = metadata["00280030"]["Value"]
+        spacing = metadata["00280030"]["Value"] if "00280030" in metadata else (1.0, 1.0)
         dr, dc = metadata.get("spacing", spacing)[:2]
         affine[0, 0] = cx * dr
         affine[0, 1] = rx * dc


Test patch: