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

Continuing refactoring of the extraction objects #686

Merged
merged 12 commits into from
Dec 16, 2024

Conversation

thibaultdvx
Copy link
Collaborator

@thibaultdvx thibaultdvx commented Dec 9, 2024

Following #685, I continued the refactoring of the extraction classes Image, ROI, Patch and Slice. I suggest to get rid of some complicated features while adding some useful ones:

  • For all: remove use_uncropped_image that should be put in Preprocessing.

  • Slice: in addition to discarded_slices that only enable slice filtering, I suggest to introduce two new arguments. slices enables slice selection (e.g. Slice(slices=[128, 129, 130])); and borders enables slice filtering at the edge of the image. For example, Slice(discarded_slices=[42], borders=5) will filter out the slice with index 42, as well as the first five and the last five slices in each direction. borders can also be specified for each border (e.g. Slice(borders=((1, 1), (2, 3), (1, 1))).

  • Patch: I suggest to enable anisotropic sliding window by allowing the user to pass anisotropic values for patch_size and stride (e.g. Patch(patch_size=(18, 16, 17), stride=(1, 1, 2)).

  • ROI: to replace roi_list, roi_mask_location, roi_crop_input, roi_crop_output, roi_template, roi_mask_pattern, roi_custom_template and roi_custom_mask_pattern, I would suggest to have only two arguments. masks, where a list of masks can be passed via their file location (e.g. ROI(masks=["masks/leftHippocampus.nii.gz", "masks/rightHippocampus.nii.gz"])); and crop that enables to filter out regions that are always 0 for all masks, in order to reduce the size of the images.
    In doing so, I purposely release some constraints on the masks that can be used, that I felt unnecessary.

Besides, I also made some minor changes:

  • Renaming BaseExtraction to Extraction, in order not to have Base... in our typings;
  • Gathering roi, patch, slice and image under the generic word sample. I changed the function names and dosctrings accordingly;
  • Uniformizing the docstring between the four child classes (there is still work the be done).

As of now, the classes are not tested. i'm waiting for your opinion first.

@thibaultdvx
Copy link
Collaborator Author

The classes are now tested with 100% coverage.

@thibaultdvx
Copy link
Collaborator Author

I just added some useful class methods to manipulate TorchIO's class Subject. This is crucial as our transforms will manipulate Subject objects. More precisely:

  • get_tio_image: puts the image and the label in a Subject, as well as other potential masks that will be used to compute the transforms.
  • extract_tio_sample: does the extraction like extract_sample, but works on Subject. All the images in the Subject object will undergo the extraction. For example, in the case of segmentation, extraction will be performed also on the segmentation map.
  • format_output: takes a Subject and some metadata and returns a Sample, which is typically the output of the __getitem__ in our CapsDataset. Sample is similar to the old CapsDatasetSample, but it is different depending on the extraction method.

So, in the __getitem__ of our CapsDataset, I imagine it this way:

    participant, session, img_index, sample_index = self._get_meta_data(idx)
    image, image_path = self._get_image(img_index)
    label = self._get_label(img_index)

    tio_image = self.transforms.extraction.get_tio_image(image, label)

    tio_image = self.transforms.image_transforms(tio_image)

    if not self.eval_mode:
        tio_image = self.transforms.image_augmentation(tio_image)

    tio_sample = self.transforms.extraction.extract_tio_sample(tio_image, sample_index)

    tio_sample = self.transforms.sample_transforms(tio_sample)

    if not self.eval_mode:
        tio_sample = self.transforms.sample_augmentation(tio_sample)

    return self.transforms.extraction.format_output(
        tio_sample,
        participant_id=participant,
        session_id=session,
        image_path=image_path,
    )

@thibaultdvx
Copy link
Collaborator Author

In Slice, I removed the argument slice_mode, as duplicating channels can easily be done with a basic transform.

@thibaultdvx thibaultdvx marked this pull request as ready for review December 11, 2024 16:10
@thibaultdvx
Copy link
Collaborator Author

As discussed in developer meeting, I removed the ROI extraction as it can be done with common transforms (e.g. Mask, potentially followed by Crop).

Copy link
Collaborator

@camillebrianceau camillebrianceau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !

@camillebrianceau camillebrianceau merged commit 464dddf into aramis-lab:clinicadl_v2 Dec 16, 2024
10 checks passed
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 this pull request may close these issues.

2 participants