def test_von_neuman_neighborhood(self): default = Neighborhood.von_neuman_neighborhood() self._are_lists_equal( default, [(-1, 0), (0, -1), (0, 1), (1, 0), (0, 0)] ) default_without_origin = Neighborhood.von_neuman_neighborhood(include_origin=False) self._are_lists_equal( default_without_origin, [(-1, 0), (0, -1), (0, 1), (1, 0)] ) neighborhood_3_2 = Neighborhood.von_neuman_neighborhood(3, 2) self._are_lists_equal( neighborhood_3_2, [ (-3, 0), (-2, -1), (-2, 0), (-2, 1), (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), (0, -3), (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), (0, 3), (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), (2, -1), (2, 0), (2, 1), (3, 0) ] ) neighborhood_1_3 = Neighborhood.von_neuman_neighborhood(1, 3) self._are_lists_equal( neighborhood_1_3, [(-1, 0, 0), (0, -1, 0), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0)] )
def test_compute_from_indices(self): coff = [ [1, 1, 4], [1, 4, 8], [2, 8, 16], ] n, c, h = Neighborhood.compute_from_indices(coff) self.assertEqual(len(n), 9) self.assertEqual(len(c), 9) self.assertEqual(h[0][0], 1, "halo in negative direction of dim 1 is 1") self.assertEqual(h[0][1], 1, "halo in negative direction of dim 1 is 1") self.assertEqual(h[1][0], 1, "halo in positive direction of dim 2 is 1") self.assertEqual(h[1][1], 1, "halo in positive direction of dim 2 is 1") self._are_lists_equal(n, Neighborhood.moore_neighborhood(1, 2)) coff = numpy.array(coff) # do this just so we can index with a tuple for index, value in enumerate(n): value = tuple([value[0] + 1, value[1] + 1]) # add one because indices have become offsets self.assertEqual(c[index], coff[value]) self._are_lists_equal(c, [1, 1, 4, 1, 4, 8, 2, 8, 16]) coff = [ [0, 1, 0], [1, 4, 8], [0, 8, 0], ] n, c, h = Neighborhood.compute_from_indices(coff) self.assertEqual(len(n), 5, "length is 5 because indices with coefficient zero are dropped") self.assertEqual(len(c), 5) self.assertEqual(h[0][0], 1, "halo in negative direction of dim 1 is 1") self.assertEqual(h[0][1], 1, "halo in negative direction of dim 1 is 1") self.assertEqual(h[1][0], 1, "halo in positive direction of dim 2 is 1") self.assertEqual(h[1][1], 1, "halo in positive direction of dim 2 is 1") coff = numpy.array(coff) # do this just so we can index with a tuple for index, value in enumerate(n): value = tuple([value[0] + 1, value[1] + 1]) # add one because indices have become offsets self.assertEqual(c[index], coff[value]) self._are_lists_equal(n, Neighborhood.von_neuman_neighborhood(1, 2))
def __init__(self, convolution_array=None, stride=1, backend='ocl'): self.convolution_array = convolution_array neighbors, coefficients, _ = \ Neighborhood.compute_from_indices(convolution_array) self.neighbor_to_coefficient = dict(zip(neighbors, coefficients)) self.coefficients = numpy.array(coefficients) self.stride = stride super(ConvolutionFilter, self).__init__( neighborhoods=[neighbors], backend=backend, boundary_handling='copy' )
def __init__(self, sigma_d=3, sigma_i=70, backend='ocl'): """ prepare the bilateral filter :param sigma_d: the smoothing associated with distance between points :param sigma_i: the smoothing factor associated with intensity difference between points :param backend: :return: """ self.sigma_d = sigma_d self.sigma_i = sigma_i self.radius = 3 * self.sigma_d self.distance_lut = BetterBilateralFilter.gaussian(self.sigma_d, self.radius*2) self.intensity_lut = BetterBilateralFilter.gaussian(self.sigma_i, 256) super(BetterBilateralFilter, self).__init__( neighborhoods=[Neighborhood.moore_neighborhood(radius=self.radius, dim=2)], backend=backend, should_unroll=False )
def test_with_kernel(self): for dimension in range(1, 7): for halo_size in range(1, 3): shape = [5 for _ in range(dimension)] halo = [halo_size for _ in range(dimension)] matrix = numpy.zeros(shape) elements = product(shape) dims = (range(0, dim) for dim in shape) all_indices = set([x for x in itertools.product(*dims)]) neighborhood = [ Neighborhood.moore_neighborhood(radius=halo_size, dim=dimension, include_origin=False) ] class Kernel(Stencil): neighborhoods = neighborhood def kernel(self): pass kernel = Kernel(backend='python', boundary_handling='zero') interior_set = set(list(kernel.interior_points(matrix))) halo_set = set(list(kernel.halo_points(matrix))) # print("halo_size {} dimension {} shape {} halo {}".format( # halo_size, dimension, shape, halo # )) # print("all indices {}".format(all_indices)) # print("halo_set {}".format(halo_set)) # print("interior_set {}".format(interior_set)) # print("intersection {}".format(interior_set.intersection(halo_set))) # print("outliers {}".format(halo_set.difference(all_indices))) self.assertTrue(len(interior_set.intersection(halo_set)) == 0) self.assertTrue(halo_set.issubset(all_indices)) self.assertTrue(interior_set.issubset(all_indices)) self.assertTrue(interior_set.union(halo_set) == all_indices)
def test_origin(self): self.assertEqual(tuple([0]), Neighborhood.origin_of_dim(1)) self.assertEqual((0, 0), Neighborhood.origin_of_dim(2)) self.assertEqual((0, 0, 0), Neighborhood.origin_of_dim(3)) self.assertEqual((0, 0, 0, 0), Neighborhood.origin_of_dim(4)) self.assertEqual((0, 0, 0, 0, 0), Neighborhood.origin_of_dim(5))
def __init__(self, radius=3, backend='ocl'): super(BilateralFilter, self).__init__( neighborhoods=[Neighborhood.moore_neighborhood(radius=radius, dim=2)], backend=backend, should_unroll=False )