Skip to content

Commit 2d0685e

Browse files
wyliKumoLiu
authored andcommitted
5432 convert metadict types (#5433)
Signed-off-by: Wenqi Li <[email protected]> Fixes #5432 Fixes Project-MONAI/MONAILabel#1064 ### Description convert potential itk types in `img.GetMetaDataDictionary()` to numpy arrays ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Wenqi Li <[email protected]> Signed-off-by: KumoLiu <[email protected]>
1 parent 5816cec commit 2d0685e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

.github/workflows/pythonapp.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ jobs:
8888
name: Install torch cpu from pytorch.org (Windows only)
8989
run: |
9090
python -m pip install torch==1.12.1+cpu torchvision==0.13.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
91+
- if: runner.os == 'Linux'
92+
name: Install itk pre-release (Linux only)
93+
run: |
94+
python -m pip install --pre -U itk
9195
- name: Install the dependencies
9296
run: |
9397
python -m pip install torch==1.12.1 torchvision==0.13.1

monai/data/image_reader.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,12 @@ def _get_meta_dict(self, img) -> Dict:
318318
319319
"""
320320
img_meta_dict = img.GetMetaDataDictionary()
321-
meta_dict = {key: img_meta_dict[key] for key in img_meta_dict.GetKeys() if not key.startswith("ITK_")}
321+
meta_dict = {}
322+
for key in img_meta_dict.GetKeys():
323+
if key.startswith("ITK_"):
324+
continue
325+
val = img_meta_dict[key]
326+
meta_dict[key] = np.asarray(val) if type(val).__name__.startswith("itk") else val
322327

323328
meta_dict["spacing"] = np.asarray(img.GetSpacing())
324329
return meta_dict

tests/test_load_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def test_channel_dim(self, input_param, filename, expected_shape):
315315
with tempfile.TemporaryDirectory() as tempdir:
316316
filename = os.path.join(tempdir, filename)
317317
nib.save(nib.Nifti1Image(test_image, np.eye(4)), filename)
318-
result = LoadImage(image_only=True, **input_param)(filename)
318+
result = LoadImage(image_only=True, **input_param)(filename) # with itk, meta has 'qto_xyz': itkMatrixF44
319319

320320
self.assertTupleEqual(
321321
result.shape, (3, 128, 128, 128) if input_param.get("ensure_channel_first", False) else expected_shape

0 commit comments

Comments
 (0)