Skip to content

NumPy's pad with reflect_type="odd" appears strange in corners #11283

@jakirkham

Description

@jakirkham

Was trying to understand what NumPy's pad does with reflect_type="odd". According to the docs...

For the ‘odd’ style, the extented part of the array is created by subtracting the reflected values from two times the edge value.

So naively would think this is a composition of pad with mode="edge" and pad with mode="reflect"/mode="symmetric". Went ahead and tried this to see if this would work. Appears it almost does with the exception of corners (though edges appear correct).

While there are some comments in the docs about how corners are dealt with, it's still not obvious to me what is happening here unlike some of the other modes' corner behavior. Not sure if this is just me not understanding or if there is a genuine bug here. Please let me know your thoughts. Example of the behavior below.

Example:
In [1]: import numpy as np

In [2]: np.set_printoptions(linewidth=157)

In [3]: np.random.seed(0)

In [4]: a = np.random.random((3, 4))

In [5]: 2 * np.pad(a, 1, "edge") - np.pad(a, 1, "reflect", reflect_type="even")
Out[5]: 
array([[0.45173289, 0.67397221, 0.78448462, 0.76793954, 0.19799337, 0.65217915],
       [0.38243764, 0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.48700299],
       [0.20141549, 0.4236548 , 0.64589411, 0.43758721, 0.891773  , 1.34595879],
       [1.543884  , 0.96366276, 0.38344152, 0.79172504, 0.52889492, 0.2660648 ],
       [1.28143141, 1.50367072, 0.12098892, 1.14586286, 0.16601684, 0.62020263]])

In [6]: np.pad(a, 1, "reflect", reflect_type="odd")
Out[6]: 
array([[ 0.5634598 ,  0.67397221,  0.78448462,  0.76793954,  0.19799337, -0.37195281],
       [ 0.38243764,  0.5488135 ,  0.71518937,  0.60276338,  0.54488318,  0.48700299],
       [ 0.20141549,  0.4236548 ,  0.64589411,  0.43758721,  0.891773  ,  1.34595879],
       [ 1.543884  ,  0.96366276,  0.38344152,  0.79172504,  0.52889492,  0.2660648 ],
       [ 2.88635252,  1.50367072,  0.12098892,  1.14586286,  0.16601684, -0.81382919]])

In [7]: (2 * np.pad(a, 1, "edge") - np.pad(a, 1, "reflect", reflect_type="even")) - (np.pad(a, 1, "reflect", reflect_type="odd"))
Out[7]: 
array([[-0.1117269 ,  0.        ,  0.        ,  0.        ,  0.        ,  1.02413197],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [-1.60492111,  0.        ,  0.        ,  0.        ,  0.        ,  1.43403182]])

Environment:
name: test
channels:
  - conda-forge
  - defaults
dependencies:
  - appnope=0.1.0=py36_0
  - backcall=0.1.0=py_0
  - blas=1.1=openblas
  - ca-certificates=2018.4.16=0
  - certifi=2018.4.16=py36_0
  - decorator=4.3.0=py_0
  - ipython=6.4.0=py36_0
  - ipython_genutils=0.2.0=py36_0
  - jedi=0.12.0=py36_0
  - libgfortran=3.0.0=0
  - ncurses=5.9=10
  - numpy=1.14.4=py36_blas_openblash24bf2e0_200
  - openblas=0.2.20=8
  - openssl=1.0.2o=0
  - parso=0.2.1=py_0
  - pexpect=4.6.0=py36_0
  - pickleshare=0.7.4=py36_0
  - prompt_toolkit=1.0.15=py36_0
  - ptyprocess=0.5.2=py36_0
  - pygments=2.2.0=py36_0
  - python=3.6.5=1
  - readline=7.0=0
  - setuptools=39.2.0=py36_0
  - simplegeneric=0.8.1=py36_0
  - six=1.11.0=py36_1
  - sqlite=3.20.1=2
  - tk=8.6.7=0
  - traitlets=4.3.2=py36_0
  - wcwidth=0.1.7=py36_0
  - xz=5.2.3=0
  - zlib=1.2.11=h470a237_3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions