Skip to content

BUG: Unexpected pad value when padding an uint64 array #11027

Closed

Description

@lagru

This seems to be an edge case, nevertheless I wanted to report this behavior. Especially because I find it kind of interesting.

Description

When padding an array of dtype uint64 there are some cases when mode "constant" doesn't append with the correct value but instead pads with 0.

Code to reproduce

>>> offset = 1024
>>> pad_value = np.uint64(-1024)
>>> x = np.zeros(5, dtype=np.uint64) - offset
>>> np.pad(x, 1, mode="constant", constant_values=pad_value)
array([                   0, 18446744073709551615, 18446744073709551615,
       18446744073709551615, 18446744073709551615, 18446744073709551615,
                          0], dtype=uint64)

Expected Result

array([18446744073709551615, 18446744073709551615, 18446744073709551615,
       18446744073709551615, 18446744073709551615, 18446744073709551615,
       18446744073709551615], dtype=uint64)

There are some interesting observations to be made:

  • This only happens for uint64 (uint32, uint16 and below are fine).
  • This behavior is only visible if offset is in [1, 1024] and the argument of np.uint64 in pad_value is in [-1, -1024].
  • np.pad(x, 1, mode="minimum") (or maximum) works fine, regardless of offset.

Platform, versions, etc.

NumPy: 1.14.2
Operating system: Manjaro 17.1.7, x86_64 Linux 4.14.36-1-MANJARO
Python version: 3.6.5 (Anaconda, [GCC 7.2.0] on linux)

I hope this is reproduce-able and not a weird quirk of my setup. Feel free to ask for further diagnostics if you can think of any.

Activity

  1. lagru commented on May 1, 2018

    @lagru
    ContributorAuthor

    Here is another case with a wrong pad value:

    >>> x = np.ones(5, dtype=np.int64) * 9223372036854775765
    >>> np.pad(x, 1, mode="constant", constant_values=x.min())
    array([-9223372036854775808,  9223372036854775765,  9223372036854775765,
            9223372036854775765,  9223372036854775765,  9223372036854775765,
           -9223372036854775808], dtype=int64)
  2. mhvk commented on May 1, 2018

    @mhvk
    Contributor

    Thanks for reporting and investigating!

    Just to dig a bit deeper, this ends up calling _prepend_const and _append_const, which share the problem:

    np.lib.arraypad._prepend_const(x.copy(), 1, pad_value, 0)
    # array([                   0, 18446744073709550592, 18446744073709550592,
    #        18446744073709550592, 18446744073709550592, 18446744073709550592],
    #       dtype=uint64)
    

    The problem boils down to this line, where the padding is constructed using:

    (np.zeros(padshape) + val).astype(arr.dtype)
    

    this creates zeros with dtype=np.float64, which is wrong. I think the following replacement will solve this:

    np.full(padshape, val).astype(arr.dtype)
    

    Any interest in making a PR?

  3. lagru commented on May 1, 2018

    @lagru
    ContributorAuthor

    Wow, thank you for the quick and, assuming this is the cause, useful answer! I didn't even think of looking into pad's code. Next time I'll try that first.

    Any interest in making a PR?

    Yeah, why not. 😉

  4. mhvk commented on May 1, 2018

    @mhvk
    Contributor

    Great, if you're new to making a PR, have a look at the "contributing to numpy" section of the developer's guide; here, most relevant is to include a new test case - which can of course just be the one you have at the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions