def solve_phase_6d(phs, ptmask): """let V = (a1 a2 a3 a4 a5 a6)^T, we want to solve: phi(s,u,r) = 2[rA1 + sA3 + A5] - [rA2 + sA4 + A6] u-pos phi(s,u,r) = -2[rA1 + sA3 + A5] - [rA2 + sA4 + A6] u-neg with our overdetermined data. so P = [neg[0] pos[0] neg[1] pos[1] ... neg[S] pos[S]]^T for all selected slices, and similarly A = [-2r0 -r0 -2s0 -s0 -2 -1; <-- -2r1 -r1 -2s0 -s0 -2 -1; <-- neg going u, s,r=0 ... 2r0 -r0 2s0 -s0 2 -1; <-- 2r1 -r1 2s0 -s0 2 -1; <-- pos going u, s,r=0 ... -2r0 -r0 -2s1 -s1 -2 -1; <-- neg going u, r=0,s=1 ... 2r0 -r0 2s1 -s1 2 -1; <-- pos going u, r=0,s=1 ...] Then with AV = P, solve V = inv(A)P """ Q3, M2, Q1 = phs.shape A1, A2, A3, A4, A5, A6 = (0, 1, 2, 3, 4, 5) # build the full matrix first, collapse the zero-rows afterwards nrows = N.product(phs.shape) A = N.zeros((nrows, 6), N.float64) P = N.reshape(phs, (nrows, )) ptmask = N.reshape(ptmask, (nrows, )) q1_line = N.arange(Q1) - Q1 / 2 q3_line = N.arange(Q3) m2sign = N.outer(checkerline(M2 * Q3), N.ones(Q1)) A[:, A1] = (2 * m2sign * q1_line).flatten() A[:, A2] = (-N.outer(N.ones(M2 * Q3), q1_line)).flatten() A[:, A3] = 2 * N.repeat(checkerline(M2 * Q3), Q1) * N.repeat( q3_line, M2 * Q1) A[:, A4] = -N.repeat(q3_line, M2 * Q1) A[:, A5] = (2 * m2sign).flatten() A[:, A6] = -1.0 nz1 = ptmask.nonzero()[0] A = A[nz1] P = P[nz1] [u, s, vt] = N.linalg.svd(A, full_matrices=0) V = N.dot(vt.transpose(), N.dot(N.diag(1 / s), N.dot(u.transpose(), P))) ## import pylab as pl ## ptmask = N.reshape(ptmask, phs.shape) ## for s in q3_line: ## r_ind_ev = N.nonzero(ptmask[s,0])[0] ## r_ind_od = N.nonzero(ptmask[s,1])[0] ## if r_ind_ev.any() and r_ind_od.any(): ## ## if self.shear_correct: ## rowpos = 2*q1_line*V[A1] - q1_line*V[A2] +\ ## 2*s*V[A3] - s*V[A4] + 2*V[A5] - V[A6] ## rowneg = -2*q1_line*V[A1] - q1_line*V[A2] + \ ## -2*s*V[A3] - s*V[A4] - 2*V[A5] - V[A6] ## ## else: ## ## rowpos = 2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A5] ## ## rowneg = -(2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A5]) ## pl.plot(rowpos, 'b.') ## pl.plot(rowneg, 'r.') ## pl.plot(phs[s,0], 'b--') ## pl.plot(r_ind_ev, phs[s,0,r_ind_ev], 'bo', alpha=0.25) ## pl.plot(phs[s,1], 'r--') ## pl.plot(r_ind_od, phs[s,1,r_ind_od], 'ro', alpha=0.25) ## pl.title("slice %d"%s) ## pl.show() return V
def solve_phase_3d(phs, ptmask): """ let V = (a1 a3 a0)^T, we want to solve: phi(q3,m2,q1) = (-1)^(m2) * 2[q1*A1 + q3*A3 + A0] with our overdetermined data. so P = [neg[q3=0] pos[q3=0] neg[q3=1] pos[q3=1] ... neg[S] pos[S]]^T for all selected slices, and similarly A = [-2q1_0 -2q3_0 -2; -2q1_1 -2q3_0 -2; <-- phi(q3=0,m2=odd; q1) ... 2q1_0 2q3_0 2; 2q1_1 2q3_0 2; <-- phi(q3=0,m2=evn; q1) ... -2q1_0 -2q3_1 -2; -2q1_1 -2q3_1 -2; <-- phi(q3=1,m2=odd; q1) ... 2q1_1 2q3_1 2; <-- phi(q3=1,m2=evn; q1) ... ] Then with AV = P, solve V = inv(A)P """ Q3, M2, Q1 = phs.shape A1, A3, A0 = (0, 1, 2) # build the full matrix first, collapse the zero-rows afterwards nrows = N.product(phs.shape) A = N.zeros((nrows, 3), N.float64) P = phs.copy() P.shape = (nrows, ) ptmask = N.reshape(ptmask, (nrows, )) q1_line = N.linspace(-Q1 / 2., Q1 / 2., Q1, endpoint=False) q3_line = N.linspace(0., Q3, Q3, endpoint=False) m2sign = N.outer(checkerline(M2 * Q3), N.ones(Q1)) A[:, A1] = (2 * m2sign * q1_line).flatten() A[:, A3] = 2 * N.repeat(checkerline(M2 * Q3), Q1) * N.repeat( q3_line, M2 * Q1) A[:, A0] = (2 * m2sign).flatten() nz1 = ptmask.nonzero()[0] A = A[nz1] P = P[nz1] [u, s, vt] = N.linalg.svd(A, full_matrices=0) V = N.dot(vt.transpose(), N.dot(N.diag(1 / s), N.dot(u.transpose(), P))) ## import pylab as pl ## ptmask = N.reshape(ptmask, phs.shape) ## for s in q3_line: ## q1_ind_ev = N.nonzero(ptmask[s,0])[0] ## q1_ind_od = N.nonzero(ptmask[s,1])[0] ## if q1_ind_ev.any() and q1_ind_od.any(): ## rowpos = 2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A0] ## rowneg = -(2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A0]) ## pl.plot(rowpos, 'b.') ## pl.plot(rowneg, 'r.') ## pl.plot(phs[s,0], 'b--') ## pl.plot(q1_ind_ev, phs[s,0,q1_ind_ev], 'bo', alpha=0.25) ## pl.plot(phs[s,1], 'r--') ## pl.plot(q1_ind_od, phs[s,1,q1_ind_od], 'ro', alpha=0.25) ## pl.title("slice %d"%s) ## pl.show() return V
def bench_fft1_time(self): from numpy.fft import fft as numpy_fft from scipy.fftpack import fft as scipy_fft print print ' 1D Double Precision Fast Fourier Transform' print '=================================================' print ' | complex input ' print '-------------------------------------------------' print ' size | recon | numpy | scipy |' print '-------------------------------------------------' for size,repeat in [(100,7000),(1000,2000), (256,10000), (512,10000), (1024,1000), (2048,1000), (2048*2,500), (2048*4,500), ]: print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(cdouble) + \ random(repeat, size).astype(cdouble)*1j tr0 = time.time() y = fft1(x, shift=False) trf = time.time() print '|%8.2f' % (trf-tr0), sys.stdout.flush() tn0 = time.time() ny = numpy_fft(x) tnf = time.time() assert_array_almost_equal(ny,y) print '|%8.2f' % (tnf-tn0), sys.stdout.flush() ts0 = time.time() sy = scipy_fft(x) tsf = time.time() assert_array_almost_equal(sy,y) print '|%8.2f' % (tsf-ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) sys.stdout.flush() print print ' 1D Double Precision Fast Fourier Transform' print '=================================================' print ' | complex input shifted ' print '-------------------------------------------------' print ' size | recon | numpy | scipy |' print '-------------------------------------------------' for size,repeat in [(100,7000),(1000,2000), (256,10000), (512,10000), (1024,1000), (2048,1000), (2048*2,500), (2048*4,500), ]: chk = checkerline(size).astype(double) print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(cdouble) + \ random(repeat, size).astype(cdouble)*1j tr0 = time.time() y = fft1(x, shift=True) trf = time.time() print '|%8.2f' % (trf-tr0), sys.stdout.flush() tn0 = time.time() ny = chk*numpy_fft(chk*x) tnf = time.time() assert_array_almost_equal(ny,y) print '|%8.2f' % (tnf-tn0), sys.stdout.flush() ts0 = time.time() sy = chk*scipy_fft(chk*x) tsf = time.time() assert_array_almost_equal(sy,y) print '|%8.2f' % (tsf-ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) sys.stdout.flush() print print ' 1D Single Precision Fast Fourier Transform' print '=================================================' print ' | complex input ' print '-------------------------------------------------' print ' size | recon | numpy | scipy* |' print '-------------------------------------------------' for size,repeat in [(100,7000),(1000,2000), (256,10000), (512,10000), (1024,1000), (2048,1000), (2048*2,500), (2048*4,500), ]: print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(csingle) + \ random(repeat, size).astype(csingle)*1j tr0 = time.time() y = fft1(x, shift=False) trf = time.time() print '|%8.2f' % (trf-tr0), sys.stdout.flush() tn0 = time.time() ny = numpy_fft(x) tnf = time.time() assert_array_almost_equal(ny,y, decimal=2) print '|%8.2f' % (tnf-tn0), sys.stdout.flush() ts0 = time.time() sy = scipy_fft(x.astype(cdouble)).astype(csingle) tsf = time.time() assert_array_almost_equal(sy,y, decimal=2) print '|%8.2f' % (tsf-ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) print "(* casted float->FT{double}->float)" sys.stdout.flush() print print ' 1D Single Precision Fast Fourier Transform' print '=================================================' print ' | complex input shifted ' print '-------------------------------------------------' print ' size | recon | numpy | scipy* |' print '-------------------------------------------------' for size,repeat in [(100,7000),(1000,2000), (256,10000), (512,10000), (1024,1000), (2048,1000), (2048*2,500), (2048*4,500), ]: chk = checkerline(size).astype(single) print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(csingle) + \ random(repeat, size).astype(csingle)*1j tr0 = time.time() y = fft1(x, shift=True) trf = time.time() print '|%8.2f' % (trf-tr0), sys.stdout.flush() tn0 = time.time() ny = chk*numpy_fft(chk*x) tnf = time.time() assert_array_almost_equal(ny,y, decimal=2) print '|%8.2f' % (tnf-tn0), sys.stdout.flush() ts0 = time.time() sy = chk*(scipy_fft((chk*x).astype(cdouble))).astype(csingle) tsf = time.time() assert_array_almost_equal(sy,y, decimal=2) print '|%8.2f' % (tsf-ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) print "(* casted float->FT{double}->float)" sys.stdout.flush()
def bench_ifft1_time(self): from numpy.fft import ifft as numpy_ifft from scipy.fftpack import ifft as scipy_ifft print print ' 1D Double Precision (I)Fast Fourier Transform' print '=================================================' print ' | complex input ' print '-------------------------------------------------' print ' size | recon | numpy | scipy |' print '-------------------------------------------------' for size, repeat in [ (100, 7000), (1000, 2000), (256, 10000), (512, 10000), (1024, 1000), (2048, 1000), (2048 * 2, 500), (2048 * 4, 500), ]: print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(cdouble) + \ random(repeat, size).astype(cdouble)*1j tr0 = time.time() y = ifft1(x, shift=False) trf = time.time() print '|%8.2f' % (trf - tr0), sys.stdout.flush() tn0 = time.time() ny = numpy_ifft(x) tnf = time.time() assert_array_almost_equal(ny, y) print '|%8.2f' % (tnf - tn0), sys.stdout.flush() ts0 = time.time() sy = scipy_ifft(x) tsf = time.time() assert_array_almost_equal(sy, y) print '|%8.2f' % (tsf - ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) sys.stdout.flush() print print ' 1D Double Precision (I)Fast Fourier Transform' print '=================================================' print ' | complex input shifted ' print '-------------------------------------------------' print ' size | recon | numpy | scipy |' print '-------------------------------------------------' for size, repeat in [ (100, 7000), (1000, 2000), (256, 10000), (512, 10000), (1024, 1000), (2048, 1000), (2048 * 2, 500), (2048 * 4, 500), ]: chk = checkerline(size).astype(double) print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(cdouble) + \ random(repeat, size).astype(cdouble)*1j tr0 = time.time() y = ifft1(x, shift=True) trf = time.time() print '|%8.2f' % (trf - tr0), sys.stdout.flush() tn0 = time.time() ny = chk * numpy_ifft(chk * x) tnf = time.time() assert_array_almost_equal(ny, y) print '|%8.2f' % (tnf - tn0), sys.stdout.flush() ts0 = time.time() sy = chk * scipy_ifft(chk * x) tsf = time.time() assert_array_almost_equal(sy, y) print '|%8.2f' % (tsf - ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) sys.stdout.flush() print print ' 1D Single Precision (I)Fast Fourier Transform' print '=================================================' print ' | complex input ' print '-------------------------------------------------' print ' size | recon | numpy | scipy* |' print '-------------------------------------------------' for size, repeat in [ (100, 7000), (1000, 2000), (256, 10000), (512, 10000), (1024, 1000), (2048, 1000), (2048 * 2, 500), (2048 * 4, 500), ]: print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(csingle) + \ random(repeat, size).astype(csingle)*1j tr0 = time.time() y = ifft1(x, shift=False) trf = time.time() print '|%8.2f' % (trf - tr0), sys.stdout.flush() tn0 = time.time() ny = numpy_ifft(x) tnf = time.time() assert_array_almost_equal(ny, y, decimal=2) print '|%8.2f' % (tnf - tn0), sys.stdout.flush() ts0 = time.time() sy = scipy_ifft(x.astype(cdouble)).astype(csingle) tsf = time.time() assert_array_almost_equal(sy, y, decimal=2) print '|%8.2f' % (tsf - ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) print "(* casted float->FT{double}->float)" sys.stdout.flush() print print ' 1D Single Precision (I)Fast Fourier Transform' print '=================================================' print ' | complex input shifted ' print '-------------------------------------------------' print ' size | recon | numpy | scipy* |' print '-------------------------------------------------' for size, repeat in [ (100, 7000), (1000, 2000), (256, 10000), (512, 10000), (1024, 1000), (2048, 1000), (2048 * 2, 500), (2048 * 4, 500), ]: chk = checkerline(size).astype(single) print '%5s' % size, sys.stdout.flush() x = random(repeat, size).astype(csingle) + \ random(repeat, size).astype(csingle)*1j tr0 = time.time() y = ifft1(x, shift=True) trf = time.time() print '|%8.2f' % (trf - tr0), sys.stdout.flush() tn0 = time.time() ny = chk * numpy_ifft(chk * x) tnf = time.time() assert_array_almost_equal(ny, y, decimal=2) print '|%8.2f' % (tnf - tn0), sys.stdout.flush() ts0 = time.time() sy = chk * (scipy_ifft((chk * x).astype(cdouble))).astype(csingle) tsf = time.time() assert_array_almost_equal(sy, y, decimal=2) print '|%8.2f' % (tsf - ts0), sys.stdout.flush() print ' (secs for %s calls)' % (repeat) print "(* casted float->FT{double}->float)" sys.stdout.flush()
def solve_phase_6d(phs, ptmask): """let V = (a1 a2 a3 a4 a5 a6)^T, we want to solve: phi(s,u,r) = 2[rA1 + sA3 + A5] - [rA2 + sA4 + A6] u-pos phi(s,u,r) = -2[rA1 + sA3 + A5] - [rA2 + sA4 + A6] u-neg with our overdetermined data. so P = [neg[0] pos[0] neg[1] pos[1] ... neg[S] pos[S]]^T for all selected slices, and similarly A = [-2r0 -r0 -2s0 -s0 -2 -1; <-- -2r1 -r1 -2s0 -s0 -2 -1; <-- neg going u, s,r=0 ... 2r0 -r0 2s0 -s0 2 -1; <-- 2r1 -r1 2s0 -s0 2 -1; <-- pos going u, s,r=0 ... -2r0 -r0 -2s1 -s1 -2 -1; <-- neg going u, r=0,s=1 ... 2r0 -r0 2s1 -s1 2 -1; <-- pos going u, r=0,s=1 ...] Then with AV = P, solve V = inv(A)P """ Q3, M2, Q1 = phs.shape A1, A2, A3, A4, A5, A6 = (0, 1, 2, 3, 4, 5) # build the full matrix first, collapse the zero-rows afterwards nrows = N.product(phs.shape) A = N.zeros((nrows, 6), N.float64) P = N.reshape(phs, (nrows,)) ptmask = N.reshape(ptmask, (nrows,)) q1_line = N.arange(Q1) - Q1 / 2 q3_line = N.arange(Q3) m2sign = N.outer(checkerline(M2 * Q3), N.ones(Q1)) A[:, A1] = (2 * m2sign * q1_line).flatten() A[:, A2] = (-N.outer(N.ones(M2 * Q3), q1_line)).flatten() A[:, A3] = 2 * N.repeat(checkerline(M2 * Q3), Q1) * N.repeat(q3_line, M2 * Q1) A[:, A4] = -N.repeat(q3_line, M2 * Q1) A[:, A5] = (2 * m2sign).flatten() A[:, A6] = -1.0 nz1 = ptmask.nonzero()[0] A = A[nz1] P = P[nz1] [u, s, vt] = N.linalg.svd(A, full_matrices=0) V = N.dot(vt.transpose(), N.dot(N.diag(1 / s), N.dot(u.transpose(), P))) ## import pylab as pl ## ptmask = N.reshape(ptmask, phs.shape) ## for s in q3_line: ## r_ind_ev = N.nonzero(ptmask[s,0])[0] ## r_ind_od = N.nonzero(ptmask[s,1])[0] ## if r_ind_ev.any() and r_ind_od.any(): ## ## if self.shear_correct: ## rowpos = 2*q1_line*V[A1] - q1_line*V[A2] +\ ## 2*s*V[A3] - s*V[A4] + 2*V[A5] - V[A6] ## rowneg = -2*q1_line*V[A1] - q1_line*V[A2] + \ ## -2*s*V[A3] - s*V[A4] - 2*V[A5] - V[A6] ## ## else: ## ## rowpos = 2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A5] ## ## rowneg = -(2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A5]) ## pl.plot(rowpos, 'b.') ## pl.plot(rowneg, 'r.') ## pl.plot(phs[s,0], 'b--') ## pl.plot(r_ind_ev, phs[s,0,r_ind_ev], 'bo', alpha=0.25) ## pl.plot(phs[s,1], 'r--') ## pl.plot(r_ind_od, phs[s,1,r_ind_od], 'ro', alpha=0.25) ## pl.title("slice %d"%s) ## pl.show() return V
def solve_phase_3d(phs, ptmask): """ let V = (a1 a3 a0)^T, we want to solve: phi(q3,m2,q1) = (-1)^(m2) * 2[q1*A1 + q3*A3 + A0] with our overdetermined data. so P = [neg[q3=0] pos[q3=0] neg[q3=1] pos[q3=1] ... neg[S] pos[S]]^T for all selected slices, and similarly A = [-2q1_0 -2q3_0 -2; -2q1_1 -2q3_0 -2; <-- phi(q3=0,m2=odd; q1) ... 2q1_0 2q3_0 2; 2q1_1 2q3_0 2; <-- phi(q3=0,m2=evn; q1) ... -2q1_0 -2q3_1 -2; -2q1_1 -2q3_1 -2; <-- phi(q3=1,m2=odd; q1) ... 2q1_1 2q3_1 2; <-- phi(q3=1,m2=evn; q1) ... ] Then with AV = P, solve V = inv(A)P """ Q3, M2, Q1 = phs.shape A1, A3, A0 = (0, 1, 2) # build the full matrix first, collapse the zero-rows afterwards nrows = N.product(phs.shape) A = N.zeros((nrows, 3), N.float64) P = phs.copy() P.shape = (nrows,) ptmask = N.reshape(ptmask, (nrows,)) q1_line = N.linspace(-Q1 / 2.0, Q1 / 2.0, Q1, endpoint=False) q3_line = N.linspace(0.0, Q3, Q3, endpoint=False) m2sign = N.outer(checkerline(M2 * Q3), N.ones(Q1)) A[:, A1] = (2 * m2sign * q1_line).flatten() A[:, A3] = 2 * N.repeat(checkerline(M2 * Q3), Q1) * N.repeat(q3_line, M2 * Q1) A[:, A0] = (2 * m2sign).flatten() nz1 = ptmask.nonzero()[0] A = A[nz1] P = P[nz1] [u, s, vt] = N.linalg.svd(A, full_matrices=0) V = N.dot(vt.transpose(), N.dot(N.diag(1 / s), N.dot(u.transpose(), P))) ## import pylab as pl ## ptmask = N.reshape(ptmask, phs.shape) ## for s in q3_line: ## q1_ind_ev = N.nonzero(ptmask[s,0])[0] ## q1_ind_od = N.nonzero(ptmask[s,1])[0] ## if q1_ind_ev.any() and q1_ind_od.any(): ## rowpos = 2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A0] ## rowneg = -(2*q1_line*V[A1] + 2*s*V[A3] + 2*V[A0]) ## pl.plot(rowpos, 'b.') ## pl.plot(rowneg, 'r.') ## pl.plot(phs[s,0], 'b--') ## pl.plot(q1_ind_ev, phs[s,0,q1_ind_ev], 'bo', alpha=0.25) ## pl.plot(phs[s,1], 'r--') ## pl.plot(q1_ind_od, phs[s,1,q1_ind_od], 'ro', alpha=0.25) ## pl.title("slice %d"%s) ## pl.show() return V