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_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 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
# set the alpha coefficients randomly if args.random: alpha1 = normal(size=alpha.size-1) alpha1 = (args.rms/norm(alpha1))*alpha1 alpha[1:] = alpha1 del alpha1 # evaluate the phase corresponding to alpha Phi = phase_pol.eval_grid(alpha) # evaluate the generalised pupil function P corresponding to alpha P = np.exp(1j*Phi) # estimate the beta coefficients from P beta_hat = ip.fit(P) # plot the results phaseplot = PhasePlot(n=args.n_alpha) # to plot beta and the PSF betaplot = BetaPlot(args) # to plot the phase # plot alpha p.figure(10) p.subplot2grid((1, 3), (0, 0), colspan=2) h1 = p.plot(range(1, phase_pol.nk + 1), alpha, marker='o') p.legend(h1, [r'$\alpha$']) p.ylabel('[rad]') p.xlabel('$k$') p.subplot2grid((1, 3), (0, 2)) phaseplot.plot_alpha(alpha) p.title(r'$\alpha$')
# complex-valued Zernike polynomials up to the 4-th radial order gpf_pol = CZern(4) # to approximate the GPF # FitZern computes the approximate inner products, see Eq. (B4) in [A2015] ip = FitZern(gpf_pol, L, K) gpf_pol.make_pol_grid(ip.rho_j, ip.theta_i) # make a polar grid # random vector of Zernike coefficients to be estimated beta_true = normal(size=gpf_pol.nk) + 1j * normal(size=gpf_pol.nk) # random generalised pupil function P P = gpf_pol.eval_grid(beta_true) # estimate the random vector from the GPF grid beta_hat = ip.fit(P) # plot the results p.figure(2) # real part of the Zernike coefficients of beta_true and beta_hat ax = p.subplot(3, 1, 1) h1 = ax.plot(range(1, gpf_pol.nk + 1), beta_true.real, marker='o') h2 = ax.plot(range(1, gpf_pol.nk + 1), beta_hat.real, marker='x') p.legend(h1 + h2, [r'$\mathcal{R}[\beta]$', r'$\mathcal{R}[\hat{\beta}]$']) p.ylabel('[rad]') p.xlabel('$k$') # imaginary part of the Zernike coefficients of beta_true and beta_hat ax = p.subplot(3, 1, 2) h1 = ax.plot(range(1, gpf_pol.nk + 1), beta_true.imag, marker='o')
# real-valued Zernike polynomials up to the 6-th radial order phase_pol = RZern(6) # FitZern computes the approximate inner products, see Eq. (B2) in [A2015] ip = FitZern(phase_pol, L, K) phase_pol.make_pol_grid(ip.rho_j, ip.theta_i) # make a polar grid # random vector of Zernike coefficients to be estimated alpha_true = normal(size=phase_pol.nk) # phase grid Phi = phase_pol.eval_grid(alpha_true) # estimate the random vector from the phase grid alpha_hat = ip.fit(Phi) # plot the results p.figure(2) # Zernike coefficients of alpha_true and alpha_hat ax = p.subplot2grid((3, 3), (0, 0), colspan=3) h1 = ax.plot(range(1, phase_pol.nk + 1), alpha_true, marker='o') h2 = ax.plot(range(1, phase_pol.nk + 1), alpha_hat, marker='x') p.legend(h1 + h2, [r'$\alpha$', r'$\hat{\alpha}$']) p.ylabel('[rad]') p.xlabel('$k$') # Zernike coefficients error between alpha_true and alpha_hat ax = p.subplot2grid((3, 3), (1, 0), colspan=3) err = alpha_true - alpha_hat
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
# real-valued Zernike polynomials up to the 6-th radial order phase_pol = RZern(6) # FitZern computes the approximate inner products, see Eq. (B2) in [A2015] ip = FitZern(phase_pol, L, K) phase_pol.make_pol_grid(ip.rho_j, ip.theta_i) # make a polar grid # random vector of Zernike coefficients to be estimated alpha_true = normal(size=phase_pol.nk) # phase grid Phi = phase_pol.eval_grid(alpha_true) # estimate the random vector from the phase grid alpha_hat = ip.fit(Phi) # plot the results p.figure(2) # Zernike coefficients of alpha_true and alpha_hat ax = p.subplot2grid((3, 3), (0, 0), colspan=3) h1 = ax.plot(range(1, phase_pol.nk + 1), alpha_true, marker='o') h2 = ax.plot(range(1, phase_pol.nk + 1), alpha_hat, marker='x') p.legend(h1 + h2, [r'$\alpha$', r'$\hat{\alpha}$']) p.ylabel('[rad]') p.xlabel('$k$') # Zernike coefficients error between alpha_true and alpha_hat ax = p.subplot2grid((3, 3), (1, 0), colspan=3) err = alpha_true - alpha_hat