dosma.core.numpy_routines.pad

dosma.core.numpy_routines.pad(x: MedicalVolume, pad_width, mode='constant', **kwargs)[source]

Implementation of numpy.pad() for MedicalVolume.

Padding a MedicalVolume can affect the affine matrix of the volume. When spatial dimensions are padded, the scanner origin changes.

In addition to standard numpy syntax for pad_width, this function provides some shortcuts for padding particular dimensions.

Either None or 0 can be used to indicate a dimension should not be padded. For example:

>>> mv = MedicalVolume(np.ones(3,4,5), affine=np.eye(4))
>>> pad(mv, (None, 0, (2,3)))  # dimensions 0 and 1 will not be padded
>>> pad(mv, ((0,0), (0,0), (2,3)))  # equivalent to previous, but in numpy syntax

Integers can also be used to indicate the dimension should be padded by the same amount on both sides:

>>> mv = MedicalVolume(np.ones(3,4,5), affine=np.eye(4))
>>> pad(mv, (5, (1,2), (2,3)))  # dimension 0 padded by 5 on both sides
>>> pad(mv, ((5,5), (1,2), (2,3)))  # equivalent to previous, but in numpy syntax

pad_width can also be shorter than the total MedicalVolume dimensions. In this case, the padding is applying in a broadcasting fashion. For example, if the MedicalVolume is 3D, then specifying padding widths for only two dimensions will pad the last two dimensions:

>>> mv = MedicalVolume(np.ones(3,4,5), affine=np.eye(4))
>>> pad(mv, (4, 6))  # last dimension padded by 6, second to last padded by 4
>>> pad(mv, ((0,0), (4,4), (6,6)))  # equivalent to previous, but in numpy syntax
Parameters:
Returns:

The padded medical image.

Return type:

MedicalVolume

Note

Currently, headers are not preserved upon padding. The returned medical image will not have any headers. This may change in the future.

Examples

>>> arr = np.random.rand(3,4,5)
>>> mv = MedicalVolume(arr, affine=np.eye(4))
>>> mv_pad = np.pad(mv, 1)  # pad all dimensions by 1