def compare_subsampled(S, s, ps, psf, pixpsf, Gmine, v, w, gal_sigma, psf_sigma, cd, get_ffts=False, eval_psf=integrate_gaussian): print() print('Subsample', s) print() step = 1. / s sz = s * (S - 1) + 1 #x = np.arange(0, S, step)[:sz+1] x = np.arange(0, S, step)[:sz] #y = np.arange(0, S, step)[:sz+1] # Create pixelized PSF (Gaussian) sx = x - 0.5 + step / 2. subpixpsf = eval_psf(psf, sx, sx) binned = bin_image(subpixpsf, s) bh, bw = binned.shape pixpsf1 = pixpsf[:bh, :bw] ph, pw = pixpsf.shape binned = binned[:ph, :pw] print('Binned PSF:') measure(binned) print('Pix PSF:') measure(pixpsf) # Recompute my convolution using the binned PSF P, FG, Gmine, v, w = galaxy_psf_convolution(gal_sigma, 0., 0., GaussianGalaxy, cd, 0., 0., binned, debug=True) xx, yy = np.meshgrid(x, x) # plt.clf() # # plt.subplot(2,2,1) # dimshow(subpixpsf) # plt.title('subpix psf') # plt.colorbar() # # plt.subplot(2,2,2) # dimshow(binned) # plt.title('binned subpix psf') # plt.colorbar() # # plt.subplot(2,2,3) # dimshow(pixpsf1) # plt.title('pix psf') # plt.colorbar() # # plt.subplot(2,2,4) # dimshow(pixpsf1 - binned) # plt.title('pix - binned') # plt.colorbar() # plt.suptitle('subsample %i' % s) # ps.savefig() # Create pixelized galaxy image #gxx,gyy = xx + step/2., yy + step/2. gxx, gyy = xx, yy #gxx,gyy = xx - step, yy - step #gxx,gyy = xx - step/2., yy - step/2. center = S / 2 subpixgal = np.exp(-0.5 * ((gxx - center)**2 + (gyy - center)**2) / gal_sigma**2) sh, sw = subpixpsf.shape subpixgal = subpixgal[:sh, :sw] print('Subpix psf, gal', subpixpsf.shape, subpixgal.shape) print('Subpix PSF:') measure(subpixpsf) print('Subpix gal:') measure(subpixgal) # FFT convolution Fpsf = np.fft.rfft2(subpixpsf) spg = np.fft.ifftshift(subpixgal) # plt.clf() # for i in range(len(w)): # plt.plot(v, Fpsf[i,:], 'c-') # for i in range(len(v)): # plt.plot(w, Fpsf[:,i], 'm-') # plt.title('PSF Fourier transform') # ps.savefig() # # IV = np.argsort(v) # IW = np.argsort(w) # plt.clf() # for i in range(len(w)): # plt.plot(v[IV], np.abs(Fpsf[i,IV]), 'c-') # for i in range(len(v)): # plt.plot(w[IW], np.abs(Fpsf[IW,i]), 'm-') # plt.title('abs PSF Fourier transform') # ps.savefig() # # plt.yscale('log') # ps.savefig() # plt.clf() # dimshow(spg) # plt.title('spg') # ps.savefig() Fgal = np.fft.rfft2(spg) if get_ffts: return Fpsf, Fgal Fconv = Fpsf * Fgal subpixfft = np.fft.irfft2(Fconv, s=subpixpsf.shape) print('Shapes:', 'subpixpsf', subpixpsf.shape, 'Fpsf', Fpsf.shape) print('spg', spg.shape, 'Fgal', Fgal.shape, 'Fconv', Fconv.shape, 'subpixfft', subpixfft.shape) print('Subpix conv', subpixfft.shape) binned = bin_image(subpixfft, s) binned /= np.sum(binned) print('Binned', binned.shape) print('Mine:', Gmine.shape) print('Mine:') measure(Gmine) print('Binned subpix FFT:') measure(binned) mh, mw = Gmine.shape binned = binned[:mh, :mw] plt.clf() plt.subplot(2, 3, 1) dimshow(subpixpsf) plt.title('subpix psf') plt.colorbar() plt.subplot(2, 3, 2) dimshow(subpixgal) plt.title('subpix galaxy') plt.colorbar() plt.subplot(2, 3, 3) dimshow(subpixfft) plt.title('subpix FFT conv') plt.colorbar() plt.subplot(2, 3, 4) dimshow(np.log10(np.maximum(binned / np.max(binned), 1e-12))) plt.title('log binned FFT conv') plt.colorbar() plt.subplot(2, 3, 5) dimshow(np.log10(np.maximum(Gmine / np.max(Gmine), 1e-12))) #dimshow(Gmine) plt.title('log my conv') plt.colorbar() gh, gw = Gmine.shape binned = binned[:gh, :gw] bh, bw = binned.shape Gmine = Gmine[:bh, :bw] diff = Gmine - binned plt.subplot(2, 3, 6) dimshow(diff) plt.title('mine - FFT') plt.colorbar() plt.suptitle('PSF %g, Gal %g, subsample %i' % (psf_sigma, gal_sigma, s)) ps.savefig() rmsdiff = np.sqrt(np.mean(diff**2)) return rmsdiff
plt.colorbar() plt.title('subpix Gal real') plt.subplot(2, 4, 8) dimshow(Fsubgal.imag) plt.colorbar() plt.title('subpix Gal imag') ps.savefig() pixconv = np.fft.irfft2(Fpsf * Fgal, s=pixpsf.shape) subpixconv = np.fft.irfft2(Fsub * Fsubgal, s=subpixpsf.shape) # my convolution pixscale = 1. cd = pixscale * np.eye(2) / 3600. Gmine = galaxy_psf_convolution(gal_re, 0., 0., gal, cd, 0., 0., pixpsf) pixconv /= np.sum(pixconv) subpixconv /= np.sum(subpixconv) subpixconv *= scale**2 Gmine /= np.sum(Gmine) plt.clf() plt.subplot(1, 3, 1) plt.imshow(pixconv, interpolation='nearest', origin='lower') plt.subplot(1, 3, 2) plt.imshow(subpixconv, interpolation='nearest', origin='lower') plt.subplot(1, 3, 3) plt.imshow(Gmine, interpolation='nearest', origin='lower') ps.savefig()
def main(): ps = PlotSequence('conv') S = 51 center = S / 2 print('Center', center) #for psf_sigma in [2., 1.5, 1.]: for psf_sigma in [2.]: rms2 = [] x = np.arange(S) y = np.arange(S) xx, yy = np.meshgrid(x, y) scale = 1.5 / psf_sigma pixpsf = render_airy((scale, center), x, y) psf = (scale, center) eval_psf = render_airy plt.clf() plt.subplot(2, 1, 1) plt.plot(x, pixpsf[center, :], 'b-') plt.plot(x, pixpsf[:, center], 'r-') plt.subplot(2, 1, 2) plt.plot(x, np.maximum(1e-16, pixpsf[center, :]), 'b-') plt.plot(x, np.maximum(1e-16, pixpsf[:, center]), 'r-') plt.yscale('log') ps.savefig() plt.clf() plt.imshow(pixpsf, interpolation='nearest', origin='lower') ps.savefig() plt.clf() plt.imshow(np.log10(np.maximum(1e-16, pixpsf)), interpolation='nearest', origin='lower') plt.colorbar() plt.title('log PSF') ps.savefig() # psf #psf = scipy.stats.norm(loc=center + 0.5, scale=psf_sigma) # plt.clf() # plt.imshow(Pcdf, interpolation='nearest', origin='lower') # ps.savefig() # #Pcdf = psf.cdf(xx) * psf.cdf(yy) # #pixpsf = integrate_gaussian(psf, xx, yy) # # padpsf = np.zeros((S*2-1, S*2-1)) # ph,pw = pixpsf.shape # padpsf[S/2:S/2+ph, S/2:S/2+pw] = pixpsf # Fpsf = np.fft.rfft2(padpsf) # # padh,padw = padpsf.shape # v = np.fft.rfftfreq(padw) # w = np.fft.fftfreq(padh) # fmax = max(max(np.abs(v)), max(np.abs(w))) # cut = fmax / 2. * 1.000001 # #print('Frequence cut:', cut) # Ffiltpsf = Fpsf.copy() # #print('Ffiltpsf', Ffiltpsf.shape) # #print((np.abs(w) < cut).shape) # #print((np.abs(v) < cut).shape) # Ffiltpsf[np.abs(w) > cut, :] = 0. # Ffiltpsf[:, np.abs(v) > cut] = 0. # #print('pad v', v) # #print('pad w', w) # # filtpsf = np.fft.irfft2(Ffiltpsf, s=(padh,padw)) # # print('filtered PSF real', np.max(np.abs(filtpsf.real))) # print('filtered PSF imag', np.max(np.abs(filtpsf.imag))) # # plt.clf() # plt.subplot(2,3,1) # dimshow(Fpsf.real) # plt.colorbar() # plt.title('Padded PSF real') # plt.subplot(2,3,4) # dimshow(Fpsf.imag) # plt.colorbar() # plt.title('Padded PSF imag') # # plt.subplot(2,3,2) # dimshow(Ffiltpsf.real) # plt.colorbar() # plt.title('Filt PSF real') # plt.subplot(2,3,5) # dimshow(Ffiltpsf.imag) # plt.colorbar() # plt.title('Filt PSF imag') # # plt.subplot(2,3,3) # dimshow(filtpsf.real) # plt.title('PSF real') # plt.colorbar() # # plt.subplot(2,3,6) # dimshow(filtpsf.imag) # plt.title('PSF imag') # plt.colorbar() # # ps.savefig() # # # pixpsf = filtpsf gal_sigmas = [2, 1, 0.5, 0.25] for gal_sigma in gal_sigmas: # plt.clf() # plt.imshow(Gcdf, interpolation='nearest', origin='lower') # plt.savefig('dcdf.png') # plt.clf() # plt.imshow(np.exp(-0.5 * ((xx-center)**2 + (yy-center)**2)/2.**2), # interpolation='nearest', origin='lower') # plt.savefig('g.png') # my convolution pixscale = 1. cd = pixscale * np.eye(2) / 3600. P, FG, Gmine, v, w = galaxy_psf_convolution(gal_sigma, 0., 0., GaussianGalaxy, cd, 0., 0., pixpsf, debug=True) #print('v:', v) #print('w:', w) #print('P:', P.shape) print() print('PSF %g, Gal %g' % (psf_sigma, gal_sigma)) rmax = np.argmax(np.abs(w)) cmax = np.argmax(np.abs(v)) l2_rmax = np.sqrt(np.sum(P[rmax, :].real**2 + P[rmax, :].imag**2)) l2_cmax = np.sqrt(np.sum(P[:, cmax].real**2 + P[:, cmax].imag**2)) print('PSF L_2 in highest-frequency rows & cols:', l2_rmax, l2_cmax) l2_rmax = np.sqrt(np.sum(FG[rmax, :].real**2 + FG[rmax, :].imag**2)) l2_cmax = np.sqrt(np.sum(FG[:, cmax].real**2 + FG[:, cmax].imag**2)) print('Gal L_2 in highest-frequency rows & cols:', l2_rmax, l2_cmax) C = P * FG l2_rmax = np.sqrt(np.sum(C[rmax, :].real**2 + C[rmax, :].imag**2)) l2_cmax = np.sqrt(np.sum(C[:, cmax].real**2 + C[:, cmax].imag**2)) print('PSF*Gal L_2 in highest-frequency rows & cols:', l2_rmax, l2_cmax) print() Fpsf, Fgal = compare_subsampled(S, 1, ps, psf, pixpsf, Gmine, v, w, gal_sigma, psf_sigma, cd, get_ffts=True, eval_psf=eval_psf) plt.clf() plt.subplot(2, 4, 1) dimshow(P.real) plt.colorbar() plt.title('PSF real') plt.subplot(2, 4, 5) dimshow(P.imag) plt.colorbar() plt.title('PSF imag') plt.subplot(2, 4, 2) dimshow(FG.real) plt.colorbar() plt.title('Gal real') plt.subplot(2, 4, 6) dimshow(FG.imag) plt.colorbar() plt.title('Gal imag') plt.subplot(2, 4, 3) dimshow((P * FG).real) plt.colorbar() plt.title('P*Gal real') plt.subplot(2, 4, 7) dimshow((P * FG).imag) plt.colorbar() plt.title('P*Gal imag') plt.subplot(2, 4, 4) dimshow((Fgal).real) plt.colorbar() plt.title('pixGal real') plt.subplot(2, 4, 8) dimshow((Fgal).imag) plt.colorbar() plt.title('pixGal imag') plt.suptitle('PSF %g, Gal %g' % (psf_sigma, gal_sigma)) ps.savefig() subsample = [1, 2, 4] rms1 = [] for s in subsample: rms = compare_subsampled(S, s, ps, psf, pixpsf, Gmine, v, w, gal_sigma, psf_sigma, cd, eval_psf=eval_psf) rms1.append(rms) rms2.append(rms1) print() print('PSF sigma =', psf_sigma) print('RMSes:') for rms1, gal_sigma in zip(rms2, gal_sigmas): print('Gal sigma', gal_sigma, 'rms:', ', '.join(['%.3g' % r for r in rms1]))
def compare(ph, pw, psf_sigma, gal_sigma, ps, subsample): pixscale = 1. cd = pixscale * np.eye(2) / 3600. cx,cy = pw/2, ph/2 xx,yy = np.meshgrid(np.arange(pw), np.arange(ph)) # Create pixelized PSF (Gaussian) pixpsf = np.exp(-0.5 * ((xx-cx)**2 + (yy-cy)**2) / psf_sigma**2) plt.clf() plt.imshow(pixpsf, interpolation='nearest', origin='lower', cmap='gray') plt.title('PSF') plt.colorbar() ps.savefig() #Gmine = galaxy_psf_convolution(gal_sigma, 0., 0., GaussianGalaxy, cd, # 0., 0., pixpsf) P,FG,Gmine,v,w = galaxy_psf_convolution(gal_sigma, 0., 0., GaussianGalaxy, cd, 0., 0., pixpsf, debug=True) Gmine /= Gmine.sum() plt.clf() plt.imshow(Gmine, interpolation='nearest', origin='lower', cmap='gray') plt.title('Mine') plt.colorbar() ps.savefig() print('Mine:') measure(Gmine) # Create pixelized PSF (x) Gaussian gal_psf_sigma = np.hypot(psf_sigma, gal_sigma) Gana = np.exp(-0.5 * ((xx-cx)**2 + (yy-cy)**2) / gal_psf_sigma**2) Gana /= Gana.sum() print('Pixelized, analytic:') measure(Gana) gal = galsim.Gaussian(flux=1., sigma=gal_sigma) psf = galsim.Gaussian(flux=1., sigma=psf_sigma) final = galsim.Convolve([gal, psf]) print('Final:', final) image = galsim.ImageF(pw,ph) final.drawImage(image, offset=(0.5, 0.5), scale=pixscale, method='sb') Ggs = image.array Ggs /= Ggs.sum() print('Galsim shifted:') measure(Ggs) plt.clf() plt.imshow(Ggs, interpolation='nearest', origin='lower', cmap='gray') plt.title('GalSim analytic') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Ggs - Gana, 'GalSim Analytic - Analytic') plt.colorbar() ps.savefig() plt.clf() plt.imshow(Gana, interpolation='nearest', origin='lower', cmap='gray') plt.title('Analytic') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gmine - Gana, 'Mine - Analytic') plt.colorbar() ps.savefig() r = np.hypot(xx - cx, yy - cy) plt.clf() plt.subplot(2,1,1) plt.loglog(r.ravel(), Gmine.ravel(), 'b.') plt.loglog(r.ravel(), Gana.ravel(), 'g.') plt.ylim(1e-20, 1.) plt.ylabel('mine(b) analytic(g)') plt.subplot(2,1,2) plt.plot(r.ravel(), (Gmine-Gana).ravel(), 'r.') plt.ylabel('mine - analytic') plt.xlabel('radius (pix)') ps.savefig() plt.clf() plt.subplot(1,2,1) plt.imshow(np.log10(np.maximum(Gana, 1e-16)), interpolation='nearest', origin='lower', cmap='gray') plt.title('Log Analytic') plt.colorbar() plt.subplot(1,2,2) plt.imshow(np.log10(np.maximum(Gmine, 1e-16)), interpolation='nearest', origin='lower', cmap='gray') plt.title('Log Mine') plt.colorbar() ps.savefig() # Create pixelized galaxy gx,gy = cx,cy pixgal = np.exp(-0.5 * ((xx-gx)**2 + (yy-gy)**2) / gal_sigma**2) pixgal /= np.sum(pixgal) # FFT convolution Fpsf = np.fft.rfft2(pixpsf) print('Fpsf:', Fpsf.shape, Fpsf.dtype) spg = np.fft.ifftshift(pixgal) Fgal = np.fft.rfft2(spg) #Fgal = np.fft.rfft2(pixgal) print('Fgal:', Fgal.shape, Fgal.dtype) plt.clf() plt.subplot(1,2,1) plt.imshow(Fpsf.real, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('Fpsf.real') plt.subplot(1,2,2) plt.imshow(Fpsf.imag, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('Fpsf.imag') ps.savefig() print('P:', P.shape, P.dtype) plt.clf() plt.subplot(1,2,1) plt.imshow(P.real, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('P.real') plt.subplot(1,2,2) plt.imshow(P.imag, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('P.imag') ps.savefig() psf2 = np.fft.irfft2(Fpsf) plt.clf() plt.imshow(psf2 - pixpsf, interpolation='nearest', origin='lower', cmap='gray') plt.title('ifft(fft(psf)) - psf') plt.colorbar() ps.savefig() plt.clf() plt.subplot(1,2,1) plt.imshow(Fgal.real, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fgal.real [%.2g,%.2g]' % (Fgal.real.min(), Fgal.real.max())) plt.colorbar() plt.subplot(1,2,2) plt.imshow(Fgal.imag, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fgal.imag [%.2g,%.2g]' % (Fgal.imag.min(), Fgal.imag.max())) plt.colorbar() ps.savefig() print('FG:', FG.shape, FG.dtype) plt.clf() plt.subplot(1,2,1) plt.imshow(FG.real, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('FG.real [%.2g,%.2g]' % (FG.real.min(), FG.real.max())) plt.subplot(1,2,2) plt.imshow(FG.imag, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('FG.imag [%.2g,%.2g]' % (FG.imag.min(), FG.imag.max())) ps.savefig() Fconv = Fpsf * Fgal plt.clf() plt.subplot(1,2,1) plt.imshow(Fconv.real, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconv.real') plt.colorbar() plt.subplot(1,2,2) plt.imshow(Fconv.imag, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconv.imag') plt.colorbar() ps.savefig() Fconvmine = P * FG plt.clf() plt.subplot(1,2,1) plt.imshow(Fconvmine.real, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconvmine.real') plt.colorbar() plt.subplot(1,2,2) plt.imshow(Fconvmine.imag, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconvmine.imag') plt.colorbar() ps.savefig() Fgal.imag[:,:] = 0. Gfft = np.fft.irfft2(Fpsf * Fgal) #Gfft = np.fft.ifftshift(Gfft) print('Gfft:', Gfft.shape, Gfft.dtype) Gfft /= Gfft.sum() print('Pixelized (fft):') measure(Gfft) plt.clf() plt.imshow(Gfft, interpolation='nearest', origin='lower', cmap='gray') plt.title('Pixelized convolution') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gfft - Gana, 'Pixelized - Analytic') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gmine - Gfft, 'Mine - Pixelized') plt.colorbar() ps.savefig() for s in subsample: step = 1./s #xx,yy = np.meshgrid(-step/2. + np.arange(0, pw, step), # -step/2. + np.arange(0, ph, step)) xx,yy = np.meshgrid(-0.5 + step/2. + np.arange(0, pw, step), -0.5 + step/2. + np.arange(0, ph, step)) # Create pixelized PSF (Gaussian) #pixpsf = np.exp(-0.5 * ((xx-cx)**2 + (yy-cy)**2) / psf_sigma**2) subpixpsf = np.repeat(np.repeat(pixpsf, s, axis=0), s, axis=1) gxx,gyy = np.meshgrid(np.arange(0, pw*s), np.arange(0, ph*s)) gx,gy = pw*s/2, ph*s/2 pixgal = np.exp(-0.5 * ((gxx-gx)**2 + (gyy-gy)**2) / (gal_sigma*s)**2) #gx,gy = cx - step/2., cy - step/2. #pixgal = np.exp(-0.5 * ((xx-gx)**2 + (yy-gy)**2) / gal_sigma**2) # FFT convolution Fpsf = np.fft.rfft2(subpixpsf) print('Fpsf:', Fpsf.shape, Fpsf.dtype) Fgal = np.fft.rfft2(pixgal) print('Fgal:', Fgal.shape, Fgal.dtype) Gfft = np.fft.irfft2(Fpsf * Fgal) Gfft = np.fft.ifftshift(Gfft) print('Gfft:', Gfft.shape, Gfft.dtype) Gfft /= Gfft.sum() print('Subsampled, pixelized:') measure(Gfft) plt.clf() plt.imshow(Gfft, interpolation='nearest', origin='lower', cmap='gray') plt.title('Subsampled convolution') plt.colorbar() ps.savefig() # Bin down s x s Gbin = np.zeros((ph, pw)) for i in range(s): for j in range(s): Gbin += Gfft[i::s, j::s] Gfft = Gbin Gfft /= Gfft.sum() print('Subsampled, pixelized:') measure(Gfft) plt.clf() plt.imshow(Gfft, interpolation='nearest', origin='lower', cmap='gray') plt.title('Subsampled & binned convolution') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gfft - Gana, 'Sub-Pixelized - Analytic') plt.colorbar() ps.savefig()
def compare(ph, pw, psf_sigma, gal_sigma, ps, subsample): pixscale = 1. cd = pixscale * np.eye(2) / 3600. cx, cy = pw / 2, ph / 2 xx, yy = np.meshgrid(np.arange(pw), np.arange(ph)) # Create pixelized PSF (Gaussian) pixpsf = np.exp(-0.5 * ((xx - cx)**2 + (yy - cy)**2) / psf_sigma**2) plt.clf() plt.imshow(pixpsf, interpolation='nearest', origin='lower', cmap='gray') plt.title('PSF') plt.colorbar() ps.savefig() #Gmine = galaxy_psf_convolution(gal_sigma, 0., 0., GaussianGalaxy, cd, # 0., 0., pixpsf) P, FG, Gmine, v, w = galaxy_psf_convolution(gal_sigma, 0., 0., GaussianGalaxy, cd, 0., 0., pixpsf, debug=True) Gmine /= Gmine.sum() plt.clf() plt.imshow(Gmine, interpolation='nearest', origin='lower', cmap='gray') plt.title('Mine') plt.colorbar() ps.savefig() print('Mine:') measure(Gmine) # Create pixelized PSF (x) Gaussian gal_psf_sigma = np.hypot(psf_sigma, gal_sigma) Gana = np.exp(-0.5 * ((xx - cx)**2 + (yy - cy)**2) / gal_psf_sigma**2) Gana /= Gana.sum() print('Pixelized, analytic:') measure(Gana) gal = galsim.Gaussian(flux=1., sigma=gal_sigma) psf = galsim.Gaussian(flux=1., sigma=psf_sigma) final = galsim.Convolve([gal, psf]) print('Final:', final) image = galsim.ImageF(pw, ph) final.drawImage(image, offset=(0.5, 0.5), scale=pixscale, method='sb') Ggs = image.array Ggs /= Ggs.sum() print('Galsim shifted:') measure(Ggs) plt.clf() plt.imshow(Ggs, interpolation='nearest', origin='lower', cmap='gray') plt.title('GalSim analytic') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Ggs - Gana, 'GalSim Analytic - Analytic') plt.colorbar() ps.savefig() plt.clf() plt.imshow(Gana, interpolation='nearest', origin='lower', cmap='gray') plt.title('Analytic') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gmine - Gana, 'Mine - Analytic') plt.colorbar() ps.savefig() r = np.hypot(xx - cx, yy - cy) plt.clf() plt.subplot(2, 1, 1) plt.loglog(r.ravel(), Gmine.ravel(), 'b.') plt.loglog(r.ravel(), Gana.ravel(), 'g.') plt.ylim(1e-20, 1.) plt.ylabel('mine(b) analytic(g)') plt.subplot(2, 1, 2) plt.plot(r.ravel(), (Gmine - Gana).ravel(), 'r.') plt.ylabel('mine - analytic') plt.xlabel('radius (pix)') ps.savefig() plt.clf() plt.subplot(1, 2, 1) plt.imshow(np.log10(np.maximum(Gana, 1e-16)), interpolation='nearest', origin='lower', cmap='gray') plt.title('Log Analytic') plt.colorbar() plt.subplot(1, 2, 2) plt.imshow(np.log10(np.maximum(Gmine, 1e-16)), interpolation='nearest', origin='lower', cmap='gray') plt.title('Log Mine') plt.colorbar() ps.savefig() # Create pixelized galaxy gx, gy = cx, cy pixgal = np.exp(-0.5 * ((xx - gx)**2 + (yy - gy)**2) / gal_sigma**2) pixgal /= np.sum(pixgal) # FFT convolution Fpsf = np.fft.rfft2(pixpsf) print('Fpsf:', Fpsf.shape, Fpsf.dtype) spg = np.fft.ifftshift(pixgal) Fgal = np.fft.rfft2(spg) #Fgal = np.fft.rfft2(pixgal) print('Fgal:', Fgal.shape, Fgal.dtype) plt.clf() plt.subplot(1, 2, 1) plt.imshow(Fpsf.real, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('Fpsf.real') plt.subplot(1, 2, 2) plt.imshow(Fpsf.imag, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('Fpsf.imag') ps.savefig() print('P:', P.shape, P.dtype) plt.clf() plt.subplot(1, 2, 1) plt.imshow(P.real, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('P.real') plt.subplot(1, 2, 2) plt.imshow(P.imag, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('P.imag') ps.savefig() psf2 = np.fft.irfft2(Fpsf) plt.clf() plt.imshow(psf2 - pixpsf, interpolation='nearest', origin='lower', cmap='gray') plt.title('ifft(fft(psf)) - psf') plt.colorbar() ps.savefig() plt.clf() plt.subplot(1, 2, 1) plt.imshow(Fgal.real, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fgal.real [%.2g,%.2g]' % (Fgal.real.min(), Fgal.real.max())) plt.colorbar() plt.subplot(1, 2, 2) plt.imshow(Fgal.imag, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fgal.imag [%.2g,%.2g]' % (Fgal.imag.min(), Fgal.imag.max())) plt.colorbar() ps.savefig() print('FG:', FG.shape, FG.dtype) plt.clf() plt.subplot(1, 2, 1) plt.imshow(FG.real, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('FG.real [%.2g,%.2g]' % (FG.real.min(), FG.real.max())) plt.subplot(1, 2, 2) plt.imshow(FG.imag, interpolation='nearest', origin='lower', cmap='gray') plt.colorbar() plt.title('FG.imag [%.2g,%.2g]' % (FG.imag.min(), FG.imag.max())) ps.savefig() Fconv = Fpsf * Fgal plt.clf() plt.subplot(1, 2, 1) plt.imshow(Fconv.real, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconv.real') plt.colorbar() plt.subplot(1, 2, 2) plt.imshow(Fconv.imag, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconv.imag') plt.colorbar() ps.savefig() Fconvmine = P * FG plt.clf() plt.subplot(1, 2, 1) plt.imshow(Fconvmine.real, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconvmine.real') plt.colorbar() plt.subplot(1, 2, 2) plt.imshow(Fconvmine.imag, interpolation='nearest', origin='lower', cmap='gray') plt.title('Fconvmine.imag') plt.colorbar() ps.savefig() Fgal.imag[:, :] = 0. Gfft = np.fft.irfft2(Fpsf * Fgal) #Gfft = np.fft.ifftshift(Gfft) print('Gfft:', Gfft.shape, Gfft.dtype) Gfft /= Gfft.sum() print('Pixelized (fft):') measure(Gfft) plt.clf() plt.imshow(Gfft, interpolation='nearest', origin='lower', cmap='gray') plt.title('Pixelized convolution') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gfft - Gana, 'Pixelized - Analytic') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gmine - Gfft, 'Mine - Pixelized') plt.colorbar() ps.savefig() for s in subsample: step = 1. / s #xx,yy = np.meshgrid(-step/2. + np.arange(0, pw, step), # -step/2. + np.arange(0, ph, step)) xx, yy = np.meshgrid(-0.5 + step / 2. + np.arange(0, pw, step), -0.5 + step / 2. + np.arange(0, ph, step)) # Create pixelized PSF (Gaussian) #pixpsf = np.exp(-0.5 * ((xx-cx)**2 + (yy-cy)**2) / psf_sigma**2) subpixpsf = np.repeat(np.repeat(pixpsf, s, axis=0), s, axis=1) gxx, gyy = np.meshgrid(np.arange(0, pw * s), np.arange(0, ph * s)) gx, gy = pw * s / 2, ph * s / 2 pixgal = np.exp(-0.5 * ((gxx - gx)**2 + (gyy - gy)**2) / (gal_sigma * s)**2) #gx,gy = cx - step/2., cy - step/2. #pixgal = np.exp(-0.5 * ((xx-gx)**2 + (yy-gy)**2) / gal_sigma**2) # FFT convolution Fpsf = np.fft.rfft2(subpixpsf) print('Fpsf:', Fpsf.shape, Fpsf.dtype) Fgal = np.fft.rfft2(pixgal) print('Fgal:', Fgal.shape, Fgal.dtype) Gfft = np.fft.irfft2(Fpsf * Fgal) Gfft = np.fft.ifftshift(Gfft) print('Gfft:', Gfft.shape, Gfft.dtype) Gfft /= Gfft.sum() print('Subsampled, pixelized:') measure(Gfft) plt.clf() plt.imshow(Gfft, interpolation='nearest', origin='lower', cmap='gray') plt.title('Subsampled convolution') plt.colorbar() ps.savefig() # Bin down s x s Gbin = np.zeros((ph, pw)) for i in range(s): for j in range(s): Gbin += Gfft[i::s, j::s] Gfft = Gbin Gfft /= Gfft.sum() print('Subsampled, pixelized:') measure(Gfft) plt.clf() plt.imshow(Gfft, interpolation='nearest', origin='lower', cmap='gray') plt.title('Subsampled & binned convolution') plt.colorbar() ps.savefig() plt.clf() imshow_diff(Gfft - Gana, 'Sub-Pixelized - Analytic') plt.colorbar() ps.savefig()
def compare_subsampled(S, s, ps, psf, pixpsf, Gmine,v,w, gal_sigma, psf_sigma, cd, get_ffts=False, eval_psf=integrate_gaussian): print() print('Subsample', s) print() step = 1./s sz = s * (S-1) + 1 #x = np.arange(0, S, step)[:sz+1] x = np.arange(0, S, step)[:sz] #y = np.arange(0, S, step)[:sz+1] # Create pixelized PSF (Gaussian) sx = x - 0.5 + step/2. subpixpsf = eval_psf(psf, sx, sx) binned = bin_image(subpixpsf, s) bh,bw = binned.shape pixpsf1 = pixpsf[:bh,:bw] ph,pw = pixpsf.shape binned = binned[:ph,:pw] print('Binned PSF:') measure(binned) print('Pix PSF:') measure(pixpsf) # Recompute my convolution using the binned PSF P,FG,Gmine,v,w = galaxy_psf_convolution( gal_sigma, 0., 0., GaussianGalaxy, cd, 0., 0., binned, debug=True) xx,yy = np.meshgrid(x,x) # plt.clf() # # plt.subplot(2,2,1) # dimshow(subpixpsf) # plt.title('subpix psf') # plt.colorbar() # # plt.subplot(2,2,2) # dimshow(binned) # plt.title('binned subpix psf') # plt.colorbar() # # plt.subplot(2,2,3) # dimshow(pixpsf1) # plt.title('pix psf') # plt.colorbar() # # plt.subplot(2,2,4) # dimshow(pixpsf1 - binned) # plt.title('pix - binned') # plt.colorbar() # plt.suptitle('subsample %i' % s) # ps.savefig() # Create pixelized galaxy image #gxx,gyy = xx + step/2., yy + step/2. gxx,gyy = xx,yy #gxx,gyy = xx - step, yy - step #gxx,gyy = xx - step/2., yy - step/2. center = S/2 subpixgal = np.exp(-0.5 * ((gxx-center)**2 + (gyy-center)**2)/gal_sigma**2) sh,sw = subpixpsf.shape subpixgal = subpixgal[:sh,:sw] print('Subpix psf, gal', subpixpsf.shape, subpixgal.shape) print('Subpix PSF:') measure(subpixpsf) print('Subpix gal:') measure(subpixgal) # FFT convolution Fpsf = np.fft.rfft2(subpixpsf) spg = np.fft.ifftshift(subpixgal) # plt.clf() # for i in range(len(w)): # plt.plot(v, Fpsf[i,:], 'c-') # for i in range(len(v)): # plt.plot(w, Fpsf[:,i], 'm-') # plt.title('PSF Fourier transform') # ps.savefig() # # IV = np.argsort(v) # IW = np.argsort(w) # plt.clf() # for i in range(len(w)): # plt.plot(v[IV], np.abs(Fpsf[i,IV]), 'c-') # for i in range(len(v)): # plt.plot(w[IW], np.abs(Fpsf[IW,i]), 'm-') # plt.title('abs PSF Fourier transform') # ps.savefig() # # plt.yscale('log') # ps.savefig() # plt.clf() # dimshow(spg) # plt.title('spg') # ps.savefig() Fgal = np.fft.rfft2(spg) if get_ffts: return Fpsf, Fgal Fconv = Fpsf * Fgal subpixfft = np.fft.irfft2(Fconv, s=subpixpsf.shape) print('Shapes:', 'subpixpsf', subpixpsf.shape, 'Fpsf', Fpsf.shape) print('spg', spg.shape, 'Fgal', Fgal.shape, 'Fconv', Fconv.shape, 'subpixfft', subpixfft.shape) print('Subpix conv', subpixfft.shape) binned = bin_image(subpixfft, s) binned /= np.sum(binned) print('Binned', binned.shape) print('Mine:', Gmine.shape) print('Mine:') measure(Gmine) print('Binned subpix FFT:') measure(binned) mh,mw = Gmine.shape binned = binned[:mh,:mw] plt.clf() plt.subplot(2,3,1) dimshow(subpixpsf) plt.title('subpix psf') plt.colorbar() plt.subplot(2,3,2) dimshow(subpixgal) plt.title('subpix galaxy') plt.colorbar() plt.subplot(2,3,3) dimshow(subpixfft) plt.title('subpix FFT conv') plt.colorbar() plt.subplot(2,3,4) dimshow(np.log10(np.maximum(binned / np.max(binned), 1e-12))) plt.title('log binned FFT conv') plt.colorbar() plt.subplot(2,3,5) dimshow(np.log10(np.maximum(Gmine / np.max(Gmine), 1e-12))) #dimshow(Gmine) plt.title('log my conv') plt.colorbar() gh,gw = Gmine.shape binned = binned[:gh,:gw] bh,bw = binned.shape Gmine = Gmine[:bh,:bw] diff = Gmine - binned plt.subplot(2,3,6) dimshow(diff) plt.title('mine - FFT') plt.colorbar() plt.suptitle('PSF %g, Gal %g, subsample %i' % (psf_sigma, gal_sigma, s)) ps.savefig() rmsdiff = np.sqrt(np.mean(diff**2)) return rmsdiff
def main(): ps = PlotSequence('conv') S = 51 center = S/2 print('Center', center) #for psf_sigma in [2., 1.5, 1.]: for psf_sigma in [2.]: rms2 = [] x = np.arange(S) y = np.arange(S) xx,yy = np.meshgrid(x, y) scale = 1.5 / psf_sigma pixpsf = render_airy((scale, center), x, y) psf = (scale,center) eval_psf = render_airy plt.clf() plt.subplot(2,1,1) plt.plot(x, pixpsf[center,:], 'b-') plt.plot(x, pixpsf[:,center], 'r-') plt.subplot(2,1,2) plt.plot(x, np.maximum(1e-16, pixpsf[center,:]), 'b-') plt.plot(x, np.maximum(1e-16, pixpsf[:,center]), 'r-') plt.yscale('log') ps.savefig() plt.clf() plt.imshow(pixpsf, interpolation='nearest', origin='lower') ps.savefig() plt.clf() plt.imshow(np.log10(np.maximum(1e-16, pixpsf)), interpolation='nearest', origin='lower') plt.colorbar() plt.title('log PSF') ps.savefig() # psf #psf = scipy.stats.norm(loc=center + 0.5, scale=psf_sigma) # plt.clf() # plt.imshow(Pcdf, interpolation='nearest', origin='lower') # ps.savefig() # #Pcdf = psf.cdf(xx) * psf.cdf(yy) # #pixpsf = integrate_gaussian(psf, xx, yy) # # padpsf = np.zeros((S*2-1, S*2-1)) # ph,pw = pixpsf.shape # padpsf[S/2:S/2+ph, S/2:S/2+pw] = pixpsf # Fpsf = np.fft.rfft2(padpsf) # # padh,padw = padpsf.shape # v = np.fft.rfftfreq(padw) # w = np.fft.fftfreq(padh) # fmax = max(max(np.abs(v)), max(np.abs(w))) # cut = fmax / 2. * 1.000001 # #print('Frequence cut:', cut) # Ffiltpsf = Fpsf.copy() # #print('Ffiltpsf', Ffiltpsf.shape) # #print((np.abs(w) < cut).shape) # #print((np.abs(v) < cut).shape) # Ffiltpsf[np.abs(w) > cut, :] = 0. # Ffiltpsf[:, np.abs(v) > cut] = 0. # #print('pad v', v) # #print('pad w', w) # # filtpsf = np.fft.irfft2(Ffiltpsf, s=(padh,padw)) # # print('filtered PSF real', np.max(np.abs(filtpsf.real))) # print('filtered PSF imag', np.max(np.abs(filtpsf.imag))) # # plt.clf() # plt.subplot(2,3,1) # dimshow(Fpsf.real) # plt.colorbar() # plt.title('Padded PSF real') # plt.subplot(2,3,4) # dimshow(Fpsf.imag) # plt.colorbar() # plt.title('Padded PSF imag') # # plt.subplot(2,3,2) # dimshow(Ffiltpsf.real) # plt.colorbar() # plt.title('Filt PSF real') # plt.subplot(2,3,5) # dimshow(Ffiltpsf.imag) # plt.colorbar() # plt.title('Filt PSF imag') # # plt.subplot(2,3,3) # dimshow(filtpsf.real) # plt.title('PSF real') # plt.colorbar() # # plt.subplot(2,3,6) # dimshow(filtpsf.imag) # plt.title('PSF imag') # plt.colorbar() # # ps.savefig() # # # pixpsf = filtpsf gal_sigmas = [2, 1, 0.5, 0.25] for gal_sigma in gal_sigmas: # plt.clf() # plt.imshow(Gcdf, interpolation='nearest', origin='lower') # plt.savefig('dcdf.png') # plt.clf() # plt.imshow(np.exp(-0.5 * ((xx-center)**2 + (yy-center)**2)/2.**2), # interpolation='nearest', origin='lower') # plt.savefig('g.png') # my convolution pixscale = 1. cd = pixscale * np.eye(2) / 3600. P,FG,Gmine,v,w = galaxy_psf_convolution( gal_sigma, 0., 0., GaussianGalaxy, cd, 0., 0., pixpsf, debug=True) #print('v:', v) #print('w:', w) #print('P:', P.shape) print() print('PSF %g, Gal %g' % (psf_sigma, gal_sigma)) rmax = np.argmax(np.abs(w)) cmax = np.argmax(np.abs(v)) l2_rmax = np.sqrt(np.sum(P[rmax,:].real**2 + P[rmax,:].imag**2)) l2_cmax = np.sqrt(np.sum(P[:,cmax].real**2 + P[:,cmax].imag**2)) print('PSF L_2 in highest-frequency rows & cols:', l2_rmax, l2_cmax) l2_rmax = np.sqrt(np.sum(FG[rmax,:].real**2 + FG[rmax,:].imag**2)) l2_cmax = np.sqrt(np.sum(FG[:,cmax].real**2 + FG[:,cmax].imag**2)) print('Gal L_2 in highest-frequency rows & cols:', l2_rmax, l2_cmax) C = P * FG l2_rmax = np.sqrt(np.sum(C[rmax,:].real**2 + C[rmax,:].imag**2)) l2_cmax = np.sqrt(np.sum(C[:,cmax].real**2 + C[:,cmax].imag**2)) print('PSF*Gal L_2 in highest-frequency rows & cols:', l2_rmax, l2_cmax) print() Fpsf, Fgal = compare_subsampled( S, 1, ps, psf, pixpsf, Gmine,v,w, gal_sigma, psf_sigma, cd, get_ffts=True, eval_psf=eval_psf) plt.clf() plt.subplot(2,4,1) dimshow(P.real) plt.colorbar() plt.title('PSF real') plt.subplot(2,4,5) dimshow(P.imag) plt.colorbar() plt.title('PSF imag') plt.subplot(2,4,2) dimshow(FG.real) plt.colorbar() plt.title('Gal real') plt.subplot(2,4,6) dimshow(FG.imag) plt.colorbar() plt.title('Gal imag') plt.subplot(2,4,3) dimshow((P * FG).real) plt.colorbar() plt.title('P*Gal real') plt.subplot(2,4,7) dimshow((P * FG).imag) plt.colorbar() plt.title('P*Gal imag') plt.subplot(2,4,4) dimshow((Fgal).real) plt.colorbar() plt.title('pixGal real') plt.subplot(2,4,8) dimshow((Fgal).imag) plt.colorbar() plt.title('pixGal imag') plt.suptitle('PSF %g, Gal %g' % (psf_sigma, gal_sigma)) ps.savefig() subsample = [1,2,4] rms1 = [] for s in subsample: rms = compare_subsampled(S, s, ps, psf, pixpsf, Gmine,v,w, gal_sigma, psf_sigma, cd, eval_psf=eval_psf) rms1.append(rms) rms2.append(rms1) print() print('PSF sigma =', psf_sigma) print('RMSes:') for rms1,gal_sigma in zip(rms2, gal_sigmas): print('Gal sigma', gal_sigma, 'rms:', ', '.join(['%.3g' % r for r in rms1]))
plt.title('subpix Gal real') plt.subplot(2,4,8) dimshow(Fsubgal.imag) plt.colorbar() plt.title('subpix Gal imag') ps.savefig() pixconv = np.fft.irfft2(Fpsf * Fgal, s=pixpsf.shape) subpixconv = np.fft.irfft2(Fsub * Fsubgal, s=subpixpsf.shape) # my convolution pixscale = 1. cd = pixscale * np.eye(2) / 3600. Gmine = galaxy_psf_convolution(gal_re, 0., 0., gal, cd, 0., 0., pixpsf) pixconv /= np.sum(pixconv) subpixconv /= np.sum(subpixconv) subpixconv *= scale**2 Gmine /= np.sum(Gmine) plt.clf() plt.subplot(1,3,1) plt.imshow(pixconv, interpolation='nearest', origin='lower') plt.subplot(1,3,2) plt.imshow(subpixconv, interpolation='nearest', origin='lower') plt.subplot(1,3,3) plt.imshow(Gmine, interpolation='nearest', origin='lower') ps.savefig()