def test_fourier_laplace_2d_periodic(self): """test for convergence of the laplace operator""" test_params = { 'size': [16, 32, 40], 'L': [1, 2, 3], # NOTE: Cannot test with less than 1 full wavelength } test_cases = [ dict(zip(test_params, v)) for v in product(*test_params.values()) ] for params in test_cases: vec = math.meshgrid(x=params['size'], y=params['size']) sine_field = math.prod( math.sin(2 * PI * params['L'] * vec / params['size'] + 1), 'vector') sin_lap_ref = -2 * ( 2 * PI * params['L'] / params['size'] )**2 * sine_field # leading 2 from from x-y cross terms sin_lap = math.fourier_laplace(sine_field, 1) try: math.assert_close(sin_lap, sin_lap_ref, rel_tolerance=0, abs_tolerance=1e-5) except BaseException as e: abs_error = math.abs(sin_lap - sin_lap_ref) max_abs_error = math.max(abs_error) max_rel_error = math.max(math.abs(abs_error / sin_lap_ref)) variation_str = "\n".join([ f"max_absolute_error: {max_abs_error}", f"max_relative_error: {max_rel_error}", ]) print(f"{variation_str}\n{params}") raise AssertionError(e, f"{variation_str}\n{params}")
def test_pad_tensor(self): for backend in BACKENDS: with backend: a = math.meshgrid(x=4, y=3) # 0 p = math.pad(a, {'x': (1, 2), 'y': (0, 1)}, ZERO) self.assertEqual((7, 4, 2), p.shape.sizes) # dimension check math.assert_close(p.x[1:-2].y[:-1], a) # copy inner math.assert_close(p.x[0], 0) # 1 p = math.pad(a, {'x': (1, 2), 'y': (0, 1)}, ONE) self.assertEqual((7, 4, 2), p.shape.sizes) # dimension check math.assert_close(p.x[1:-2].y[:-1], a) # copy inner math.assert_close(p.x[0], 1) # periodic p = math.pad(a, {'x': (1, 2), 'y': (0, 1)}, PERIODIC) self.assertEqual((7, 4, 2), p.shape.sizes) # dimension check math.assert_close(p.x[1:-2].y[:-1], a) # copy inner math.assert_close(p.x[0].y[:-1], a.x[-1]) math.assert_close(p.x[-2:].y[:-1], a.x[:2]) # boundary p = math.pad(a, {'x': (1, 2), 'y': (0, 1)}, BOUNDARY) self.assertEqual((7, 4, 2), p.shape.sizes) # dimension check math.assert_close(p.x[1:-2].y[:-1], a) # copy inner math.assert_close(p.x[0].y[:-1], a.x[0]) math.assert_close(p.x[-2:].y[:-1], a.x[-1]) # mixed p = math.pad(a, {'x': (1, 2), 'y': (0, 1)}, combine_sides({'x': PERIODIC, 'y': (ONE, REFLECT)})) math.print(p) self.assertEqual((7, 4, 2), p.shape.sizes) # dimension check math.assert_close(p.x[1:-2].y[:-1], a) # copy inner math.assert_close(p.x[0].y[:-1], a.x[-1]) # periodic math.assert_close(p.x[-2:].y[:-1], a.x[:2]) # periodic
def test_gradient_vector(self): meshgrid = math.meshgrid(x=4, y=3) cases = dict(difference=('central', 'forward', 'backward'), padding=(None, extrapolation.ONE, extrapolation.BOUNDARY, extrapolation.PERIODIC, extrapolation.SYMMETRIC), dx=(0.1, 1), dims=( None, ('x', 'y'), )) for case_dict in [ dict(zip(cases, v)) for v in product(*cases.values()) ]: grad = math.gradient(meshgrid, **case_dict) inner = grad.x[1:-1].y[1:-1] math.assert_close(inner.spatial_gradient[0].vector[1], 0) math.assert_close(inner.spatial_gradient[1].vector[0], 0) math.assert_close(inner.spatial_gradient[0].vector[0], 1 / case_dict['dx']) math.assert_close(inner.spatial_gradient[1].vector[1], 1 / case_dict['dx']) self.assertEqual(grad.shape.vector, 2) self.assertEqual(grad.shape.spatial_gradient, 2) ref_shape = (4, 3) if case_dict['padding'] is not None else (( 2, 1) if case_dict['difference'] == 'central' else (3, 2)) self.assertEqual((grad.shape.x, grad.shape.y), ref_shape)
def test_downsample2x(self): meshgrid = math.meshgrid(x=(0, 1, 2, 3), y=(0, -1, -2)) half_size = math.downsample2x(meshgrid, extrapolation.BOUNDARY) math.print(meshgrid, 'Full size') math.print(half_size, 'Half size') math.assert_close(half_size.vector[0], wrap([[0.5, 2.5], [0.5, 2.5]], spatial('y,x'))) math.assert_close(half_size.vector[1], wrap([[-0.5, -0.5], [-2, -2]], spatial('y,x')))
def test_vector_laplace(self): meshgrid = math.meshgrid(x=(0, 1, 2, 3), y=(0, -1)) cases = dict(padding=(extrapolation.ZERO, extrapolation.ONE, extrapolation.BOUNDARY, extrapolation.PERIODIC, extrapolation.SYMMETRIC), dx=(0.1, 1), dims=(None, ('x',), ('y',), ('x', 'y'))) for case_dict in [dict(zip(cases, v)) for v in product(*cases.values())]: laplace = math.laplace(meshgrid, **case_dict)
def test_grid_sample(self): for backend in BACKENDS: with backend: grid = math.sum(math.meshgrid(x=[1, 2, 3], y=[0, 3]), 'vector') # 1 2 3 | 4 5 6 coords = math.tensor([(0, 0), (0.5, 0), (0, 0.5), (-2, -1)], instance('list'), channel('vector')) interp = math.grid_sample(grid, coords, extrapolation.ZERO) math.assert_close(interp, [1, 1.5, 2.5, 0], msg=backend.name)
def test_upsample2x(self): meshgrid = math.meshgrid(x=(0, 1, 2, 3), y=(0, -1, -2)) double_size = math.upsample2x(meshgrid, extrapolation.BOUNDARY) same_size = math.downsample2x(double_size) math.print(meshgrid, 'Normal size') math.print(double_size, 'Double size') math.print(same_size, 'Same size') math.assert_close(meshgrid.x[1:-1].y[1:-1], same_size.x[1:-1].y[1:-1])
def center(self): local_coords = math.meshgrid( **{ dim: math.linspace(0.5 / size, 1 - 0.5 / size, size) for dim, size in zip(self.resolution.names, self.resolution.sizes) }) points = self.bounds.local_to_global(local_coords) return points
def test_grid_sample(self): for backend in (math.NUMPY_BACKEND, tf.TF_BACKEND, torch.TORCH_BACKEND): with backend: grid = math.sum(math.meshgrid(x=[1, 2, 3], y=[0, 3]), 'vector') # 1 2 3 | 4 5 6 coords = math.tensor([(0, 0), (0.5, 0), (0, 0.5), (-2, -1)], names=('list', 'vector')) interp = math.grid_sample(grid, coords, extrapolation.ZERO) math.assert_close(interp, [1, 1.5, 2.5, 0])
def test_reduction_properties(self): for backend in BACKENDS: with backend: t = math.meshgrid(x=2, y=2) self.assertEqual(0.5, t.mean) self.assertEqual(0.5, t.std) self.assertEqual(1, t.max) self.assertEqual(0, t.min) self.assertEqual(4, t.sum) self.assertEqual(False, t.all) self.assertEqual(True, t.any)
def test_fourier_laplace_2d_periodic(self): """test for convergence of the laplace operator""" test_params = { 'size': [16, 32, 40], 'L': [1, 2, 3], # NOTE: Cannot test with less than 1 full wavelength } test_cases = [dict(zip(test_params, v)) for v in product(*test_params.values())] for params in test_cases: vec = math.meshgrid(x=params['size'], y=params['size']) sine_field = math.prod(math.sin(2 * PI * params['L'] * vec / params['size'] + 1), 'vector') sin_lap_ref = - 2 * (2 * PI * params['L'] / params['size']) ** 2 * sine_field # leading 2 from from x-y cross terms sin_lap = math.fourier_laplace(sine_field, 1) # try: math.assert_close(sin_lap, sin_lap_ref, rel_tolerance=0, abs_tolerance=1e-5)
def center(self): """ Center point of the box or batch of boxes. The shape of the location extends the shape of the Box instance by a `vector` dimension. :return: Tensor describing the center location(s) Args: Returns: """ local_coords = math.meshgrid( **{ dim: math.linspace(0.5 / size, 1 - 0.5 / size, size) for dim, size in self.resolution.named_sizes }) points = self.bounds.local_to_global(local_coords) return points
def test_slice_by_int_tensor(self): indices = math.meshgrid(x=2, y=2) sel = indices.x[wrap((1, 0), spatial('x'))] math.assert_close((1, 0), sel.vector['x'].y[0])
def test_slice_by_bool_tensor(self): indices = math.meshgrid(x=2, y=2) sel = indices.x[wrap((True, False), spatial('x'))] self.assertEqual(1, sel.x.size)
def test_pad_negative(self): a = math.meshgrid(x=4, y=3) p = math.pad(a, {'x': (-1, -1), 'y': (1, -1)}, ZERO) math.assert_close(p.y[1:], a.x[1:-1].y[:-1])