Cubius
Cubius

Reputation: 1334

padding arrays using numpy

in my program i have a numpy array and do some convolution filtering on it. i am looking for some way to make array padding (and then unpad for output) easily using numpy to avoid boundary checking. i know that scipy can do convolution, but i have reasons to make it by myself. gnuplot.py is used for output.

def touch(field, coords, value):
    field[coords[0], coords[1]] = value
    if coords[0] - 1 > 0:
        field[coords[0] - 1, coords[1]] = value / 2
    if coords[1] - 1 > 0:
        field[coords[0], coords[1] - 1] = value / 2
    if coords[0] < field.shape[0] - 1:
        field[coords[0] + 1, coords[1]] = value / 2
    if coords[1] < field.shape[1] - 1:
        field[coords[0], coords[1] + 1] = value / 2

Upvotes: 4

Views: 3548

Answers (1)

ᅠᅠᅠ
ᅠᅠᅠ

Reputation: 67040

There's a pad module scheduled for inclusion in Numpy 1.7.0 – see this ticket. For now, just download it and use its with_constant function.

Unpadding is as simple as field[1:-1, 1:-1].

Upvotes: 6

Related Questions