10 examples of 'numpy pad 2d array' in Python

Every line of 'numpy pad 2d array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
590def pad(array):
591 return numpy.pad(array, ((0, 0), (0, 1)), "constant")
261def _pad(self, arr, num_step):
262 """Pad sequence to full fit within network"""
263
264 size = len(arr)
265 pad_size = math.ceil(size / num_step) * num_step
266 arr_pad = np.zeros(pad_size)
267 arr_pad[:size] = arr
268
269 return arr_pad
27def _pad(data, shape):
28 """Pad the data to the given shape with zeros.
29
30 Parameters
31 ----------
32 data : 2-d ndarray
33 Input data
34 shape : (2,) tuple
35
36 """
37 out = np.zeros(shape)
38 out[tuple(slice(0, n) for n in data.shape)] = data
39 return out
28def _zero_pad(array, amount, axes=(1, 2)):
29 """
30 .. todo::
31
32 WRITEME
33 """
34 if amount == 0:
35 return array
36 new_shape = []
37 slices = []
38 for i, s in enumerate(array.shape):
39 if i in axes:
40 new_shape.append(s + 2 * amount)
41 slices.append(slice(amount, -amount))
42 else:
43 new_shape.append(s)
44 slices.append(slice(None))
45 new_shape = tuple(new_shape)
46 slices = tuple(slices)
47 new_array = numpy.zeros(new_shape, dtype=array.dtype)
48 new_array[slices] = array
49 return new_array
25def __call__(self, x):
26 if self.pre_pad:
27 return x
28 else:
29 np_pad = ((self.pad_size[0], self.pad_size[1]), (self.pad_size[2], self.pad_size[3]))
30 return np.pad(x, ((0,0), (0,0), np_pad[0], np_pad[1]), mode='reflect')
17def prepad(vec, length, value=0):
18 if length > len(vec):
19 return np.r_[value * np.ones(length - len(vec)), vec]
20 elif length < len(vec):
21 return vec[-length:]
22 else:
23 return vec
288def pad3d( data, nx, ny, nz ):
289 #create undersampling mask
290 datsize = data.shape
291 padsize = np.array(datsize)
292 padsize[0] = nx
293 padsize[1] = ny
294 padsize[2] = nz
295 ndata = np.zeros(tuple(padsize),dtype = data.dtype)
296
297 # center k-space index range
298 datrx = np.int(datsize[0]/2)
299 datry = np.int(datsize[1]/2)
300 datrz = np.int(datsize[2]/2)
301 cx = np.int(nx/2)
302 cy = np.int(ny/2)
303 cz = np.int(nz/2)
304 cxr = np.arange(round(cx-datrx),round(cx-datrx+datsize[0]))
305 cyr = np.arange(round(cy-datry),round(cy-datry+datsize[1]))
306 czr = np.arange(round(cz-datrz),round(cz-datrz+datsize[2]))
307 #print cxr,cyr
308 ndata[np.ix_(map(int,cxr),map(int,cyr),map(int,czr))] = data
309 return ndata
27def pad_image(x, pad_amount, mode='reflect', constant=0.):
28 e = pad_amount
29 shape = list(x.shape)
30 shape[:2] += 2*e
31 if mode == 'constant':
32 x_padded = np.ones(shape, dtype=np.float32)*constant
33 x_padded[e:-e, e:-e] = x.copy()
34 else:
35 x_padded = np.zeros(shape, dtype=np.float32)
36 x_padded[e:-e, e:-e] = x.copy()
37
38 if mode == 'reflect':
39 x_padded[:e, e:-e] = np.flipud(x[:e, :]) # left edge
40 x_padded[-e:, e:-e] = np.flipud(x[-e:, :]) # right edge
41 x_padded[e:-e, :e] = np.fliplr(x[:, :e]) # top edge
42 x_padded[e:-e, -e:] = np.fliplr(x[:, -e:]) # bottom edge
43 x_padded[:e, :e] = np.fliplr(np.flipud(x[:e, :e])) # top-left corner
44 x_padded[-e:, :e] = np.fliplr(np.flipud(x[-e:, :e])) # top-right corner
45 x_padded[:e, -e:] = np.fliplr(np.flipud(x[:e, -e:])) # bottom-left corner
46 x_padded[-e:, -e:] = np.fliplr(np.flipud(x[-e:, -e:])) # bottom-right corner
47 elif mode == 'zero' or mode == 'constant':
48 pass
49 elif mode == 'nearest':
50 x_padded[:e, e:-e] = x[[0], :] # left edge
51 x_padded[-e:, e:-e] = x[[-1], :] # right edge
52 x_padded[e:-e, :e] = x[:, [0]] # top edge
53 x_padded[e:-e, -e:] = x[:, [-1]] # bottom edge
54 x_padded[:e, :e] = x[[0], [0]] # top-left corner
55 x_padded[-e:, :e] = x[[-1], [0]] # top-right corner
56 x_padded[:e, -e:] = x[[0], [-1]] # bottom-left corner
57 x_padded[-e:, -e:] = x[[-1], [-1]] # bottom-right corner
58 else:
59 raise ValueError("Unsupported padding mode \"{}\"".format(mode))
60 return x_padded
60def pad_by(image,r,dtype=None):
61 """Symmetrically pad the image by the given amount"""
62 if dtype is None: dtype = image.dtype
63 w,h = image.shape
64 result = zeros((w+2*r,h+2*r))
65 result[r:(w+r),r:(h+r)] = image
66 return result
162def add_padding(img, image_size=128, verbose=False, pad_value=None):
163 height, width = img.shape
164 if not pad_value:
165 pad_value = img[0][0]
166 if verbose:
167 print('original cropped image size:', img.shape)
168
169 # Adding padding of x axis - left, right
170 pad_x_width = (image_size - width) // 2
171 pad_x = np.full((height, pad_x_width), pad_value, dtype=np.float32)
172 img = np.concatenate((pad_x, img), axis=1)
173 img = np.concatenate((img, pad_x), axis=1)
174
175 width = img.shape[1]
176
177 # Adding padding of y axis - top, bottom
178 pad_y_height = (image_size - height) // 2
179 pad_y = np.full((pad_y_height, width), pad_value, dtype=np.float32)
180 img = np.concatenate((pad_y, img), axis=0)
181 img = np.concatenate((img, pad_y), axis=0)
182
183 # Match to original image size
184 width = img.shape[1]
185 if img.shape[0] % 2:
186 pad = np.full((1, width), pad_value, dtype=np.float32)
187 img = np.concatenate((pad, img), axis=0)
188 height = img.shape[0]
189 if img.shape[1] % 2:
190 pad = np.full((height, 1), pad_value, dtype=np.float32)
191 img = np.concatenate((pad, img), axis=1)
192
193 if verbose:
194 print('final image size:', img.shape)
195
196 return img

Related snippets