def test_fit_real(self): log = logging.getLogger('TestFitZern.test_fit_real') z = RZern(4) F = FitZern(z, self.L, self.K) theta_i = F.theta_i rho_j = F.rho_j c = normal(size=z.nk) time1 = time() Phi = [z.eval_a(c, rh, th) for rh in rho_j for th in theta_i] time2 = time() log.debug('eval Phi {:.4f}'.format(time2 - time1)) time1 = time() ce = F._fit_slow(Phi) time2 = time() log.debug('elapsed time {:.4f}'.format(time2 - time1)) err1 = norm(np.array(c) - np.array(ce)) max1 = max([abs(c[i] - ce[i]) for i in range(z.nk)]) log.debug('err1 {:e} max1 {:e}'.format(err1, max1)) self.assertTrue(err1 < self.max_fit_norm)
def test_fit_real_numpy(self): log = logging.getLogger('TestFitZern.test_fit_real_numpy') z = RZern(4) F = FitZern(z, self.L, self.K) theta_i = F.theta_i rho_j = F.rho_j c = normal(size=z.nk) Phi = [z.eval_a(c, rh, th) for rh in rho_j for th in theta_i] time1 = time() ce = F._fit_slow(Phi) time2 = time() log.debug('elapsed FIT_LIST {:.6f}'.format(time2 - time1)) time1 = time() ce2 = F.fit(np.array(Phi, order='F')) time2 = time() log.debug('elapsed FIT_NUMPY {:.6f}'.format(time2 - time1)) enorm = norm(ce2 - np.array(ce, order='F')) log.debug('enorm {:e}'.format(enorm)) self.assertTrue(enorm < self.max_enorm)
def test_normalisations_complex(self): log = logging.getLogger('TestZern.test_normalisations_complex') n_beta = 6 L, K = 400, 393 # polar grid pol = CZern(n_beta) fitBeta = FitZern(pol, L, K) t1 = time() pol.make_pol_grid(fitBeta.rho_j, fitBeta.theta_i) t2 = time() log.debug('make pol grid {:.6f}'.format(t2 - t1)) # cartesian grid cart = CZern(n_beta) dd = np.linspace(-1.0, 1.0, max(L, K)) xx, yy = np.meshgrid(dd, dd) t1 = time() cart.make_cart_grid(xx, yy) t2 = time() log.debug('make cart grid {:.6f}'.format(t2 - t1)) smap = np.isfinite(cart.eval_grid(np.zeros(cart.nk))) scale = (1.0/np.sum(smap)) log.debug('') log.debug('{} modes, {} x {} grid'.format(n_beta, L, K)) for i in range(pol.nk): a = np.zeros(pol.nk) a[i] = 1.0 Phi_a = cart.eval_grid(a) for j in range(pol.nk): b = np.zeros(pol.nk) b[j] = 1.0 Phi_b = cart.eval_grid(b) ip = scale*np.sum(Phi_a[smap]*(Phi_b[smap].conj())) if i == j: eip = 1.0 else: eip = 0.0 iperr = abs(ip - eip) log.debug('<{:02},{:02}> = {:+e} {:+e}'.format( i + 1, j + 1, ip, iperr)) self.assertTrue(iperr < self.max_ip_err)
def do_test(cls, complex_a): z = cls(4) F = FitZern(z, self.L, self.K) theta_i = F.theta_i rho_j = F.rho_j z.make_pol_grid(rho_j, theta_i) if complex_a: c = normal(size=z.nk) + 1j*normal(size=z.nk) else: c = normal(size=z.nk) PhiN = np.array( [z.eval_a(c, rh, th) for rh in rho_j for th in theta_i], order='F') ce1 = F.fit(PhiN) # create tmp path tmpfile = NamedTemporaryFile() tmppath = tmpfile.name tmpfile.close() F.save(tmppath) F2 = FitZern.load(tmppath) PhiN1 = np.array( [F.z.eval_a(c, rh, th) for rh in rho_j for th in theta_i], order='F') PhiN2 = np.array( [F2.z.eval_a(c, rh, th) for rh in rho_j for th in theta_i], order='F') self.assertTrue(isinstance(F, FitZern)) self.assertTrue(isinstance(F2, FitZern)) if complex_a: self.assertTrue(isinstance(F.z, CZern)) self.assertTrue(isinstance(F2.z, CZern)) else: self.assertTrue(isinstance(F.z, RZern)) self.assertTrue(isinstance(F2.z, RZern)) self.assertTrue(norm(PhiN - PhiN1) == 0) self.assertTrue(norm(PhiN - PhiN2) == 0) self.assertTrue(norm(F.z.coefnorm - F2.z.coefnorm) == 0) self.assertTrue(norm(F.z.ntab - F2.z.ntab) == 0) self.assertTrue(norm(F.z.mtab - F2.z.mtab) == 0) self.assertTrue(F.z.n == F2.z.n) self.assertTrue(F.z.nk == F2.z.nk) self.assertTrue(F.z.normalise == F2.z.normalise) self.assertTrue(norm(F.z.rhoitab - F2.z.rhoitab) == 0) self.assertTrue(norm(F.z.rhotab - F2.z.rhotab) == 0) self.assertTrue(F.z.numpy_dtype == F2.z.numpy_dtype) self.assertTrue(norm(F.z.ZZ - F2.z.ZZ) == 0) self.assertTrue(norm(F.A - F2.A) == 0) self.assertTrue(norm(F.I_cosm - F2.I_cosm) == 0) self.assertTrue(norm(F.I_sinm - F2.I_sinm) == 0) self.assertTrue(norm(F.I_Rnmrho - F2.I_Rnmrho) == 0) self.assertTrue(F.K == F2.K) self.assertTrue(F.L == F2.L) self.assertTrue(norm(F.rho_a - F2.rho_a) == 0) self.assertTrue(norm(F.rho_b - F2.rho_b) == 0) self.assertTrue(norm(F.rho_j - F2.rho_j) == 0) self.assertTrue(norm(F.theta_a - F2.theta_a) == 0) self.assertTrue(norm(F.theta_b - F2.theta_b) == 0) self.assertTrue(norm(F.theta_i - F2.theta_i) == 0) ce2 = F2.fit(PhiN) self.assertTrue(norm(ce2 - ce1) < self.max_enorm) os.unlink(tmppath) del z, F, F2, c, ce1, ce2, PhiN, PhiN1, PhiN2