You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow torchio.Image to be sliced like numpy arrays
image=torchio.Image(...)
print(image.spatial_shape) # -> (100, 100, 100)crop=image[10:20, :, ::2] # returns a new ScalarImage where the affine matrix and data have been modified accordinglyprint(crop.spatial_shape) # -> (10, 100, 50)# image['data'] or image['affine'] would still work
If the image is not yet loaded when the slicing is applied, it would modify the affine and when the image is finally loaded, it would load only the given slice, making use of NiBabel's slicing https://nipy.org/nibabel/nibabel_images.html#image-slicing.
Motivation
More pythonic cropping.
Easier subsampling and flipping.
Much faster patch loading for patch-based pipelines.
Additional context
The __getitem__ method of torchio.Image could look like this:
def__getitem__(self, item):
ifisinstance(item, str):
ifitemin (DATA, AFFINE):
ifitemnotinself:
self.load()
returnsuper().__getitem__(item)
else:
check_validity(item) # do some checksifnotisinstance(item, tuple):
item= (item,)
self.affine=modify_affine(self.affine, item) # to be implemented.ifnotself._loaded:
self._slice=modify_slice(self._slice, item) # to be implemented. self._slice will be used when the# image is finally loaded into memory. It would be# initialized as an empty tuple.else:
self.set_data(self.data[(slice(None), *item)]) # add the channel dimreturnself
Whether to modify the image inplace or to return a new image must be discussed.
The text was updated successfully, but these errors were encountered:
馃殌 Feature
Allow torchio.Image to be sliced like numpy arrays
If the image is not yet loaded when the slicing is applied, it would modify the affine and when the image is finally loaded, it would load only the given slice, making use of NiBabel's slicing https://nipy.org/nibabel/nibabel_images.html#image-slicing.
Motivation
Additional context
The
__getitem__
method oftorchio.Image
could look like this:Whether to modify the image inplace or to return a new image must be discussed.
The text was updated successfully, but these errors were encountered: