def setup_method(self, test_method): # Initialize arrays self.eps = bops.precision(dtype) * np.prod(shape) self.x_np_ones = bops.ones(shape, dtype, 'numpy') self.x_ocl_ones = bops.ones(shape, dtype, 'arrayfire') self.x_ocl_randn = bops.randn(shape, dtype, 'arrayfire') self.x_np_randn = np.asarray(self.x_ocl_randn) self.x_ocl_randu = bops.randu(shape, dtype, 'arrayfire') self.x_np_randu = np.asarray(self.x_ocl_randu) # Create random matricies self.A_ocl = bops.randn((10, 10), backend='arrayfire') self.A_np = np.asarray(self.A_ocl) self.sz = (10, 20) self.x_np_rect = bops.randn(self.sz, backend='numpy') assert bops.shape(self.x_np_rect) == self.sz self.x_ocl_rect = bops.randn(self.sz, backend='arrayfire') assert bops.shape(self.x_ocl_rect) == self.sz # Load object and crop to size brain = imageio.imread(object_file_name) x_0 = sp.misc.imresize( brain, size=image_size)[:, :, object_color_channel].astype( bops.getNativeDatatype(dtype, 'numpy')) / 255. # Convert object to desired backend self.x = bops.changeBackend(x_0, 'arrayfire')
def test_fill(self): x_ocl_tofill = bops.randn(shape, dtype, 'arrayfire') x_np_tofill = np.asarray(x_ocl_tofill) fill_value = 10 bops.fill(x_np_tofill, fill_value) bops.fill(x_ocl_tofill, fill_value) assert np.sum(np.abs(np.asarray(x_ocl_tofill) - fill_value)) < 1e-4 assert np.sum(np.abs(np.asarray(x_np_tofill) - fill_value)) < 1e-4
def setup_method(self, test_method): # Load object and crop to size x_0 = yp.rand(image_size) # Convert object to desired backend self.x = yp.changeBackend(x_0, global_backend) # Generate convolution kernel h h_size = np.array([4, 4]) self.h = yp.zeros(image_size, global_dtype, global_backend) self.h[image_size[0] // 2 - h_size[0] // 2:image_size[0] // 2 + h_size[0] // 2, image_size[1] // 2 - h_size[1] // 2:image_size[1] // 2 + h_size[1] // 2] = yp.randn((h_size[0], h_size[1]), global_dtype, global_backend) # A = ops.Convolution(image_size, h, dtype=global_dtype, fft_backend='numpy', backend=global_backend) self.A = ops.FourierTransform(image_size, dtype=global_dtype, backend=global_backend, center=True) self.y = self.A(yp.vectorize(self.x))
def test_indexing(self): q = bops.randn((10, 10), backend='numpy', dtype='complex32') q_ocl = bops.changeBackend(q, 'arrayfire') q_ocl_np = bops.changeBackend(q_ocl, 'numpy') assert np.sum(np.abs(q - q_ocl_np)) < eps m = bops.rand((10, 20), dtype, "numpy") m_ocl = bops.changeBackend(m, 'arrayfire') assert abs(m[0, 1] - bops.scalar(m_ocl[0, 1])) < eps assert abs(m[1, 1] - bops.scalar(m_ocl[1, 1])) < eps assert abs(bops.scalar(m_ocl[4, 1]) - m[4, 1]) < eps assert bops.scalar(self.x_np_rect[5, 15]) == bops.scalar( bops.changeBackend(self.x_np_rect, 'arrayfire')[5, 15]) assert bops.scalar(self.x_ocl_rect[5, 15]) == bops.scalar( bops.changeBackend(self.x_ocl_rect, 'numpy')[5, 15])
def add(signal, type='gaussian', **kwargs): """ Add noise to a measurement""" if type == 'gaussian': snr = kwargs.get('snr', 1.0) signal_mean = yp.abs(yp.mean(signal)) noise_gaussian = np.random.normal(0, signal_mean / snr, yp.shape(signal)) return signal + noise_gaussian elif type == 'poisson': noise_poisson = np.random.poisson(np.real(signal)) return signal + noise_poisson elif type == 'saltpepper' or type == 'saltandpepper': salt_pepper_ratio = kwargs.get('ratio', 0.5) salt_pepper_saturation = kwargs.get('saturation', 0.004) output = yp.changeBackend(signal, 'numpy') # Salt mode num_salt = np.ceil(salt_pepper_saturation * signal.size * salt_pepper_ratio) coords = [ np.random.randint(0, i - 1, int(num_salt)) for i in signal.shape ] output[tuple(coords)] = 1 # Pepper mode num_pepper = np.ceil(salt_pepper_saturation * signal.size * (1. - salt_pepper_ratio)) coords = [ np.random.randint(0, i - 1, int(num_pepper)) for i in signal.shape ] output[tuple(coords)] = 0 return yp.cast_like(output, signal) elif type == 'speckle': noise_speckle = yp.randn(signal.shape) return signal + signal * noise_speckle