Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invertd error when using spline interpolation order 0-5 (integers) in spacing transform #6717

Closed
chaoscls opened this issue Jul 14, 2023 · 1 comment · Fixed by #6719
Closed

Comments

@chaoscls
Copy link

When I use spline interpolation order 0-5 (integers) for spacing transform, an error occurs if I invert item. I found may be there is a bug in https://github.com/Project-MONAI/MONAI/blob/a7bdb9d2611b611a9dd1a3183b025d1d8aeeea90/monai/transforms/utils.py#L1401C45-L1401C45. This line does not support current_mode is an integer.

Here is the error code:

import os.path as osp
import glob

from monai.transforms import LoadImaged, Spacingd, SaveImaged, Compose, Invertd
from tqdm import tqdm

tar_spacing = [1.5, 1.5, 1.5]

def main():
    data_dir = 'data'
    out_dir = 'out'
    image_files = sorted(glob.glob(osp.join(data_dir, 'images', '*.nii.gz')))
    label_files = sorted(glob.glob(osp.join(data_dir, 'labels', '*.nii.gz')))
    files_dict = [{'image': image, 'label': label} for image, label in zip(image_files, label_files)]
    
    transforms = Compose([
        LoadImaged(keys=['image', 'label'], image_only=True, ensure_channel_first=True),
        Spacingd(keys=['image', 'label'], pixdim=tar_spacing, mode=(1, 0), padding_mode='nearest'),
    ])

    post_transforms = Compose([
        Invertd(keys='label', transform=transforms, orig_keys='image', nearest_interp=True),
        SaveImaged(keys='label', output_dir=out_dir, output_postfix='', resample=False, separate_folder=False),
    ])

    for file_dict in tqdm(files_dict):
        data = transforms(file_dict)
        post_transforms(data)
    

if __name__ == '__main__':
    main()

And the error info:

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/transform.py", line 141, in apply_transform
    return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/transform.py", line 98, in _apply_transform
    return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/post/dictionary.py", line 683, in __call__
    transform_info = convert_applied_interp_mode(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/utils.py", line 1395, in convert_applied_interp_mode
    return [convert_applied_interp_mode(x, mode=mode, align_corners=align_corners) for x in trans_info]
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/utils.py", line 1395, in <listcomp>
    return [convert_applied_interp_mode(x, mode=mode, align_corners=align_corners) for x in trans_info]
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/utils.py", line 1412, in convert_applied_interp_mode
    return {
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/utils.py", line 1413, in <dictcomp>
    k: convert_applied_interp_mode(trans_info[k], mode=mode, align_corners=align_corners) for k in trans_info
  File "/opt/homebrew/Caskroom/miniforge/base/envs/dpl/lib/python3.10/site-packages/monai/transforms/utils.py", line 1401, in convert_applied_interp_mode
    if current_mode[0] in _interp_modes:
TypeError: 'int' object is not subscriptable
KumoLiu added a commit to KumoLiu/MONAI that referenced this issue Jul 14, 2023
Signed-off-by: KumoLiu <[email protected]>
@KumoLiu
Copy link
Contributor

KumoLiu commented Jul 14, 2023

Hi @chaoscls, thanks for reporting, just create a PR to fix it.

wyli pushed a commit that referenced this issue Jul 14, 2023
Fixes #6717.

Add int check for  `current_mode` in `convert_applied_interp_mode`.

### 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).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: KumoLiu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants