def test_ignore_none_and_missing_properties(): 'gravmag.prism ignores None and prisms without the required property' inc, dec = 50, -30 model = [None, Prism(-6000, -2000, 2000, 4000, 0, 3000, {'density': 1000, 'magnetization': utils.ang2vec(10, inc, dec)}), Prism(2000, 6000, 2000, 4000, 0, 1000, {'magnetization': utils.ang2vec(15, inc, dec)}), None, Prism(-6000, -2000, -4000, -2000, 500, 2000, {'density': -1000})] area = [-10000, 10000, -5000, 5000] x, y, z = gridder.regular(area, (101, 51), z=-1) for mod in [prism, _prism_numpy]: # Test gravity functions funcs = ['potential', 'gx', 'gy', 'gz', 'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz'] for f in funcs: combined = getattr(mod, f)(x, y, z, model) separate = getattr(mod, f)(x, y, z, [model[1], model[4]]) precision = 10 assert_almost(separate, combined, precision, 'Field = %s' % (f)) # Test magnetic functions funcs = ['tf', 'bx', 'by', 'bz'] for f in funcs: mag_only = [model[1], model[2]] if f == 'tf': combined = getattr(mod, f)(x, y, z, model, inc, dec) separate = getattr(mod, f)(x, y, z, mag_only, inc, dec) else: combined = getattr(mod, f)(x, y, z, model) separate = getattr(mod, f)(x, y, z, mag_only) precision = 10 assert_almost(separate, combined, precision, 'Field = %s' % (f))
def test_pad_and_unpad_equal_1d(): 'gridder.pad_array and subsequent .unpad_array gives original array: 1D' prng = RandomState(12345) x = prng.rand(21) xpad, nps = gridder.pad_array(x) xunpad = gridder.unpad_array(xpad, nps) assert_almost(xunpad, x)
def test_base_K2_P8(self): adj_rad = [[ 0, 0.25 * PI, 0.5 * PI, 0.75 * PI, PI, 1.25 * PI, 1.5 * PI, 1.75 * PI, 2 * PI ]] adj_rad = sp.coo_matrix(adj_rad, dtype=np.float32) adj_rad = sparse_to_tensor(adj_rad) with self.test_session(): p0 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=0)) self.assertAllEqual(p0.eval(), [[0, 1, 0, 0, 0, 0, 0, 0, 0]]) p1 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=1)) self.assertAllEqual(p1.eval(), [[0, 0, 1, 0, 0, 0, 0, 0, 0]]) p2 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=2)) self.assertAllEqual(p2.eval(), [[0, 0, 0, 1, 0, 0, 0, 0, 0]]) p3 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=3)) assert_almost(p3.eval(), [[0, 0, 0, 0, 1, 0, 0, 0, 0]], 6) p4 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=4)) assert_almost(p4.eval(), [[0, 0, 0, 0, 0, 1, 0, 0, 0]], 6) p5 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=5)) assert_almost(p5.eval(), [[0, 0, 0, 0, 0, 0, 1, 0, 0]], 6) p6 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=6)) assert_almost(p6.eval(), [[0, 0, 0, 0, 0, 0, 0, 1, 0]], 6) p7 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=8, p=7)) assert_almost(p7.eval(), [[0, 0, 0, 0, 0, 0, 0, 0, 1]], 6)
def test_coordinatevec_padding_1d(): 'gridder.padcoords accurately pads coordinate vector in 1D' prng = RandomState(12345) f = prng.rand(72) * 10 x = np.arange(100, 172) fpad, nps = gridder.pad_array(f) N = gridder.pad_coords(x, f.shape, nps) assert_almost(N[0][nps[0][0]:-nps[0][1]], x)
def test_determinant_minimiser(): # Arrange a, b, c = 4, 3, 5 expected_det = 6.24859462836 sut = DeterminantMinimiser(2) # Act det = sut.calculate_determinant(a, b, c) # Assert assert_almost(det, expected_det, 7, "determinants are not equal")
def test_force_physical_property(): 'gravmag.prism gives correct results when passed a property value as arg' inc, dec = 10, 0 model = [ Prism(-6000, -2000, 2000, 4000, 0, 3000, { 'density': 1000, 'magnetization': utils.ang2vec(10, inc, dec) }), Prism(2000, 6000, 2000, 4000, 0, 1000, { 'density': -1000, 'magnetization': utils.ang2vec(15, inc, dec) }) ] density = -500 mag = utils.ang2vec(-5, -30, 15) reference = [ Prism(-6000, -2000, 2000, 4000, 0, 3000, { 'density': density, 'magnetization': mag }), Prism(2000, 6000, 2000, 4000, 0, 1000, { 'density': density, 'magnetization': mag }) ] area = [-10000, 10000, -5000, 5000] x, y, z = gridder.regular(area, (51, 101), z=-1) for mod in [prism, _prism_numpy]: # Test gravity functions funcs = [ 'potential', 'gx', 'gy', 'gz', 'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz' ] for f in funcs: forced = getattr(mod, f)(x, y, z, model, dens=density) ref = getattr(mod, f)(x, y, z, reference) precision = 10 assert_almost(forced, ref, precision, 'Field = %s' % (f)) # Test magnetic functions funcs = ['tf', 'bx', 'by', 'bz'] for f in funcs: if f == 'tf': forced = getattr(mod, f)(x, y, z, model, inc, dec, pmag=mag) ref = getattr(mod, f)(x, y, z, reference, inc, dec) else: forced = getattr(mod, f)(x, y, z, model, pmag=mag) ref = getattr(mod, f)(x, y, z, reference) precision = 10 assert_almost(forced, ref, precision, 'Field = %s' % (f))
def test_base_K2_P2(self): adj_rad = [[ 0, 0.25 * PI, 0.5 * PI, 0.75 * PI, PI, 1.25 * PI, 1.5 * PI, 1.75 * PI, 2 * PI ]] adj_rad = sp.coo_matrix(adj_rad, dtype=np.float32) adj_rad = sparse_to_tensor(adj_rad) with self.test_session(): p0 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=2, p=0)) assert_almost(p0.eval(), [[0, 0.25, 0.5, 0.75, 1, 0.75, 0.5, 0.25, 0]]) p1 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=2, p=1)) assert_almost(p1.eval(), [[0, 0.75, 0.5, 0.25, 0, 0.25, 0.5, 0.75, 1]])
def test_coordinatevec_padding_2d(): 'gridder.padcoords accurately pads coordinate vector in 2D' shape = (101, 172) x, y, z = gridder.regular((-5000., 5000., -5000., 5000.), shape, z=-150) gz = np.zeros(shape) xy = [] xy.append(x) xy.append(y) gpad, nps = gridder.pad_array(gz) N = gridder.pad_coords(xy, gz.shape, nps) Yp = N[1].reshape(gpad.shape) Xp = N[0].reshape(gpad.shape) assert N[0].reshape(gpad.shape).shape == gpad.shape assert_almost(Yp[nps[0][0]:-nps[0][1], nps[1][0]:-nps[1][1]].ravel(), y) assert_almost(Xp[nps[0][0]:-nps[0][1], nps[1][0]:-nps[1][1]].ravel(), x)
def test_pad_and_unpad_equal_2d(): 'gridder.pad_array and subsequent .unpad_array gives original array: 2D' shape = (100, 101) x, y, z = gridder.regular((-5000., 5000., -5000., 5000.), shape, z=-150) # rosenbrock: (a-x)^2 + b(y-x^2)^2 a=1 b=100 usually X = x.reshape(shape) Y = y.reshape(shape) xy = [x, y] gz = scipy.optimize.rosen([Y/100000., X/100000.]) pads = ['mean', 'edge', 'lintaper', 'reflection', 'oddreflection', 'oddreflectiontaper', '0'] for p in pads: gpad, nps = gridder.pad_array(gz, padtype=p) gunpad = gridder.unpad_array(gpad, nps) assert_almost(gunpad, gz)
def test_coordinatevec_padding_2d(): 'gridder.padcoords accurately pads coordinate vector in 2D' shape = (101, 172) x, y, z = gridder.regular((-5000., 5000., -5000., 5000.), shape, z=-150) gz = np.zeros(shape) xy = [] xy.append(x) xy.append(y) gpad, nps = gridder.pad_array(gz) N = gridder.pad_coords(xy, gz.shape, nps) Yp = N[1].reshape(gpad.shape) Xp = N[0].reshape(gpad.shape) assert_equal(N[0].reshape(gpad.shape).shape, gpad.shape) assert_almost(Yp[nps[0][0]:-nps[0][1], nps[1][0]:-nps[1][1]].ravel(), y) assert_almost(Xp[nps[0][0]:-nps[0][1], nps[1][0]:-nps[1][1]].ravel(), x)
def test_pad_and_unpad_equal_2d(): 'gridder.pad_array and subsequent .unpad_array gives original array: 2D' shape = (100, 101) x, y, z = gridder.regular((-5000., 5000., -5000., 5000.), shape, z=-150) # rosenbrock: (a-x)^2 + b(y-x^2)^2 a=1 b=100 usually X = x.reshape(shape) Y = y.reshape(shape) xy = [x, y] gz = scipy.optimize.rosen([Y / 100000., X / 100000.]) pads = [ 'mean', 'edge', 'lintaper', 'reflection', 'oddreflection', 'oddreflectiontaper', '0' ] for p in pads: gpad, nps = gridder.pad_array(gz, padtype=p) gunpad = gridder.unpad_array(gpad, nps) assert_almost(gunpad, gz)
def test_cython_agains_numpy(): "gravmag.prism numpy and cython implementations give same result" inc, dec = -30, 50 model = [ Prism(100, 300, -100, 100, 0, 400, { 'density': -1000, 'magnetization': utils.ang2vec(-2, inc, dec) }), Prism(-300, -100, -100, 100, 0, 200, { 'density': 2000, 'magnetization': utils.ang2vec(5, 25, -10) }) ] tmp = np.linspace(-500, 500, 101) xp, yp = [i.ravel() for i in np.meshgrid(tmp, tmp)] zp = -1 * np.ones_like(xp) kernels = ['xx', 'xy', 'xz', 'yy', 'yz', 'zz'] for comp in kernels: for p in model: py = getattr(_prism_numpy, 'kernel' + comp)(xp, yp, zp, p) cy = getattr(prism, 'kernel' + comp)(xp, yp, zp, p) assert_almost( py, cy, 10, 'Kernel = %s, max field %.15g max diff %.15g' % (comp, np.abs(cy).max(), np.abs(py - cy).max())) funcs = [ 'potential', 'gx', 'gy', 'gz', 'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz', 'bx', 'by', 'bz', 'tf' ] for f in funcs: if f == 'tf': py = getattr(_prism_numpy, f)(xp, yp, zp, model, inc, dec) cy = getattr(prism, f)(xp, yp, zp, model, inc, dec) else: py = getattr(_prism_numpy, f)(xp, yp, zp, model) cy = getattr(prism, f)(xp, yp, zp, model) if f in ['bx', 'by', 'bz', 'tf']: precision = 8 else: precision = 10 assert_almost( py, cy, precision, 'Field = %s, max field %.15g max diff %.15g' % (f, np.abs(cy).max(), np.abs(py - cy).max()))
def test_force_physical_property(): 'gravmag.prism gives correct results when passed a property value as arg' inc, dec = 10, 0 model = [Prism(-6000, -2000, 2000, 4000, 0, 3000, {'density': 1000, 'magnetization': utils.ang2vec(10, inc, dec)}), Prism(2000, 6000, 2000, 4000, 0, 1000, {'density': -1000, 'magnetization': utils.ang2vec(15, inc, dec)})] density = -500 mag = utils.ang2vec(-5, -30, 15) reference = [ Prism(-6000, -2000, 2000, 4000, 0, 3000, {'density': density, 'magnetization': mag}), Prism(2000, 6000, 2000, 4000, 0, 1000, {'density': density, 'magnetization': mag})] area = [-10000, 10000, -5000, 5000] x, y, z = gridder.regular(area, (51, 101), z=-1) for mod in [prism, _prism_numpy]: # Test gravity functions funcs = ['potential', 'gx', 'gy', 'gz', 'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz'] for f in funcs: forced = getattr(mod, f)(x, y, z, model, dens=density) ref = getattr(mod, f)(x, y, z, reference) precision = 10 assert_almost(forced, ref, precision, 'Field = %s' % (f)) # Test magnetic functions funcs = ['tf', 'bx', 'by', 'bz'] for f in funcs: if f == 'tf': forced = getattr(mod, f)(x, y, z, model, inc, dec, pmag=mag) ref = getattr(mod, f)(x, y, z, reference, inc, dec) else: forced = getattr(mod, f)(x, y, z, model, pmag=mag) ref = getattr(mod, f)(x, y, z, reference) precision = 10 assert_almost(forced, ref, precision, 'Field = %s' % (f))
def test_cython_agains_numpy(): "gravmag.prism numpy and cython implementations give same result" inc, dec = -30, 50 model = [ Prism(100, 300, -100, 100, 0, 400, {'density': -1000, 'magnetization': utils.ang2vec(-2, inc, dec)}), Prism(-300, -100, -100, 100, 0, 200, {'density': 2000, 'magnetization': utils.ang2vec(5, 25, -10)})] tmp = np.linspace(-500, 500, 101) xp, yp = [i.ravel() for i in np.meshgrid(tmp, tmp)] zp = -1 * np.ones_like(xp) kernels = ['xx', 'xy', 'xz', 'yy', 'yz', 'zz'] for comp in kernels: for p in model: py = getattr(_prism_numpy, 'kernel' + comp)(xp, yp, zp, p) cy = getattr(prism, 'kernel' + comp)(xp, yp, zp, p) assert_almost(py, cy, 10, 'Kernel = %s, max field %.15g max diff %.15g' % (comp, np.abs(cy).max(), np.abs(py - cy).max())) funcs = ['potential', 'gx', 'gy', 'gz', 'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz', 'bx', 'by', 'bz', 'tf'] for f in funcs: if f == 'tf': py = getattr(_prism_numpy, f)(xp, yp, zp, model, inc, dec) cy = getattr(prism, f)(xp, yp, zp, model, inc, dec) else: py = getattr(_prism_numpy, f)(xp, yp, zp, model) cy = getattr(prism, f)(xp, yp, zp, model) if f in ['bx', 'by', 'bz', 'tf']: precision = 8 else: precision = 10 assert_almost(py, cy, precision, 'Field = %s, max field %.15g max diff %.15g' % (f, np.abs(cy).max(), np.abs(py - cy).max()))
def test_base_K2_P4(self): adj_rad = [[ 0, 0.25 * PI, 0.5 * PI, 0.75 * PI, PI, 1.25 * PI, 1.5 * PI, 1.75 * PI, 2 * PI ]] adj_rad = sp.coo_matrix(adj_rad, dtype=np.float32) adj_rad = sparse_to_tensor(adj_rad) with self.test_session(): p0P4 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=4, p=0)) self.assertAllEqual(p0P4.eval(), [[0, 0.5, 1, 0.5, 0, 0, 0, 0, 0]]) p1P4 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=4, p=1)) assert_almost(p1P4.eval(), [[0, 0, 0, 0.5, 1, 0.5, 0, 0, 0]], 6) p2P4 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=4, p=2)) assert_almost(p2P4.eval(), [[0, 0, 0, 0, 0, 0.5, 1, 0.5, 0]], 6) p3P4 = tf.sparse_tensor_to_dense(base(adj_rad, K=2, P=4, p=3)) assert_almost(p3P4.eval(), [[0, 0.5, 0, 0, 0, 0, 0, 0.5, 1]], 6)
def test_around(): "gravmag.prism gravitational results are consistent around the prism" funcs = ['potential', 'gx', 'gy', 'gz', 'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz'] model = [Prism(-300, 300, -300, 300, -300, 300, {'density': 1000})] # Make the computation points surround the prism shape = (101, 101) area = [-600, 600, -600, 600] distance = 310 grids = [gridder.regular(area, shape, z=-distance), gridder.regular(area, shape, z=distance), gridder.regular(area, shape, z=distance)[::-1], gridder.regular(area, shape, z=-distance)[::-1], np.array(gridder.regular(area, shape, z=distance))[[0, 2, 1]], np.array(gridder.regular(area, shape, z=-distance))[[0, 2, 1]]] xp, yp, zp = grids[0] # Test if each component is consistent # POTENTIAL face = [prism.potential(x, y, z, model) for x, y, z in grids] for i in range(6): for j in range(i + 1, 6): assert_almost(face[i], face[j], 10, 'Failed potential, faces %d and %d' % (i, j)) # GX top, bottom, north, south, east, west = [prism.gx(x, y, z, model) for x, y, z in grids] assert_almost(top, bottom, 10, 'Failed gx, top and bottom') assert_almost(north, -south, 10, 'Failed gx, north and south') assert_almost(east, west, 10, 'Failed gx, east and west') assert_almost(east, top, 10, 'Failed gx, east and top') assert_almost(north, -prism.gz(xp, yp, zp, model), 10, 'Failed gx, north and gz') assert_almost(south, prism.gz(xp, yp, zp, model), 10, 'Failed gx, south and gz') # GY top, bottom, north, south, east, west = [prism.gy(x, y, z, model) for x, y, z in grids] assert_almost(top, bottom, 10, 'Failed gy, top and bottom') assert_almost(north, south, 10, 'Failed gy, north and south') assert_almost(east, -west, 10, 'Failed gy, east and west') assert_almost(north, top, 10, 'Failed gy, north and top') assert_almost(east, -prism.gz(xp, yp, zp, model), 10, 'Failed gy, east and gz') assert_almost(west, prism.gz(xp, yp, zp, model), 10, 'Failed gy, west and gz') # GZ top, bottom, north, south, east, west = [prism.gz(x, y, z, model) for x, y, z in grids] assert_almost(top, -bottom, 10, 'Failed gz, top and bottom') assert_almost(north, south, 10, 'Failed gz, north and south') assert_almost(east, west, 10, 'Failed gz, east and west') assert_almost(north, prism.gx(xp, yp, zp, model), 10, 'Failed gz, north and gx') assert_almost(south, prism.gx(xp, yp, zp, model), 10, 'Failed gz, south and gx') assert_almost(east, prism.gy(xp, yp, zp, model), 10, 'Failed gz, east and gy') assert_almost(west, prism.gy(xp, yp, zp, model), 10, 'Failed gz, west and gy') # GXX top, bottom, north, south, east, west = [prism.gxx(x, y, z, model) for x, y, z in grids] assert_almost(top, bottom, 10, 'Failed gxx, top and bottom') assert_almost(north, south, 10, 'Failed gxx, north and south') assert_almost(east, west, 10, 'Failed gxx, east and west') assert_almost(east, top, 10, 'Failed gxx, east and top') assert_almost(north, prism.gzz(xp, yp, zp, model), 10, 'Failed gxx, north and gzz') assert_almost(south, prism.gzz(xp, yp, zp, model), 10, 'Failed gxx, south and gzz') # GXY top, bottom, north, south, east, west = [prism.gxy(x, y, z, model) for x, y, z in grids] assert_almost(top, bottom, 4, 'Failed gxy, top and bottom') assert_almost(north, -south, 10, 'Failed gxy, north and south') assert_almost(east, -west, 10, 'Failed gxy, east and west') assert_almost(north, -prism.gyz(xp, yp, zp, model), 10, 'Failed gxy, north and gyz') assert_almost(south, prism.gyz(xp, yp, zp, model), 10, 'Failed gxy, south and gyz') # GXZ top, bottom, north, south, east, west = [prism.gxz(x, y, z, model) for x, y, z in grids] assert_almost(top, -bottom, 10, 'Failed gxz, top and bottom') assert_almost(north, -south, 10, 'Failed gxz, north and south') assert_almost(east, west, 4, 'Failed gxz, east and west') assert_almost(bottom, north, 10, 'Failed gxz, bottom and north') assert_almost(top, south, 10, 'Failed gxz, top and south') assert_almost(east, prism.gxy(xp, yp, zp, model), 4, 'Failed gxz, east and gxy') assert_almost(west, prism.gxy(xp, yp, zp, model), 10, 'Failed gxz, west and gxy') # GYY top, bottom, north, south, east, west = [prism.gyy(x, y, z, model) for x, y, z in grids] assert_almost(top, bottom, 10, 'Failed gyy, top and bottom') assert_almost(north, south, 10, 'Failed gyy, north and south') assert_almost(east, west, 10, 'Failed gyy, east and west') assert_almost(top, north, 10, 'Failed gyy, top and north') assert_almost(east, prism.gzz(xp, yp, zp, model), 10, 'Failed gyy, east and gzz') assert_almost(west, prism.gzz(xp, yp, zp, model), 10, 'Failed gyy, west and gzz') # GYZ top, bottom, north, south, east, west = [prism.gyz(x, y, z, model) for x, y, z in grids] assert_almost(top, -bottom, 10, 'Failed gyz, top and bottom') assert_almost(north, south, 4, 'Failed gyz, north and south') assert_almost(east, -west, 10, 'Failed gyz, east and west') assert_almost(top, west, 10, 'Failed gyz, top and west') assert_almost(bottom, east, 10, 'Failed gyz, bottom and east') assert_almost(north, prism.gxy(xp, yp, zp, model), 4, 'Failed gyz, north and gxy') assert_almost(south, prism.gxy(xp, yp, zp, model), 10, 'Failed gyz, south and gxy') # GZZ top, bottom, north, south, east, west = [prism.gzz(x, y, z, model) for x, y, z in grids] assert_almost(top, bottom, 10, 'Failed gzz, top and bottom') assert_almost(north, south, 10, 'Failed gzz, north and south') assert_almost(east, west, 10, 'Failed gzz, east and west') assert_almost(north, prism.gxx(xp, yp, zp, model), 10, 'Failed gzz, north and gxx') assert_almost(south, prism.gxx(xp, yp, zp, model), 10, 'Failed gzz, south and gxx') assert_almost(east, prism.gyy(xp, yp, zp, model), 10, 'Failed gzz, east and gyy') assert_almost(west, prism.gyy(xp, yp, zp, model), 10, 'Failed gzz, west and gyy')