2

Another numpy array handling question: I have an approx. 2000³ entries numpy array with fixed size (that I know), containing integers. I want to pad the array with another integer, so that it's surrounded in all dimensions. This integer is fixed for the whole padding process.

example (2D)
1----->000
       010
       000

I have two ideas, leading to that result:

  1. Creating a larger numpy array, containing the padded values and "slicing" the old area in the padded:

    padded=np.zeros((z+2,x+2,y+2))
    padded[1:z+1,1:x+1,1:y+1]=olddata
    
  2. Using np.insert or hstack,vstack,dstack to add the values:

    padded=np.insert(data,0,0,axis=0)
    padded=np.insert(data,x+1,0,axis=0) etc.
    

The Problem is, that all these methods are not in-place and allocate a new array (1.) or copy the old one (2.). Is there a way to do the padding in-place? I know, that since numpy 1.7. there's the numpy.pad module. But that also seems to use some kind of allocation and overriding (like in my 1. way).

6
  • Could you work with a padded array to begin with? Commented Apr 10, 2013 at 9:50
  • @JanneKarila : I would not know how to start with a padded array? I have the rawdata and on some point I have to pad it or don't I? Commented Apr 10, 2013 at 11:17
  • 1
    How do you get the raw data into a NumPy array? Perhaps you could read it into a slice inside the padding at that point. Commented Apr 10, 2013 at 11:22
  • @JanneKarila I could do that, but I need to create the Padding as an array before, which takes a lot of time and memory (np.ones(2000,2000,2000) for example takes nearly 4 seconds) Commented Apr 10, 2013 at 11:36
  • 1
    Instead of np.ones you could use np.empty and slice assignments to set the padding. Commented Apr 10, 2013 at 11:40

1 Answer 1

1

You can't add padding in-place because there is no room for it in the memory layout. You can go the other way though: allocate the padded array first, and use a view into it when accessing unpadded data.

padded = np.empty((2002,2002,2002))
padded[0] = 0
padded[-1] = 0
padded[:,0] = 0
padded[:,-1] = 0
padded[:,:,0] = 0
padded[:,:,-1] = 0

unpadded = padded[1:-1, 1:-1, 1:-1]
Sign up to request clarification or add additional context in comments.

Comments

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.