我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.pad()。
def restore_labels(labels, roi, read_info): if roi == -1: # Pad first, then resize to original shape labels = np.pad(labels, ((0, 0), (CROP, CROP), (CROP, CROP)), 'constant') restored_labels = np.zeros(read_info['shape'], dtype=np.float32) for z in range(N_CLASSES): roi = resize((labels == z + 1).astype(np.float32), read_info['shape'], mode='constant') roi[roi >= 0.5] = 1 roi[roi < 0.5] = 0 roi = clean_contour(roi, is_prob=False) restored_labels[roi == 1] = z + 1 else: labels = clean_contour(labels, is_prob=True) # Resize to extracted shape, then pad to original shape labels = resize(labels, read_info['extract_shape'], mode='constant') restored_labels = np.zeros(read_info['shape'], dtype=np.float32) extract = read_info['extract'] restored_labels[extract[0][0] : extract[0][1], extract[1][0] : extract[1][1], extract[2][0] : extract[2][1]] = labels return restored_labels
def plot_hist(baseline_samples, target_samples, true_x, true_y): baseline_samples = baseline_samples.squeeze() target_samples = target_samples.squeeze() bmin, bmax = baseline_samples.min(), baseline_samples.max() ax = sns.kdeplot(baseline_samples, shade=True, color=(0.6, 0.1, 0.1, 0.2)) ax = sns.kdeplot(target_samples, shade=True, color=(0.1, 0.1, 0.6, 0.2)) ax.set_xlim(bmin, bmax) y0, y1 = ax.get_ylim() plt.plot([true_y, true_y], [0, y1 - (y1 - y0) * 0.01], linewidth=1, color='r') plt.title('Predictive' + (f' at {true_x:.2f}' if true_x is not None else '')) fig = plt.gcf() fig.set_size_inches(9, 9) # plt.tight_layout() # pad=0.4, w_pad=0.5, h_pad=1.0) name = utils.DATA_DIR.replace('/', '-') # plt.tight_layout(pad=0.6) utils.save_fig('predictive-at-point-' + name)
def prepare_train_data(padding_size): ''' Read all the train data into numpy array and add padding_size of 0 paddings on each side of the image :param padding_size: int. how many layers of zero pads to add on each side? :return: all the train data and corresponding labels ''' path_list = [] for i in range(1, NUM_TRAIN_BATCH+1): path_list.append(full_data_dir + str(i)) data, label = read_in_all_images(path_list) pad_width = ((0, 0), (padding_size, padding_size), (padding_size, padding_size), (0, 0)) data = np.pad(data, pad_width=pad_width, mode='constant', constant_values=0) return data, label
def generateCountMaps(self, coords): '''Generates a count map for the provided list of coordinates. It can count at most 256 object within the receptive field. Beyond that it overflows. ''' s = self.config['receptive_field_size'] pad = s // 2 unpadded_size = self.config['tile_size'] target_size = 1 + unpadded_size + 2 * pad countMaps = np.zeros((self.config['cls_nb'], target_size, target_size), dtype=np.int16) y_min = 0 y_max = unpadded_size x_min = 0 x_max = unpadded_size for coord in coords: if coord[1] >= y_min and coord[1] < y_max and coord[2] >= x_min and coord[2] < x_max: self.inc_region(countMaps[coord[0]], coord[1] + pad, coord[2] + pad, s, s) return np.moveaxis(countMaps, 0, -1).astype(np.float32)
def load_ROI_mask(self): proxy = nib.load(self.FLAIR_FILE) image_array = np.asarray(proxy.dataobj) mask = np.ones_like(image_array) mask[np.where(image_array < 90)] = 0 # img = nib.Nifti1Image(mask, proxy.affine) # nib.save(img, join(modalities_path,'mask.nii.gz')) struct_element_size = (20, 20, 20) mask_augmented = np.pad(mask, [(21, 21), (21, 21), (21, 21)], 'constant', constant_values=(0, 0)) mask_augmented = binary_closing(mask_augmented, structure=np.ones(struct_element_size, dtype=bool)).astype( np.int) return mask_augmented[21:-21, 21:-21, 21:-21].astype('bool')
def resize_image(image,target_shape, pad_value = 0): assert isinstance(target_shape, list) or isinstance(target_shape, tuple) add_shape, subs_shape = [], [] image_shape = image.shape shape_difference = np.asarray(target_shape, dtype=int) - np.asarray(image_shape,dtype=int) for diff in shape_difference: if diff < 0: subs_shape.append(np.s_[int(np.abs(np.ceil(diff/2))):int(np.floor(diff/2))]) add_shape.append((0, 0)) else: subs_shape.append(np.s_[:]) add_shape.append((int(np.ceil(1.0*diff/2)),int(np.floor(1.0*diff/2)))) output = np.pad(image, tuple(add_shape), 'constant', constant_values=(pad_value, pad_value)) output = output[subs_shape] return output
def alignData(self, data): """ Align data to a multiple of the macro batch size, pad last incomplete minibatch with random samples :param data: data for alignment :return: padded data """ # pad with zeros to macro batch size, but only along dimension 0 ie samples topad = self.getNumSamplesPerMacroBatch() - data.shape[0] % self.getNumSamplesPerMacroBatch() sz = [] sz.append((0, topad)) for i in range(len(data.shape) - 1): sz.append((0, 0)) padded = numpy.pad(data, sz, mode='constant', constant_values=0) # fill last incomplete minibatch with random samples if (data.shape[0] % self.cfgParams.batch_size) != 0: # start from same random seed every time the data is padded, otherwise labels and data mix up rng = numpy.random.RandomState(data.shape[0]) for i in xrange(0, self.cfgParams.batch_size - (data.shape[0] % self.cfgParams.batch_size)): padded[data.shape[0]+i] = padded[rng.randint(0, data.shape[0])] return padded
def org_data(utt_feat, win_size_before, win_size_after): frm_num, feat_dim = utt_feat.shape width = win_size_before + win_size_after + 1 out_feat = np.zeros((frm_num, 1, feat_dim, width)) utt_feat = np.pad(utt_feat, ((win_size_before, win_size_after), (0,0)), mode = 'edge') # pad the starting and ending frames for i in range(frm_num): frm_idx = i + win_size_before block_data = utt_feat[frm_idx - win_size_before : frm_idx + win_size_after + 1, :] block_data = block_data.T block_data = block_data.reshape(1, block_data.shape[0], block_data.shape[1]) out_feat[i] = block_data return out_feat
def vis_square(data): """Take an array of shape (n, height, width) or (n, height, width, 3) and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)""" # normalize data for display data = (data - data.min()) / (data.max() - data.min()) # force the number of filters to be square n = int(np.ceil(np.sqrt(data.shape[0]))) padding = (((0, n ** 2 - data.shape[0]), (0, 1), (0, 1)) # add some space between filters + ((0, 0),) * (data.ndim - 3)) # don't pad the last dimension (if there is one) data = np.pad(data, padding, mode='constant', constant_values=1) # pad with ones (white) # tile the filters into an image data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) plt.imshow(data, interpolation='nearest'); plt.axis('off')
def crop_pad(image, corner, shape): ndim = len(corner) corner = [int(round(c)) for c in corner] shape = [int(round(s)) for s in shape] original = image.shape[-ndim:] zipped = zip(corner, shape, original) if np.any(c < 0 or c + s > o for (c, s, o) in zipped): no_padding = [(0, 0)] * (image.ndim - ndim) padding = [(max(-c, 0), max(c + s - o, 0)) for (c, s, o) in zipped] corner = [c + max(-c, 0) for c in corner] image_temp = np.pad(image, no_padding + padding, mode=str('constant')) else: image_temp = image no_crop = [slice(o+1) for o in image.shape[:-ndim]] crop = [slice(c, c+s) for (c, s) in zip(corner, shape)] return image_temp[no_crop + crop]
def pad2d(array, factor=1, value=0): ''' Symmetrically pads a 2D array with a value. Args: array (`numpy.ndarray`): source array. factor (`number`): number of widths of source array to add to each side (L/R/U/D). value (`number`): value with which to pad the array. Returns `numpy.ndarray`: padded array. ''' x, y = array.shape pad_shape = ((int(x * factor), int(x * factor)), (int(y * factor), int(y * factor))) return np.pad(array, pad_width=pad_shape, mode='constant', constant_values=value)
def preprocess_image(img_path, img_size): if not os.path.exists(img_path): print(img_path) return None, 0, 0 input_image = 255 * caffe.io.load_image(img_path) image = PILImage.fromarray(np.uint8(input_image)) image = np.array(image) mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3); preprocess_img = image[:,:,::-1] preprocess_img = preprocess_img - reshaped_mean_vec # Pad as necessary cur_h, cur_w, cur_c = preprocess_img.shape pad_h = img_size - cur_h pad_w = img_size - cur_w preprocess_img = np.pad(preprocess_img, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode = 'constant', constant_values = 0) return preprocess_img, cur_h, cur_w
def prepare_olapsequences(ms, vs, lsize, olap, bsize): from numpy.lib import stride_tricks global trimframe trimframe = ms.shape[0] % (lsize - olap) print(trimframe) if trimframe != 0: ms = np.pad(ms, ((0,trimframe), (0,0)), 'constant', constant_values=(0,0)) vs = np.pad(vs, ((0,trimframe), (0,0)), 'constant', constant_values=(0,0)) ms = stride_tricks.as_strided(ms, shape=(ms.shape[0] / (lsize - olap), lsize, ms.shape[1]), strides=(ms.strides[0] * (lsize - olap), ms.strides[0], ms.strides[1])) ms = ms[:-1, :, :] vs = stride_tricks.as_strided(vs, shape=(vs.shape[0] / (lsize - olap), lsize, vs.shape[1]), strides=(vs.strides[0] * (lsize - olap), vs.strides[0], vs.strides[1])) vs = vs[:-1, :, :] btrimframe = (ms.shape[0] % bsize) if btrimframe != 0: ms = ms[:-btrimframe, :, :] vs = vs[:-btrimframe, :, :] #print(ms.max(), ms.min(), vs.max(), vs.min()) #print(ms.shape, vs.shape) return ms, vs
def rel_crop(im, rel_cx, rel_cy, crop_size): map_size = im.shape[1] r = crop_size / 2 abs_cx = rel_cx * map_size abs_cy = rel_cy * map_size na = np.floor([abs_cy-r, abs_cy+r, abs_cx-r, abs_cx+r]).astype(np.int32) a = np.clip(na, 0, map_size) px0 = a[2] - na[2] px1 = na[3] - a[3] py0 = a[0] - na[0] py1 = na[1] - a[1] crop = im[a[0]:a[1], a[2]:a[3]] crop = np.pad(crop, ((py0, py1), (px0, px1), (0, 0)), mode='reflect') assert crop.shape == (crop_size, crop_size, im.shape[2]) return crop
def test_adjast_frame_length_divisible(): D = 5 T = 10 x = np.random.rand(T, D) assert T == adjast_frame_length(x, pad=True, divisible_by=1).shape[0] assert T == adjast_frame_length(x, pad=True, divisible_by=2).shape[0] print(adjast_frame_length(x, pad=True, divisible_by=3).shape[0]) assert T + 2 == adjast_frame_length(x, pad=True, divisible_by=3).shape[0] assert T + 2 == adjast_frame_length(x, pad=True, divisible_by=4).shape[0] assert T == adjast_frame_length(x, pad=False, divisible_by=1).shape[0] assert T == adjast_frame_length(x, pad=False, divisible_by=2).shape[0] assert T - 1 == adjast_frame_length(x, pad=False, divisible_by=3).shape[0] assert T - 2 == adjast_frame_length(x, pad=False, divisible_by=4).shape[0] # Should preserve dtype for dtype in [np.float32, np.float64]: x = np.random.rand(T, D).astype(dtype) assert x.dtype == adjast_frame_length(x, pad=True, divisible_by=3).dtype assert x.dtype == adjast_frame_length(x, pad=False, divisible_by=3).dtype
def prepare_binary_classification_data(real, fake): import numpy as np shape = real.shape[1:] both = np.concatenate((real, fake),axis=0) both = np.reshape(both, (len(both), -1)) # flatten data_dim = both.shape[1] both2 = np.pad(both, ((0,0),(0,1)), 'constant') # default 0 both2[:len(real),-1] = 1 # tag true np.random.shuffle(both2) train_in = np.reshape(both2[:int(0.9*len(both2)), :data_dim], (-1, *shape)) train_out = both2[:int(0.9*len(both2)), -1] test_in = np.reshape(both2[int(0.9*len(both2)):, :data_dim], (-1, *shape)) test_out = both2[int(0.9*len(both2)):, -1] return train_in, train_out, test_in, test_out
def imgread(img_path, scale = 4): img = scipy.misc.imread(img_path) img = img /256.0 h,w,c = img.shape tmp1 = h % scale new_h = h + scale - tmp1 tmp2 = w % scale new_w = w +scale-tmp2 img = np.pad(img, ((0,scale-tmp1), (0, scale-tmp2),(0,0)), mode = 'reflect') if scale != None: img = np.expand_dims(img,0) img = tf.convert_to_tensor(img) lr_w = new_w / scale lr_h = new_h /scale img = tf.cast(img, tf.float32) img_lr = tf.image.resize_images(img, [lr_h, lr_w]) img_lr = tf.cast(img_lr,tf.float32) return img_lr, img return img
def imgread(img_path, scale = 4): img = scipy.misc.imread(img_path) #img = scipy.misc.imresize(img, (128, 128)) img = img /256.0 h,w,c = img.shape new_h = pow(2, int(math.log(h, 2))+1) tmp1 = new_h - h new_w = pow(2, int(math.log(w, 2))+1) tmp2 = new_w - w img = np.pad(img, ((0,tmp1), (0, tmp2),(0,0)), mode = 'constant') if scale != None: img = np.expand_dims(img,0) img = tf.convert_to_tensor(img) lr_w = new_w / scale lr_h = new_h /scale img = tf.cast(img, tf.float32) img_lr = tf.image.resize_images(img, [lr_h, lr_w]) img_lr = tf.cast(img_lr,tf.float32) return img_lr, img return img
def update_grad_input(self, input, grad_output, scale=1): x_shape, x_cols = self.x_shape, self.x_cols w = self.weight stride, pad = self.dW, self.padW N, C, H, W = x_shape F, _, HH, WW = w.shape _, _, out_h, out_w = grad_output.shape self.grad_bias[:] = np.sum(grad_output, axis=(0, 2, 3))[:] dout_reshaped = grad_output.transpose(1, 0, 2, 3).reshape(F, -1) self.grad_weight[:] = dout_reshaped.dot(x_cols.T).reshape(w.shape)[:] dx_cols = w.reshape(F, -1).T.dot(dout_reshaped) #dx_cols.shape = (C, HH, WW, N, out_h, out_w) # dx = col2im_6d_cython(dx_cols, N, C, H, W, HH, WW, pad, stride) dx = col2im_cython(dx_cols, N, C, H, W, HH, WW, pad, stride) self.grad_input = dx return dx
def test_zero_padding_shortcuts(self): test = np.arange(120).reshape(4, 5, 6) pad_amt = [(0, 0) for axis in test.shape] modes = ['constant', 'edge', 'linear_ramp', 'maximum', 'mean', 'median', 'minimum', 'reflect', 'symmetric', 'wrap', ] for mode in modes: assert_array_equal(test, pad(test, pad_amt, mode=mode))
def test_check_mean_stat_length(self): a = np.arange(100).astype('f') a = pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), )) b = np.array( [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98., 98. ]) assert_array_equal(a, b)
def test_check_maximum_1(self): a = np.arange(100) a = pad(a, (25, 20), 'maximum') b = np.array( [99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99] ) assert_array_equal(a, b)
def test_check_maximum_stat_length(self): a = np.arange(100) + 1 a = pad(a, (25, 20), 'maximum', stat_length=10) b = np.array( [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100] ) assert_array_equal(a, b)
def test_check_minimum_1(self): a = np.arange(100) a = pad(a, (25, 20), 'minimum') b = np.array( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ) assert_array_equal(a, b)
def test_check_minimum_2(self): a = np.arange(100) + 2 a = pad(a, (25, 20), 'minimum') b = np.array( [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] ) assert_array_equal(a, b)
def test_check_minimum_stat_length(self): a = np.arange(100) + 1 a = pad(a, (25, 20), 'minimum', stat_length=10) b = np.array( [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91] ) assert_array_equal(a, b)
def test_check_median(self): a = np.arange(100).astype('f') a = pad(a, (25, 20), 'median') b = np.array( [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5] ) assert_array_equal(a, b)
def test_check_median_stat_length(self): a = np.arange(100).astype('f') a[1] = 2. a[97] = 96. a = pad(a, (25, 20), 'median', stat_length=(3, 5)) b = np.array( [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 2., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92., 93., 94., 95., 96., 96., 98., 99., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.] ) assert_array_equal(a, b)
def test_check_mean_shape_one(self): a = [[4, 5, 6]] a = pad(a, (5, 7), 'mean', stat_length=2) b = np.array( [[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6], [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]] ) assert_array_equal(a, b)
def test_check_mean_2(self): a = np.arange(100).astype('f') a = pad(a, (25, 20), 'mean') b = np.array( [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5] ) assert_array_equal(a, b)
def test_check_constant(self): a = np.arange(100) a = pad(a, (25, 20), 'constant', constant_values=(10, 20)) b = np.array( [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20] ) assert_array_equal(a, b)
def test_check_constant_float(self): # If input array is int, but constant_values are float, the dtype of # the array to be padded is kept arr = np.arange(30).reshape(5, 6) test = pad(arr, (1, 2), mode='constant', constant_values=1.1) expected = np.array( [[ 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 1, 0, 1, 2, 3, 4, 5, 1, 1], [ 1, 6, 7, 8, 9, 10, 11, 1, 1], [ 1, 12, 13, 14, 15, 16, 17, 1, 1], [ 1, 18, 19, 20, 21, 22, 23, 1, 1], [ 1, 24, 25, 26, 27, 28, 29, 1, 1], [ 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) assert_allclose(test, expected)
def test_check_constant_float2(self): # If input array is float, and constant_values are float, the dtype of # the array to be padded is kept - here retaining the float constants arr = np.arange(30).reshape(5, 6) arr_float = arr.astype(np.float64) test = pad(arr_float, ((1, 2), (1, 2)), mode='constant', constant_values=1.1) expected = np.array( [[ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1], [ 1.1, 0. , 1. , 2. , 3. , 4. , 5. , 1.1, 1.1], [ 1.1, 6. , 7. , 8. , 9. , 10. , 11. , 1.1, 1.1], [ 1.1, 12. , 13. , 14. , 15. , 16. , 17. , 1.1, 1.1], [ 1.1, 18. , 19. , 20. , 21. , 22. , 23. , 1.1, 1.1], [ 1.1, 24. , 25. , 26. , 27. , 28. , 29. , 1.1, 1.1], [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1], [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]] ) assert_allclose(test, expected)
def test_check_constant_float3(self): a = np.arange(100, dtype=float) a = pad(a, (25, 20), 'constant', constant_values=(-1.1, -1.2)) b = np.array( [-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2] ) assert_allclose(a, b)
def test_check_constant_odd_pad_amount(self): arr = np.arange(30).reshape(5, 6) test = pad(arr, ((1,), (2,)), mode='constant', constant_values=3) expected = np.array( [[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [ 3, 3, 0, 1, 2, 3, 4, 5, 3, 3], [ 3, 3, 6, 7, 8, 9, 10, 11, 3, 3], [ 3, 3, 12, 13, 14, 15, 16, 17, 3, 3], [ 3, 3, 18, 19, 20, 21, 22, 23, 3, 3], [ 3, 3, 24, 25, 26, 27, 28, 29, 3, 3], [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]] ) assert_allclose(test, expected)
def test_check_simple(self): a = np.arange(100).astype('f') a = pad(a, (25, 20), 'linear_ramp', end_values=(4, 5)) b = np.array( [4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56, 2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96, 0.80, 0.64, 0.48, 0.32, 0.16, 0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0, 47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.] ) assert_allclose(a, b, rtol=1e-5, atol=1e-5)
def test_check_simple(self): a = np.arange(100) a = pad(a, (25, 20), 'reflect') b = np.array( [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79] ) assert_array_equal(a, b)
def test_check_odd_method(self): a = np.arange(100) a = pad(a, (25, 20), 'reflect', reflect_type='odd') b = np.array( [-25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119] ) assert_array_equal(a, b)
def test_check_large_pad(self): a = [[4, 5, 6], [6, 7, 8]] a = pad(a, (5, 7), 'reflect') b = np.array( [[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]] ) assert_array_equal(a, b)
def test_check_shape(self): a = [[4, 5, 6]] a = pad(a, (5, 7), 'reflect') b = np.array( [[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5], [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]] ) assert_array_equal(a, b)
def test_check_simple(self): a = np.arange(100) a = pad(a, (25, 20), 'symmetric') b = np.array( [24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80] ) assert_array_equal(a, b)
def test_check_odd_method(self): a = np.arange(100) a = pad(a, (25, 20), 'symmetric', reflect_type='odd') b = np.array( [-24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118] ) assert_array_equal(a, b)
def test_check_large_pad(self): a = [[4, 5, 6], [6, 7, 8]] a = pad(a, (5, 7), 'symmetric') b = np.array( [[5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8], [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8], [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8], [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6], [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]] ) assert_array_equal(a, b)
def test_check_large_pad_odd(self): a = [[4, 5, 6], [6, 7, 8]] a = pad(a, (5, 7), 'symmetric', reflect_type='odd') b = np.array( [[-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6], [-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6], [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10], [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10], [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12], [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12], [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14], [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14], [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16], [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16], [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18], [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18]] ) assert_array_equal(a, b)
def test_check_simple(self): a = np.arange(100) a = pad(a, (25, 20), 'wrap') b = np.array( [75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] ) assert_array_equal(a, b)
def test_check_simple(self): a = np.arange(30) a = np.reshape(a, (6, 5)) a = pad(a, ((2, 3), (3, 2)), mode='mean', stat_length=(3,)) b = np.array( [[6, 6, 6, 5, 6, 7, 8, 9, 8, 8], [6, 6, 6, 5, 6, 7, 8, 9, 8, 8], [1, 1, 1, 0, 1, 2, 3, 4, 3, 3], [6, 6, 6, 5, 6, 7, 8, 9, 8, 8], [11, 11, 11, 10, 11, 12, 13, 14, 13, 13], [16, 16, 16, 15, 16, 17, 18, 19, 18, 18], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], [26, 26, 26, 25, 26, 27, 28, 29, 28, 28], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23]] ) assert_array_equal(a, b)
def test_check_simple(self): a = np.arange(12) a = np.reshape(a, (4, 3)) a = pad(a, ((2, 3), (3, 2)), 'edge') b = np.array( [[0, 0, 0, 0, 1, 2, 2, 2], [0, 0, 0, 0, 1, 2, 2, 2], [0, 0, 0, 0, 1, 2, 2, 2], [3, 3, 3, 3, 4, 5, 5, 5], [6, 6, 6, 6, 7, 8, 8, 8], [9, 9, 9, 9, 10, 11, 11, 11], [9, 9, 9, 9, 10, 11, 11, 11], [9, 9, 9, 9, 10, 11, 11, 11], [9, 9, 9, 9, 10, 11, 11, 11]] ) assert_array_equal(a, b)
def test_legacy_vector_functionality(self): def _padwithtens(vector, pad_width, iaxis, kwargs): vector[:pad_width[0]] = 10 vector[-pad_width[1]:] = 10 return vector a = np.arange(6).reshape(2, 3) a = pad(a, 2, _padwithtens) b = np.array( [[10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 0, 1, 2, 10, 10], [10, 10, 3, 4, 5, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10]] ) assert_array_equal(a, b)
def im2col_indices(x, field_height, field_width, padding=1, stride=1, precomputed_indices=None): """ An implementation of im2col based on some fancy indexing """ x_padded = None if padding > 0: # Zero-pad the input p = padding x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') else: x_padded = np.copy(x) if precomputed_indices is None: k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, stride) else: k, i, j = precomputed_indices cols = x_padded[k, i, j] return cols ######### End of External Code ##########
def init_worker(num_instances, kernel_h, kernel_w, pad, stride, indices, pdfs): global g_num_instances g_num_instances = num_instances global g_kernel_h g_kernel_h = kernel_h global g_kernel_w g_kernel_w = kernel_w global g_pad g_pad = pad global g_stride g_stride = stride global g_indices g_indices = indices global g_pdfs g_pdfs = pdfs signal.signal(signal.SIGINT, signal.SIG_IGN) np.random.seed(None)