def pad(self, value, pad_width, mode='constant', constant_values=0): dims = range(len(self.staticshape(value))) if isinstance(mode, six.string_types) and len( self.staticshape(constant_values)) == 0: return self._single_mode_single_constant_pad( value, pad_width, mode, constant_values) else: mode = expand(mode, shape=(len(dims), 2)) passes = [('wrap', 0), ('symmetric', 0), ('reflect', 0)] constant_values = expand(constant_values, shape=(len(dims), 2)) constant_value_set = set() for d in dims: for upper in (False, True): constant_value_set.add(constant_values[d][upper]) for const in constant_value_set: passes.append(('constant', const)) for single_mode, constant_value in passes: # order matters! wrap first widths = [[ collapsed_gather_nd(pad_width, [d, upper]) if mode[d][upper] == single_mode and constant_values[d][upper] == constant_value else 0 for upper in (False, True) ] for d in dims] value = self._single_mode_single_constant_pad( value, widths, single_mode, constant_value) return value
def pad(self, value, pad_width, mode='constant', constant_values=0): dims = range(len(self.shape(value))) constant_values = expand(constant_values, shape=(len(dims), 2)) if isinstance(mode, six.string_types): return self._single_mode_pad(value, pad_width, mode, constant_values) else: mode = expand(mode, shape=(len(dims), 2)) for single_mode in ('wrap', 'symmetric', 'reflect', 'constant'): # order matters! wrap first widths = [[collapsed_gather_nd(pad_width, [d, upper]) if mode[d][upper] == single_mode else 0 for upper in (False, True)] for d in dims] value = self._single_mode_pad(value, widths, single_mode, constant_values) return value
def test_expand(self): numpy.testing.assert_equal(expand(1, shape=(2, 2)), [[1, 1], [1, 1]]) numpy.testing.assert_equal(expand(['a', ('b', 'c')], shape=(2, 2)), [['a', 'a'], ['b', 'c']])