def ifftvec(vec):
    """
    performs a fft on a vector with 3 components in the last index position
    This is a wrapper for ifft and ifftn
    """
    try:
        from anfft import ifft, ifftn
        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 ifft, ifftn
        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 float32, real, array, empty, complex64
    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([ifftn(i,measure=True) for i in vec]).astype(float32)
#                result = empty(vec.shape, dtype=float32)
#                result[0] = real(ifftn(vec[0], measure=True)).astype(float32)
#                result[1] = real(ifftn(vec[1], measure=True)).astype(float32)
#                result[2] = real(ifftn(vec[2], measure=True)).astype(float32)
#                return result
            elif fft_type==0:
                return ifftn(vec, axes=range(1,vec.ndim)).astype(float32)
            elif fft_type==2:
#                return array([gpu_ifft(i) for i in vec]).astype(float32)
                result = empty(vec.shape, dtype=float32)
                result[0] = gpu_ifft(vec[0].copy()).astype(float32)
                result[1] = gpu_ifft(vec[1].copy()).astype(float32)
                result[2] = gpu_ifft(vec[2].copy()).astype(float32)
                return result
        else: # "Scalar", fft the whole thing
            if fft_type==1:
                return ifftn(vec,measure=True).astype(float32)
            elif fft_type==0:
                return ifftn(vec).astype(float32)
            elif fft_type==2:
                return gpu_ifft(vec.copy()).astype(float32)
    elif vec.ndim == 1: #Not a vector, so use fft
        if fft_type==1:
            return ifft(vec,measure = True).astype(float32)
        elif fft_type==0:
            return ifft(vec).astype(float32)
        elif fft_type==2:
            return gpu_ifft(vec).astype(float32)
    else:
        #0th index is 3, so its a vector
        #return fft(vec, axis=1).astype(complex64)
        return array([ifft(i) for i in vec]).astype(float32)
Beispiel #2
0
def fft_ifftn(*args, **kwargs):
    """Call the core fft library function ifftn (inverse).

    If the anfft library is used, enable 'measure' optimization.

    """
    if using_fft_library == "anfft":
        return fft.ifftn(*args, measure=True, **kwargs)
    else:
        return fft.ifftn(*args, **kwargs)
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()
Beispiel #4
0
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);
Beispiel #6
0
def get_idft(array, ndim=None):
    array = array.astype(TYPE)
    result = anfft.ifftn(array, ndim, measure=True)
    return result
	def ifftn(B,shape=None):
		if shape != None:
			B = _checkffttype(B)
			B = procrustes(B,target=shape,side='after',padval=0)
		return _anfft.ifftn(B,measure=True)
Beispiel #8
0
 def do_ifft(self, signal_matrix):
     if self.fft_module == 'anfft':
         return anfft.ifftn(signal_matrix)
     else:
         return np.fft.ifftn(signal_matrix)
Beispiel #9
0
        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)
Beispiel #10
0
def get_idft(array, ndim=None):
    array = array.astype(TYPE)
    result = anfft.ifftn(array, ndim, measure=True)
    return result
Beispiel #11
0
 def ifftn(B, shape=None):
     if shape != None:
         B = _checkffttype(B)
         B = procrustes(B, target=shape, side='after', padval=0)
     return _anfft.ifftn(B, measure=True)
Beispiel #12
0
 def ifft2(B):
     return _anfft.ifftn(_checkffttype(B), measure=True)