[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 21:36:31 EDT 2021

It is great to hear that this might be useful.  I would LOVE to create a PR
on this idea and contribute back to numpy...but let's not get ahead of
ourselves :-)

Regarding the name, I kinda like "unpad" since it relates directly to
"pad", analogous to "ravel" and "unravel" for instance.  Or maybe "depad".
Although, it's possible to use this on any array, not just a previously
padded one, so maybe tying it too directly to "pad" is not right, in which
case "trim" and "crop" are both perfect.  I must admit that I find it odd
that these functions are not in numpy already.  I just searched the docs
and they show up as keyword args for a few functions but are otherwise
conspicuously absent.  Also, funnily, there is a link to "padding arrays"
but it is basically empty:

Alternatively, I don't hate the idea of passing negative pad widths into
"pad".  I actually tried this at one point to see if there was a hidden
functionality there, to no avail.

BTW, we just adding a custom "unpad" function to our PoreSpy package for
this purpose:

On Mon, Apr 12, 2021 at 9:15 PM Stephan Hoyer <shoyer at gmail.com> wrote:

> On Mon, Apr 12, 2021 at 5:12 PM Jeff Gostick <jgostick at gmail.com> wrote:
>> 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.
> Indeed, this is a fair question.
> Given that this is not entirely trivial to write correctly, I think it
> would be reasonable to add the inverse operation for pad() into NumPy. This
> is generally better than encouraging users to write their own thing.
> From a naming perspective, here are some possibilities:
> unpad
> trim
> crop
> I think "trim" would be pretty descriptive, probably slightly better than
> "unpad."
> _______________________________________________
> 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/e307e232/attachment-0001.html>

More information about the NumPy-Discussion mailing list