[Numpy-discussion] Style for pad implementation in 'pad' namespace or functions under np.lib

Tim Cera tim@cerazone....
Fri Mar 30 07:56:12 CDT 2012


I rearranged your questions.

Why is this function allocating new arrays that will just be
> copied into the big array and then discarded, instead of filling in
> the big array directly? (Again, this is a speed issue.)


My example in the e-mail was incorrect (sorry about that).  The way it
actually has always worked is exactly what you describe.

If padding a matrix, do we need a way to do padding
> of all rows simultaneously? (Certainly that'd be just as easy for
> something like padwithzeros, and would let us avoid a python-level for
> loop.)


The core of pad is apply_along_axis which passes a vector to a function.
apply_along_axis will pass each 'row' into a function, then I use
apply_along_axis again for each 'column' (which now includes padded values
from each 'row') progressing through the axes.  The function just works on
a vector (rank 1 array).  The vector that the function sees has to already
have place holder pad values.  That is why the function also needs the pad
width to know which values to manipulate.  So a working thought untested
function would be something like:

 def padwithzeros(vector, pad_width, iaxis, **kwargs):
         vector[:pad_tuple[0]] = 0
         vector[-pad_tuple[1]:] = 0
         return vector

 b = pad(padwithzeros, a, 2)

Note that iaxis isn't used!  But wait...

 I like the idea, but this interface feels undercooked to me. What is
> iaxis? (I couldn't figure that out from the docstrings in the pull
> request either.)




>   If it's
> working with single rows at a time, then how will padwithconstant know
> which constant to pull out of kwargs["constant_value"] when it's an
> array?


These questions actually go together.  First, keywords like constant_values
are the same for all rows in the axis - they are defined for the entire
axis.

Now iaxis.  It is only needed inside the function to use the correct
constant_values, end_values, or stat_length.  For example something like...

 def padwithconstants(vector, pad_width, iaxis, **kwargs):
         vector[:pad_tuple[0]] = kwargs['constant_values'][iaxis][0]
         vector[-pad_tuple[1]:] =  kwargs['constant_values'][iaxis][1]
         return vector

 constants = ((10,20), (30,40))
 # 10 will be prepended to all rows (axis 0)
 # 20 will be appended to all rows (axis 0)
 # 30
 b = pad(padwithconstants, a, 2, constant_values=constants)

Kindest regards,
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120330/cefc5a91/attachment-0001.html 


More information about the NumPy-Discussion mailing list