Exemplo n.º 1
0
    def frt_gpu(self, A0: np.ndarray, d1: float, z: float):
        """
        Implements propagation using Fresnel diffraction. Runs on a GPU using CuPy with a CUDA backend.
        :param A0: Field to propagate
        :param d1: Sampling size of the field A0
        :param z : Propagation distance in metres
        :return: A : Propagated field
        """
        wv = self.wavelength
        k = 2 * np.pi / wv
        N = A0.shape[0]
        x = cp.linspace(0, N - 1, N) - (N / 2) * cp.ones(N)
        y = cp.linspace(0, N - 1, N) - (N / 2) * cp.ones(N)
        d2 = wv * z / (N * d1)
        X1, Y1 = d1 * cp.meshgrid(x, y)[0], d1 * cp.meshgrid(x, y)[1]
        X2, Y2 = d2 * cp.meshgrid(x, y)[0], d2 * cp.meshgrid(x, y)[1]
        R1 = cp.sqrt(X1**2 + Y1**2)
        R2 = cp.sqrt(X2**2 + Y2**2)
        D = 1 / (1j * wv * abs(z))
        Q1 = cp.exp(1j * (k / (2 * z)) * R1**2)
        Q2 = cp.exp(1j * (k / (2 * z)) * R2**2)
        if z >= 0:
            A = D * Q2 * (d1**2) * cp.fft.fftshift(
                cp.fft.fft2(cp.fft.ifftshift(A0 * Q1), norm='ortho'))
        elif z < 0:
            A = D * Q2 * ((N * d1)**2) * cp.fft.fftshift(
                cp.fft.ifft2(cp.fft.ifftshift(A0 * Q1), norm='ortho'))

        return A
Exemplo n.º 2
0
  def local_cov_bet_class_NN(self,key,label,nb_class,batchsize,k):
    key_broadcast=cp.broadcast_to(key,(batchsize,batchsize,key.shape[1]))
    key_broadcast_transpose=cp.transpose(cp.broadcast_to(key,(batchsize,batchsize,key.shape[1])),axes=(1,0,2))
    sub_key_broadcast=key_broadcast-key_broadcast_transpose
    norm_sub_broadcast=cp.linalg.norm(sub_key_broadcast,axis=2)
    sorted_d=cp.sort(norm_sub_broadcast,axis=0)
    kth_d=sorted_d[k]
    kth_d=kth_d.reshape([batchsize,1])
    sigma=cp.matmul(kth_d,cp.transpose(kth_d))

    batchsize_per_class=batchsize//nb_class
    index=cp.arange(key.shape[0])
    xx,yy=cp.meshgrid(index,index)
    sub=key[xx]-key[yy]
    norm_sub=cp.linalg.norm(sub,axis=2)
    a1=cp.exp(-norm_sub*norm_sub/sigma)
    lindex=cp.arange(label.shape[0])
    lx,ly=cp.meshgrid(lindex,lindex)
    l=(label[lx]==label[ly])
    a1=a1*l*(1.0/(batchsize*nb_class)-1.0/batchsize_per_class)
    l2=(label[lx]!=label[ly])
    a2=l2*(1.0/batchsize)
    a=a1+a2
    a=a.reshape([a.shape[0],a.shape[1],1])
    a_sub=a*sub
    Sb=cp.einsum('ijk,ijl->kl',a_sub,sub,dtype='float32')*0.5
    return Sb
Exemplo n.º 3
0
    def local_cov_in_class(self,key,label,nb_class,batchsize):
        index = cp.arange(key.shape[0])
        xx,yy = cp.meshgrid(index,index)
        sub = key[xx] - key[yy]

        norm_sub = cp.linalg.norm(sub,axis=2)
        a = cp.exp(-norm_sub*norm_sub/100)

        lindex = cp.arange(label.shape[0])
        lx,ly = cp.meshgrid(lindex,lindex)
        l = (label[lx]==label[ly])
        a = a*l

        Sw = cp.einsum('ij,ijk,ijl->kl',a,sub,sub,dtype='float32')*0.5*(1.0/batchsize)
        return Sw
Exemplo n.º 4
0
 def local_cov_in_class(self,key,label,nb_class,batchsize,affinity):
   batchsize_per_class=batchsize//nb_class
   index = cp.arange(key.shape[0])
   xx,yy=cp.meshgrid(index,index)
   sub=key[xx]-key[yy]
   norm_sub=cp.linalg.norm(sub,axis=2)
   a=cp.exp(-norm_sub*norm_sub*affinity)
   lindex=cp.arange(label.shape[0])
   lx,ly=cp.meshgrid(lindex,lindex)
   l=(label[lx]==label[ly])
   a=a*l
   a=a.reshape([a.shape[0],a.shape[1],1])
   a_sub=a*sub
   Sw=cp.einsum('ijk,ijl->kl',a_sub,sub,dtype='float32')*0.5*(1.0/batchsize_per_class)
   return Sw
Exemplo n.º 5
0
    def calHampkernel(self):
        prefactor = 1 / (4 * math.pi)
        dx = self.lcx
        dy = self.lcy
        dz = self.lcz

        a1 = (np.array(range(2 * self.nx - 1)) - self.nx + 1) * dx
        a2 = (np.array(range(2 * self.ny - 1)) - self.ny + 1) * dy
        a3 = (np.array(range(2 * self.nz - 1)) - self.nz + 1) * dz

        a3, a2, a1 = np.meshgrid(a3, a2, a1, sparse=False, indexing='ij')

        print('Start calculating demag factors')
        Kxy_val = self.Kxy(a1, a2, a3, dx, dy, dz)
        Kxz_val = self.Kxz(a1, a2, a3, dx, dy, dz)
        Kyz_val = self.Kyz(a1, a2, a3, dx, dy, dz)
        print('End calculating demag factors')

        self.Ktotxy = prefactor * Kxy_val
        self.Ktotxz = prefactor * Kxz_val
        self.Ktotyz = prefactor * Kyz_val

        self.FKtotxy = np.fft.fftn(self.Ktotxy, axes=(0, 1, 2), norm=None)
        self.FKtotxz = np.fft.fftn(self.Ktotxz, axes=(0, 1, 2), norm=None)
        self.FKtotyz = np.fft.fftn(self.Ktotyz, axes=(0, 1, 2), norm=None)
    def spatial_decode(self, latent, sigmoid=True):

        image_size = 128

        a = cp.linspace(-1, 1, image_size)
        b = cp.linspace(-1, 1, image_size)

        x, y = cp.meshgrid(a, b)

        x = x.reshape(image_size, image_size, 1)
        y = y.reshape(image_size, image_size, 1)

        batchsize = len(latent)

        xy = cp.concatenate((x, y), axis=-1)
        xy_tiled = cp.tile(xy, (batchsize, 1, 1, 1)).astype(cp.float32)

        latent_tiled = F.tile(latent, (1, 1, image_size * image_size)).reshape(
            batchsize, image_size, image_size, self.latent_n)
        latent_and_xy = F.concat((latent_tiled, xy_tiled), axis=-1)
        latent_and_xy = F.swapaxes(latent_and_xy, 1, 3)

        sp_3_decoded = F.relu(self.sp_dec_3(latent_and_xy))
        sp_2_decoded = F.relu(self.sp_dec_2(sp_3_decoded))
        sp_1_decoded = F.relu(self.sp_dec_1(sp_2_decoded))
        out_img = self.sp_dec_0(sp_1_decoded)

        # need the check because the bernoulli_nll has a sigmoid in it
        if sigmoid:
            return F.sigmoid(out_img)
        else:
            return out_img
Exemplo n.º 7
0
    def get_measurement_matrix(self,ix,iy):
        shifted_theta = self.theta+self.theta[1]
        theta_grid,r_grid = cp.meshgrid(shifted_theta*util.PI/180,self.r) #Because theta will be on the x axis, and r will be on the y axis
        
        # theta_grid = theta_grid[:,::-1]
        H = cp.zeros((r_grid.size,ix.size),dtype=cp.complex64)
        
        if not self.use_skimage:
            #launch CUDA kernel
            if cuda.is_available():
                bpg=((H.shape[0]+TPBn-1)//TPBn,(H.shape[1]+TPBn-1)//TPBn)
                print("Cuda is available, now running CUDA kernel with Thread perblock = {}, Block Per Grids = {}, and H shape {}".format(TPB,bpg,H.shape))
                util._calculate_H_Tomography[bpg,TPB](r_grid.ravel(ORDER),theta_grid.ravel(ORDER),ix,iy,H)
                ratio = self.n_r/(2*(self.n_r//2))
                H *= ratio

                #Some Temporary Fixing
                nPart=4
                n_theta = self.n_theta
                n_r = self.n_r
                for i in range(n_theta//nPart):
                    H[i*n_r:(i+1)*n_r,:] = cp.roll(H[i*n_r:(i+1)*n_r,:],1,axis=0)
            
            # util.calculate_H_Tomography(r_grid.ravel(ORDER),theta_grid.ravel(ORDER),ix,iy,H)
            #due to some problem the resulted H is flipped upside down
            #hence 
            # H = cp.flipud(H)
            # norm_ratio = (self.n_r/2)/(self.n_r//2)
        else:
            H_n = cp.asnumpy(H)
            util.calculate_H_Tomography_skimage(cp.asnumpy(self.theta),cp.asnumpy(ix),cp.asnumpy(iy),H_n,self.target_image.shape[0])
            H = cp.asarray(H_n)
        return H.astype(cp.complex64)
Exemplo n.º 8
0
def transform_affine(volume, ref_shape, affine, order=1):

    ndim = volume.ndim
    affine = cupy.asarray(affine)
    if True:
        out = ndi.affine_transform(
            volume,
            matrix=affine,
            order=order,
            mode="constant",
            output_shape=tuple(ref_shape),
        )
    else:
        # use map_coordinates instead of affine_transform
        xcoords = cupy.meshgrid(
            *[cupy.arange(s, dtype=volume.dtype) for s in ref_shape],
            indexing="ij",
            sparse=True,
        )
        coords = _apply_affine_to_field(
            xcoords,
            affine[:ndim, :],
            include_translations=True,
            coord_axis=0,
        )
        out = ndi.map_coordinates(volume, coords, order=1)

    return out
Exemplo n.º 9
0
def spherical_cosmask(n,mask_radius, edge_width, origin=None):
    """mask = spherical_cosmask(n, mask_radius, edge_width, origin)
    """

    if type(n) is int:
        n = np.array([n])

    sz = np.array([1, 1, 1])
    sz[0:np.size(n)] = n[:]

    szl = -np.floor(sz/2)
    szh = szl + sz

    x,y,z = np.meshgrid( np.arange(szl[0],szh[0]), 
                         np.arange(szl[1],szh[1]), 
                         np.arange(szl[2],szh[2]), indexing='ij', sparse=True)

    r = np.sqrt(x*x + y*y + z*z)

    m = np.zeros(sz.tolist())

#    edgezone = np.where( (x*x + y*y + z*z >= mask_radius) & (x*x + y*y + z*z <= np.square(mask_radius + edge_width)))

    edgezone = np.all( [ (x*x + y*y + z*z >= mask_radius), (x*x + y*y + z*z <= np.square(mask_radius + edge_width))], axis=0)
    m[edgezone] = 0.5 + 0.5*np.cos( 2*np.pi*(r[edgezone] - mask_radius) / (2*edge_width))
    m[ np.all( [ (x*x + y*y + z*z <= mask_radius*mask_radius) ], axis=0 ) ] = 1

#    m[ np.where(x*x + y*y + z*z <= mask_radius*mask_radius)] = 1

    return m
Exemplo n.º 10
0
def centerCBED(data4D_flat, x_cen, y_cen, chunks=8):
    stops = np.zeros(chunks + 1, dtype=np.int)
    stops[0:chunks] = np.arange(0, data4D_flat.shape[0],
                                (data4D_flat.shape[0] / chunks))
    stops[chunks] = data4D_flat.shape[0]
    max_size = int(np.amax(np.diff(stops)))
    centered4D = np.zeros_like(data4D_flat)
    image_size = np.asarray(data4D_flat.shape[1:3])
    fourier_cal_y = (cp.linspace(
        (-image_size[0] / 2),
        ((image_size[0] / 2) - 1), image_size[0])) / image_size[0]
    fourier_cal_x = (cp.linspace(
        (-image_size[1] / 2),
        ((image_size[1] / 2) - 1), image_size[1])) / image_size[1]
    [fourier_mesh_x, fourier_mesh_y] = cp.meshgrid(fourier_cal_x,
                                                   fourier_cal_y)
    move_pixels = np.flip(image_size / 2) - np.asarray((x_cen, y_cen))
    move_phase = cp.exp(
        (-2) * np.pi * 1j * ((fourier_mesh_x * move_pixels[0]) +
                             (fourier_mesh_y * move_pixels[1])))
    for ii in range(chunks):
        startval = stops[ii]
        stop_val = stops[ii + 1]
        gpu_4Dchunk = cp.asarray(data4D_flat[startval:stop_val, :, :])
        FFT_4D = cp.fft.fftshift(cp.fft.fft2(gpu_4Dchunk, axes=(-1, -2)),
                                 axes=(-1, -2))
        FFT_4Dmove = cp.absolute(
            cp.fft.ifft2(cp.multiply(FFT_4D, move_phase), axes=(-1, -2)))
        centered4D[startval:stop_val, :, :] = cp.asnumpy(FFT_4Dmove)
    del FFT_4D, gpu_4Dchunk, FFT_4Dmove, move_phase, fourier_cal_y, fourier_cal_x, fourier_mesh_x, fourier_mesh_y
    return centered4D
Exemplo n.º 11
0
 def __init__(self,
              file_name,
              target_size=128,
              stdev=0.1,
              relative_location=""):
     self.globalprefix = pathlib.Path.cwd() / relative_location
     if not self.globalprefix.exists():
         self.globalprefix.mkdir()
     self.file_name = self.globalprefix / file_name
     if target_size > 512:
         raise Exception("Target image dimension are too large (" +
                         str(target_size) + " x " + str(target_size) + ")")
     self.dim = target_size
     self.stdev = stdev
     img = imread(self.file_name.absolute(), as_gray=True)
     if (img.shape[0] != img.shape[1]):
         raise Exception("Image is not square")
     self.target_image = resize(img, (self.dim, self.dim),
                                anti_aliasing=False,
                                preserve_range=True,
                                order=1,
                                mode='symmetric')
     self.target_image = cp.asarray(self.target_image, dtype=cp.float32)
     self.corrupted_image = self.target_image + self.stdev * cp.random.randn(
         self.target_image.shape[0],
         self.target_image.shape[1],
         dtype=cp.float32)
     self.v = self.target_image.ravel(ORDER) / self.stdev  #Normalized
     self.y = self.corrupted_image.ravel(ORDER) / self.stdev  #Normalized
     self.num_sample = self.y.size
     temp = cp.linspace(0., 1., num=self.dim, endpoint=True)
     ty, tx = cp.meshgrid(temp, temp)
     self.ty = ty.ravel(ORDER)
     self.tx = tx.ravel(ORDER)
Exemplo n.º 12
0
def _sin_flow_gen(image0, max_motion=4.5, npics=5):
    """Generate a synthetic ground truth optical flow with a sinusoid as
      first component.

    Parameters:
    ----
    image0: ndarray
        The base image to be warped.
    max_motion: float
        Maximum flow magnitude.
    npics: int
        Number of sinusoid pics.

    Returns
    -------
    flow, image1 : ndarray
        The synthetic ground truth optical flow with a sinusoid as
        first component and the corresponding warped image.

    """
    grid = cp.meshgrid(*[cp.arange(n) for n in image0.shape], indexing='ij')
    grid = cp.stack(grid)
    # TODO: make upstream scikit-image PR changing gt_flow dtype to float
    gt_flow = cp.zeros_like(grid, dtype=float)
    gt_flow[0,
            ...] = max_motion * cp.sin(grid[0] / grid[0].max() * npics * np.pi)
    image1 = warp(image0, grid - gt_flow, mode="nearest")
    return gt_flow, image1
Exemplo n.º 13
0
def getShapeMatrix(ro=1.3, N=1000, gpu=False):
    if gpu:
        import cupy as np
    else:
        import numpy as np

    def f(ro, N):
        ''' give the surface map matrix '''
        # polar coordinate
        theta = np.linspace(0, 2 * np.pi, N)
        x = ro * np.cos(theta)
        y = ro * np.sin(theta)

        u, v = np.meshgrid(x, y)
        RO = u**2 + v**2
        # map z to u,v
        Z = 1 - u / RO - RO
        return x, y, Z

    x, y, Z = f(ro, N)
    # remap z to the grid index
    index_x, index_y = np.argsort(x), np.argsort(y)
    Y, X = np.meshgrid(
        index_x,
        index_y)  # meshgrid always will use numpy array NOT mxnet NDArray
    Z = Z[X, Y]
    return Z
Exemplo n.º 14
0
    def movepixels_3d(self, Iin, Tx):
        '''
        This function will translate the pixels of an image
        according to Tx translation images. 
        
        Inputs;
        Tx: The transformation image, dscribing the
                     (backwards) translation of every pixel in the x direction.
       
        Outputs,
          Iout : The transformed image
        
        '''

        nx, ny, nz = Iin.shape

        tmpx = cp.linspace(0, nx - 1, num=nx, dtype=np.float32)
        tmpy = cp.linspace(0, ny - 1, num=ny, dtype=np.float32)
        tmpz = cp.linspace(0, nz - 1, num=nz, dtype=np.float32)

        x, y, z = cp.meshgrid(tmpx, tmpy, tmpz, indexing='ij')
        Tlocalx = cp.expand_dims(x + Tx, axis=0)
        y = cp.expand_dims(y, axis=0)
        z = cp.expand_dims(z, axis=0)
        coordinates = cp.vstack((Tlocalx, y, z))

        return map_coordinates(Iin, coordinates, order=1, cval=0)
Exemplo n.º 15
0
def transform_affine(volume, ref_shape, affine, order=1):

    from cupyimg.scipy.ndimage._kernels import interp

    ndim = volume.ndim
    legacy_mode = interp.const_legacy_mode
    interp.const_legacy_mode = False
    try:
        affine = cupy.asarray(affine)
        if True:
            out = ndi.affine_transform(
                volume,
                matrix=affine,
                order=order,
                mode="constant",
                output_shape=tuple(ref_shape),
            )
        else:
            # use map_coordinates instead of affine_transform
            xcoords = cupy.meshgrid(
                *[cupy.arange(s, dtype=volume.dtype) for s in ref_shape],
                indexing="ij",
                sparse=True,
            )
            coords = _apply_affine_to_field(
                xcoords,
                affine[:ndim, :],
                include_translations=True,
                coord_axis=0,
            )
            out = ndi.map_coordinates(volume, coords, order=1)
    finally:
        interp.const_legacy_mode = legacy_mode
    return out
Exemplo n.º 16
0
def get_perlin_init(shape=(1000, 1000),
                    n=100000,
                    cutoff=None,
                    repetition=(1000, 1000),
                    scale=100,
                    octaves=20.0,
                    persistence=0.1,
                    lacunarity=2.0):
    """Returns a tuple of x,y-coordinates sampled from Perlin noise.
    This can be used to initialize the starting positions of a physarum-
    population, as well as to generate a cloudy feeding-pattern that will
    have a natural feel to it. This function wraps the one from the noise-
    library from Casey Duncan, and is in parts borrowed from here (see also this for a good explanation of the noise-parameters):
    https://medium.com/@yvanscher/playing-with-perlin-noise-generating-realistic-archipelagos-b59f004d8401
    The most relevant paramaters for our purposes are:

    :param shape: The shape of the area in which the noise is to be generated. Defaults to (1000,1000)
    :type shape: Tuple of integers with the form (width, height).
    :param n: Number of particles to sample. When used as a feeeding trace,
    this translates to the relative strength of the pattern. defaults to 100000.
    :param cutoff: value below which noise should be set to zero. Default is None. Will lead to probabilities 'contains NaN-error, if to high'
    :param scale: (python-noise parameter) The scale of the noise -- larger or smaller patterns, defaults to 100.
    :param repetition: (python-noise parameter) Tuple that denotes the size of the area in which the noise should repeat itself. Defaults to (1000,1000)

    """
    import numpy as np
    import cupy as cp  # vectorized not present in cupy, so for now to conversion at the end

    shape = [i - 1 for i in shape]

    # make coordinate grid on [0,1]^2
    x_idx = np.linspace(0, shape[0], shape[0])
    y_idx = np.linspace(0, shape[1], shape[1])
    world_x, world_y = np.meshgrid(x_idx, y_idx)

    # apply perlin noise, instead of np.vectorize, consider using itertools.starmap()
    world = np.vectorize(noise.pnoise2)(
        world_x / scale,
        world_y / scale,
        octaves=int(octaves),
        persistence=persistence,
        lacunarity=lacunarity,
        repeatx=repetition[0],
        repeaty=repetition[1],
        base=np.random.randint(0, 100),
    )
    # world = world * 3
    # 	 Sample particle init from map:
    world[world <= 0.0] = 0.0  # filter negative values
    if cutoff is not None:
        world[world <= cutoff] = 0.0
    linear_idx = np.random.choice(world.size,
                                  size=n,
                                  p=world.ravel() / float(world.sum()))
    x, y = np.unravel_index(linear_idx, shape)
    x = x.reshape(-1, 1)
    y = y.reshape(-1, 1)

    return cp.asarray(np.hstack([x, y]))
Exemplo n.º 17
0
 def initCoords(self):
     size=self.vol_dim[0]/2
     x, y=np.meshgrid(np.arange(-size+1,size+1),np.arange(-size+1,size+1))
     #temp=np.arctan2(y, x)
     #temp=np.reshape(temp,(size*2,size*2,1))
     #self.radmatrix=np.repeat(temp, size*2, axis=2)
     self.radmatrix=np.remainder(np.arctan2(x, y)+2*np.pi,2*np.pi)-2*np.pi
     self.zline=np.arange(-size+1,size+1)
Exemplo n.º 18
0
	def reshape(self, bottom, top):
		outer_num = bottom[0].data.shape[0]
		channel = bottom[0].data.shape[1]
		height,width = bottom[0].data.shape[2],bottom[0].data.shape[3]
		self.index_0 = cp.arange(outer_num).reshape([outer_num,1,1,1])
		self.index_3,self.index_2 = cp.meshgrid(cp.arange(width),cp.arange(height))

		top[0].reshape(1)
Exemplo n.º 19
0
 def test_lab_full_gamut(self):
     a, b = cp.meshgrid(cp.arange(-100, 100), cp.arange(-100, 100))
     L = cp.ones(a.shape)
     lab = cp.dstack((L, a, b))
     for value in [0, 10, 20]:
         lab[:, :, 0] = value
         with expected_warnings(['Color data out of range']):
             lab2xyz(lab)
Exemplo n.º 20
0
 def set_args(self, dtype):
     coords = cp.meshgrid(
         *[cp.linspace(0, 6 * cp.pi, s) for s in self.shape], sparse=True)
     bumps = functools.reduce(operator.add, [cp.sin(c) for c in coords])
     h = 0.6
     seed = bumps - h
     self.args_cpu = (cp.asnumpy(seed), cp.asnumpy(bumps))
     self.args_gpu = (seed, bumps)
Exemplo n.º 21
0
    def adj(self, coeffs, shifts=None):
        """Adjoint operator for GWP decomposition
        Parameters
        ----------
        coeffs : [K](Nangles,L0,L1) complex64
            Decomposition coefficients for box levels 0:K, angles 0:Nangles, 
            defined box grids with sizes [L0[k],L1[k]], k=0:K        
        Returns
        -------
        f : [N,N] complex64
            2D function in the space domain        
        """
        print('adj transform')
        # build spectrum by using gwp coefficients
        F = cp.zeros(self.fgridshape, dtype="complex64")
        # loop over box orientations
        for ang in range(0, self.nangles):
            print('angle', ang)
            g = cp.zeros(int(np.prod(self.boxshape[-1])), dtype='complex64')
            for k in range(self.K):
                fcoeffs = cp.array(coeffs[k][ang])
                # normalize
                fcoeffs /= (np.prod(self.boxshape[-1]))
                if(shifts is not None):
                    # shift wrt region in rectangular grid
                    fcoeffs = util.checkerboard(cp.fft.fftn(
                        util.checkerboard(fcoeffs), norm='ortho'), inverse=True)
                    [py, px] = cp.meshgrid(
                        self.phasemanyy[k]*shifts[ang, k, 0], self.phasemanyx[k]*shifts[ang, k, 1], indexing='ij')
                    fcoeffs *= cp.exp(1j*(px+py))
                    fcoeffs = util.checkerboard(cp.fft.ifftn(
                        util.checkerboard(fcoeffs), norm='ortho'), inverse=True)
                # shift wrt xi_cent
                fcoeffs *= cp.exp(1j*self.phase[k])
                fcoeffs = util.checkerboard(cp.fft.fftn(
                    util.checkerboard(fcoeffs), norm='ortho'), inverse=True)
                # broadcast values to smaller boxes, multiply by the gwp kernel function
                g[self.inds[k]] += self.gwpf[k]*fcoeffs.flatten()
            g = g.reshape(self.boxshape[-1])
            # 2) Interpolation to the global grid
            # Conventional scattering operation from the global to local grid is replaced
            # by an equivalent gathering operation.
            # calculate global grid coordinates in the space domain
            xr = self.coordinates_adj(ang)
            # extract ids of the global spectrum subregion contatining the box
            [idsy, idsx] = self.subregion_ids(ang)
            # gathering to the global grid
            F[cp.ix_(idsy, idsx)] += self.U.gather(g, xr,
                                                   g.shape).reshape(len(idsy), len(idsx))
           # util.mplt(F)
        # 3) apply 2D IFFT, compensate for the USFFT kernel function in the space domain
        f = self.U.ifftcomp(
            F.reshape(self.fgridshape)).get().astype('complex64')

        # free cupy pools
        # mempool.free_all_blocks()
        # pinned_mempool.free_all_blocks()
        return f
Exemplo n.º 22
0
 def calc_theta_tx(self, tx_ant):
     x, y = cp.meshgrid(tx_ant.x_position, tx_ant.y_position)
     # ブロードキャストを使って30x30に各受信点からの角度(shape=7)を格納
     rx_x = self.x_position[cp.newaxis, cp.newaxis, :]  # shape=1,1,7
     rx_y = self.y_position[cp.newaxis, cp.newaxis, :]  # shape=1,1,7
     x_diff = rx_x - x[:, :, cp.newaxis]                 # shape=30,30,7
     y_diff = rx_y - y[:, :, cp.newaxis]                 # shape=30,30,7
     theta = cp.arctan(y_diff / x_diff)
     return theta
Exemplo n.º 23
0
def calc_rad(size, binning, return_float=False):
    '''Calculate radius and binned radius of 3D grid of voxels with given size'''
    cen = size // 2
    ind = np.arange(size, dtype='f4') - cen
    x, y, z = np.meshgrid(ind, ind, ind, indexing='ij')
    rad = np.sqrt(x * x + y * y + z * z)

    if return_float:
        return rad
    return np.rint(rad / binning).astype('i4')
Exemplo n.º 24
0
    def local_cov_bet_class(self,key,label,nb_class,batchsize):
        index = cp.arange(key.shape[0])
        xx,yy = cp.meshgrid(index,index)
        sub = key[xx] - key[yy]

        norm_sub = cp.linalg.norm(sub,axis=2)
        a1= cp.exp(-norm_sub*norm_sub/100)

        lindex = cp.arange(label.shape[0])
        lx,ly = cp.meshgrid(lindex,lindex)
        l = (label[lx]==label[ly])
        a1 = a1*l*(1.0/(batchsize*nb_class)-1.0/batchsize)

        l2 = (label[lx]!=label[ly])
        a2 = l2*(1.0/(nb_class*batchsize))
        a=a1+a2

        Sb = cp.einsum('ij,ijk,ijl->kl',a,sub,sub,dtype='float32')*0.5
        return Sb     
Exemplo n.º 25
0
def LoadLFs5K():

    print("LoadLFs5K...\n")

    x = cp.arange(Params.HEIGHT)
    y = cp.arange(Params.WIDTH)
    TY, TX = cp.meshgrid(x, y)
    TX = TX.reshape(-1, 1)
    TY = TY.reshape(-1, 1)

    LF = [[], []]

    for i in range(0, len(LF)):
        LF[i] = cp.zeros(3 * Params.LFU_W * Params.HEIGHT * Params.WIDTH * 3,
                         'uint8')
        for n in range(1, Params.LFU_W * 3 + 1):
            if (i == 0):
                file = '%s/00/%04d.jpeg' % (Params.cur_dir, n)
            else:
                file = '%s/02/%04d.jpeg' % (Params.cur_dir, n)

            #img = Img.imread(file)

            #for GPU
            img = imresize(Img.imread(file),
                           output_shape=(Params.HEIGHT, Params.WIDTH))
            #img = cv2.UMat(file)

            img_C1 = img[:, :, 0]
            img_C2 = img[:, :, 1]
            img_C3 = img[:, :, 2]
            nn = n - 1

            img_C1 = img_C1.T
            img_C1 = img_C1.reshape(-1, 1)

            img_C2 = img_C2.T
            img_C2 = img_C2.reshape(-1, 1)

            img_C3 = img_C3.T
            img_C3 = img_C3.reshape(-1, 1)

            TX = TX.astype('int64')
            TY = TY.astype('int64')

            LF[i][(nn) * (Params.HEIGHT * Params.WIDTH * 3) + (TX) *
                  (Params.HEIGHT * 3) + (TY) * 3 + 3 - 1] = img_C1[:]
            LF[i][(nn) * (Params.HEIGHT * Params.WIDTH * 3) + (TX) *
                  (Params.HEIGHT * 3) + (TY) * 3 + 2 - 1] = img_C2[:]
            LF[i][(nn) * (Params.HEIGHT * Params.WIDTH * 3) + (TX) *
                  (Params.HEIGHT * 3) + (TY) * 3 + 1 - 1] = img_C3[:]

            print(['LF', i, ' - ', n, ', ', nn])

    return LF
Exemplo n.º 26
0
    def test_gaussian(self):
        cx, cy = cp.meshgrid(cp.arange(5), cp.arange(5))
        c = (cx.flatten(), cy.flatten())        

        cs_gauss = cp.asnumpy(gaussian_rect(self.som._neigx, self.som._neigy, self.som._std_coeff, c, 1))

        for i in range(len(c[0])):
            x = cp.asnumpy(c[0][i]).item()
            y = cp.asnumpy(c[1][i]).item()
            ms_gauss = self.minisom._gaussian((x,y), 1)
            np.testing.assert_array_almost_equal(ms_gauss, cs_gauss[i])
Exemplo n.º 27
0
    def test_mexican_hat(self):
        cx, cy = cp.meshgrid(cp.arange(5), cp.arange(5))
        c = (cx.flatten(), cy.flatten())        

        cs_mex = cp.asnumpy(mexican_hat_generic(self.som._xx, self.som._yy, self.som._std_coeff, c, 1))

        for i in range(len(c[0])):
            x = cp.asnumpy(c[0][i]).item()
            y = cp.asnumpy(c[1][i]).item()
            ms_mex = self.minisom._mexican_hat((x,y), 1)
            np.testing.assert_array_almost_equal(ms_mex, cs_mex[i])
Exemplo n.º 28
0
    def test_bubble(self):
        cx, cy = cp.meshgrid(cp.arange(5), cp.arange(5))
        c = (cx.flatten(), cy.flatten())        

        cs_mex = cp.asnumpy(bubble(self.som._neigx, self.som._neigy, c, 1))

        for i in range(len(c[0])):
            x = cp.asnumpy(c[0][i]).item()
            y = cp.asnumpy(c[1][i]).item()
            ms_mex = self.minisom._bubble((x,y), 1)
            np.testing.assert_array_almost_equal(ms_mex, cs_mex[i])            
Exemplo n.º 29
0
    def __init__(self,basis_number,extended_basis_number,t_start = 0,t_end=1,mempool=None):
        self.basis_number = basis_number
        self.extended_basis_number = extended_basis_number
        self.basis_number_2D = (2*basis_number-1)*basis_number
        self.basis_number_2D_ravel = (2*basis_number*basis_number-2*basis_number+1)
        self.basis_number_2D_sym = (2*basis_number-1)*(2*basis_number-1)
        self.extended_basis_number_2D = (2*extended_basis_number-1)*extended_basis_number
        self.extended_basis_number_2D_sym = (2*extended_basis_number-1)*(2*extended_basis_number-1)
        self.t_end = t_end
        self.t_start = t_start
        self.verbose = True
        if mempool is None:
            mempool = cp.get_default_memory_pool()
        # pinned_mempool = cp.get_default_pinned_memory_pool()
        
        
        # self.ix = cp.zeros((2*self.basis_number-1,2*self.basis_number-1),dtype=cp.int32)
        # self.iy = cp.zeros((2*self.basis_number-1,2*self.basis_number-1),dtype=cp.int32)
        temp = cp.arange(-(self.basis_number-1),self.basis_number,dtype=cp.int32)
        # for i in range(2*self.basis_number-1):
        #     self.ix[i,:] = temp
        #     self.iy[:,i] = temp

        self.ix,self.iy = cp.meshgrid(temp,temp)
        if self.verbose:
            print("Used bytes so far, after creating ix and iy {}".format(mempool.used_bytes()))
        
        
        self.Ddiag = -(2*util.PI)**2*(self.ix.ravel(ORDER)**2+self.iy.ravel(ORDER)**2)
        self.OnePerDdiag = 1/self.Ddiag
        self.Dmatrix = cpx.scipy.sparse.diags(self.Ddiag,dtype=cp.float32)
        if self.verbose:
            print("Used bytes so far, after creating Dmatrix {}".format(mempool.used_bytes()))

        # self.Imatrix = cp.eye((2*self.basis_number-1)**2,dtype=cp.int8)
        self.Imatrix = cpx.scipy.sparse.identity((2*self.basis_number-1)**2,dtype=cp.float32)
        self.Imatrix_dense = cp.eye((2*self.basis_number-1)**2,dtype=cp.float32)
        if self.verbose:
            print("Used bytes so far, after creating Imatrix {}".format(mempool.used_bytes()))

        #K matrix --> 1D fourier index to 2D fourier index
        ones_temp = cp.ones_like(temp)
        self.Kmatrix = cp.vstack((cp.kron(temp,ones_temp),cp.kron(ones_temp,temp)))
        if self.verbose:
            print("Used bytes so far, after creating Kmatrix {}".format(mempool.used_bytes()))
        
        #implement fft plan here
        self._plan_fft2 = None
        # self._fft2_axes = (-2, -1)
        self._plan_ifft2 = None

        x = np.concatenate((np.arange(self.basis_number)+1,np.zeros(self.basis_number-1)))
        toep = sla.toeplitz(x)
        self._Umask = cp.asarray(np.kron(toep,toep),dtype=cp.int16)
Exemplo n.º 30
0
def _calc_offset(offset_map, region, parent_y, parent_x):
    y0, y1, x0, x1 = region
    x_area, y_area = offset_map[:, y0:y1, x0:x1]
    x_vec = np.power(np.subtract(np.arange(x0, x1), parent_x), 2)
    y_vec = np.power(np.subtract(np.arange(y0, y1), parent_y), 2)
    xv, yv = np.meshgrid(x_vec, y_vec)
    dist = np.sqrt(xv + yv)  # sqrt(y^2 + x^2)
    xv = np.divide(xv, dist)  # normalize x
    yv = np.divide(yv, dist)  # normalize y
    offset_map[0, y0:y1, x0:x1] = np.maximum(x_area, xv)
    offset_map[1, y0:y1, x0:x1] = np.maximum(y_area, yv)