Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This PR addresses a bug in the
vista2d/components.py:LoadTiffd
transform that causesRuntimeError: each element in list of batch should be of equal size
during data collation when usingpad_list_data_collate
or similar functions.Problem:
The current implementation calculates and stores the
ImageMetaKey.SPATIAL_SHAPE
metadata before normalizing the image array's dimensions and channel count. Input image files can have different original dimensions (e.g., 2D grayscale(H, W)
, 3D channels-last(H, W, C)
, or 3D channels-first(C, H, W)
). This leads to the storedspatial_shape
metadata being a tuple of inconsistent lengths (e.g., length 2 or length 3) across different samples in the dataset.When the
DataLoader
attempts to collate a batch containing samples with differentspatial_shape
lengths, the default collation logic for metadata (monai.data.utils.collate_meta_tensor_fn
->torch.utils.data._utils.collate.default_collate
) fails because it cannot stack tuples/lists of varying sizes.Solution:
The fix implemented in this PR modifies
LoadTiffd
as follows:ImageMetaKey.SPATIAL_SHAPE
is moved to after all image array manipulations (loading, transposing, adding/repeating/selecting channels).(C, H, W)
format internally before metadata creation.H
andW
) from the final processed 3D array.(H, W)
tuple, which always has a length of 2, is stored as the value forImageMetaKey.SPATIAL_SHAPE
in theMetaTensor
.Outcome:
This change guarantees that the
spatial_shape
metadata associated with loaded images has a consistent length (always 2) across all samples, regardless of the original file's dimensions. This resolves the downstream collation error. Robustness for handling different input dimensions (2D, 3D channels-last/first, >3D) has also been slightly improved with added logging.