4

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
2
  • 1
    Is that really convolution? That code will overwrite all neighboring cells, without regard to their previous value. What are you really trying to do? Commented Oct 8, 2011 at 15:05
  • it's kinda triggering function Commented Oct 8, 2011 at 15:26

1 Answer 1

6

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].

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to edit the answer, padding is now live in 1.7.0. The doc link is here docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.