def test_half_bt24cell_geom(): cellquatang = 2 * np.arccos( np.clip(np.sum(half_bt24cell_faces * half_bt24cell_faces[:, None], -1), -1, 1)) cellquatang = np.minimum(cellquatang, 2 * np.pi - cellquatang) cellcen = homog.quat.quat_to_xform(half_bt24cell_faces) cellcenang = homog.angle_of(homog.hinv(cellcen) @ cellcen[:, None]) cellcenanground = np.round(cellcenang * 180 / np.pi, 5) # print(np.unique(cellcenanground)) assert np.allclose(cellcenang, cellquatang) # np.fill_diagonal(cellcenang, 10) q = homog.quat.rand_quat((1000, )) qang = 2 * np.arccos( np.clip(np.abs(np.sum(q * half_bt24cell_faces[:, None], -1)), -1, 1)) minqang = np.min(qang, 0) # print('Qang to closest', # np.percentile(minqang * 180 / np.pi, np.arange(0, 101, 10))) assert np.max(minqang) < 62.81 / 180 * np.pi x = homog.rand_xform(1000) cellxform = homog.quat.quat_to_xform(half_bt24cell_faces) xang = homog.angle_of(x @ homog.hinv(cellxform)[:, None]) minxang = np.min(xang, 0) # print('Xang to closest:', # np.percentile(minxang * 180 / np.pi, np.arange(0, 101, 10))) assert np.max(minqang) < 62.81 / 180 * np.pi
def test_numba_xbin(): cart_resl = 1.0 ori_resl = 10.0 xbinner = XformBinner(cart_resl, ori_resl) xbin_indexer = numba_xbin_indexer(cart_resl, ori_resl) for i in range(100): x = homog.rand_xform(1) assert xbinner.get_bin_index(x) == xbin_indexer(x[0]) xbin_indexer = gu_xbin_indexer(cart_resl, ori_resl) x = homog.rand_xform(100000, cart_sd=100) idx0 = xbinner.get_bin_index(x) idx1 = xbin_indexer(x) assert np.all(idx0 == idx1)
def test_f6_to_xform_invertibility(): nexamples = 100000 xforms = homog.rand_xform((nexamples, )) face, f6 = xform_to_f6(xforms) assert np.all(np.min(f6[..., 3:], axis=0) < 0.1) assert np.all(np.max(f6[..., 3:], axis=0) > 0.9) assert np.all((0 <= face) * (face < 24)) assert np.all(f6[..., 3:] >= 0) assert np.all(f6[..., 3:] <= 1) xhat = f6_to_xform(face, f6) assert np.allclose(xhat, xforms)
def test_numba_xform_to_f6(): for i in range(1000): x = homog.rand_xform(1) face_0, f6_0 = xform_to_f6(x) face_1, f6_1 = numba_xform_to_f6(x[0]) assert face_0 == face_1 if not np.allclose(f6_0, f6_1): print(i, face_0, face_1) print(f6_0) print(f6_1) print(hg.quat.rot_to_quat(x)) print(hg.quat.numba_rot_to_quat(x[0])) assert 0
def test_rot_quat_conversion_rand_numba(): x = homog.rand_xform((5, 6, 7), cart_sd=0) assert np.all(homog.is_homog_xform(x)) q = gu_rot_to_quat(x) assert np.all(is_valid_quat_rot(q)) y = quat_to_xform(q) assert np.all(homog.is_homog_xform(y)) assert x.shape == y.shape assert np.allclose(x, y) q = homog.quat.rand_quat() assert np.all(is_valid_quat_rot(q)) x = quat_to_xform(q) assert np.all(homog.is_homog_xform(x)) p = gu_rot_to_quat(x) assert np.all(is_valid_quat_rot(p)) assert p.shape == q.shape assert np.allclose(p, q)
def test_xform_to_f6(): x = homog.rand_xform(24000) face, f6 = xform_to_f6(x) assert np.all(face >= 0) assert np.all(face < 24) assert np.all(f6[..., 3:] >= 0) assert np.all(f6[..., 3:] <= 1) counts = np.sum(face == np.arange(24)[..., None], axis=-1) assert np.all(counts < 1200) assert np.all(counts > 800) xbt24 = homog.quat.quat_to_xform(half_bt24cell_faces) assert np.all(homog.is_homog_xform(xbt24)) qbt24 = homog.quat.xform_to_quat(xbt24) assert np.all(homog.quat.is_valid_quat_rot(qbt24)) assert np.allclose(qbt24, half_bt24cell_faces) face = half_bt24cell_face(qbt24) assert np.all(face == np.arange(24))
def test_XformBinner_covrad(): niter = 30 nsamp = 10000 for i in range(niter): cart_resl = np.random.rand() * 10 + 0.1 ori_resl = np.random.rand() * 50 + 2.5 xforms = homog.rand_xform(nsamp) xb = XformBinner(cart_resl, ori_resl) idx = xb.get_bin_index(xforms) cen, f6 = xb.get_bin_center(idx, debug=True) cart_dist = np.linalg.norm(xforms[..., :3, 3] - cen[..., :3, 3], axis=-1) ori_dist = homog.angle_of(homog.hinv(cen) @ xforms) # if not np.all(cart_dist < cart_resl): # print('ori_resl', ori_resl, 'nside:', xb.ori_nside, # 'max(cart_dist):', np.max(cart_dist), cart_resl) # if not np.all(cart_dist < cart_resl): # print('ori_resl', ori_resl, 'nside:', xb.ori_nside, # 'max(ori_dist):', np.max(ori_dist)) assert np.sum(cart_dist < cart_resl) > nsamp * 0.99 assert np.sum(ori_dist < ori_resl / 180 * np.pi) > nsamp * 0.99
import numpy as np import sys, os import homog with redirect_stdout(open(os.devnull,'w')): from xbin import * ori_nside = int(sys.argv[1]) Nsamp = 1000000 Nptiles = 101 ptiles = np.concatenate([np.arange(Nptiles), 100 - 10**np.linspace(2, -6, Nptiles)]) estimate = np.zeros_like(ptiles) coherent = np.zeros_like(ptiles) while True: xforms = homog.rand_xform(Nsamp) xb = XformBinner(ori_nside=ori_nside) idx = xb.get_bin_index(xforms) cen = xb.get_bin_center(idx) ori_dist = homog.angle_of(homog.hinv(cen) @ xforms) ehat = np.percentile(ori_dist, ptiles) old = estimate estimate = np.maximum(estimate, ehat) if np.any(old != estimate): print('independent', Nsamp, ori_nside, str(estimate.tolist()).replace(',','')[1:-1]) sys.stdout.flush() if np.sum(ehat) > np.sum(coherent): coherent = ehat print('coherent', Nsamp, ori_nside, str(coherent.tolist()).replace(',','')[1:-1])