def test_sph_harm_isft(): """ Tests the SFT by doing ISFT -> SFT -> ISFT. """ for n in [8, 16]: # sft for real signals: c_{-m} = (-1)^m Re(c_m) + (-1)^{m+1} Im(c_m) coeffs = [np.zeros(2 * l + 1, dtype=np.complex) for l in range(n // 2)] coeffs[0][0], coeffs[1][1], coeffs[2][2] = [ np.random.rand() for _ in range(3) ] c = np.random.rand() + 1j * np.random.rand() coeffs[1][0] = c coeffs[1][2] = -np.real(c) + 1j * np.imag(c) c = np.random.rand() + 1j * np.random.rand() coeffs[2][0] = c coeffs[2][4] = np.real(c) - 1j * np.imag(c) c = np.random.rand() + 1j * np.random.rand() coeffs[2][1] = c coeffs[2][3] = -np.real(c) + 1j * np.imag(c) f = spherical.sph_harm_inverse(coeffs) coeffs_ = spherical.sph_harm_transform(f) f_ = spherical.sph_harm_inverse(coeffs_) for c1, c2 in zip(coeffs, coeffs_): assert np.allclose(c1, c2) assert np.allclose(f, f_)
def test_sph_harm_sft(): """ Tests the SFT by doing SFT -> ISFT -> SFT -> ISFT """ f = np.random.rand(16, 16) coeffs = spherical.sph_harm_transform(f) # we are constraining the bandwidth to n/2 here, so f1 != f f1 = spherical.sph_harm_inverse(coeffs) coeffs1 = spherical.sph_harm_transform(f1) f2 = spherical.sph_harm_inverse(coeffs1) # now both f1 and f2 have constrained bandwidths, so they must be equal assert np.allclose(f1, f2)
def test_sph_conv(): """ Test spherical convolution and rotation commutativity. sph_conv and sphrot_shtools are exercised here. """ if 'pyshtools' not in sys.modules: warnings.warn('pyshtools not available; skipping test_sph_conv') return n = 32 f = np.random.rand(n, n) # lowpass f = spherical.sph_harm_inverse(spherical.sph_harm_transform(f)) g = np.zeros_like(f) g[:, :5] = np.random.rand(5) g /= g.sum() ang = np.random.rand(3) * 2 * np.pi # check if (pi f * g) == pi(f * g) rot_conv = util.sphrot_shtools(spherical.sph_conv(f, g), ang) conv_rot = spherical.sph_conv(util.sphrot_shtools(f, ang), g) assert not np.allclose(rot_conv, f) assert not np.allclose(rot_conv, g) assert np.allclose(rot_conv, conv_rot)
def test_sph_harm_tf(): """ Test spherical harmonics expansion/inversion with tensorflow Tensors. """ n = 32 f = np.random.rand(n, n) ref = spherical.sph_harm_inverse(spherical.sph_harm_transform(f)) inp = tf.placeholder('complex128', shape=[n, n]) coeffs = spherical.sph_harm_transform(inp) recons = spherical.sph_harm_inverse(coeffs) with tf.Session(config=tf_config()).as_default() as sess: c, r = sess.run([coeffs, recons], feed_dict={inp: f}) assert np.allclose(r, ref) for x1, x2 in zip(c, spherical.sph_harm_transform(f)): assert np.allclose(x1, x2)
def test_sph_harm_tf_harmonics_input(): """ Test spherical harmonics inputs as tensorflow Variables. """ n = 32 f = np.random.rand(n, n) for real in [False, True]: dtype = tf.complex64 harmonics = [[tf.Variable(hh.astype('complex64')) for hh in h] for h in spherical.sph_harm_all(n, real=real)] inp = tf.placeholder(dtype, shape=[n, n]) c1 = spherical.sph_harm_transform(inp, harmonics=harmonics) r1 = spherical.sph_harm_inverse(c1, harmonics=harmonics) c2 = spherical.sph_harm_transform(inp, harmonics=harmonics) r2 = spherical.sph_harm_inverse(c2, harmonics=harmonics) with tf.Session(config=tf_config()).as_default() as sess: sess.run(tf.global_variables_initializer()) c1v, c2v, r1v, r2v = sess.run([c1, c2, r1, r2], feed_dict={inp: f}) for x1, x2 in zip(c1v, c2v): assert np.allclose(x1, x2) for x1, x2 in zip(r1v, r2v): assert np.allclose(x1, x2)
def test_sph_harm_shtools(): """ Compare our sph harmonics expansion with pyshtools. """ if 'pyshtools' not in sys.modules: warnings.warn( 'pyshtools not available; skipping test_sph_harm_shtools') return n = 32 f = np.random.rand(n, n) # lowpass f = spherical.sph_harm_inverse(spherical.sph_harm_transform(f)) c_mine = spherical.sph_harm_transform(f) c_pysh = pyshtools.SHGrid.from_array(f.T).expand(csphase=-1, normalization='ortho') c1 = spherical.sph_harm_to_shtools(c_mine) c2 = c_pysh.coeffs # there seems to be a bug on the coefficient of highes degree l, order -l in pyshtools # we don't test that value c1[1][(n // 2) - 1][-1] = c2[1][(n // 2) - 1][-1] = 0 assert np.allclose(c1, c2)