def check(j): func = FILTER1D_FUNCTIONS[j] im = np.tile(np.hstack((np.zeros(10), np.ones(10))), (10, 1)) filter_size = 3 res = ndimage.generic_filter1d(im, func(filter_size), filter_size) std = ndimage.generic_filter1d(im, filter1d, filter_size, extra_arguments=(filter_size,)) assert_allclose(res, std, err_msg="#{} failed".format(j))
def step(self, states): """ Apply the specified rule to the states. Parameters ---------- states : array An array holding the current states of the automata. Returns ------- states : array The new states of the automata after the rule has been applied. """ states = super(NDimRule, self).step(states) wrap_args = {'mode': self.boundary} if self.boundary == 'empty': wrap_args['mode'] = 'constant' wrap_args['cval'] = 0 elif self.boundary == 'filled': wrap_args['mode'] = 'constant' wrap_args['cval'] = 1 filled = (states == self.filled_state) filled = ndimage.generic_filter1d( filled, self._rule_filter, filter_size=3, **wrap_args) states = np.full(filled.shape, self.empty_state, dtype='uint8') states[filled] = self.filled_state return states
def test_generic_filter1d(): def filter1d(input_line, output_line, filter_size): for i in range(output_line.size): output_line[i] = 0 for j in range(filter_size): output_line[i] += input_line[i+j] output_line /= filter_size im = np.tile(np.hstack((np.zeros(10), np.ones(10))), (10, 1)) filter_size = 3 for mod in MODULES: res = ndimage.generic_filter1d(im, mod.filter1d(filter_size), filter_size) std = ndimage.generic_filter1d(im, filter1d, filter_size, extra_arguments=(filter_size,)) assert_allclose(res, std, err_msg="{} failed".format(mod.__name__))
def test_generic_filter1d(arr_dim, fsize): arr = np.random.random(arr_dim) axis = np.random.randint(0, arr.ndim - 1) fsize = arr_dim[axis] // fsize # just make sure it runs filtered = generic_filter1d(arr, jit_filter1d_function(fnc_zero_inplace), fsize, axis=axis) assert np.allclose(filtered, 0.0)
def max_except_center_filter1d(a, size, axis=-1): '''Applies the following digital filter to a {-1,0,1}-valued array a: If a is not 1, return 0; otherwise return the maximum over the window [-(size-1)/2, (size-1)/2] with the central element excluded.''' return ndimage.generic_filter1d(a, __max_except_center_1d, size, extra_arguments=(size, ), axis=axis, mode='constant', cval=0)
def max_except_center_filter1d(a, size, axis= -1): '''Applies the following digital filter to a {-1,0,1}-valued array a: If a is not 1, return 0; otherwise return the maximum over the window [-(size-1)/2, (size-1)/2] with the central element excluded.''' return ndimage.generic_filter1d(a, __max_except_center_1d, size, extra_arguments=(size,), axis=axis, mode='constant', cval=0)