def fftvec(vec): """ performs a fft on a vector with 3 components in the first index position This is really just a wrapper for fft, fftn and their inverses """ try: from anfft import fft, fftn fft_type = 1 except: # print "Could not import anfft, importing scipy instead." #Update 9/18/2013 -- numpy with mkl is way faster than scipy import mkl mkl.set_num_threads(8) from numpy.fft import fft, fftn fft_type = 0 if force_gpu: fft_type = 2 #set gpu fft's manually -- not sure what a automatic way would look like from numpy import complex64, shape, array, empty if vec.ndim > 2: if vec.shape[0] == 3: # "Vector": first index has size 3 so fft the other columns if fft_type==1: return array([fftn(i,measure=True) for i in vec]).astype(complex64) # result = empty(vec.shape, dtype=complex64) # result[0] = fftn(vec[0], measure=True).astype(complex64) # result[1] = fftn(vec[1], measure=True).astype(complex64) # result[2] = fftn(vec[2], measure=True).astype(complex64) # return result elif fft_type==0: return fftn(vec, axes=range(1,vec.ndim)).astype(complex64) elif fft_type==2: # return array([gpu_fft(i) for i in vec.astype(complex64)]) result = empty(vec.shape, dtype=complex64) result[0] = gpu_fft(vec[0].copy()) result[1] = gpu_fft(vec[1].copy()) result[2] = gpu_fft(vec[2].copy()) return result else: # "Scalar", fft the whole thing if fft_type==1: return fftn(vec,measure=True).astype(complex64) elif fft_type==0: return fftn(vec).astype(complex64) elif fft_type==2: return gpu_fft(vec.copy()) elif vec.ndim == 1: #Not a vector, so use fft if fft_type==1: return fft(vec,measure = True).astype(complex64) elif fft_type==0: return fft(vec).astype(complex64) elif fft_type==2: return gpu_fft(vec.astype(complex64)) else: #0th index is 3, so its a vector #return fft(vec, axis=1).astype(complex64) return array([fft(i) for i in vec])
def fft_fftn(*args, **kwargs): """Call the core fft library function fftn. If the anfft library is used, enable 'measure' optimization. """ if using_fft_library == "anfft": return fft.fftn(*args, measure=True, **kwargs) else: return fft.fftn(*args, **kwargs)
def do_fft(self, signal_matrix): """ Maybe zero-padding is necessary, it could be done with: fftn(signal_matrix, [next pow of 2, next pow of 2, next pow of 2]) """ if self.fft_module == 'anfft': return anfft.fftn(signal_matrix) else: return np.fft.fftn(signal_matrix)
def digthis(region_coeff): # for i in range(0,len(ra_sam)): # image[x_sam[i].round(),y_sam[i].round()]=flux_sam[i]+(10**region_coeff[0])# adding background level to the pixels #print region_coeff # if (np.any(region_coeff)>3 or np.any(region_coeff<-10)): if (np.any(abs(region_coeff-desired)>1.5)): return -np.inf # if (np.any(abs(region_coeff[faint]-desired[faint])>1)): # return -np.inf # for qq in index_photo_z: #image[x_cat_photo_z[qq].round(),y_cat_photo_z[qq].round()]=(10**region_coeff[pp]) # because the zero's index is used for background. for qq in izip(sam_corresponding_index[desired_index],cat_corresponding_index[desired_index]): yo,xo=wcs_pacs160.wcs_sky2pix(cat.ra[qq[1]],cat.dec[qq[1]],0) xn,yn = ZOOM*(xo-xc_orig)+xc_expanded,ZOOM*(yo-yc_orig)+yc_expanded pacs160_image[xn[0],yn[0]]=0 pp=0 for qq in izip(sam_corresponding_index[desired_index],cat_corresponding_index[desired_index]): yo,xo=wcs_pacs160.wcs_sky2pix(cat.ra[qq[1]],cat.dec[qq[1]],0) xn,yn = ZOOM*(xo-xc_orig)+xc_expanded,ZOOM*(yo-yc_orig)+yc_expanded pacs160_image[xn[0],yn[0]]=pacs160_image[xn[0],yn[0]]+(10**region_coeff[pp]) pp+=1 # chi2[pp]=np.log(np.interp(region_coeff[pp+1],np.log10(bins),pdffile[qq,:]/pdffile[qq,:].max()) ) # this is -chi2/2 in fact that we retrun. # return len((chi2==-np.inf).nonzero()[0]*(-1e6) image_p = hconvolve.padzero2d_i(pacs160_image, r, c) fftimage = anfft.fftn(image_p)*fftkernel# * anfft.fftn(kernel_p) test_k_temp = anfft.ifftn(fftimage)[:r1,:c1].real test_k = interpolation.zoom(test_k_temp,1./ZOOM)/normfactor test_chi2=0 for i in range(x_min,x_max): for j in range(y_min,y_max): test_chi2+=(test_k[i,j]-herschel[i,j])**2/(RMS_pacs160[i,j]**2) print >>chi2file, -1*(test_chi2/2) print -1*(test_chi2/2) return -1*(test_chi2/2)#+chi2.sum()
def explorefluxes(region_coeff): #weak prior of 3 dex wide on each source is imposed to limit the exploration of walkers with that plausible range. if (np.any(abs(region_coeff - desired) > 1.5)): return -np.inf #at each step the pixels that contain the sources of interest is set to zero. for qq in izip(sam_corresponding_index[desired_index], cat_corresponding_index[desired_index]): yo, xo = wcs_pacs160.wcs_sky2pix(cat.ra[qq[1]], cat.dec[qq[1]], 0) xn, yn = ZOOM * (xo - xc_orig) + xc_expanded, ZOOM * ( yo - yc_orig) + yc_expanded pacs160_image[xn[0], yn[0]] = 0 # Pixel values are set to the sources fluxes guesses by the walkers at each step pp = 0 for qq in izip(sam_corresponding_index[desired_index], cat_corresponding_index[desired_index]): yo, xo = wcs_pacs160.wcs_sky2pix(cat.ra[qq[1]], cat.dec[qq[1]], 0) xn, yn = ZOOM * (xo - xc_orig) + xc_expanded, ZOOM * ( yo - yc_orig) + yc_expanded pacs160_image[xn[0], yn[0]] += (10**region_coeff[pp]) pp += 1 #convolution is performed after sources are put in their relevent pixel image_p = hconvolve.padzero2d_i(pacs160_image, r, c) # taking the FFT of the image fftimage = anfft.fftn(image_p) * fftkernel # mutiply by FFT of Kernel test_k_temp = anfft.ifftn( fftimage )[:r1, :c1].real # Inverse FFT the result and take the real part test_k = interpolation.zoom( test_k_temp, 1. / ZOOM) / normfactor # zoom out by the factor PSF was zoomed in #compute the chi2 at each step test_chi2 = 0 for i in range(x_min, x_max): for j in range(y_min, y_max): test_chi2 += (test_k[i, j] - herschel[i, j])**2 / (RMS_pacs160[i, j]**2) print -1 * (test_chi2 / 2) return -1 * (test_chi2 / 2)
def diffusion_omega(dt, omega, args): ''' d(omega)/dt = nu*((omega)_xx + (omega)_yy) - mu*omega => d(omega)Hat/(omega)Hat = -nu*(k_x^2 + k_y^2)*dt - mu*dt => (omega)Hat = e^(-nu*|k|^2 - mu)dt*(omega)Hat_0 Dynamic viscosity nu = 0.01 for now ''' omegaHat = anfft.fftn(omega,2, measure=True); [xVal,yVal] = shape(omegaHat); omegaHat[xVal/6:5*xVal/6,yVal/6:5*yVal/6] = 0; k = array(range(N), dtype=complex128) - N/2; k = ifftshift(k); [KX, KY] = meshgrid(k,k); mu = 0.0001; mask = zeros(N); for i in arange(N/100): mask[i] = 1; mask[N-i-1] = 1; [dragx,dragy] = meshgrid(mask,mask); # delsq = -(KX*KX + KY*KY) delsq = -(pow(KX,6) + pow(KY,6)) omegasq = sqrt(sum(omega*omega)/(N*N)); if (args == 0): nu = omegasq/pow(N,4); else: nu = args; # print 'viscosity=',nu; delsq = (nu*delsq - mu*(dragy + dragx))*dt; multiplier = exp(delsq); omegaHat = omegaHat*multiplier; return (real(anfft.ifftn(omegaHat,2)), nu);
xn,yn = ZOOM*(xo-xc_orig)+xc_expanded,ZOOM*(yo-yc_orig)+yc_expanded pacs160_image[xn[0],yn[0]]+=pacs160_sam[i[0]] #=================================== r1, c1 = pacs160_image.shape r2, c2 = kernel_pacs160_zoom2.shape if r2 % 2 == 0: r = r1 + r2/2 else: r = r1 + (r2 + 1) / 2 if c2 % 2 == 0: c = c1 + c2/2 + 1 else: c = c1 + (c2 + 1) / 2 kernel_p = hconvolve.padzero2d_k(kernel_pacs160_zoom2, r, c) fftkernel=anfft.fftn(kernel_p) #=================================== #bins=np.logspace(-10,3,49) chi2=np.zeros(len(index_photo_z)) #chi2file_gt_4=open('./chi2file_gt_4_38_sources_correct.txt', 'w+') #chi2file_gt_4=open('./chi2file_testing.txt', 'w+') chi2file=open('./chi2file_4.5_gt_1_np_inf_65_sources_startingattrueflux_sgima0.4.txt','w+') def digthis(region_coeff): # for i in range(0,len(ra_sam)): # image[x_sam[i].round(),y_sam[i].round()]=flux_sam[i]+(10**region_coeff[0])# adding background level to the pixels #print region_coeff # if (np.any(region_coeff)>3 or np.any(region_coeff<-10)): if (np.any(abs(region_coeff-desired)>1.5)): return -np.inf # if (np.any(abs(region_coeff[faint]-desired[faint])>1)):
def get_dft(array, ndim=None): array = array.astype(TYPE) result = anfft.fftn(array, ndim, measure=True) return result
def fftn(A,shape=None): if shape != None: A = _checkffttype(A) A = procrustes(A,target=shape,side='after',padval=0) return _anfft.fftn(A,measure=True)
xn, yn = ZOOM * (xo - xc_orig) + xc_expanded, ZOOM * ( yo - yc_orig) + yc_expanded pacs160_image[xn[0], yn[0]] = pacs160_sam[i[0]] #=================================== r1, c1 = pacs160_image.shape r2, c2 = kernel_pacs160_zoom2.shape if r2 % 2 == 0: r = r1 + r2 / 2 else: r = r1 + (r2 + 1) / 2 if c2 % 2 == 0: c = c1 + c2 / 2 + 1 else: c = c1 + (c2 + 1) / 2 kernel_p = hconvolve.padzero2d_k(kernel_pacs160_zoom2, r, c) fftkernel = anfft.fftn(kernel_p) #=================================== image_p = hconvolve.padzero2d_i(pacs160_image, r, c) fftimage = anfft.fftn(image_p) * fftkernel # * anfft.fftn(kernel_p) image_conv = anfft.ifftn(fftimage)[:r1, :c1].real image_blk = interpolation.zoom(image_conv, 1. / ZOOM) / normfactor final = image_blk + noise_pacs160 pyfits.writeto( '/astro/ferguson1/safar/SAMS/simulated_images/simulated_pacs160_with_photo_z_zoom2_new_correct_ID.fits', final, header_pacs160)
def fftn(A, shape=None): if shape != None: A = _checkffttype(A) A = procrustes(A, target=shape, side='after', padval=0) return _anfft.fftn(A, measure=True)
def fft2(A): return _anfft.fftn(_checkffttype(A), measure=True)