[Numpy-discussion] Is there a defined way to "unpad" an array, and if not, should there be?

Jeff Gostick jgostick at gmail.com
Mon Apr 12 20:12:07 EDT 2021


I guess I should have clarified that I was inquiring about proposing a
'feature request'.  The github site suggested I open a discussion on this
list first.  There are several ways to effectively unpad an array as has
been pointed out, but they all require more than a little bit of thought
and care, are dependent on array shape, and honestly error prone.  It would
be very valuable to me to have such a 'predefined' function, so I was
wondering if (a) I was unaware of some function that already does this and
(b) if I'm alone in thinking this would be useful.



On Mon, Apr 12, 2021 at 7:42 PM Aaron Meurer <asmeurer at gmail.com> wrote:

> On Mon, Apr 12, 2021 at 2:29 PM Stephan Hoyer <shoyer at gmail.com> wrote:
> >
> > The easy way to unpad an array is by indexing with slices, e.g.,
> x[20:-4] to undo a padding of [(20, 4)]. Just be careful about unpadding
> "zero" elements on the right hand side, because Python interprets an ending
> slice of zero differently -- you need to write something like x[20:] to
> undo padding by [(20, 0)].
>
> You can use x[20:x.shape[0] - 4] to avoid this inconsistency. Or
> construct the slice based on the original unpadded shape
> (x[20:20+orig_x.shape[0]]).
>
> Aaron Meurer
>
> >
> >
> > On Mon, Apr 12, 2021 at 1:15 PM Jeff Gostick <jgostick at gmail.com> wrote:
> >>
> >> I often find myself padding an array to do some processing on it (i.e.
> to avoid edge artifacts), then I need to remove the padding.  I wish there
> was either a built in "unpad" function that accepted the same arguments as
> "pad", or that "pad" accepted negative numbers (e.g [-20, -4] would undo a
> padding of [20, 4]).  This seems like a pretty obvious feature to me so
> maybe I've just missed something, but I have looked through all the open
> and closed issues on github and don't see anything related to this.
> >>
> >>
> >> Jeff G
> >>
> >> _______________________________________________
> >> NumPy-Discussion mailing list
> >> NumPy-Discussion at python.org
> >> https://mail.python.org/mailman/listinfo/numpy-discussion
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at python.org
> > https://mail.python.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/numpy-discussion/attachments/20210412/9427abba/attachment.html>


More information about the NumPy-Discussion mailing list