1.4. How to pad a ChannelΒΆ

In this example, we access the Channel Numpy array of a source dataset and copy it to another section of a destination dataset, in order to pad a few slices at the lower Z and at the higher Z.

The source dataset is named sourceChannel:

# Creating the destination Channel
destinationChannel = sourceChannel.copy()

# Adapting the shape of the destination Channel to contain the padding slices
countPaddingSlicesOnEachSide = 10
originalZSize = sourceChannel.getZSize()
destinationChannel.setZSize(originalZSize + 2*countPaddingSlicesOnEachSide)
destinationChannelBox = destinationChannel.getBox()
destinationChannelDirection2 = destinationChannelBox.getDirection2()
destinationChannelSpacingDirection2 = destinationChannelBox.getDirection2Spacing()
destinationChannelOrigin = destinationChannelBox.getOrigin()
destinationChannelNewOrigin = destinationChannelOrigin - countPaddingSlicesOnEachSide*destinationChannelSpacingDirection2*destinationChannelDirection2
destinationChannelBox.setOrigin(destinationChannelNewOrigin)
destinationChannel.setBox(destinationChannelBox)
destinationChannel.initializeData()

# Accessing the data of the source channel and the destination channel
timeStep = 0
npDataSourceChannel = sourceChannel.getNDArray(timeStep)
npDataDestinationChannel = destinationChannel.getNDArray(timeStep)

# Copying common region data
npDataDestinationChannel[countPaddingSlicesOnEachSide:-countPaddingSlicesOnEachSide, :, :] = npDataSourceChannel[:, :, :]

# Padding at lower Z
for zIndex in range(countPaddingSlicesOnEachSide):
    npDataDestinationChannel[zIndex, :, :] = npDataSourceChannel[0, :, :]

# Padding at higher Z
for zIndex in range(countPaddingSlicesOnEachSide):
    npDataDestinationChannel[-zIndex-1, :, :] = npDataSourceChannel[-1, :, :]

# Setting the properties of the new channel
destinationChannel.setTitle(sourceChannel.getTitle() + ' (Padded)')

# Publishing the new object
destinationChannel.publish()