Skip to content

Commit

Permalink
fixing psize in mlcommons#29
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthakpati committed Mar 26, 2021
1 parent 6b4b760 commit 9d09dc1
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions GANDLF/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def znorm(tensor: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
return tensor

# adapted from https://codereview.stackexchange.com/questions/132914/crop-black-border-of-image-using-numpy/132933#132933
def crop_image_outside_zeros(array, psize):
def crop_image_outside_zeros(array, patch_size):
dimensions = len(array.shape)
if dimensions != 4:
raise ValueError("Array expected to be 4D but got {} dimensions.".format(dimensions))
Expand All @@ -156,13 +156,13 @@ def crop_image_outside_zeros(array, psize):
# for each axis
for i in range(3):
# if less than patch size, extend the small corner out
if large[i] - small[i] < psize[i]:
small[i] = large[i] - psize[i]
if large[i] - small[i] < patch_size[i]:
small[i] = large[i] - patch_size[i]

# if bottom fell off array, extend the large corner and set small to 0
if small[i] < 0:
small[i] = 0
large[i] = psize[i]
large[i] = patch_size[i]

# calculate pixel location of new bounding box corner (will use to update the reference of the image to physical space)
new_corner_idxs = np.array([small[0], small[1], small[2]])
Expand All @@ -181,15 +181,15 @@ class CropExternalZeroplanes(SpatialTransform):
Transformation class to enable taking the whole image stack (including segmentation) and removing
(starting from edges) physical-coordinate planes with all zero voxels until you reach a non-zero voxel.
Args:
psize: patch size (used to ensure we do not crop to smaller size than this)
patch_size: patch size (used to ensure we do not crop to smaller size than this)
**kwargs: See :class:`~torchio.transforms.Transform` for additional
keyword arguments.
"""

def __init__(self, psize, **kwargs):
def __init__(self, patch_size, **kwargs):
super().__init__(**kwargs)
self.psize = psize
self.args_names = ('psize',)
self.patch_size = patch_size
self.args_names = ('patch_size',)

def apply_transform(self, subject):

Expand All @@ -209,7 +209,7 @@ def apply_transform(self, subject):
numpy_stack = np.concatenate(numpy_stack_list, axis=0)

# crop away the external zero-planes on the whole stack
new_corner_idxs, new_stack = crop_image_outside_zeros(array=numpy_stack, psize=self.psize)
new_corner_idxs, new_stack = crop_image_outside_zeros(array=numpy_stack, patch_size=self.patch_size)

# recompute origin of affine matrix using initial image shape
new_origin = nib.affines.apply_affine(example_image_affine, new_corner_idxs)
Expand Down

0 comments on commit 9d09dc1

Please sign in to comment.