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

Custom loader not used when loading data lazily #1084

Closed
1 task done
Zhack47 opened this issue May 24, 2023 · 2 comments · Fixed by #1091
Closed
1 task done

Custom loader not used when loading data lazily #1084

Zhack47 opened this issue May 24, 2023 · 2 comments · Fixed by #1091
Labels
bug Something isn't working

Comments

@Zhack47
Copy link
Contributor

Zhack47 commented May 24, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Bug summary

While using a custom loader (in my example .npy loader), if the data is loaded lazily (i.e.: not with subject.load(), but during the application of a transformation for example) Torchio doesnt seem to use the provided thee custom loader, and instead tries to find a fitting SimpleITIKIO reader

This throws an error in this case because no such reader exists for .npy files.

Code for reproduction

import torchio
import numpy as np


def numpy_reader2D_no_affine(path: PosixPath):
    data = np.load(path).astype(np.int32)
    data = data[:, :, 0]
    return data, np.eye(4)

root_dir = "your/npy/data/here" # patient1 ->
                                #            |ct.npy
                                #            |mask.npy
                                # patient2 -> 
                                #            ....

pad_tr = torchio.CropOrPad(patch_size)
for patient in sorted(os.listdir(root_dir)):
    slice_path = os.path.join(root_dir, patient, "ct.npy")
    mask_path = os.path.join(root_dir, patient, "mask.npy")

    l3_slice = torchio.ScalarImage(slice_path, reader=numpy_reader2D_no_affine)
    mask = torchio.ScalarImage(mask_path, reader=numpy_reader2D_no_affine)
    sub = torchio.Subject(ct=l3_slice, gt =mask)
    # sub.load()  # Uncommenting this, the code works properly
    sub = pad_tr(sub)

Actual outcome

Throws error: Unable to determine ImageIO reader for "/mnt/disk_2//Sarco///mask.npy"

Error messages

/home/<user>/.local/lib/python3.11/site-packages/torchio/data/inference/aggregator.py:229: RuntimeWarning: Medical image frameworks such as ITK do not support int64. Casting to int32...
  warnings.warn(message, RuntimeWarning)
Traceback (most recent call last):
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/subject.py", line 309, in check_consistent_space
    self.check_consistent_attribute('spacing')
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/subject.py", line 273, in check_consistent_attribute
    current_attribute = getattr(image, attribute)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/image.py", line 317, in spacing
    _, spacing = get_rotation_and_spacing_from_affine(self.affine)
                                                      ^^^^^^^^^^^
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/image.py", line 253, in affine
    affine = read_affine(self.path)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/io.py", line 119, in read_affine
    reader = get_reader(path)
             ^^^^^^^^^^^^^^^^
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/io.py", line 128, in get_reader
    reader.ReadImageInformation()
  File "/home/<user>/.local/lib/python3.11/site-packages/SimpleITK/SimpleITK.py", line 8455, in ReadImageInformation
    return _SimpleITK.ImageFileReader_ReadImageInformation(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Exception thrown in SimpleITK ImageFileReader_ReadImageInformation: /tmp/SimpleITK/Code/IO/src/sitkImageReaderBase.cxx:105:
sitk::ERROR: Unable to determine ImageIO reader for "/mnt/disk_2/<user>/<data>/<patient>/mask.npy"

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/<user>/example.py", line 34, in <module>
    sub = pad_tr(sub)
          ^^^^^^^^^^^
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/transforms/transform.py", line 158, in __call__
    transformed = self.apply_transform(subject)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/transforms/preprocessing/spatial/crop_or_pad.py", line 278, in apply_transform
    subject.check_consistent_space()
  File "/home/<user>/.local/lib/python3.11/site-packages/torchio/data/subject.py", line 320, in check_consistent_space
    raise RuntimeError(message) from e
RuntimeError: As described above, some images in the subject are not in the same space. You probably can use the transforms ToCanonical and Resample to fix this, as explained at https://github.com/fepegar/torchio/issues/647#issuecomment-913025695

Expected outcome

No error

System info

Platform:   Linux-5.15.0-74-generic-x86_64-with-glibc2.35
TorchIO:    0.18.78
PyTorch:    1.13.1+cu117
SimpleITK:  2.1.1.2 (ITK 5.2)
NumPy:      1.22.0
Python:     3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
@Zhack47 Zhack47 added the bug Something isn't working label May 24, 2023
@fepegar fepegar changed the title Custiom loader not used when loading data lazily Custom loader not used when loading data lazily Jun 14, 2023
@fepegar
Copy link
Owner

fepegar commented Jun 14, 2023

Thanks for reporting, @Zhack47. This should be fixed in v0.18.92.

@all-contributors please add @Zhack47 for bug

@allcontributors
Copy link
Contributor

@fepegar

I couldn't determine any contributions to add, did you specify any contributions?
Please make sure to use valid contribution names.

I've put up a pull request to add @Zhack47! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants