juliapaddingzero-padding

Julia equivalent of pad_width in np.pad


Numpy has a padding function with a pad_width parameter that does the following:

pad_width: Number of values padded to the edges of each axis. ((before_1, after_1), ... (before_N, after_N)) unique pad widths for each axis. (before, after) or ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes.

Is there an equivalent function in Julia with similar functionality for zero-padding? Creating a 2D matrix with complex data and zero-padding in Python:

# Python Implementation
import numpy as np

data = np.random.random((620, 327)) + np.random.random((620, 327)) * 1j
padWidths = ((12,12),(327,327))
# Returns an array of size (644, 981) as the pad_widths parameter specified 
# zero-padding of length 644 = 620 + 12 + 12 and 981 =  327 + 327 + 327
zeroPaddedData = np.pad(data, padWidths)

Performing a similar analysis with a 2D complex array in Julia:

# Julia Implementation
using Random
using PaddedViews
using ImageFiltering

data = randn(ComplexF32, (620, 327))
padWidth = ((12,12),(327,327))

# This returns an array of size (620,327)
zeroPaddedDataOne= PaddedView(0, data,(620,327))

# This returns an array of size (620,981)
zeroPaddedDataTwo = padarray(data, Fill(0,(0,327)))

# This returns an array of size (644,327)
zeroPaddedDataThree= padarray(data, Fill(0,(12,0)))

# This returns an array of size (644,981)
zeroPaddedDataFour = padarray(data, Fill(0,(12,327)))

# This doesn't work as you can't pass in a tuple of tuples into an array with 2-dimensions
zeroPaddedDataFive = padarray(data, Fill(0,padWidth))
zeroPaddedDataSix = PaddedView(0, data,padWidth)

It appears that one solution is to use

zeroPaddedData = padarray(data, Fill(0,(12,327)))

to match the functionality of pad_width in Numpy (which, instead of passing in a tuple of tuples, is a single tuple containing the amount of padding to perform along each dimension of the array). Is this the recommended approach to match the pad_width parameter in Numpy?


Solution

  • In PaddedViews the first tuple parameter provides the size and the second a location of the top left corner the original matrix in the padded matrix. For an example consider a matrix:

    julia> data = rand(0:9,2,3)+im*rand(0:9,2,3)
    2×3 Matrix{Complex{Int64}}:
     8+8im  7+2im  9+0im
     8+0im  2+5im  3+9im
    

    Than after padding you would get:

    julia> collect(PaddedView(Complex{Int32}(0),data,(4,6),(2,2)))
    4×6 Matrix{Complex{Int64}}:
     0+0im  0+0im  0+0im  0+0im  0+0im  0+0im
     0+0im  8+8im  7+2im  9+0im  0+0im  0+0im
     0+0im  8+0im  2+5im  3+9im  0+0im  0+0im
     0+0im  0+0im  0+0im  0+0im  0+0im  0+0im
    

    The new matrix is 4x6 (first tuple parameter (4,6)) and the original matrix starts at the location (2,2).

    So what you are looking for is probably:

    zeroPaddedData= PaddedView(ComplexF32(0.), data, (620 + 12 + 12, 327 + 327 + 327),(12+1, 327+1))
    

    This will yield the size 644x981 and place data inside it starting at the location (13,328) having the pads like your Python code. You might want to materialize your padded matrix by using collect as in my example.