def plotCloudImage(self): """Generate some additional information and plots about the cloud image, if desired.""" from pImagePlots import PImagePlots import pylab im = PImagePlots() im.setImage(self.cloudimage) im.showImage(copy=True) im.hanningFilter() im.calcAll() im.showPsd2d() im.showAcovf2d() im.showAcovf1d() im.showSf(linear=True) #pylab.show() return
# image intensity information (and certainly lose zeropoint) #im.invertAcovf2d(usePhasespec=False) #im.invertAcovf2d(useI=True) # Start here to invert from 1d PSD (and use phase info or not) #im.invertPsd1d(phasespec=im.phasespec) # Start here to invert from 2d PSD (useI = False then, and uses phase info - get perfect reconstruction) im.invertPsd2d(usePhasespec=False) #im.invertPsd2d(useI=True) # Start here to invert from FFT (useI = False, and will get perfect reconstruction). im.invertFft(useI=True) # Use im2 to recalculate 1d PSD/ACovF starting from the reconstructed image, without altering the original. im2 = PImagePlots() im2.setImage(im.imageI.real, copy=True) im2.calcAll(min_dr=1.0, min_npix=2) im2.plotMore() # Now start plotting things, in comparison. clims = im.showImage() #print clims im2.showImage(clims=clims) im2.showImage() im.showFft(clims=clims) im2.showFft(clims=clims) im.showPsd2d() im2.showPsd2d() im.showPhases() im2.showPhases()
kappa = 1. # And also try to make an image with the same (final) pixel scale, but that will be created (first) # larger and then scaled down, to avoid introducing artifacts from the ACovF1d being truncated. final_imsize = 1500 # pixels desired in final image final_rad_fov = 1.75*numpy.sqrt(2) # degrees final_pixscale = 2*final_rad_fov / float(final_imsize) # Start with larger image pixscale = final_pixscale rad_fov = final_rad_fov imsize = int(2*rad_fov / pixscale) if (imsize%2 != 0): imsize = imsize + 1 xr = xsf / pixscale # xr = in pixels, over range that want to simulate print 'Image: Rad_fov', rad_fov, 'Imsize', imsize, 'Pixscale', pixscale, 'deg/pix', '(', pixscale*60.*60., 'arcsec/pix)' im = PImagePlots(shift=True, nx=imsize, ny=imsize) im.makeImageFromSf(sfx=xr, sf=SF) im.showAcovf1d() im.showPsd2dI() # Trim image to desired final size trim = round((imsize - final_imsize)/2.0) print 'Trimming about %d pixels from each side' %(trim) image = im.imageI[trim:trim+final_imsize, trim:trim+final_imsize] image = rescaleImage(image.real, sigma_goal, kappa) imsize = len(image) pixscale = pixscale rad_fov = imsize/2.0*pixscale print 'Image After Trimming: Rad_fov', rad_fov, 'Imsize', imsize, 'Pixscale', pixscale, 'deg/pix', '(', pixscale*60.*60., 'arcsec/pix)' im.setImage(image.real) imageStats(im.image) im.showImage(copy=True)
pixscale = 2*rad_fov / float(imsize) print 'Rad_fov', rad_fov, 'Imsize', imsize, 'Pixscale', pixscale, 'deg/pix', '(', pixscale*60.*60., 'arcsec/pix)' # set up 1d SF with desired scale and pixel scale """ condition = (xsf <= rad_fov) xr = xsf[condition] / pixscale # xr = in pixels, over range that want to simulate xrpix = numpy.arange(0, imsize, 1.0) sfpix = numpy.interp(xrpix, xr, SF[condition]) """ condition = (xsf <= rad_fov*numpy.sqrt(2)) xr = xsf[condition] / pixscale # xr = in pixels, over range that want to simulate xrpix = numpy.arange(0, imsize/2.*numpy.sqrt(2), 1.0) sfpix = numpy.interp(xrpix, xr, SF[condition]) # try making image im = PImagePlots(shift=True, nx= imsize, ny=imsize) im.makeImageFromSf(sfx=xrpix, sf=sfpix) im.showImageI() pylab.savefig('clouds_1.png', format='png') im.image = im.imageI.real #im.hanningFilter() im.calcAll(min_npix=2, min_dr=1) im.plotMore() pylab.savefig('clouds_1_dat.png', format='png') # Rescale sfx to be in physical (degrees) im.sfx = im.sfx * pixscale # make another image, as should see difference due to different random phases im2 = PImagePlots(shift=True, nx= imsize, ny=imsize) im2.invertSf(sfx=xrpix, sf=sfpix)
def clouds(): """Read an example of the french group's cloud generation.""" # oldCloud.npy and newCloud.npy are images of size 240x240 that cover a fov of 4.0 deg # (if cloud generation code is understood correctly). # old clouds oldClouds = numpy.load('oldCloud.npy') fov = 4.0 #rad_fov = 2.0 nx = len(oldClouds) pixscale = fov / float(nx) im = PImagePlots(shift=True) im.setImage(oldClouds) im.showImage() pylab.savefig('clouds_oldimage.%s' %(figformat), format='%s' %(figformat)) #im.hanningFilter() im.calcAll(min_npix=2, min_dr=1) im.plotMore() pylab.savefig('clouds_old.%s' %(figformat), format='%s' %(figformat)) # new clouds newClouds = numpy.load('newCloud.npy') im2 = PImagePlots(shift=True) im2.setImage(newClouds) im2.showImage() pylab.savefig('clouds_newimage.%s' %(figformat), format='%s' %(figformat)) #im2.hanningFilter() im2.calcAll(min_npix=2, min_dr=1) im2.plotMore() pylab.savefig('clouds_new.%s' %(figformat), format='%s' %(figformat)) # compare structure functions # translate x axis from pixels to degrees .. 240 pix = 4.0 deg (?) im.sfx = im.sfx *pixscale im2.sfx = im2.sfx *pixscale # and scale SF's to just run between 0 and 1 (because of loss of amplitude info with random phases) im.sf = im.sf / im.sf.max() im2.sf = im2.sf / im2.sf.max() legendlabels = ['Old clouds (scaled SF)', 'New clouds (scaled SF)'] im.showSf(comparison=im2, legendlabels=legendlabels, linear=True) pylab.xlim(0, fov/2.0) pylab.ylim(0, 1.2) pylab.title('Structure Function') pylab.xlabel('Degrees') pylab.savefig('clouds_sf.%s' %(figformat), format='%s' %(figformat)) # look at phase spectrum pylab.figure() n, b, p = pylab.hist(im.phasespec.flatten(), bins=75, range=[-numpy.pi, numpy.pi], alpha=0.2, label='Old clouds phases') n, b, p = pylab.hist(im2.phasespec.flatten(), bins=b, range=[-numpy.pi, numpy.pi], alpha=0.2, label='New clouds phases') pylab.legend(fancybox=True, fontsize='smaller') pylab.savefig('clouds_phasehist.%s' %(figformat), format='%s' %(figformat)) # the phase spectrum seems to be flatly distributed between -pi and pi pylab.figure() pylab.subplot(121) pylab.title('Old clouds') pylab.imshow(im.phasespec, origin='lower') pylab.colorbar(shrink=0.6) pylab.subplot(122) pylab.title('New clouds') pylab.imshow(im2.phasespec, origin='lower') pylab.colorbar(shrink=0.6) pylab.suptitle('Phase spectrum') pylab.savefig('clouds_phasespec.%s' %(figformat), format='%s' %(figformat)) pylab.close() return
def inversion(): """Generate some example images & invert them to reconstruct the original image.""" im = TestImage(shift=True, nx=1000, ny=1000) #im.addEllipseGrid(gridX=200, gridY=100, semiX=50, semiY=25, value=1) im.addLines(width=20, spacing=200, value=1, angle=45) im.addSin(scale=300) im.hanningFilter() im.zeroPad() #cmap = pylab.cm.gray_r cmap = None clims = im.showImage(cmap=cmap) pylab.savefig('invert_image.%s' %(figformat), format='%s' %(figformat)) im.calcAll(min_npix=1, min_dr=1) # Invert from ACovF and show perfect reconstruction. im.invertAcovf2d() im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_acovf2d_good.%s' %(figformat), format='%s' %(figformat)) # Invert from ACovF 2d without phases im.invertAcovf2d(usePhasespec=False, seed=42) im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_acovf2d_nophases.%s' %(figformat), format='%s' %(figformat)) # Invert from ACovF 1d with phases im.invertAcovf1d(phasespec=im.phasespec) im.invertAcovf2d(useI=True) im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_acovf1d_phases.%s' %(figformat), format='%s' %(figformat)) # Invert from ACovF 1d without phases im.invertAcovf1d(seed=42) im.invertAcovf2d(useI=True) im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_acovf1d_nophases.%s' %(figformat), format='%s' %(figformat)) # Recalculate 1-d PSD and ACovF from this last reconstructed image (ACovF1d no phases) im2 = PImagePlots() im2.setImage(im.imageI) im2.calcAll(min_npix=1, min_dr=1) legendlabels=['Reconstructed', 'Original'] im2.showPsd1d(comparison=im, legendlabels=legendlabels) pylab.savefig('invert_recalc_ACovF_Psd1d.%s' %(figformat), format='%s' %(figformat)) im2.showAcovf1d(comparison=im, legendlabels=legendlabels) pylab.savefig('invert_recalc_ACovF_Acovf1d.%s' %(figformat), format='%s' %(figformat)) # Invert from PSD and show perfect reconstruction. im.invertPsd2d() im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_psd2d_good.%s' %(figformat), format='%s' %(figformat)) # Invert from PSD 2d without phases im.invertPsd2d(usePhasespec=False, seed=42) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_psd2d_nophases.%s' %(figformat), format='%s' %(figformat)) # Invert from PSD 1d with phases im.invertPsd1d(phasespec=im.phasespec) im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_psd1d_phases.%s' %(figformat), format='%s' %(figformat)) # Invert from PSD 1d without phases im.invertPsd1d(seed=42) im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageI(clims=clims, cmap=cmap) pylab.savefig('invert_psd1d_nophases.%s' %(figformat), format='%s' %(figformat)) # Recalculate 1-d PSD and ACovF from this last reconstructed image (PSD1d no phases) im2 = PImagePlots() im2.setImage(im.imageI) im2.calcAll(min_npix=1, min_dr=1) im2.showPsd1d(comparison=im, legendlabels=legendlabels) pylab.savefig('invert_recalc_PSD_Psd1d.%s' %(figformat), format='%s' %(figformat)) im2.showAcovf1d(comparison=im, legendlabels=legendlabels) pylab.savefig('invert_recalc_PSD_Acovf1d.%s' %(figformat), format='%s' %(figformat)) pylab.close() return