def test_scalar_coefs_ops(self): """::Check to make sure the scalar coefficient ops are working """ _ = sp.ones_coefs(12, 8) _ = sp.random_coefs(10, 10) + 1j * sp.random_coefs(10, 10) _ = sp.zeros_coefs(21, 18) ss = 1j - 2 / (sp.zeros_coefs(5, 4) + .001) + \ 4 * sp.random_coefs(5, 4) / 6.0 ss += sp.ones_coefs(5, 4, coef_type=sp.scalar) _ = 1 + 2 * sp.ones_coefs(3, 3) / 4 * sp.ones_coefs(3, 3) - 2 ds1 = 1j + 4 * sp.ones_patt_uniform(5, 8) / 3 - \ sp.ones_patt_uniform(5, 8) - 1 + \ 10 * sp.random_patt_uniform(5, 8) ds1.single_val sp.array(ds1) _ = sp.zeros_coefs(10, 10) self.assertTrue(True)
def test_misc(self): """::exercise misc functions""" z1 = sp.random_patt_uniform(11, 10, patt_type=sp.scalar) _ = sp.abs(z1) _ = sp.mag(z1) _ = sp.mag2(z1) _ = sp.L2_patt(z1) _ = sp.LInf_patt(z1) z1 = sp.random_patt_uniform(11, 10, patt_type=sp.vector) _ = sp.mag(z1) _ = sp.mag2(z1) _ = sp.L2_patt(z1) _ = sp.LInf_patt(z1) z1 = sp.random_coefs(11, 10, coef_type=sp.scalar) _ = sp.abs(z1) _ = sp.mag(z1) _ = sp.mag2(z1) _ = sp.L2_coef(z1) _ = sp.LInf_coef(z1) z1 = sp.random_coefs(11, 10, coef_type=sp.vector) _ = sp.mag(z1) _ = sp.mag2(z1) _ = sp.L2_coef(z1) _ = sp.LInf_coef(z1) self.assertTrue(True)
def test_probe_correct_and_probe_response(self): """:: Test probe_correct and probe_response based on inverses probe_response is the inverse of probe_correct, this tests that to make sure it's true. """ p_m = sp.random_coefs(5, 1, coef_type=sp.vector) for Nmax in range(10, 14): for Mmax in range(Nmax - 1, Nmax + 1): c_m = sp.random_coefs(Nmax, Mmax, coef_type=sp.vector) R_python = nss.translate_symmetric_probe(60, p_m, 27, region=nss.external) # Do probe response and probe correct here dnm_response = nss.probe_response(c_m, R_python) dnm_corrected = nss.probe_correct(dnm_response, R_python) diff = sp.LInf_coef(dnm_corrected - c_m) self.assertLess(diff, 1e-12)
def test_mult_same_sizes(self): """::can multiply two VectorCoefs that are same sized """ z1 = sp.random_coefs(11, 10, coef_type=sp.vector) z2 = sp.random_coefs(11, 10, coef_type=sp.vector) a = z1 * z2 b = z1 * z2 c = a - b res = True if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False self.assertTrue(res)
def test_sub_same_sizes(self): """::can sub two ScalarCoefs that are same sized """ z1 = sp.random_coefs(11, 10) z2 = sp.random_coefs(11, 10) a = z1 - z2 b = z1 - z2 c = a - b res = True if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False self.assertTrue(res)
def test_div_same_sizes(self): """::can add two VectorCoefs that are same sized """ z1 = sp.random_coefs(11, 10, coef_type=sp.vector) z2 = sp.random_coefs(11, 10, coef_type=sp.vector) a = z1 / (z2 + 0.0001) b = z1 / (z2 + 0.0001) c = a - b res = True if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False self.assertTrue(res)
def test_rotate_around_y_by_pi_on_large_random_coefs(self): """:: Test rotate_around_y_by_pi using large randomly generated coeff sets. Large coefficient sets are generated and rotate_around_y_by_pi is applied twice two the set of coefficients to return the set back to its original form. """ print(" ") #generate five sets of random variabls for nmax in range(100, 104): for mmax in range(nmax - 2, nmax + 1): c = sp.random_coefs(nmax, mmax, coef_type=sp.vector) print("Nmax = %d, Mmax = %d" % (nmax, mmax)) rc = nss.rotate_around_y_by_pi(c) rcc = nss.rotate_around_y_by_pi(rc) diff = sp.LInf_coef(rcc - c) print("WARNING: This should be 0: err = %.16e" % diff) #TODO: I don't understand something here. The difference #between rcc and c should be exactly zero but I am #getting an error roughly 1e-13. I am not doing any #arithmetic so I shouldn't have any error??? #NEED TO UNDERSTAND THIS self.assertAlmostEqual(diff, 0, places=10)
def test__repr_str(self): """::exercise __repr__ and __str__""" z1 = sp.random_coefs(2, 1) _ = z1.__repr__() _ = str(z1) self.assertTrue(True)
def test___reshape_m_vecs(self): """::exercise _reshape_m_vecs""" z1 = sp.random_coefs(11, 10) nv = z1._reshape_m_vecs() res = False if len(nv) == z1.nmax + 1: res = True self.assertTrue(res)
def test_scalar_power_spectrum(self): """ test the power spectrum routines """ c = sp.random_coefs(10, 10) aps = c.angular_power_spectrum() p = c.power() msg = "not calculating power spectrum to good enough precision" self.assertAlmostEqual(np.sum(aps), p, places=10, msg=msg)
def test_size(self): """::make sure c.size works.""" z1 = sp.random_coefs(11, 10) N = z1.nmax + 1 NC = N + z1.mmax * (2 * N - z1.mmax - 1) res = False if z1.size == NC: res = True self.assertTrue(res)
def test_vspht_vispht_no_added_args(self): """::Test spht and ispht when nmax, mmax, nrows, ncols are not passed. """ c = sp.random_coefs(100, 100, coef_type=sp.vector) p = sp.vispht(c) c2 = sp.vspht(p) res = True if (sp.L2_coef(c - c2) / sp.L2_coef(c)) > 1e-13: res = False self.assertTrue(res)
def test_vspht_vispht_one_added_args(self): """::Test vspht when only nmax is passed. """ c = sp.random_coefs(100, 100, coef_type=sp.vector) p = sp.vispht(c) c2 = sp.vspht(p, 5) res = True if (sp.L2_coef(c[0:5, :] - c2) / sp.L2_coef(c[0:5, :])) > 1e-13: res = False self.assertTrue(res)
def test_spht_with_large_random(self): """::Generate 100 random modes and test both spht and ispht. """ c = sp.random_coefs(100, 100) p = sp.ispht(c, 202, 202) c2 = sp.spht(p, 100, 100) res = True if (sp.L2_coef(c - c2) / sp.L2_coef(c)) > 1e-13: res = False self.assertTrue(res)
def test_mult_constant_left_right(self): """::can mult a scalar to ScalarCoefs from both sides""" z1 = sp.random_coefs(11, 10) a = z1 * 1j * 1.1 b = z1 * 1j * 1.1 c = a - b res = True if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False a = 1j * 1.1 * z1 b = 1j * 1.1 * z1 c = a - b if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False self.assertTrue(res)
def test_div_constant_left_right(self): """::can mult a scalar to VectorCoefs from both sides""" z1 = sp.random_coefs(11, 10, coef_type=sp.vector) a = z1 / 1j * 1.1 b = z1 / 1j * 1.1 c = a - b res = True if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False a = 1j * 1.1 / z1 b = 1j * 1.1 / z1 c = a - b if sp.L2_coef(c) / sp.L2_coef(b) > 1e-13: res = False self.assertTrue(res)
def test_ispht(self): """::Compare ispht within pysphi and csphi""" Nmax = 50 Nrows = 102 res = True for MM in xrange(1, 3): Mmax = Nmax - MM c = sp.random_coefs(Nmax, Mmax) ppy = sp.ispht_slow(c, Nrows / 2, Nrows) pc = sp.ispht(c, Nrows / 2, Nrows) rerr = np.sum(sp.abs(ppy - pc)) / np.sum(sp.abs(ppy)) if (rerr > 1e-13): res = False self.assertTrue(res)
def test_reciprocity_on_large_random_coefs(self): """:: Test reciprocity using large randomly generated coeff sets. Large coefficient sets are generated and reciprocity is applied twice two the set of coefficients to return the set back to its original form. """ print(" ") #generate five sets of random variabls for nmax in range(100, 104): for mmax in range(nmax - 2, nmax + 1): c = sp.random_coefs(nmax, mmax, coef_type=sp.vector) print("Nmax = %d, Mmax = %d" % (nmax, mmax)) rc = nss.reciprocity(c) rcc = nss.reciprocity(rc) diff = sp.LInf_coef(rcc - c) self.assertAlmostEqual(diff, 0, places=14)
def test_bounds_checking_access_m_greater_n(self): """::raise an error if I go out of bounds m > n access""" zz = sp.random_coefs(11, 10) with self.assertRaises(AttributeError): _ = zz[2, 3]
def test_ispht_size_error1(self): """::Test ispht error when nrows is too small. """ c = sp.random_coefs(100, 100) with self.assertRaises(ValueError): _ = sp.ispht(c, 100, 204)
import sys sys.path.append('../spherepy') import spherepy as sp #TODO: Change all xrange instances to range #and do a 'from six.moves import range' here from six.moves import xrange patt1 = sp.random_patt_uniform(5, 10, patt_type=sp.scalar) sp.file.save_patt(patt1, 't1.txt') patt2 = sp.file.load_patt('t1.txt') a = sp.L2_patt(patt1 - patt2) scoef1 = sp.random_coefs(10, 7) sp.file.save_coef(scoef1, 't2.txt') scoef2 = sp.file.load_coef('t2.txt') a = sp.L2_coef(scoef1 - scoef2) a = 1
"""scoef[11,-7]""", """scoef[11,-8]""", """scoef[2,1:3] = vec""", """scoef[1:,3] = vec""", """a = scoef + scoef2""", """a = scoef - scoef2""", """a = scoef * scoef2""", """a = scoef / scoef2""", """a = scoef + scoef3""", """a = scoef - scoef3""", """a = scoef * scoef3""", """a = scoef / scoef3""", """a = scoef4 + scoef""", """a = scoef4 - scoef""", """a = scoef4 * scoef""", """a = scoef4 / scoef""", """a = scoef4 + 2.1""", """a = scoef4 - 2.1""", """a = scoef4 * 2.1""", """a = scoef4 / 2.1""", """a = 4.5 + scoef2""", """a = 4.5 - scoef2""", """a = 4.5 * scoef2""", """a = 4.5 / scoef2""", """a = scoef / 0""", """sc = sp.ScalarCoefs(vec, 2, 2)""" ] scoef = sp.zeros_coefs(11, 7) scoef2 = sp.random_coefs(11, 7) scoef3 = sp.random_coefs(11, 8) scoef4 = sp.random_coefs(12, 7) vec = np.zeros(11, dtype=np.complex128) test_executes(executes_scoefs) if test_vector_coefs: print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") print("") print(" VectorCoefs structure with exceptions") print("") print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") executes_vcoefs = [ """vz = sp.zeros_coefs(11, 13, coef_type=sp.vector)""",
#TODO: Change all xrange instances to range #and do a 'from six.moves import range' here from six.moves import xrange # @UnresolvedImport Nmax = 400 Nrows = 1024 ISPHT = 0 SPHT = 1 VISPHT = 2 VSPHT = 3 FFT = 4 ctrl = SPHT c = sp.random_coefs(Nmax, Nmax) vc = sp.random_coefs(Nmax, Nmax, coef_type = sp.vector) if ctrl == ISPHT: profile.run('p = sp.ispht(c,Nrows,Nrows)',sort=1) if ctrl == SPHT: p = sp.ispht(c, Nrows, Nrows) profile.run('c2 = sp.spht(p,Nmax,Nmax)',sort=1) if ctrl == VISPHT: profile.run('p = sp.vispht(vc,Nrows,Nrows)',sort=1) if ctrl == VSPHT: vp = sp.vispht(vc, Nrows, Nrows) profile.run('vc2 = sp.vspht(vp,Nmax,Nmax)',sort=1)
python version. ***************************************************************************""" import spherepy as sp import profile import time #TODO: Change all xrange instances to range #and do a 'from six.moves import range' here from six.moves import xrange Nmax = 200 Mmax = Nmax Nrows = 2048 c = sp.random_coefs(Nmax, Nmax) N = Nmax + 1; NModes = N + Mmax * (2 * N - Mmax - 1); head = """******************************************************************** Benchmark test to compare python version of the spherical code to the c version of the spherical code ********************************************************************""" data_sec = """ A random set of scalar coefficients has been generated with: Nmax = {0} and Mmax = {1}
def test_bounds_checking_set_m_greater_mmax(self): """::raise an error if I go out of bounds m > mmax set""" zz = sp.random_coefs(11, 10) with self.assertRaises(AttributeError): zz[11, 11] = 8
def test_two_many_indecies(self): """::see if something like this z[1,2,3]""" zz = sp.random_coefs(11, 10) with self.assertRaises(AttributeError): zz[2, 3, 1] = 8
def test_bounds_checking_access_n_greater_nmax(self): """::raise an error if I go out of bounds n > nmax access""" zz = sp.random_coefs(11, 10) with self.assertRaises(AttributeError): _ = zz[12, -9]
def test_size(self): """::can add get the size? """ z1 = sp.random_coefs(11, 10, coef_type=sp.vector) z1.size self.assertTrue(True)
def test_vispht_size_error2(self): """::Test ispht error when ncols is too small. """ c = sp.random_coefs(100, 100, coef_type=sp.vector) with self.assertRaises(ValueError): _ = sp.vispht(c, 104, 198)
def test_make_sure_mmax_not_greater_nmax_r(self): """::raise an error if I try to set mmax > nmax: random""" with self.assertRaises(ValueError): _ = sp.random_coefs(11, 13)